Coverage Report - org.mule.transport.servlet.HttpRequestMessageAdapter
 
Classes in this File Line Coverage Branch Coverage Complexity
HttpRequestMessageAdapter
35%
22/63
17%
6/36
2.909
 
 1  
 /*
 2  
  * $Id: HttpRequestMessageAdapter.java 12327 2008-07-14 02:41:54Z dfeist $
 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.servlet;
 12  
 
 13  
 import org.mule.api.MessagingException;
 14  
 import org.mule.api.ThreadSafeAccess;
 15  
 import org.mule.api.config.MuleProperties;
 16  
 import org.mule.api.transport.MessageTypeNotSupportedException;
 17  
 import org.mule.transport.AbstractMessageAdapter;
 18  
 import org.mule.transport.http.HttpConstants;
 19  
 import org.mule.util.UUID;
 20  
 
 21  
 import java.io.IOException;
 22  
 import java.util.Enumeration;
 23  
 import java.util.Iterator;
 24  
 import java.util.Map;
 25  
 
 26  
 import javax.servlet.http.HttpServletRequest;
 27  
 import javax.servlet.http.HttpSession;
 28  
 
 29  
 /**
 30  
  * <code>HttpRequestMessageAdapter</code> is a Mule message adapter for
 31  
  * javax.servletHttpServletRequest objects.
 32  
  */
 33  
 
 34  
 public class HttpRequestMessageAdapter extends AbstractMessageAdapter
 35  
 {
 36  
     /**
 37  
      * Serial version
 38  
      */
 39  
     private static final long serialVersionUID = -4238448252206941125L;
 40  
 
 41  
     private HttpServletRequest request;
 42  
 
 43  
     public HttpRequestMessageAdapter(Object message) throws MessagingException
 44  6
     {
 45  6
         if (message instanceof HttpServletRequest)
 46  
         {
 47  6
             setPayload((HttpServletRequest)message);
 48  6
             setContentEncoding((HttpServletRequest)message);
 49  
 
 50  6
             final Map parameterMap = request.getParameterMap();
 51  6
             if (parameterMap != null && parameterMap.size() > 0)
 52  
             {
 53  0
                 for (Iterator iterator = parameterMap.entrySet().iterator(); iterator.hasNext();)
 54  
                 {
 55  0
                     Map.Entry entry = (Map.Entry)iterator.next();
 56  0
                     String key = (String)entry.getKey();
 57  0
                     Object value = entry.getValue();
 58  0
                     if (value != null)
 59  
                     {
 60  0
                         if (value.getClass().isArray() && ((Object[])value).length == 1)
 61  
                         {
 62  0
                             setProperty(key, ((Object[])value)[0]);
 63  
                         }
 64  
                         else
 65  
                         {
 66  0
                             setProperty(key, value);
 67  
                         }
 68  
                     }
 69  0
                 }
 70  
             }
 71  
             String key;
 72  6
             for (Enumeration e = request.getAttributeNames(); e.hasMoreElements();)
 73  
             {
 74  0
                 key = (String)e.nextElement();
 75  0
                 properties.setProperty(key, request.getAttribute(key));
 76  
             }
 77  
             String realKey;
 78  6
             for (Enumeration e = request.getHeaderNames(); e.hasMoreElements();)
 79  
             {
 80  0
                 key = (String)e.nextElement();
 81  0
                 realKey = key;
 82  0
                 if (key.startsWith(HttpConstants.X_PROPERTY_PREFIX))
 83  
                 {
 84  0
                     realKey = key.substring(2);
 85  
                 }
 86  0
                 setProperty(realKey, request.getHeader(key));
 87  
             }
 88  6
         }
 89  
         else
 90  
         {
 91  0
             throw new MessageTypeNotSupportedException(message, getClass());
 92  
         }
 93  6
     }
 94  
 
 95  
     protected void setContentEncoding(HttpServletRequest request)
 96  
     {
 97  6
         String contentType = request.getContentType();
 98  6
         if (contentType != null)
 99  
         {
 100  0
             int i = contentType.indexOf("charset");
 101  0
             if (i > -1)
 102  
             {
 103  0
                 int x = contentType.lastIndexOf(";");
 104  0
                 if (x > i)
 105  
                 {
 106  0
                     setEncoding(contentType.substring(i + 8, x));
 107  
                 }
 108  
                 else
 109  
                 {
 110  0
                     setEncoding(contentType.substring(i + 8));
 111  
                 }
 112  
             }
 113  
         }
 114  6
     }
 115  
 
 116  
     protected HttpRequestMessageAdapter(HttpRequestMessageAdapter template)
 117  
     {
 118  0
         super(template);
 119  0
         request = template.request;
 120  0
     }
 121  
 
 122  
     /*
 123  
      * (non-Javadoc)
 124  
      * 
 125  
      * @see org.mule.api.providers.UMOMessageAdapter#getMessage()
 126  
      */
 127  
     public Object getPayload()
 128  
     {
 129  
         try
 130  
         {
 131  10
             return request.getInputStream();
 132  
         }
 133  0
         catch (IOException e)
 134  
         {
 135  0
             throw new RuntimeException(e);
 136  
         }
 137  
     }
 138  
 
 139  
     public boolean isBinary()
 140  
     {
 141  0
         return !request.getContentType().startsWith("text");
 142  
     }
 143  
 
 144  
     /*
 145  
      * (non-Javadoc)
 146  
      * 
 147  
      * @see org.mule.api.providers.UMOMessageAdapter#setMessage(java.lang.Object)
 148  
      */
 149  
     private void setPayload(HttpServletRequest message) throws MessagingException
 150  
     {
 151  6
         request = message;
 152  6
     }
 153  
 
 154  
     public HttpServletRequest getRequest()
 155  
     {
 156  2
         return request;
 157  
     }
 158  
 
 159  
     public String getUniqueId()
 160  
     {
 161  2
         HttpSession session = null;
 162  
 
 163  
         try
 164  
         {
 165  
             // We wrap this call as on some App Servers (Websfear) it can cause an
 166  
             // NPE
 167  2
             session = getRequest().getSession(false);
 168  
         }
 169  0
         catch (Exception e)
 170  
         {
 171  0
             return UUID.getUUID();
 172  
             //throw new UniqueIdNotSupportedException(this, CoreMessages.objectIsNull("Http session"));
 173  2
         }
 174  2
         if (session == null)
 175  
         {
 176  
             //throw new UniqueIdNotSupportedException(this, CoreMessages.objectIsNull("Http session"));
 177  2
             return UUID.getUUID();
 178  
         }
 179  0
         return session.getId();
 180  
     }
 181  
 
 182  
     /**
 183  
      * Sets a replyTo address for this message. This is useful in an asynchronous
 184  
      * environment where the caller doesn't wait for a response and the response
 185  
      * needs to be routed somewhere for further processing. The value of this field
 186  
      * can be any valid endpointUri url.
 187  
      * 
 188  
      * @param replyTo the endpointUri url to reply to
 189  
      */
 190  
     public void setReplyTo(Object replyTo)
 191  
     {
 192  0
         if (replyTo != null && replyTo.toString().startsWith("http"))
 193  
         {
 194  0
             setProperty(HttpConstants.HEADER_LOCATION, replyTo);
 195  
         }
 196  0
         setProperty(MuleProperties.MULE_REPLY_TO_PROPERTY, replyTo);
 197  0
     }
 198  
 
 199  
     /**
 200  
      * Sets a replyTo address for this message. This is useful in an asynchronous
 201  
      * environment where the caller doesn't wait for a response and the response
 202  
      * needs to be routed somewhere for further processing. The value of this field
 203  
      * can be any valid endpointUri url.
 204  
      * 
 205  
      * @return the endpointUri url to reply to or null if one has not been set
 206  
      */
 207  
     public Object getReplyTo()
 208  
     {
 209  0
         Object replyto = getProperty(MuleProperties.MULE_REPLY_TO_PROPERTY);
 210  0
         if (replyto == null)
 211  
         {
 212  0
             replyto = getProperty(HttpConstants.HEADER_LOCATION);
 213  
         }
 214  0
         return replyto;
 215  
     }
 216  
 
 217  
     public ThreadSafeAccess newThreadCopy()
 218  
     {
 219  0
         return new HttpRequestMessageAdapter(this);
 220  
     }
 221  
 
 222  
 }