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