View Javadoc

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      {
45          if (message instanceof HttpServletRequest)
46          {
47              setPayload((HttpServletRequest)message);
48              setContentEncoding((HttpServletRequest)message);
49  
50              final Map parameterMap = request.getParameterMap();
51              if (parameterMap != null && parameterMap.size() > 0)
52              {
53                  for (Iterator iterator = parameterMap.entrySet().iterator(); iterator.hasNext();)
54                  {
55                      Map.Entry entry = (Map.Entry)iterator.next();
56                      String key = (String)entry.getKey();
57                      Object value = entry.getValue();
58                      if (value != null)
59                      {
60                          if (value.getClass().isArray() && ((Object[])value).length == 1)
61                          {
62                              setProperty(key, ((Object[])value)[0]);
63                          }
64                          else
65                          {
66                              setProperty(key, value);
67                          }
68                      }
69                  }
70              }
71              String key;
72              for (Enumeration e = request.getAttributeNames(); e.hasMoreElements();)
73              {
74                  key = (String)e.nextElement();
75                  properties.setProperty(key, request.getAttribute(key));
76              }
77              String realKey;
78              for (Enumeration e = request.getHeaderNames(); e.hasMoreElements();)
79              {
80                  key = (String)e.nextElement();
81                  realKey = key;
82                  if (key.startsWith(HttpConstants.X_PROPERTY_PREFIX))
83                  {
84                      realKey = key.substring(2);
85                  }
86                  setProperty(realKey, request.getHeader(key));
87              }
88          }
89          else
90          {
91              throw new MessageTypeNotSupportedException(message, getClass());
92          }
93      }
94  
95      protected void setContentEncoding(HttpServletRequest request)
96      {
97          String contentType = request.getContentType();
98          if (contentType != null)
99          {
100             int i = contentType.indexOf("charset");
101             if (i > -1)
102             {
103                 int x = contentType.lastIndexOf(";");
104                 if (x > i)
105                 {
106                     setEncoding(contentType.substring(i + 8, x));
107                 }
108                 else
109                 {
110                     setEncoding(contentType.substring(i + 8));
111                 }
112             }
113         }
114     }
115 
116     protected HttpRequestMessageAdapter(HttpRequestMessageAdapter template)
117     {
118         super(template);
119         request = template.request;
120     }
121 
122     /*
123      * (non-Javadoc)
124      * 
125      * @see org.mule.api.providers.UMOMessageAdapter#getMessage()
126      */
127     public Object getPayload()
128     {
129         try
130         {
131             return request.getInputStream();
132         }
133         catch (IOException e)
134         {
135             throw new RuntimeException(e);
136         }
137     }
138 
139     public boolean isBinary()
140     {
141         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         request = message;
152     }
153 
154     public HttpServletRequest getRequest()
155     {
156         return request;
157     }
158 
159     public String getUniqueId()
160     {
161         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             session = getRequest().getSession(false);
168         }
169         catch (Exception e)
170         {
171             return UUID.getUUID();
172             //throw new UniqueIdNotSupportedException(this, CoreMessages.objectIsNull("Http session"));
173         }
174         if (session == null)
175         {
176             //throw new UniqueIdNotSupportedException(this, CoreMessages.objectIsNull("Http session"));
177             return UUID.getUUID();
178         }
179         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         if (replyTo != null && replyTo.toString().startsWith("http"))
193         {
194             setProperty(HttpConstants.HEADER_LOCATION, replyTo);
195         }
196         setProperty(MuleProperties.MULE_REPLY_TO_PROPERTY, replyTo);
197     }
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         Object replyto = getProperty(MuleProperties.MULE_REPLY_TO_PROPERTY);
210         if (replyto == null)
211         {
212             replyto = getProperty(HttpConstants.HEADER_LOCATION);
213         }
214         return replyto;
215     }
216 
217     public ThreadSafeAccess newThreadCopy()
218     {
219         return new HttpRequestMessageAdapter(this);
220     }
221 
222 }