KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: HttpResponseToString.java 3167 2006-09-22 15:47:04Z holger $
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 java.io.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.io.OutputStream JavaDoc;
16 import java.io.UnsupportedEncodingException JavaDoc;
17 import java.util.Iterator JavaDoc;
18
19 import org.apache.commons.httpclient.ChunkedOutputStream;
20 import org.apache.commons.httpclient.Header;
21 import org.apache.commons.io.IOUtils;
22 import org.apache.commons.io.output.ByteArrayOutputStream;
23 import org.mule.providers.http.HttpConstants;
24 import org.mule.providers.http.HttpResponse;
25 import org.mule.providers.http.ResponseWriter;
26 import org.mule.transformers.AbstractTransformer;
27 import org.mule.umo.transformer.TransformerException;
28
29 /**
30  * Converts an Http Response object to String. Note that the response headers are
31  * preserved.
32  *
33  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
34  * @version $Revision: 3167 $
35  */

36 public class HttpResponseToString extends AbstractTransformer
37 {
38     /**
39      * Serial version
40      */

41     private static final long serialVersionUID = -6700649887307138101L;
42
43     public HttpResponseToString()
44     {
45         registerSourceType(HttpResponse.class);
46         setReturnClass(String JavaDoc.class);
47     }
48
49     /**
50      * Perform the transformation to always return a String object
51      */

52     protected Object JavaDoc doTransform(Object JavaDoc src, String JavaDoc encoding) throws TransformerException
53     {
54         try
55         {
56             HttpResponse response = (HttpResponse)src;
57             ByteArrayOutputStream bos = new ByteArrayOutputStream(8192);
58             OutputStream JavaDoc outstream = bos;
59             ResponseWriter writer = new ResponseWriter(outstream, encoding);
60             writer.println(response.getStatusLine());
61             Iterator JavaDoc item = response.getHeaderIterator();
62             while (item.hasNext())
63             {
64                 Header header = (Header)item.next();
65                 writer.print(header.toExternalForm());
66             }
67             writer.println();
68             writer.flush();
69
70             InputStream JavaDoc content = response.getBody();
71             if (content != null)
72             {
73                 Header transferenc = response.getFirstHeader(HttpConstants.HEADER_TRANSFER_ENCODING);
74                 if (transferenc != null)
75                 {
76                     response.removeHeaders(HttpConstants.HEADER_CONTENT_LENGTH);
77                     if (transferenc.getValue().indexOf(HttpConstants.TRANSFER_ENCODING_CHUNKED) != -1)
78                     {
79                         outstream = new ChunkedOutputStream(outstream);
80                     }
81                 }
82
83                 IOUtils.copy(content, outstream);
84
85                 if (outstream instanceof ChunkedOutputStream)
86                 {
87                     ((ChunkedOutputStream)outstream).finish();
88                 }
89             }
90
91             outstream.flush();
92             bos.flush();
93             byte[] result = bos.toByteArray();
94             outstream.close();
95             writer.close();
96             bos.close();
97
98             String JavaDoc output = null;
99             try
100             {
101                 output = new String JavaDoc(result, encoding);
102             }
103             catch (UnsupportedEncodingException JavaDoc uee)
104             {
105                 // I believe this is never reached since a TransformerExcpetion
106
// is thrown before at new ResponseWriter(outstream, encoding) if
107
// encoding is not supported
108
output = new String JavaDoc(result);
109             }
110
111             return output;
112         }
113         catch (IOException JavaDoc e)
114         {
115             throw new TransformerException(this, e);
116         }
117     }
118 }
119
Popular Tags