Coverage Report - org.mule.providers.http.HttpMessageAdapter
 
Classes in this File Line Coverage Branch Coverage Complexity
HttpMessageAdapter
82%
49/60
64%
27/42
4.5
 
 1  
 /*
 2  
  * $Id: HttpMessageAdapter.java 7963 2007-08-21 08:53:15Z 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;
 12  
 
 13  
 import org.mule.impl.ThreadSafeAccess;
 14  
 import org.mule.providers.AbstractMessageAdapter;
 15  
 import org.mule.transformers.simple.SerializableToByteArray;
 16  
 import org.mule.umo.provider.MessageTypeNotSupportedException;
 17  
 import org.mule.umo.transformer.UMOTransformer;
 18  
 
 19  
 import java.util.Iterator;
 20  
 import java.util.Map;
 21  
 
 22  
 import org.apache.commons.httpclient.Header;
 23  
 import org.apache.commons.httpclient.HeaderElement;
 24  
 import org.apache.commons.httpclient.NameValuePair;
 25  
 
 26  
 /**
 27  
  * <code>HttpMessageAdapter</code> Wraps an incoming Http Request making the
 28  
  * payload and headers available as standard message adapter.
 29  
  */
 30  
 public class HttpMessageAdapter extends AbstractMessageAdapter
 31  
 {
 32  
     /**
 33  
      * Serial version
 34  
      */
 35  
     private static final long serialVersionUID = -1544495479333000422L;
 36  
 
 37  2
     private static final UMOTransformer transformer = new SerializableToByteArray();
 38  
 
 39  
     private final Object message;
 40  294
     private boolean http11 = true;
 41  
 
 42  
     public HttpMessageAdapter(Object message) throws MessageTypeNotSupportedException
 43  284
     {
 44  284
         if (message instanceof Object[])
 45  
         {
 46  274
             this.message = ((Object[])message)[0];
 47  274
             if (((Object[])message).length > 1)
 48  
             {
 49  274
                 Map props = (Map)((Object[])message)[1];
 50  274
                 for (Iterator iterator = props.entrySet().iterator(); iterator.hasNext();)
 51  
                 {
 52  2158
                     Map.Entry e = (Map.Entry)iterator.next();
 53  2158
                     String key = (String)e.getKey();
 54  2158
                     Object value = e.getValue();
 55  
                     // skip incoming null values
 56  2158
                     if (value != null)
 57  
                     {
 58  2024
                         setProperty(key, value);
 59  
                     }
 60  2158
                 }
 61  274
             }
 62  
         }
 63  10
         else if (message instanceof byte[])
 64  
         {
 65  8
             this.message = message;
 66  
             // If the adapter is being created as part of a response flow, just wrap
 67  
             // the HttpResponse
 68  
         }
 69  2
         else if (message instanceof HttpResponse)
 70  
         {
 71  0
             this.message = message;
 72  0
             return;
 73  
         }
 74  
         else
 75  
         {
 76  2
             throw new MessageTypeNotSupportedException(message, getClass());
 77  
         }
 78  282
         String temp = getStringProperty(HttpConnector.HTTP_VERSION_PROPERTY, null);
 79  282
         if (HttpConstants.HTTP10.equalsIgnoreCase(temp))
 80  
         {
 81  0
             http11 = false;
 82  
         }
 83  
 
 84  
         // set the encoding
 85  282
         Header contenttype = getHeader(HttpConstants.HEADER_CONTENT_TYPE);
 86  282
         if (contenttype != null)
 87  
         {
 88  250
             HeaderElement values[] = contenttype.getElements();
 89  250
             if (values.length == 1)
 90  
             {
 91  250
                 NameValuePair param = values[0].getParameterByName("charset");
 92  250
                 if (param != null)
 93  
                 {
 94  218
                     encoding = param.getValue();
 95  
                 }
 96  
             }
 97  
         }
 98  282
     }
 99  
 
 100  
     protected HttpMessageAdapter(HttpMessageAdapter template)
 101  
     {
 102  10
         super(template);
 103  10
         message = template.message;
 104  10
         http11 = template.http11;
 105  10
     }
 106  
 
 107  
     /*
 108  
      * (non-Javadoc)
 109  
      * 
 110  
      * @see org.mule.umo.providers.UMOMessageAdapter#getPayload()
 111  
      */
 112  
     public Object getPayload()
 113  
     {
 114  548
         return message;
 115  
     }
 116  
 
 117  
     /*
 118  
      * (non-Javadoc)
 119  
      * 
 120  
      * @see org.mule.umo.providers.UMOMessageAdapter#getPayloadAsBytes()
 121  
      */
 122  
     public byte[] getPayloadAsBytes() throws Exception
 123  
     {
 124  2
         if (message instanceof byte[])
 125  
         {
 126  2
             return (byte[])message;
 127  
         }
 128  0
         else if (message instanceof String)
 129  
         {
 130  0
             return message.toString().getBytes();
 131  
         }
 132  
         else
 133  
         {
 134  0
             return (byte[])transformer.transform(message);
 135  
         }
 136  
     }
 137  
 
 138  
     /*
 139  
      * (non-Javadoc)
 140  
      * 
 141  
      * @see org.mule.umo.providers.UMOMessageAdapter#getPayloadAsString(String
 142  
      *      encoding)
 143  
      */
 144  
     public String getPayloadAsString(String encoding) throws Exception
 145  
     {
 146  78
         if (message instanceof byte[])
 147  
         {
 148  70
             if (encoding != null)
 149  
             {
 150  70
                 return new String((byte[])message, encoding);
 151  
             }
 152  
             else
 153  
             {
 154  0
                 return new String((byte[])message);
 155  
             }
 156  
         }
 157  
         else
 158  
         {
 159  8
             return message.toString();
 160  
         }
 161  
     }
 162  
 
 163  
     /*
 164  
      * (non-Javadoc)
 165  
      * 
 166  
      * @see org.mule.providers.UMOMessageAdapter#getProperty(java.lang.Object)
 167  
      */
 168  
     public Object getProperty(String key)
 169  
     {
 170  4670
         if (HttpConstants.HEADER_KEEP_ALIVE.equals(key) || HttpConstants.HEADER_CONNECTION.equals(key))
 171  
         {
 172  64
             if (!http11)
 173  
             {
 174  0
                 String connection = super.getStringProperty(HttpConstants.HEADER_CONNECTION, null);
 175  0
                 if (connection != null && connection.equalsIgnoreCase("close"))
 176  
                 {
 177  0
                     return "false";
 178  
                 }
 179  
                 else
 180  
                 {
 181  0
                     return "true";
 182  
                 }
 183  
             }
 184  
             else
 185  
             {
 186  64
                 return (super.getProperty(HttpConstants.HEADER_CONNECTION) != null ? "true" : "false");
 187  
             }
 188  
         }
 189  
         else
 190  
         {
 191  4606
             return super.getProperty(key);
 192  
         }
 193  
     }
 194  
 
 195  
     public Header getHeader(String name)
 196  
     {
 197  282
         String value = getStringProperty(name, null);
 198  282
         if (value == null)
 199  
         {
 200  32
             return null;
 201  
         }
 202  250
         return new Header(name, value);
 203  
     }
 204  
 
 205  
     public ThreadSafeAccess newThreadCopy()
 206  
     {
 207  10
         return new HttpMessageAdapter(this);
 208  
     }
 209  
 
 210  
 }