Subversion Repositories XServices

Rev

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