1 17 package org.apache.servicemix.components.http; 18 19 import org.apache.commons.httpclient.Header; 20 import org.apache.commons.httpclient.HttpMethod; 21 import org.apache.commons.httpclient.methods.InputStreamRequestEntity; 22 import org.apache.commons.httpclient.methods.PostMethod; 23 import org.apache.commons.httpclient.methods.StringRequestEntity; 24 import org.apache.servicemix.jbi.jaxp.SourceTransformer; 25 import org.apache.servicemix.jbi.jaxp.StringSource; 26 27 import javax.jbi.messaging.MessageExchange; 28 import javax.jbi.messaging.NormalizedMessage; 29 import javax.xml.transform.Source ; 30 import javax.xml.transform.TransformerException ; 31 import javax.xml.transform.stream.StreamSource ; 32 33 import java.util.Iterator ; 34 35 40 public class HttpClientMarshaler { 41 42 protected SourceTransformer sourceTransformer; 43 private boolean streaming; 44 private String contentType = "text/xml"; 45 46 public HttpClientMarshaler() { 47 this(false); 48 } 49 50 public HttpClientMarshaler(boolean streaming) { 51 this.sourceTransformer = new SourceTransformer(); 52 this.streaming = streaming; 53 } 54 55 58 public boolean isStreaming() { 59 return streaming; 60 } 61 62 65 public void setStreaming(boolean streaming) { 66 this.streaming = streaming; 67 } 68 69 72 public String getContentType() { 73 return contentType; 74 } 75 76 79 public void setContentType(String contentType) { 80 this.contentType = contentType; 81 } 82 83 public void toNMS(NormalizedMessage normalizedMessage, HttpMethod method) throws Exception { 84 addNmsProperties(normalizedMessage, method); 85 if (streaming) { 86 normalizedMessage.setContent(new StreamSource (method.getResponseBodyAsStream())); 87 } else { 88 normalizedMessage.setContent(new StringSource(method.getResponseBodyAsString())); 89 } 90 } 91 92 public void fromNMS(PostMethod method, MessageExchange exchange, NormalizedMessage normalizedMessage) throws Exception , TransformerException { 93 addHttpHeaders(method, normalizedMessage); 94 if (streaming) { 95 method.setContentChunked(true); 96 Source src = normalizedMessage.getContent(); 97 if (src instanceof StreamSource && ((StreamSource ) src).getInputStream() != null) { 98 method.setRequestEntity(new InputStreamRequestEntity( 99 ((StreamSource ) src).getInputStream(), -1)); 100 } else { 101 String text = sourceTransformer.toString(normalizedMessage.getContent()); 102 method.setRequestEntity(new StringRequestEntity(text)); 103 104 } 105 } else { 106 String text = sourceTransformer.toString(normalizedMessage.getContent()); 107 method.setRequestEntity(new StringRequestEntity(text)); 108 } 109 } 110 111 protected void addHttpHeaders(HttpMethod method, NormalizedMessage message) { 112 for (Iterator iter = message.getPropertyNames().iterator(); iter.hasNext();) { 113 String name = (String ) iter.next(); 114 Object value = message.getProperty(name); 115 if (shouldIncludeHeader(message, name, value)) { 116 method.addRequestHeader(name, value.toString()); 117 } 118 } 119 if (method.getRequestHeader("Content-Type") == null) { 120 method.setRequestHeader("Content-Type", contentType); 121 } 122 } 123 124 protected void addNmsProperties(NormalizedMessage message, HttpMethod method) { 125 Header[] headers = method.getResponseHeaders(); 126 for (int i = 0; i < headers.length; i++) { 127 Header header = headers[i]; 128 String name = header.getName(); 129 String value = header.getValue(); 130 message.setProperty(name, value); 131 } 132 } 133 134 138 protected boolean shouldIncludeHeader(NormalizedMessage normalizedMessage, String name, Object value) { 139 return value instanceof String && 140 !"Content-Length".equalsIgnoreCase(name) && 141 !"Content-Type".equalsIgnoreCase(name); 142 } 143 144 } 145 | Popular Tags |