View Javadoc

1   /*
2    * $Id: HttpMessageAdapter.java 7976 2007-08-21 14:26:13Z 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      private static final UMOTransformer transformer = new SerializableToByteArray();
38  
39      private final Object message;
40      private boolean http11 = true;
41  
42      public HttpMessageAdapter(Object message) throws MessageTypeNotSupportedException
43      {
44          if (message instanceof Object[])
45          {
46              this.message = ((Object[])message)[0];
47              if (((Object[])message).length > 1)
48              {
49                  Map props = (Map)((Object[])message)[1];
50                  for (Iterator iterator = props.entrySet().iterator(); iterator.hasNext();)
51                  {
52                      Map.Entry e = (Map.Entry)iterator.next();
53                      String key = (String)e.getKey();
54                      Object value = e.getValue();
55                      // skip incoming null values
56                      if (value != null)
57                      {
58                          setProperty(key, value);
59                      }
60                  }
61              }
62          }
63          else if (message instanceof byte[])
64          {
65              this.message = message;
66              // If the adapter is being created as part of a response flow, just wrap
67              // the HttpResponse
68          }
69          else if (message instanceof HttpResponse)
70          {
71              this.message = message;
72              return;
73          }
74          else
75          {
76              throw new MessageTypeNotSupportedException(message, getClass());
77          }
78          String temp = getStringProperty(HttpConnector.HTTP_VERSION_PROPERTY, null);
79          if (HttpConstants.HTTP10.equalsIgnoreCase(temp))
80          {
81              http11 = false;
82          }
83  
84          // set the encoding
85          Header contenttype = getHeader(HttpConstants.HEADER_CONTENT_TYPE);
86          if (contenttype != null)
87          {
88              HeaderElement values[] = contenttype.getElements();
89              if (values.length == 1)
90              {
91                  NameValuePair param = values[0].getParameterByName("charset");
92                  if (param != null)
93                  {
94                      encoding = param.getValue();
95                  }
96              }
97          }
98      }
99  
100     protected HttpMessageAdapter(HttpMessageAdapter template)
101     {
102         super(template);
103         message = template.message;
104         http11 = template.http11;
105     }
106 
107     /*
108      * (non-Javadoc)
109      * 
110      * @see org.mule.umo.providers.UMOMessageAdapter#getPayload()
111      */
112     public Object getPayload()
113     {
114         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         if (message instanceof byte[])
125         {
126             return (byte[])message;
127         }
128         else if (message instanceof String)
129         {
130             return message.toString().getBytes();
131         }
132         else
133         {
134             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         if (message instanceof byte[])
147         {
148             if (encoding != null)
149             {
150                 return new String((byte[])message, encoding);
151             }
152             else
153             {
154                 return new String((byte[])message);
155             }
156         }
157         else
158         {
159             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         if (HttpConstants.HEADER_KEEP_ALIVE.equals(key) || HttpConstants.HEADER_CONNECTION.equals(key))
171         {
172             if (!http11)
173             {
174                 String connection = super.getStringProperty(HttpConstants.HEADER_CONNECTION, null);
175                 if (connection != null && connection.equalsIgnoreCase("close"))
176                 {
177                     return "false";
178                 }
179                 else
180                 {
181                     return "true";
182                 }
183             }
184             else
185             {
186                 return (super.getProperty(HttpConstants.HEADER_CONNECTION) != null ? "true" : "false");
187             }
188         }
189         else
190         {
191             return super.getProperty(key);
192         }
193     }
194 
195     public Header getHeader(String name)
196     {
197         String value = getStringProperty(name, null);
198         if (value == null)
199         {
200             return null;
201         }
202         return new Header(name, value);
203     }
204 
205     public ThreadSafeAccess newThreadCopy()
206     {
207         return new HttpMessageAdapter(this);
208     }
209 
210 }