KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > http > transformers > HttpClientMethodResponseToObject


1 /*
2  * $Id: HttpClientMethodResponseToObject.java 3798 2006-11-04 04:07:14Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

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 JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Map JavaDoc;
25
26 /**
27  * <code>HttpClientMethodResponseToObject</code> transforms a http client response
28  * to a MuleMessage.
29  *
30  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
31  * @version $Revision: 3798 $
32  */

33
34 public class HttpClientMethodResponseToObject extends AbstractTransformer
35 {
36     /**
37      * Serial version
38      */

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 JavaDoc doTransform(Object JavaDoc src, String JavaDoc encoding) throws TransformerException
48     {
49         Object JavaDoc 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                 // TODO properly do streaming
57
msg = IOUtils.toByteArray(httpMethod.getResponseBodyAsStream());
58             }
59             else
60             {
61                 msg = httpMethod.getResponseBodyAsString();
62             }
63         }
64         catch (IOException JavaDoc e)
65         {
66             throw new TransformerException(this, e);
67         }
68         // Standard headers
69
Map JavaDoc headerProps = new HashMap JavaDoc();
70         Header[] headers = httpMethod.getRequestHeaders();
71         String JavaDoc 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         // Set Mule Properties
82

83         return new MuleMessage(msg, headerProps);
84     }
85 }
86
Popular Tags