1 17 package org.apache.servicemix.components.http; 18 19 import org.apache.commons.httpclient.HostConfiguration; 20 import org.apache.commons.httpclient.HttpClient; 21 import org.apache.commons.httpclient.HttpStatus; 22 import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager; 23 import org.apache.commons.httpclient.URI; 24 import org.apache.commons.httpclient.methods.PostMethod; 25 import org.apache.servicemix.MessageExchangeListener; 26 import org.apache.servicemix.components.util.TransformComponentSupport; 27 28 import javax.jbi.JBIException; 29 import javax.jbi.messaging.MessageExchange; 30 import javax.jbi.messaging.MessagingException; 31 import javax.jbi.messaging.NormalizedMessage; 32 33 38 public class HttpInvoker extends TransformComponentSupport implements MessageExchangeListener { 39 40 protected HttpClientMarshaler marshaler = new HttpClientMarshaler(); 41 protected MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager(); 42 protected HttpClient httpClient = new HttpClient(connectionManager); 43 protected HostConfiguration hostConfiguration = new HostConfiguration(); 44 protected String url; 45 protected boolean defaultInOut = true; 46 47 public void stop() throws JBIException { 48 super.stop(); 49 connectionManager.shutdown(); 50 } 51 52 public HttpInvoker() { 53 setCopyAttachments(false); 55 setCopyProperties(false); 56 } 57 58 protected boolean transform(MessageExchange exchange, NormalizedMessage in, NormalizedMessage out) throws MessagingException { 59 PostMethod method = new PostMethod(url); 60 try { 61 marshaler.fromNMS(method, exchange, in); 62 if (url != null) { 63 hostConfiguration.setHost(new URI(url, false)); 64 } 65 int response = httpClient.executeMethod(hostConfiguration, method); 66 67 if (response != HttpStatus.SC_OK && response != HttpStatus.SC_ACCEPTED) { 68 throw new InvalidStatusResponseException(response); 69 } 70 71 if (defaultInOut) { 73 marshaler.toNMS(out, method); 74 } 75 return defaultInOut; 76 } 77 catch (Exception e) { 78 throw new MessagingException("Error executing http request", e); 79 } 80 finally { 81 method.releaseConnection(); 82 } 83 } 84 85 public HttpClient getHttpClient() { 86 return httpClient; 87 } 88 89 public void setHttpClient(HttpClient httpClient) { 90 this.httpClient = httpClient; 91 } 92 93 public String getUrl() { 94 return url; 95 } 96 97 public void setUrl(String url) { 98 this.url = url; 99 } 100 101 public boolean isDefaultInOut() { 102 return defaultInOut; 103 } 104 105 public void setDefaultInOut(boolean defaultInOut) { 106 this.defaultInOut = defaultInOut; 107 } 108 109 public HttpClientMarshaler getMarshaler() { 110 return marshaler; 111 } 112 113 public void setMarshaler(HttpClientMarshaler marshaler) { 114 this.marshaler = marshaler; 115 } 116 } 117 | Popular Tags |