View Javadoc

1   /*
2    * $Id: HttpResponseToString.java 11433 2008-03-20 03:43:57Z dirk.olmes $
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 CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.transport.http.transformers;
12  
13  import org.mule.RequestContext;
14  import org.mule.api.transformer.TransformerException;
15  import org.mule.api.transport.OutputHandler;
16  import org.mule.transformer.AbstractTransformer;
17  import org.mule.transport.http.HttpConstants;
18  import org.mule.transport.http.HttpResponse;
19  import org.mule.transport.http.ResponseWriter;
20  
21  import java.io.IOException;
22  import java.io.OutputStream;
23  import java.io.UnsupportedEncodingException;
24  import java.util.Iterator;
25  
26  import org.apache.commons.httpclient.ChunkedOutputStream;
27  import org.apache.commons.httpclient.Header;
28  import org.apache.commons.io.output.ByteArrayOutputStream;
29  
30  /**
31   * Converts an Http Response object to String. Note that the response headers are
32   * preserved.
33   */
34  public class HttpResponseToString extends AbstractTransformer
35  {
36  
37      public HttpResponseToString()
38      {
39          registerSourceType(HttpResponse.class);
40          setReturnClass(String.class);
41      }
42  
43      /**
44       * Perform the transformation to always return a String object
45       */
46      protected Object doTransform(Object src, String encoding) throws TransformerException
47      {
48          try
49          {
50              HttpResponse response = (HttpResponse)src;
51              ByteArrayOutputStream bos = new ByteArrayOutputStream(8192);
52              OutputStream outstream = bos;
53              ResponseWriter writer = new ResponseWriter(outstream, encoding);
54              writer.println(response.getStatusLine());
55              Iterator item = response.getHeaderIterator();
56              while (item.hasNext())
57              {
58                  Header header = (Header)item.next();
59                  writer.print(header.toExternalForm());
60              }
61              writer.println();
62              writer.flush();
63  
64              if (response.hasBody())
65              {
66                  OutputHandler handler = response.getBody();
67                  Header transferenc = response.getFirstHeader(HttpConstants.HEADER_TRANSFER_ENCODING);
68                  if (transferenc != null)
69                  {
70                      response.removeHeaders(HttpConstants.HEADER_CONTENT_LENGTH);
71                      if (transferenc.getValue().indexOf(HttpConstants.TRANSFER_ENCODING_CHUNKED) != -1)
72                      {
73                          outstream = new ChunkedOutputStream(outstream);
74                      }
75                  }
76  
77                  handler.write(RequestContext.getEvent(), outstream);
78  
79                  if (outstream instanceof ChunkedOutputStream)
80                  {
81                      ((ChunkedOutputStream)outstream).finish();
82                  }
83              }
84  
85              outstream.flush();
86              bos.flush();
87              byte[] result = bos.toByteArray();
88              outstream.close();
89              writer.close();
90              bos.close();
91  
92              String output = null;
93              try
94              {
95                  output = new String(result, encoding);
96              }
97              catch (UnsupportedEncodingException uee)
98              {
99                  // I believe this is never reached since a TransformerExcpetion
100                 // is thrown before at new ResponseWriter(outstream, encoding) if
101                 // encoding is not supported
102                 output = new String(result);
103             }
104 
105             return output;
106         }
107         catch (IOException e)
108         {
109             throw new TransformerException(this, e);
110         }
111     }
112 }