1
2
3
4
5
6
7
8
9
10
11 package org.mule.transport.http;
12
13
14 import org.mule.util.IOUtils;
15
16 import java.io.IOException;
17 import java.io.InputStream;
18 import java.util.Iterator;
19
20 import org.apache.commons.httpclient.ChunkedInputStream;
21 import org.apache.commons.httpclient.ContentLengthInputStream;
22 import org.apache.commons.httpclient.Header;
23 import org.apache.commons.httpclient.HeaderElement;
24 import org.apache.commons.httpclient.HeaderGroup;
25 import org.apache.commons.httpclient.NameValuePair;
26
27
28
29
30 public class HttpRequest
31 {
32
33 private RequestLine requestLine = null;
34 private HeaderGroup headers = new HeaderGroup();
35 private InputStream entity = null;
36 private String defaultEncoding;
37
38 public HttpRequest(final RequestLine requestLine, final Header[] headers, final InputStream content, String defaultEncoding)
39 throws IOException
40 {
41 super();
42 if (requestLine == null)
43 {
44 throw new IllegalArgumentException("Request line may not be null");
45 }
46 this.defaultEncoding = defaultEncoding;
47 this.requestLine = requestLine;
48 if (headers != null)
49 {
50 this.headers.setHeaders(headers);
51 }
52 if (content != null)
53 {
54
55 String methodname = requestLine.getMethod();
56 if (HttpConstants.METHOD_POST.equalsIgnoreCase(methodname)
57 || HttpConstants.METHOD_PUT.equalsIgnoreCase(methodname))
58 {
59 Header contentLength = this.headers.getFirstHeader(HttpConstants.HEADER_CONTENT_LENGTH);
60 Header transferEncoding = this.headers.getFirstHeader(HttpConstants.HEADER_TRANSFER_ENCODING);
61 InputStream in = content;
62 if (transferEncoding != null)
63 {
64 if (transferEncoding.getValue().indexOf(HttpConstants.TRANSFER_ENCODING_CHUNKED) != -1)
65 {
66 in = new ChunkedInputStream(in);
67 }
68 }
69 else if (contentLength != null)
70 {
71 long len = getContentLength();
72 if (len >= 0)
73 {
74 in = new ContentLengthInputStream(in, len);
75 }
76 }
77 this.entity = in;
78 }
79 }
80 }
81
82 public HttpRequest(final RequestLine requestLine, final Header[] headers, String defaultEncoding) throws IOException
83 {
84 this(requestLine, headers, null, defaultEncoding);
85 }
86
87 public RequestLine getRequestLine()
88 {
89 return this.requestLine;
90 }
91
92 public void setRequestLine(final RequestLine requestline)
93 {
94 if (requestline == null)
95 {
96 throw new IllegalArgumentException("Request line may not be null");
97 }
98 this.requestLine = requestline;
99 }
100
101 public boolean containsHeader(final String name)
102 {
103 return this.headers.containsHeader(name);
104 }
105
106 public Header[] getHeaders()
107 {
108 return this.headers.getAllHeaders();
109 }
110
111 public Header getFirstHeader(final String s)
112 {
113 return this.headers.getFirstHeader(s);
114 }
115
116 public void removeHeaders(final String s)
117 {
118 if (s == null)
119 {
120 return;
121 }
122 Header[] headers = this.headers.getHeaders(s);
123 for (int i = 0; i < headers.length; i++)
124 {
125 this.headers.removeHeader(headers[i]);
126 }
127 }
128
129 public void addHeader(final Header header)
130 {
131 if (header == null)
132 {
133 return;
134 }
135 this.headers.addHeader(header);
136 }
137
138 public void setHeader(final Header header)
139 {
140 if (header == null)
141 {
142 return;
143 }
144 removeHeaders(header.getName());
145 addHeader(header);
146 }
147
148 public Iterator<?> getHeaderIterator()
149 {
150 return this.headers.getIterator();
151 }
152
153 public String getContentType()
154 {
155 Header contenttype = this.headers.getFirstHeader(HttpConstants.HEADER_CONTENT_TYPE);
156 if (contenttype != null)
157 {
158 return contenttype.getValue();
159 }
160 else
161 {
162 return HttpConstants.DEFAULT_CONTENT_TYPE;
163 }
164 }
165
166 public String getCharset()
167 {
168 String charset = null;
169 Header contenttype = this.headers.getFirstHeader(HttpConstants.HEADER_CONTENT_TYPE);
170 if (contenttype != null)
171 {
172 HeaderElement values[] = contenttype.getElements();
173 if (values.length == 1)
174 {
175 NameValuePair param = values[0].getParameterByName("charset");
176 if (param != null)
177 {
178 charset = param.getValue();
179 }
180 }
181 }
182 if (charset != null)
183 {
184 return charset;
185 }
186 else
187 {
188 return defaultEncoding;
189 }
190 }
191
192 public long getContentLength()
193 {
194 Header contentLength = this.headers.getFirstHeader(HttpConstants.HEADER_CONTENT_LENGTH);
195 if (contentLength != null)
196 {
197 try
198 {
199 return Long.parseLong(contentLength.getValue());
200 }
201 catch (NumberFormatException e)
202 {
203 return -1;
204 }
205 }
206 else
207 {
208 return -1;
209 }
210 }
211
212 public InputStream getBody()
213 {
214 return this.entity;
215 }
216
217 public byte[] getBodyBytes() throws IOException
218 {
219 InputStream in = getBody();
220 if (in != null)
221 {
222 return IOUtils.toByteArray(in);
223 }
224 else
225 {
226 return null;
227 }
228 }
229
230 public String getBodyString() throws IOException
231 {
232 byte[] raw = getBodyBytes();
233 if (raw != null)
234 {
235 return new String(raw, getCharset());
236 }
237 else
238 {
239 return null;
240 }
241 }
242
243 }