Subversion Repositories XServices

Compare Revisions

Ignore whitespace Rev 196 → Rev 202

/xservices/trunk/src/main/java/net/brutex/xservices/util/SimpleSoap.java/SimpleHttpEvent.java
13,21 → 13,23
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.brutex.emitter;
package net.brutex.xservices.util;
 
 
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.EntityBuilder;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.log4j.Logger;
import org.apache.http.client.fluent.Request;
import org.apache.http.client.fluent.Response;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.concurrent.atomic.AtomicBoolean;
 
 
/**
* Construct a HTTP POST and send it.
*
34,12 → 36,16
* @author Brian Rosenberger, bru(at)brutex.de
* @since 0.1
*/
public class SimpleHttpEvent {
private final Logger logger = Logger.getLogger(SimpleHttpEvent.class);
@Slf4j
public class SimpleSoap {
 
private final String url;
private final String soapBody;
private final String id;
private long duration = 0;
 
 
final AtomicBoolean isInterrupted = new AtomicBoolean(false);
/**
* Instantiates a new simple http event.
47,54 → 53,65
* @param url the url
* @param soapBody the soap body
*/
public SimpleHttpEvent(String url, String soapBody) {
public SimpleSoap(String url, String id, String soapBody) {
this.url = url;
this.id = id;
this.soapBody = soapBody;
}
 
/**
* Send soap.
*
* @param isDropResponse show interest in response or not
* @throws ClientProtocolException the client protocol exception
* @throws IOException Signals that an I/O exception has occurred.
*/
public void sendSoap(boolean isDropResponse) throws ClientProtocolException, IOException {
long start = System.currentTimeMillis();
HttpPost post = new HttpPost(url);
post.addHeader("Accept", "text/xml");
post.addHeader("Content-Type", "text/xml; charset=utf-8");
post.addHeader("SOAPAction", "");
EntityBuilder entitybuilder = EntityBuilder.create();
entitybuilder.setContentEncoding("UTF-8");
entitybuilder.setText(soapBody);
HttpEntity entity = entitybuilder.build();
post.setEntity(entity);
/**
* Send soap.
*
* @param isDropResponse show interest in response or not
* @throws ClientProtocolException the client protocol exception
* @throws IOException Signals that an I/O exception has occurred.
*/
public Reader sendSoap(boolean isDropResponse) {
Reader response = null;
long start = System.currentTimeMillis();
EntityBuilder entitybuilder = EntityBuilder.create();
entitybuilder.setContentEncoding("UTF-8");
entitybuilder.setText(soapBody);
HttpEntity entity = entitybuilder.build();
 
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpResponse r = httpclient.execute(post);
logger.debug("Sending event to ALF event manager");
if (!isDropResponse) {
HttpEntity e = r.getEntity();
StringBuilder sb = new StringBuilder();
BufferedReader in = new BufferedReader(new InputStreamReader(e.getContent()));
String s;
while( (s=in.readLine()) != null) {
sb.append(s);
}
logger.debug("Response: \n " + sb.toString());
} else {
logger.debug("Response was dropped.");
}
duration = System.currentTimeMillis() - start;
log.trace("Sending event '{}' to target ALF Event Manager.", id);
 
if(isInterrupted.get()) return null;
 
try {
Response resp = Request.Post(url)
.addHeader("Accept", "text/xml")
.addHeader("Content-Type", "text/xml; charset=utf-8")
.addHeader("SOAPAction", "")
.body(entity).execute();
 
if (!isDropResponse) {
HttpEntity e = resp.returnResponse().getEntity();
response = new BufferedReader(new InputStreamReader(e.getContent()));
/*
StringBuilder sb = new StringBuilder();
BufferedReader in = new BufferedReader(new InputStreamReader(e.getContent()));
String s;
while ((s = in.readLine()) != null) {
sb.append(s);
}
log.trace("Response: \n {}", sb.toString());
if (sb.toString().contains("<soap:Fault>")) { return false;};
if (! sb.toString().contains(":Envelope ")) { return false;};
 
*/
} else {
log.debug("Response intentionally ignored.");
}
} catch (IOException e) {
log.error("Error sending ALF Event '{}'. Got IOException: {}", id, e.getMessage());
}
 
duration = System.currentTimeMillis() - start;
return response;
}
 
public void interrupt() {
this.isInterrupted.set(true);
}
/**
* Get the response time of the soap call in milliseconds.
*
* @return duration or '0' if not yet executed
*/
public long getDuration() {
return duration;
}
}