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
31
32 package org.apache.commons.httpclient.server;
33
34 import java.util.Iterator;
35
36 import org.apache.commons.httpclient.Header;
37 import org.apache.commons.httpclient.HeaderElement;
38 import org.apache.commons.httpclient.HeaderGroup;
39 import org.apache.commons.httpclient.NameValuePair;
40
41 /***
42 * A generic HTTP request.
43 *
44 * @author Oleg Kalnichevski
45 */
46 public class SimpleRequest {
47
48 private RequestLine requestLine = null;
49 private String contentType = "text/plain";
50 private String charSet = null;
51 private String bodyString = null;
52 private HeaderGroup headers = new HeaderGroup();
53
54 public SimpleRequest() {
55 super();
56 }
57
58 public SimpleRequest(
59 final RequestLine requestLine,
60 final Header[] headers,
61 final String bodyString)
62 {
63 super();
64 if (requestLine == null) {
65 throw new IllegalArgumentException("Request line may not be null");
66 }
67 this.requestLine = requestLine;
68 if (headers != null) {
69 this.headers.setHeaders(headers);
70 Header content = this.headers.getFirstHeader("Content-Type");
71 if (content != null) {
72 HeaderElement values[] = content.getElements();
73 if (values.length == 1) {
74 this.contentType = values[0].getName();
75 NameValuePair param = values[0].getParameterByName("charset");
76 if (param != null) {
77 this.charSet = param.getValue();
78 }
79 }
80 }
81 }
82 this.bodyString = bodyString;
83
84
85 }
86
87 public String getContentType() {
88 return this.contentType;
89 }
90
91 public String getCharSet() {
92 return this.charSet;
93 }
94
95 public String getBodyString() {
96 return this.bodyString;
97 }
98
99 public RequestLine getRequestLine() {
100 return this.requestLine;
101 }
102
103 public boolean containsHeader(final String name) {
104 return this.headers.containsHeader(name);
105 }
106
107 public Header[] getHeaders() {
108 return this.headers.getAllHeaders();
109 }
110
111 public Header getFirstHeader(final String s) {
112 return this.headers.getFirstHeader(s);
113 }
114
115 public Iterator getHeaderIterator() {
116 return this.headers.getIterator();
117 }
118 }