Subversion Repositories XServices

Rev

Rev 150 | Go to most recent revision | Details | 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 java.io.BufferedReader;
19
import java.io.IOException;
20
import java.io.InputStreamReader;
21
 
22
import org.apache.http.HttpEntity;
23
import org.apache.http.HttpResponse;
24
import org.apache.http.client.ClientProtocolException;
25
import org.apache.http.client.entity.EntityBuilder;
26
import org.apache.http.client.methods.HttpPost;
27
import org.apache.http.impl.client.CloseableHttpClient;
28
import org.apache.http.impl.client.HttpClients;
29
import org.apache.http.util.EntityUtils;
30
import org.apache.log4j.Logger;
31
 
32
/**
33
 * Construct a HTTP POST and send it.
34
 *
35
 * @author Brian Rosenberger, bru(at)brutex.de
36
 * @since 0.1
37
 */
38
public class SimpleHttpEvent {
39
 
40
	private final Logger logger = Logger.getLogger(SimpleHttpEvent.class);
41
	private final String url;
42
	private final String soapBody;
43
	private long duration = 0;
44
 
45
	/**
46
	 * Instantiates a new simple http event.
47
	 *
48
	 * @param url the url
49
	 * @param soapBody the soap body
50
	 */
51
	public SimpleHttpEvent(String url, String soapBody) {
52
		this.url = url;
53
		this.soapBody = soapBody;
54
	}
55
 
56
	/**
57
	 * Send soap.
58
	 *
59
	 * @param url the url
60
	 * @param soapBody the soap body
61
	 * @throws ClientProtocolException the client protocol exception
62
	 * @throws IOException Signals that an I/O exception has occurred.
63
	 */
64
	public void sendSoap(boolean isDropResponse) throws ClientProtocolException, IOException {
65
		long start = System.currentTimeMillis();
66
		HttpPost post = new HttpPost(url);
67
        post.addHeader("Accept" , "text/xml");
68
        post.addHeader("SOAPAction","");
69
        EntityBuilder entitybuilder = EntityBuilder.create();
70
        entitybuilder.setContentEncoding("UTF-8");
71
        entitybuilder.setText(soapBody);
72
        HttpEntity entity = entitybuilder.build();
73
        post.setEntity(entity);
74
 
75
        CloseableHttpClient httpclient = HttpClients.createDefault();
76
        HttpResponse r = httpclient.execute(post);
77
        if(! isDropResponse) {
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
}