Subversion Repositories XServices

Rev

Rev 201 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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