Subversion Repositories XServices

Rev

Rev 150 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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