Subversion Repositories XServices

Rev

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