Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
HttpMessageAdapter |
|
| 4.5;4.5 |
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 | 446 | private boolean http11 = true; |
36 | ||
37 | private Object message; | |
38 | ||
39 | public HttpMessageAdapter(Object message) | |
40 | 296 | { |
41 | 296 | if (message instanceof Object[]) |
42 | { | |
43 | // This case comes from the HttpMessageReceiver... | |
44 | 288 | Map headers = new HashMap(); |
45 | 288 | this.message = ((Object[]) message)[0]; |
46 | 288 | if (((Object[]) message).length > 1) |
47 | { | |
48 | 288 | Object second = ((Object[]) message)[1]; |
49 | 288 | if (second instanceof Map) |
50 | { | |
51 | 148 | Map props = (Map) second; |
52 | 148 | for (Iterator iterator = props.entrySet().iterator(); iterator.hasNext();) |
53 | { | |
54 | 1414 | Map.Entry e = (Map.Entry) iterator.next(); |
55 | 1414 | String key = (String) e.getKey(); |
56 | 1414 | Object value = e.getValue(); |
57 | // skip incoming null values | |
58 | 1414 | if (value != null) |
59 | { | |
60 | 1266 | headers.put(key, value); |
61 | } | |
62 | 1414 | } |
63 | 148 | } |
64 | 140 | else if (second instanceof Header[]) |
65 | { | |
66 | 140 | Header[] inboundHeaders = (Header[]) second; |
67 | 868 | for (int i = 0; i < inboundHeaders.length; i++) |
68 | { | |
69 | 728 | headers.put(inboundHeaders[i].getName(), inboundHeaders[i].getValue()); |
70 | } | |
71 | } | |
72 | 288 | addInboundProperties(headers); |
73 | } | |
74 | 288 | } |
75 | 8 | else if (message instanceof HttpResponse) |
76 | { | |
77 | 0 | this.message = message; |
78 | 0 | return; |
79 | } | |
80 | else | |
81 | { | |
82 | 8 | this.message = message; |
83 | } | |
84 | ||
85 | 296 | String temp = getStringProperty(HttpConnector.HTTP_VERSION_PROPERTY, null); |
86 | 296 | if (HttpConstants.HTTP10.equalsIgnoreCase(temp)) |
87 | { | |
88 | 0 | http11 = false; |
89 | } | |
90 | ||
91 | // set the encoding | |
92 | 296 | Header contenttype = getHeader(HttpConstants.HEADER_CONTENT_TYPE); |
93 | 296 | if (contenttype != null) |
94 | { | |
95 | 262 | HeaderElement values[] = contenttype.getElements(); |
96 | 262 | if (values.length == 1) |
97 | { | |
98 | 262 | NameValuePair param = values[0].getParameterByName("charset"); |
99 | 262 | if (param != null) |
100 | { | |
101 | 238 | encoding = param.getValue(); |
102 | } | |
103 | } | |
104 | } | |
105 | 296 | } |
106 | ||
107 | protected HttpMessageAdapter(HttpMessageAdapter template) | |
108 | { | |
109 | 150 | super(template); |
110 | 150 | message = template.message; |
111 | 150 | http11 = template.http11; |
112 | 150 | } |
113 | ||
114 | /** @return the current message */ | |
115 | public Object getPayload() | |
116 | { | |
117 | 1278 | 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 | 7728 | if (HttpConstants.HEADER_KEEP_ALIVE.equals(key) || HttpConstants.HEADER_CONNECTION.equals(key)) |
128 | { | |
129 | 0 | if (!http11) |
130 | { | |
131 | 0 | String connection = super.getStringProperty(HttpConstants.HEADER_CONNECTION, null); |
132 | 0 | if (connection != null && connection.equalsIgnoreCase("close")) |
133 | { | |
134 | 0 | return "false"; |
135 | } | |
136 | else | |
137 | { | |
138 | 0 | return "true"; |
139 | } | |
140 | } | |
141 | else | |
142 | { | |
143 | 0 | return (super.getProperty(HttpConstants.HEADER_CONNECTION) != null ? "true" : "false"); |
144 | } | |
145 | } | |
146 | else | |
147 | { | |
148 | 7728 | return super.getProperty(key); |
149 | } | |
150 | } | |
151 | ||
152 | public Header getHeader(String name) | |
153 | { | |
154 | 296 | String value = getStringProperty(name, null); |
155 | 296 | if (value == null) |
156 | { | |
157 | 34 | return null; |
158 | } | |
159 | 262 | return new Header(name, value); |
160 | } | |
161 | ||
162 | public ThreadSafeAccess newThreadCopy() | |
163 | { | |
164 | 150 | return new HttpMessageAdapter(this); |
165 | } | |
166 | ||
167 | } |