Coverage Report - org.mule.transport.http.transformers.HttpResponseToString
 
Classes in this File Line Coverage Branch Coverage Complexity
HttpResponseToString
95%
38/40
80%
8/10
5.5
 
 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  8
     {
 39  8
         registerSourceType(HttpResponse.class);
 40  8
         setReturnClass(String.class);
 41  8
     }
 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  6
             HttpResponse response = (HttpResponse)src;
 51  6
             ByteArrayOutputStream bos = new ByteArrayOutputStream(8192);
 52  6
             OutputStream outstream = bos;
 53  6
             ResponseWriter writer = new ResponseWriter(outstream, encoding);
 54  4
             writer.println(response.getStatusLine());
 55  4
             Iterator item = response.getHeaderIterator();
 56  14
             while (item.hasNext())
 57  
             {
 58  10
                 Header header = (Header)item.next();
 59  10
                 writer.print(header.toExternalForm());
 60  10
             }
 61  4
             writer.println();
 62  4
             writer.flush();
 63  
 
 64  4
             if (response.hasBody())
 65  
             {
 66  4
                 OutputHandler handler = response.getBody();
 67  4
                 Header transferenc = response.getFirstHeader(HttpConstants.HEADER_TRANSFER_ENCODING);
 68  4
                 if (transferenc != null)
 69  
                 {
 70  2
                     response.removeHeaders(HttpConstants.HEADER_CONTENT_LENGTH);
 71  2
                     if (transferenc.getValue().indexOf(HttpConstants.TRANSFER_ENCODING_CHUNKED) != -1)
 72  
                     {
 73  2
                         outstream = new ChunkedOutputStream(outstream);
 74  
                     }
 75  
                 }
 76  
 
 77  4
                 handler.write(RequestContext.getEvent(), outstream);
 78  
 
 79  4
                 if (outstream instanceof ChunkedOutputStream)
 80  
                 {
 81  2
                     ((ChunkedOutputStream)outstream).finish();
 82  
                 }
 83  
             }
 84  
 
 85  4
             outstream.flush();
 86  4
             bos.flush();
 87  4
             byte[] result = bos.toByteArray();
 88  4
             outstream.close();
 89  4
             writer.close();
 90  4
             bos.close();
 91  
 
 92  4
             String output = null;
 93  
             try
 94  
             {
 95  4
                 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  4
             }
 104  
 
 105  4
             return output;
 106  
         }
 107  2
         catch (IOException e)
 108  
         {
 109  2
             throw new TransformerException(this, e);
 110  
         }
 111  
     }
 112  
 }