Coverage Report - org.mule.providers.http.transformers.HttpResponseToString
 
Classes in this File Line Coverage Branch Coverage Complexity
HttpResponseToString
0%
0/39
0%
0/7
5.5
 
 1  
 /*
 2  
  * $Id: HttpResponseToString.java 7976 2007-08-21 14:26:13Z 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.providers.http.transformers;
 12  
 
 13  
 import org.mule.providers.http.HttpConstants;
 14  
 import org.mule.providers.http.HttpResponse;
 15  
 import org.mule.providers.http.ResponseWriter;
 16  
 import org.mule.transformers.AbstractTransformer;
 17  
 import org.mule.umo.transformer.TransformerException;
 18  
 
 19  
 import java.io.IOException;
 20  
 import java.io.InputStream;
 21  
 import java.io.OutputStream;
 22  
 import java.io.UnsupportedEncodingException;
 23  
 import java.util.Iterator;
 24  
 
 25  
 import org.apache.commons.httpclient.ChunkedOutputStream;
 26  
 import org.apache.commons.httpclient.Header;
 27  
 import org.apache.commons.io.IOUtils;
 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  0
     {
 39  0
         registerSourceType(HttpResponse.class);
 40  0
         setReturnClass(String.class);
 41  0
     }
 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  0
             HttpResponse response = (HttpResponse)src;
 51  0
             ByteArrayOutputStream bos = new ByteArrayOutputStream(8192);
 52  0
             OutputStream outstream = bos;
 53  0
             ResponseWriter writer = new ResponseWriter(outstream, encoding);
 54  0
             writer.println(response.getStatusLine());
 55  0
             Iterator item = response.getHeaderIterator();
 56  0
             while (item.hasNext())
 57  
             {
 58  0
                 Header header = (Header)item.next();
 59  0
                 writer.print(header.toExternalForm());
 60  
             }
 61  0
             writer.println();
 62  0
             writer.flush();
 63  
 
 64  0
             InputStream content = response.getBody();
 65  0
             if (content != null)
 66  
             {
 67  0
                 Header transferenc = response.getFirstHeader(HttpConstants.HEADER_TRANSFER_ENCODING);
 68  0
                 if (transferenc != null)
 69  
                 {
 70  0
                     response.removeHeaders(HttpConstants.HEADER_CONTENT_LENGTH);
 71  0
                     if (transferenc.getValue().indexOf(HttpConstants.TRANSFER_ENCODING_CHUNKED) != -1)
 72  
                     {
 73  0
                         outstream = new ChunkedOutputStream(outstream);
 74  
                     }
 75  
                 }
 76  
 
 77  0
                 IOUtils.copy(content, outstream);
 78  
 
 79  0
                 if (outstream instanceof ChunkedOutputStream)
 80  
                 {
 81  0
                     ((ChunkedOutputStream)outstream).finish();
 82  
                 }
 83  
             }
 84  
 
 85  0
             outstream.flush();
 86  0
             bos.flush();
 87  0
             byte[] result = bos.toByteArray();
 88  0
             outstream.close();
 89  0
             writer.close();
 90  0
             bos.close();
 91  
 
 92  0
             String output = null;
 93  
             try
 94  
             {
 95  0
                 output = new String(result, encoding);
 96  
             }
 97  0
             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  0
                 output = new String(result);
 103  0
             }
 104  
 
 105  0
             return output;
 106  
         }
 107  0
         catch (IOException e)
 108  
         {
 109  0
             throw new TransformerException(this, e);
 110  
         }
 111  
     }
 112  
 }