Subversion Repositories XServices

Rev

Rev 196 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 196 Rev 201
Line 11... Line 11...
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.xservices.util;
Line -... Line 17...
-
 
17
 
-
 
18
 
17
 
19
import lombok.extern.slf4j.Slf4j;
18
import org.apache.http.HttpEntity;
-
 
19
import org.apache.http.HttpResponse;
20
import org.apache.http.HttpEntity;
20
import org.apache.http.client.ClientProtocolException;
21
import org.apache.http.client.ClientProtocolException;
21
import org.apache.http.client.entity.EntityBuilder;
22
import org.apache.http.client.entity.EntityBuilder;
22
import org.apache.http.client.methods.HttpPost;
-
 
23
import org.apache.http.impl.client.CloseableHttpClient;
23
import org.apache.http.client.fluent.Request;
24
import org.apache.http.impl.client.HttpClients;
-
 
Line 25... Line 24...
25
import org.apache.log4j.Logger;
24
import org.apache.http.client.fluent.Response;
26
 
25
 
27
import java.io.BufferedReader;
26
import java.io.BufferedReader;
-
 
27
import java.io.IOException;
-
 
28
import java.io.InputStreamReader;
-
 
29
import java.io.Reader;
Line 28... Line 30...
28
import java.io.IOException;
30
import java.util.concurrent.atomic.AtomicBoolean;
29
import java.io.InputStreamReader;
31
 
30
 
32
 
31
/**
33
/**
32
 * Construct a HTTP POST and send it.
34
 * Construct a HTTP POST and send it.
33
 *
35
 *
-
 
36
 * @author Brian Rosenberger, bru(at)brutex.de
34
 * @author Brian Rosenberger, bru(at)brutex.de
37
 * @since 0.1
35
 * @since 0.1
38
 */
36
 */
-
 
37
public class SimpleHttpEvent {
39
@Slf4j
38
	
40
public class SimpleSoap {
-
 
41
 
39
	private final Logger logger = Logger.getLogger(SimpleHttpEvent.class);
42
	private final String url;
-
 
43
	private final String soapBody;
-
 
44
	private final String id;
-
 
45
	private long duration = 0;
Line 40... Line 46...
40
	private final String url;
46
 
41
	private final String soapBody;
47
 
42
	private long duration = 0;
48
	final AtomicBoolean isInterrupted = new AtomicBoolean(false);
43
	
49
	
44
	/**
50
	/**
45
	 * Instantiates a new simple http event.
51
	 * Instantiates a new simple http event.
46
	 *
52
	 *
47
	 * @param url the url
53
	 * @param url the url
-
 
54
	 * @param soapBody the soap body
48
	 * @param soapBody the soap body
55
	 */
49
	 */
56
	public SimpleSoap(String url, String id, String soapBody) {
Line 50... Line 57...
50
	public SimpleHttpEvent(String url, String soapBody) {
57
		this.url = url;
51
		this.url = url;
58
		this.id = id;
52
		this.soapBody = soapBody;
59
		this.soapBody = soapBody;
53
	}
60
	}
54
 
61
 
55
	/**
62
		/**
56
	 * Send soap.
63
		 * Send soap.
57
	 *
64
		 *
58
	 * @param isDropResponse show interest in response or not
-
 
59
	 * @throws ClientProtocolException the client protocol exception
65
		 * @param isDropResponse show interest in response or not
60
	 * @throws IOException             Signals that an I/O exception has occurred.
66
		 * @throws ClientProtocolException the client protocol exception
61
	 */
-
 
62
	public void sendSoap(boolean isDropResponse) throws ClientProtocolException, IOException {
-
 
63
		long start = System.currentTimeMillis();
67
		 * @throws IOException             Signals that an I/O exception has occurred.
64
		HttpPost post = new HttpPost(url);
68
		 */
65
		post.addHeader("Accept", "text/xml");
69
		public Reader sendSoap(boolean isDropResponse) {
66
		post.addHeader("Content-Type", "text/xml; charset=utf-8");
70
			Reader response = null;
-
 
71
			long start = System.currentTimeMillis();
-
 
72
			EntityBuilder entitybuilder = EntityBuilder.create();
-
 
73
			entitybuilder.setContentEncoding("UTF-8");
67
		post.addHeader("SOAPAction", "");
74
			entitybuilder.setText(soapBody);
68
		EntityBuilder entitybuilder = EntityBuilder.create();
75
			HttpEntity entity = entitybuilder.build();
-
 
76
 
69
		entitybuilder.setContentEncoding("UTF-8");
77
			log.trace("Sending event '{}' to target ALF Event Manager.", id);
70
		entitybuilder.setText(soapBody);
78
 
71
		HttpEntity entity = entitybuilder.build();
79
			if(isInterrupted.get()) return null;
-
 
80
 
-
 
81
			try {
-
 
82
				Response resp = Request.Post(url)
72
		post.setEntity(entity);
83
						.addHeader("Accept", "text/xml")
73
 
84
						.addHeader("Content-Type", "text/xml; charset=utf-8")
-
 
85
						.addHeader("SOAPAction", "")
-
 
86
						.body(entity).execute();
74
		CloseableHttpClient httpclient = HttpClients.createDefault();
87
 
75
		HttpResponse r = httpclient.execute(post);
88
				if (!isDropResponse) {
76
		logger.debug("Sending event to ALF event manager");
89
					HttpEntity e = resp.returnResponse().getEntity();
77
		if (!isDropResponse) {
90
					response = new BufferedReader(new InputStreamReader(e.getContent()));
78
			HttpEntity e = r.getEntity();
91
					/*
79
			StringBuilder sb = new StringBuilder();
92
					StringBuilder sb = new StringBuilder();
80
			BufferedReader in = new BufferedReader(new InputStreamReader(e.getContent()));
93
					BufferedReader in = new BufferedReader(new InputStreamReader(e.getContent()));
-
 
94
					String s;
-
 
95
					while ((s = in.readLine()) != null) {
-
 
96
						sb.append(s);
-
 
97
					}
81
			String s;
98
					log.trace("Response: \n {}", sb.toString());
82
			while( (s=in.readLine()) != null) {
99
					if (sb.toString().contains("<soap:Fault>")) { return false;};
83
			   sb.append(s);
100
					if (! sb.toString().contains(":Envelope ")) { return false;};
-
 
101
 
84
			}
102
					 */
85
			logger.debug("Response: \n " + sb.toString());
103
				} else {
86
        } else {
104
					log.debug("Response intentionally ignored.");
87
        	logger.debug("Response was dropped.");
-
 
88
        }
105
				}
-
 
106
			} catch (IOException e) {
89
        duration = System.currentTimeMillis() - start;
107
				log.error("Error sending ALF Event '{}'. Got IOException: {}", id, e.getMessage());
90
	}
-
 
91
	
108
            }
92
	/**
109
 
93
	 * Get the response time of the soap call in milliseconds.
110
            duration = System.currentTimeMillis() - start;
94
	 * 
111
			return response;
95
	 * @return duration or '0' if not yet executed
112
		}