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.io.ByteArrayOutputStream;
35 import java.io.IOException;
36
37 import org.apache.commons.httpclient.Header;
38
39 /***
40 * A generic HTTP response.
41 *
42 * @author Christian Kohlschuetter
43 */
44 public class GenericResponse implements HttpRequestHandler {
45 private ByteArrayOutputStream bos = new ByteArrayOutputStream();
46 private String statusLine, contentType;
47 private String bodyString;
48 private byte[] bodyBytes;
49 private Header[] responseHeaders;
50
51 public GenericResponse() throws IOException {
52 this("HTTP/1.0 200 OK", "text/plain");
53 }
54 public GenericResponse(String statusLine, String contentType) {
55 this(statusLine, contentType, (Header[])null);
56 }
57
58 public GenericResponse(
59 String statusLine,
60 String contentType,
61 Header[] headers) {
62
63 this(statusLine, (String) null, contentType, headers);
64 }
65
66 public GenericResponse(
67 String statusLine,
68 String bodyString,
69 String contentType) {
70
71 this(statusLine, bodyString, contentType, null);
72 }
73
74 public GenericResponse(
75 String statusLine,
76 String bodyString,
77 String contentType,
78 Header[] headers) {
79
80 setStatusLine(statusLine);
81 setContentType(contentType);
82 setBodyString(bodyString);
83 setupBody();
84 }
85 public GenericResponse(
86 String statusLine,
87 byte[] bodyBytes,
88 String contentType,
89 Header[] headers) {
90 setStatusLine(statusLine);
91 setContentType(contentType);
92 setBodyBytes(bodyBytes);
93 setupBody();
94 }
95
96 public String getContentType() {
97 return contentType;
98 }
99 public void setContentType(String string) {
100 this.contentType = string;
101 }
102
103 public void setBodyString(String string) {
104 bodyString = string;
105 bodyBytes = null;
106 }
107 public void setBodyBytes(byte[] body) {
108 bodyBytes = body;
109 bodyString = null;
110 }
111
112 public String getStatusLine() {
113 return statusLine;
114 }
115
116 public void setStatusLine(String string) {
117 statusLine = string;
118 }
119
120 public Header[] getResponseHeaders() {
121 return responseHeaders;
122 }
123 public void setResponseHeaders(Header[] headers) {
124 responseHeaders = headers;
125 }
126
127 public void setupBody() {
128 try {
129 if (bodyString != null) {
130 ResponseWriter body = new ResponseWriter(bos);
131
132 if (bodyString != null) {
133 body.print(bodyString);
134 } else if (bodyBytes != null) {
135 body.write(bodyBytes);
136 }
137
138 body.close();
139 }
140 } catch (IOException e) {
141 e.printStackTrace(System.err);
142 }
143 }
144
145 public boolean processRequest(SimpleHttpServerConnection conn) throws IOException {
146
147 boolean haveContentLength = false;
148 boolean haveContentType = false;
149 ResponseWriter out = conn.getWriter();
150 out.println(getStatusLine());
151 if (responseHeaders != null) {
152 for (int i = 0; i < responseHeaders.length; i++) {
153 Header h = responseHeaders[i];
154 String name = h.getName();
155 if (name.equals("Content-Type")) {
156 haveContentType = true;
157 } else if (name.equals("Content-Length")) {
158 haveContentLength = true;
159 }
160
161 String value = h.getValue();
162 out.println(
163 ((null == name ? "" : name)
164 + ": "
165 + (null == value ? "" : value)));
166 }
167 }
168 if (!haveContentLength) {
169 out.print("Content-Length: ");
170 out.println(bos.size());
171 }
172 if (!haveContentType && getContentType() != null) {
173 out.print("Content-Type: ");
174 out.print(getContentType());
175 if (out.getEncoding() != null) {
176 out.print("; charset=");
177 out.println(out.getEncoding());
178 }
179 }
180 out.println();
181 out.write(bos.toByteArray());
182
183 bos.close();
184 return true;
185 }
186 }