Subversion Repositories XServices

Rev

Rev 150 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 150 Rev 196
1
/*
1
/*
2
 *   Copyright 2013 Brian Rosenberger (Brutex Network)
2
 *   Copyright 2013 Brian Rosenberger (Brutex Network)
3
 *
3
 *
4
 *   Licensed under the Apache License, Version 2.0 (the "License");
4
 *   Licensed under the Apache License, Version 2.0 (the "License");
5
 *   you may not use this file except in compliance with the License.
5
 *   you may not use this file except in compliance with the License.
6
 *   You may obtain a copy of the License at
6
 *   You may obtain a copy of the License at
7
 *
7
 *
8
 *       http://www.apache.org/licenses/LICENSE-2.0
8
 *       http://www.apache.org/licenses/LICENSE-2.0
9
 *
9
 *
10
 *   Unless required by applicable law or agreed to in writing, software
10
 *   Unless required by applicable law or agreed to in writing, software
11
 *   distributed under the License is distributed on an "AS IS" BASIS,
11
 *   distributed under the License is distributed on an "AS IS" BASIS,
12
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 *   See the License for the specific language governing permissions and
13
 *   See the License for the specific language governing permissions and
14
 *   limitations under the License.
14
 *   limitations under the License.
15
 */
15
 */
16
package net.brutex.emitter;
16
package net.brutex.emitter;
17
 
-
 
18
import java.io.BufferedReader;
-
 
19
import java.io.IOException;
-
 
20
import java.io.InputStreamReader;
-
 
21
 
17
 
22
import org.apache.http.HttpEntity;
18
import org.apache.http.HttpEntity;
23
import org.apache.http.HttpResponse;
19
import org.apache.http.HttpResponse;
24
import org.apache.http.client.ClientProtocolException;
20
import org.apache.http.client.ClientProtocolException;
25
import org.apache.http.client.entity.EntityBuilder;
21
import org.apache.http.client.entity.EntityBuilder;
26
import org.apache.http.client.methods.HttpPost;
22
import org.apache.http.client.methods.HttpPost;
27
import org.apache.http.impl.client.CloseableHttpClient;
23
import org.apache.http.impl.client.CloseableHttpClient;
28
import org.apache.http.impl.client.HttpClients;
24
import org.apache.http.impl.client.HttpClients;
29
import org.apache.http.util.EntityUtils;
-
 
30
import org.apache.log4j.Logger;
25
import org.apache.log4j.Logger;
-
 
26
 
-
 
27
import java.io.BufferedReader;
-
 
28
import java.io.IOException;
-
 
29
import java.io.InputStreamReader;
31
 
30
 
32
/**
31
/**
33
 * Construct a HTTP POST and send it.
32
 * Construct a HTTP POST and send it.
34
 *
33
 *
35
 * @author Brian Rosenberger, bru(at)brutex.de
34
 * @author Brian Rosenberger, bru(at)brutex.de
36
 * @since 0.1
35
 * @since 0.1
37
 */
36
 */
