View Javadoc

1   /*
2    * $Id: HttpMessageAdapter.java 10489 2008-01-23 17:53:38Z 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.http;
12  
13  import org.mule.api.ThreadSafeAccess;
14  import org.mule.transport.AbstractMessageAdapter;
15  
16  import java.util.HashMap;
17  import java.util.Iterator;
18  import java.util.Map;
19  
20  import org.apache.commons.httpclient.Header;
21  import org.apache.commons.httpclient.HeaderElement;
22  import org.apache.commons.httpclient.NameValuePair;
23  
24  /**
25   * <code>HttpMessageAdapter</code> Wraps an incoming Http Request making the
26   * payload and headers available as standard message adapter.
27   */
28  public class HttpMessageAdapter extends AbstractMessageAdapter
29  {
30      /**
31       * Serial version
32       */
33      private static final long serialVersionUID = -1544495479333000422L;
34  
35      private boolean http11 = true;
36  
37      private Object message;
38  
39      public HttpMessageAdapter(Object message)
40      {
41          if (message instanceof Object[])
42          {
43              // This case comes from the HttpMessageReceiver...
44              Map headers = new HashMap();
45              this.message = ((Object[]) message)[0];
46              if (((Object[]) message).length > 1)
47              {
48                  Object second = ((Object[]) message)[1];
49                  if (second instanceof Map)
50                  {
51                      Map props = (Map) second;
52                      for (Iterator iterator = props.entrySet().iterator(); iterator.hasNext();)
53                      {
54                          Map.Entry e = (Map.Entry) iterator.next();
55                          String key = (String) e.getKey();
56                          Object value = e.getValue();
57                          // skip incoming null values
58                          if (value != null)
59                          {
60                              headers.put(key, value);
61                          }
62                      }
63                  }
64                  else if (second instanceof Header[])
65                  {
66                      Header[] inboundHeaders = (Header[]) second;
67                      for (int i = 0; i < inboundHeaders.length; i++)
68                      {
69                          headers.put(inboundHeaders[i].getName(), inboundHeaders[i].getValue());
70                      }
71                  }
72                  addInboundProperties(headers);
73              }
74          }
75          else if (message instanceof HttpResponse)
76          {
77              this.message = message;
78              return;
79          }
80          else
81          {
82              this.message = message;
83          }
84  
85          String temp = getStringProperty(HttpConnector.HTTP_VERSION_PROPERTY, null);
86          if (HttpConstants.HTTP10.equalsIgnoreCase(temp))
87          {
88              http11 = false;
89          }
90  
91          // set the encoding
92          Header contenttype = getHeader(HttpConstants.HEADER_CONTENT_TYPE);
93          if (contenttype != null)
94          {
95              HeaderElement values[] = contenttype.getElements();
96              if (values.length == 1)
97              {
98                  NameValuePair param = values[0].getParameterByName("charset");
99                  if (param != null)
100                 {
101                     encoding = param.getValue();
102                 }
103             }
104         }
105     }
106 
107     protected HttpMessageAdapter(HttpMessageAdapter template)
108     {
109         super(template);
110         message = template.message;
111         http11 = template.http11;
112     }
113 
114     /** @return the current message */
115     public Object getPayload()
116     {
117         return message;
118     }
119 
120     /*
121     * (non-Javadoc)
122     *
123     * @see org.mule.transport.UMOMessageAdapter#getProperty(java.lang.Object)
124     */
125     public Object getProperty(String key)
126     {
127         if (HttpConstants.HEADER_KEEP_ALIVE.equals(key) || HttpConstants.HEADER_CONNECTION.equals(key))
128         {
129             if (!http11)
130             {
131                 String connection = super.getStringProperty(HttpConstants.HEADER_CONNECTION, null);
132                 if (connection != null && connection.equalsIgnoreCase("close"))
133                 {
134                     return "false";
135                 }
136                 else
137                 {
138                     return "true";
139                 }
140             }
141             else
142             {
143                 return (super.getProperty(HttpConstants.HEADER_CONNECTION) != null ? "true" : "false");
144             }
145         }
146         else
147         {
148             return super.getProperty(key);
149         }
150     }
151 
152     public Header getHeader(String name)
153     {
154         String value = getStringProperty(name, null);
155         if (value == null)
156         {
157             return null;
158         }
159         return new Header(name, value);
160     }
161 
162     public ThreadSafeAccess newThreadCopy()
163     {
164         return new HttpMessageAdapter(this);
165     }
166 
167 }