KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > http > HttpMessageAdapter


1 /*
2  * $Id: HttpMessageAdapter.java 3982 2006-11-22 14:28:01Z lajos $
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 MuleSource MPL
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.apache.commons.httpclient.Header;
14 import org.apache.commons.httpclient.HeaderElement;
15 import org.apache.commons.httpclient.NameValuePair;
16 import org.mule.MuleManager;
17 import org.mule.providers.AbstractMessageAdapter;
18 import org.mule.transformers.simple.SerializableToByteArray;
19 import org.mule.umo.MessagingException;
20 import org.mule.umo.provider.MessageTypeNotSupportedException;
21 import org.mule.umo.transformer.UMOTransformer;
22
23 import java.util.Iterator JavaDoc;
24 import java.util.Map JavaDoc;
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 JavaDoc message;
40     private boolean http11 = true;
41
42     public HttpMessageAdapter(Object JavaDoc message) throws MessagingException
43     {
44         if (message instanceof Object JavaDoc[])
45         {
46             this.message = ((Object JavaDoc[])message)[0];
47             if (((Object JavaDoc[])message).length > 1)
48             {
49                 Map JavaDoc props = (Map JavaDoc)((Object JavaDoc[])message)[1];
50                 for (Iterator JavaDoc iterator = props.entrySet().iterator(); iterator.hasNext();)
51                 {
52                     Map.Entry JavaDoc e = (Map.Entry JavaDoc)iterator.next();
53                     String JavaDoc key = (String JavaDoc)e.getKey();
54                     Object JavaDoc 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 JavaDoc temp = getStringProperty(HttpConnector.HTTP_VERSION_PROPERTY, null);
79         if (HttpConstants.HTTP10.equalsIgnoreCase(temp))
80         {
81             http11 = false;
82         }
83
84         // set the encoding
85
String JavaDoc charset = null;
86         Header contenttype = getHeader(HttpConstants.HEADER_CONTENT_TYPE);
87         if (contenttype != null)
88         {
89             HeaderElement values[] = contenttype.getElements();
90             if (values.length == 1)
91             {
92                 NameValuePair param = values[0].getParameterByName("charset");
93                 if (param != null)
94                 {
95                     charset = param.getValue();
96                 }
97             }
98         }
99         if (charset != null)
100         {
101             encoding = charset;
102         }
103         else
104         {
105             encoding = MuleManager.getConfiguration().getEncoding();
106         }
107     }
108
109     /*
110      * (non-Javadoc)
111      *
112      * @see org.mule.umo.providers.UMOMessageAdapter#getPayload()
113      */

114     public Object JavaDoc getPayload()
115     {
116         return message;
117     }
118
119     /*
120      * (non-Javadoc)
121      *
122      * @see org.mule.umo.providers.UMOMessageAdapter#getPayloadAsBytes()
123      */

124     public byte[] getPayloadAsBytes() throws Exception JavaDoc
125     {
126         if (message instanceof byte[])
127         {
128             return (byte[])message;
129         }
130         else if (message instanceof String JavaDoc)
131         {
132             return message.toString().getBytes();
133         }
134         else
135         {
136             return (byte[])transformer.transform(message);
137         }
138     }
139
140     /*
141      * (non-Javadoc)
142      *
143      * @see org.mule.umo.providers.UMOMessageAdapter#getPayloadAsString(String
144      * encoding)
145      */

146     public String JavaDoc getPayloadAsString(String JavaDoc encoding) throws Exception JavaDoc
147     {
148         if (message instanceof byte[])
149         {
150             if (encoding != null)
151             {
152                 return new String JavaDoc((byte[])message, encoding);
153             }
154             else
155             {
156                 return new String JavaDoc((byte[])message);
157             }
158         }
159         else
160         {
161             return message.toString();
162         }
163     }
164
165     /*
166      * (non-Javadoc)
167      *
168      * @see org.mule.providers.UMOMessageAdapter#getProperty(java.lang.Object)
169      */

170     public Object JavaDoc getProperty(String JavaDoc key)
171     {
172         if (HttpConstants.HEADER_KEEP_ALIVE.equals(key) || HttpConstants.HEADER_CONNECTION.equals(key))
173         {
174             if (!http11)
175             {
176                 String JavaDoc connection = super.getStringProperty(HttpConstants.HEADER_CONNECTION, null);
177                 if (connection != null && connection.equalsIgnoreCase("close"))
178                 {
179                     return "false";
180                 }
181                 else
182                 {
183                     return "true";
184                 }
185             }
186             else
187             {
188                 return (super.getProperty(HttpConstants.HEADER_CONNECTION) != null ? "true" : "false");
189             }
190         }
191         else
192         {
193             return super.getProperty(key);
194         }
195     }
196
197     public Header getHeader(String JavaDoc name)
198     {
199         String JavaDoc value = getStringProperty(name, null);
200         if (value == null)
201         {
202             return null;
203         }
204         return new Header(name, value);
205     }
206 }
207
Popular Tags