38
public class SimpleHttpEvent {
37
public class SimpleHttpEvent {
39
	
38
	
40
	private final Logger logger = Logger.getLogger(SimpleHttpEvent.class);
39
	private final Logger logger = Logger.getLogger(SimpleHttpEvent.class);
41
	private final String url;
40
	private final String url;
42
	private final String soapBody;
41
	private final String soapBody;
43
	private long duration = 0;
42
	private long duration = 0;
44
	
43
	
45
	/**
44
	/**
46
	 * Instantiates a new simple http event.
45
	 * Instantiates a new simple http event.
47
	 *
46
	 *
48
	 * @param url the url
47
	 * @param url the url
49
	 * @param soapBody the soap body
48
	 * @param soapBody the soap body
50
	 */
49
	 */
51
	public SimpleHttpEvent(String url, String soapBody) {
50
	public SimpleHttpEvent(String url, String soapBody) {
52
		this.url = url;
51
		this.url = url;
53
		this.soapBody = soapBody;
52
		this.soapBody = soapBody;
54
	}
53
	}
55
	
54
 
56
	/**
55
	/**
57
	 * Send soap.
56
	 * Send soap.
58
	 *
57
	 *
59
	 * @param url the url
-
 
60
	 * @param soapBody the soap body
58
	 * @param isDropResponse show interest in response or not
61
	 * @throws ClientProtocolException the client protocol exception
59
	 * @throws ClientProtocolException the client protocol exception
62
	 * @throws IOException Signals that an I/O exception has occurred.
60
	 * @throws IOException             Signals that an I/O exception has occurred.
63
	 */
61
	 */
64
	public void sendSoap(boolean isDropResponse) throws ClientProtocolException, IOException {
62
	public void sendSoap(boolean isDropResponse) throws ClientProtocolException, IOException {
65
		long start = System.currentTimeMillis();
63
		long start = System.currentTimeMillis();
66
		HttpPost post = new HttpPost(url);
64
		HttpPost post = new HttpPost(url);
67
        post.addHeader("Accept" , "text/xml");
65
		post.addHeader("Accept", "text/xml");
-
 
66
		post.addHeader("Content-Type", "text/xml; charset=utf-8");
68
        post.addHeader("SOAPAction","");
67
		post.addHeader("SOAPAction", "");
69
        EntityBuilder entitybuilder = EntityBuilder.create();
68
		EntityBuilder entitybuilder = EntityBuilder.create();
70
        entitybuilder.setContentEncoding("UTF-8");
69
		entitybuilder.setContentEncoding("UTF-8");
71
        entitybuilder.setText(soapBody);
70
		entitybuilder.setText(soapBody);
72
        HttpEntity entity = entitybuilder.build();		           
71
		HttpEntity entity = entitybuilder.build();
73
        post.setEntity(entity);
72
		post.setEntity(entity);
74
        
73
 
75
        CloseableHttpClient httpclient = HttpClients.createDefault();
74
		CloseableHttpClient httpclient = HttpClients.createDefault();
76
        HttpResponse r = httpclient.execute(post);
75
		HttpResponse r = httpclient.execute(post);
77
        logger.debug("Sending event to ALF event manager");
76
		logger.debug("Sending event to ALF event manager");
78
        if(! isDropResponse) {
77
		if (!isDropResponse) {
79
			HttpEntity e = r.getEntity();
78
			HttpEntity e = r.getEntity();
80
			StringBuilder sb = new StringBuilder();
79
			StringBuilder sb = new StringBuilder();
81
			BufferedReader in = new BufferedReader(new InputStreamReader(e.getContent()));
80
			BufferedReader in = new BufferedReader(new InputStreamReader(e.getContent()));
82
			String s;
81
			String s;
83
			while( (s=in.readLine()) != null) {
82
			while( (s=in.readLine()) != null) {
84
			   sb.append(s);
83
			   sb.append(s);
85
			}
84
			}
86
			logger.debug("Response: \n " + sb.toString());
85
			logger.debug("Response: \n " + sb.toString());
87
        } else {
86
        } else {
88
        	logger.debug("Response was dropped.");
87
        	logger.debug("Response was dropped.");
89
        }
88
        }
90
        duration = System.currentTimeMillis() - start;
89
        duration = System.currentTimeMillis() - start;
91
	}
90
	}
92
	
91
	
93
	/**
92
	/**
94
	 * Get the response time of the soap call in milliseconds.
93
	 * Get the response time of the soap call in milliseconds.
95
	 * 
94
	 * 
96
	 * @return duration or '0' if not yet executed
95
	 * @return duration or '0' if not yet executed
97
	 */
96
	 */
98
	public long getDuration() {
97
	public long getDuration() {
99
		return duration;
98
		return duration;
100
	}
99
	}
101
}
100
}