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