1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
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 | |
|
28 | |
|
29 | |
|
30 | |
public class HttpMessageAdapter extends AbstractMessageAdapter |
31 | |
{ |
32 | |
|
33 | |
|
34 | |
|
35 | |
private static final long serialVersionUID = -1544495479333000422L; |
36 | |
|
37 | 2 | private static final UMOTransformer transformer = new SerializableToByteArray(); |
38 | |
|
39 | |
private final Object message; |
40 | 294 | private boolean http11 = true; |
41 | |
|
42 | |
public HttpMessageAdapter(Object message) throws MessageTypeNotSupportedException |
43 | 284 | { |
44 | 284 | if (message instanceof Object[]) |
45 | |
{ |
46 | 274 | this.message = ((Object[])message)[0]; |
47 | 274 | if (((Object[])message).length > 1) |
48 | |
{ |
49 | 274 | Map props = (Map)((Object[])message)[1]; |
50 | 274 | for (Iterator iterator = props.entrySet().iterator(); iterator.hasNext();) |
51 | |
{ |
52 | 2158 | Map.Entry e = (Map.Entry)iterator.next(); |
53 | 2158 | String key = (String)e.getKey(); |
54 | 2158 | Object value = e.getValue(); |
55 | |
|
56 | 2158 | if (value != null) |
57 | |
{ |
58 | 2024 | setProperty(key, value); |
59 | |
} |
60 | 2158 | } |
61 | 274 | } |
62 | |
} |
63 | 10 | else if (message instanceof byte[]) |
64 | |
{ |
65 | 8 | this.message = message; |
66 | |
|
67 | |
|
68 | |
} |
69 | 2 | else if (message instanceof HttpResponse) |
70 | |
{ |
71 | 0 | this.message = message; |
72 | 0 | return; |
73 | |
} |
74 | |
else |
75 | |
{ |
76 | 2 | throw new MessageTypeNotSupportedException(message, getClass()); |
77 | |
} |
78 | 282 | String temp = getStringProperty(HttpConnector.HTTP_VERSION_PROPERTY, null); |
79 | 282 | if (HttpConstants.HTTP10.equalsIgnoreCase(temp)) |
80 | |
{ |
81 | 0 | http11 = false; |
82 | |
} |
83 | |
|
84 | |
|
85 | 282 | Header contenttype = getHeader(HttpConstants.HEADER_CONTENT_TYPE); |
86 | 282 | if (contenttype != null) |
87 | |
{ |
88 | 250 | HeaderElement values[] = contenttype.getElements(); |
89 | 250 | if (values.length == 1) |
90 | |
{ |
91 | 250 | NameValuePair param = values[0].getParameterByName("charset"); |
92 | 250 | if (param != null) |
93 | |
{ |
94 | 218 | encoding = param.getValue(); |
95 | |
} |
96 | |
} |
97 | |
} |
98 | 282 | } |
99 | |
|
100 | |
protected HttpMessageAdapter(HttpMessageAdapter template) |
101 | |
{ |
102 | 10 | super(template); |
103 | 10 | message = template.message; |
104 | 10 | http11 = template.http11; |
105 | 10 | } |
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
public Object getPayload() |
113 | |
{ |
114 | 548 | return message; |
115 | |
} |
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
public byte[] getPayloadAsBytes() throws Exception |
123 | |
{ |
124 | 2 | if (message instanceof byte[]) |
125 | |
{ |
126 | 2 | return (byte[])message; |
127 | |
} |
128 | 0 | else if (message instanceof String) |
129 | |
{ |
130 | 0 | return message.toString().getBytes(); |
131 | |
} |
132 | |
else |
133 | |
{ |
134 | 0 | return (byte[])transformer.transform(message); |
135 | |
} |
136 | |
} |
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
public String getPayloadAsString(String encoding) throws Exception |
145 | |
{ |
146 | 78 | if (message instanceof byte[]) |
147 | |
{ |
148 | 70 | if (encoding != null) |
149 | |
{ |
150 | 70 | return new String((byte[])message, encoding); |
151 | |
} |
152 | |
else |
153 | |
{ |
154 | 0 | return new String((byte[])message); |
155 | |
} |
156 | |
} |
157 | |
else |
158 | |
{ |
159 | 8 | return message.toString(); |
160 | |
} |
161 | |
} |
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
public Object getProperty(String key) |
169 | |
{ |
170 | 4670 | if (HttpConstants.HEADER_KEEP_ALIVE.equals(key) || HttpConstants.HEADER_CONNECTION.equals(key)) |
171 | |
{ |
172 | 64 | if (!http11) |
173 | |
{ |
174 | 0 | String connection = super.getStringProperty(HttpConstants.HEADER_CONNECTION, null); |
175 | 0 | if (connection != null && connection.equalsIgnoreCase("close")) |
176 | |
{ |
177 | 0 | return "false"; |
178 | |
} |
179 | |
else |
180 | |
{ |
181 | 0 | return "true"; |
182 | |
} |
183 | |
} |
184 | |
else |
185 | |
{ |
186 | 64 | return (super.getProperty(HttpConstants.HEADER_CONNECTION) != null ? "true" : "false"); |
187 | |
} |
188 | |
} |
189 | |
else |
190 | |
{ |
191 | 4606 | return super.getProperty(key); |
192 | |
} |
193 | |
} |
194 | |
|
195 | |
public Header getHeader(String name) |
196 | |
{ |
197 | 282 | String value = getStringProperty(name, null); |
198 | 282 | if (value == null) |
199 | |
{ |
200 | 32 | return null; |
201 | |
} |
202 | 250 | return new Header(name, value); |
203 | |
} |
204 | |
|
205 | |
public ThreadSafeAccess newThreadCopy() |
206 | |
{ |
207 | 10 | return new HttpMessageAdapter(this); |
208 | |
} |
209 | |
|
210 | |
} |