1 10 11 package org.mule.providers.http.transformers; 12 13 import org.apache.commons.httpclient.Header; 14 import org.apache.commons.httpclient.HttpMethod; 15 import org.apache.commons.io.IOUtils; 16 import org.mule.impl.MuleMessage; 17 import org.mule.providers.http.HttpConstants; 18 import org.mule.transformers.AbstractTransformer; 19 import org.mule.umo.UMOMessage; 20 import org.mule.umo.transformer.TransformerException; 21 22 import java.io.IOException ; 23 import java.util.HashMap ; 24 import java.util.Map ; 25 26 33 34 public class HttpClientMethodResponseToObject extends AbstractTransformer 35 { 36 39 private static final long serialVersionUID = 7699394620081967116L; 40 41 public HttpClientMethodResponseToObject() 42 { 43 registerSourceType(HttpMethod.class); 44 setReturnClass(UMOMessage.class); 45 } 46 47 public Object doTransform(Object src, String encoding) throws TransformerException 48 { 49 Object msg; 50 HttpMethod httpMethod = (HttpMethod)src; 51 Header contentType = httpMethod.getResponseHeader(HttpConstants.HEADER_CONTENT_TYPE); 52 try 53 { 54 if (contentType != null && !contentType.getValue().startsWith("text/")) 55 { 56 msg = IOUtils.toByteArray(httpMethod.getResponseBodyAsStream()); 58 } 59 else 60 { 61 msg = httpMethod.getResponseBodyAsString(); 62 } 63 } 64 catch (IOException e) 65 { 66 throw new TransformerException(this, e); 67 } 68 Map headerProps = new HashMap (); 70 Header[] headers = httpMethod.getRequestHeaders(); 71 String name; 72 for (int i = 0; i < headers.length; i++) 73 { 74 name = headers[i].getName(); 75 if (name.startsWith(HttpConstants.X_PROPERTY_PREFIX)) 76 { 77 name = name.substring(2); 78 } 79 headerProps.put(name, headers[i].getValue()); 80 } 81 83 return new MuleMessage(msg, headerProps); 84 } 85 } 86 | Popular Tags |