Subversion Repositories XServices

Rev

Rev 196 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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