Subversion Repositories XServices

Rev

Rev 196 | Go to most recent revision | Show entire file | Regard 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
Line 35... Line -...
35
 * @since 0.1
-
 
36
 */
38
 */
37
public class SimpleHttpEvent {
39
@Slf4j
-
 
40
public class SimpleSoap {
38
	
41
 
Line -... Line 42...
-
 
42
	private final String url;
-
 
43
	private final String soapBody;
-
 
44
	private final String id;
39
	private final Logger logger = Logger.getLogger(SimpleHttpEvent.class);
45
	private long duration = 0;
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
	 *
-
 
53
	 * @param url the url
47
	 * @param url the url
54
	 * @param soapBody the soap body
48
	 * @param soapBody the soap body
55
	 */
Line 49... Line 56...
49
	 */
56
	public SimpleSoap(String url, String id, String soapBody) {
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.
-
 
64
		 *
57
	 *
65
		 * @param isDropResponse show interest in response or not
58
	 * @param isDropResponse show interest in response or not
-
 
59
	 * @throws ClientProtocolException the client protocol exception
-
 
60
	 * @throws IOException             Signals that an I/O exception has occurred.
-
 
61
	 */
-
 
62
	public void sendSoap(boolean isDropResponse) throws ClientProtocolException, IOException {
66
		 * @throws ClientProtocolException the client protocol exception
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");
-
 
Line 67... Line 70...
67
		post.addHeader("SOAPAction", "");
70
			Reader response = null;
-
 
71
			long start = System.currentTimeMillis();
-
 
72
			EntityBuilder entitybuilder = EntityBuilder.create();
-
 
73
			entitybuilder.setContentEncoding("UTF-8");
-
 
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);
-
 
78
 
-
 
79
			if(isInterrupted.get()) return null;
-
 
80
 
70
		entitybuilder.setText(soapBody);
81
			try {
71
		HttpEntity entity = entitybuilder.build();
82
				Response resp = Request.Post(url)
-
 
83
						.addHeader("Accept", "text/xml")
-
 
84
						.addHeader("Content-Type", "text/xml; charset=utf-8")
72
		post.setEntity(entity);
85
						.addHeader("SOAPAction", "")
73
 
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
					/*
-
 
92
					StringBuilder sb = new StringBuilder();
-
 
93
					BufferedReader in = new BufferedReader(new InputStreamReader(e.getContent()));
-
 
94
					String s;
-
 
95
					while ((s = in.readLine()) != null) {
79
			StringBuilder sb = new StringBuilder();
96
						sb.append(s);
80
			BufferedReader in = new BufferedReader(new InputStreamReader(e.getContent()));
97
					}
81
			String s;
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
					 */
82
			while( (s=in.readLine()) != null) {
103
				} else {
-
 
104
					log.debug("Response intentionally ignored.");
83
			   sb.append(s);
105
				}
Line 84... Line -...
84
			}
-
 
85
			logger.debug("Response: \n " + sb.toString());
-
 
86
        } else {
-
 
87
        	logger.debug("Response was dropped.");
-
 
88
        }
-
 
89
        duration = System.currentTimeMillis() - start;
106
			} catch (IOException e) {
90
	}
107
				log.error("Error sending ALF Event '{}'. Got IOException: {}", id, e.getMessage());
91
	
108
            }
92
	/**
109