1 /*
2 * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/GenericResponse.java,v 1.1.2.2 2003/11/24 20:41:11 oglueck Exp $
3 * $Revision: 1.1.2.2 $
4 * $Date: 2003/11/24 20:41:11 $
5 *
6 * ====================================================================
7 *
8 * The Apache Software License, Version 1.1
9 *
10 * Copyright (c) 1999-2003 The Apache Software Foundation. All rights
11 * reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 *
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 *
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in
22 * the documentation and/or other materials provided with the
23 * distribution.
24 *
25 * 3. The end-user documentation included with the redistribution, if
26 * any, must include the following acknowlegement:
27 * "This product includes software developed by the
28 * Apache Software Foundation (http://www.apache.org/)."
29 * Alternately, this acknowlegement may appear in the software itself,
30 * if and wherever such third-party acknowlegements normally appear.
31 *
32 * 4. The names "The Jakarta Project", "Commons", and "Apache Software
33 * Foundation" must not be used to endorse or promote products derived
34 * from this software without prior written permission. For written
35 * permission, please contact apache@apache.org.
36 *
37 * 5. Products derived from this software may not be called "Apache"
38 * nor may "Apache" appear in their names without prior written
39 * permission of the Apache Group.
40 *
41 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
42 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
45 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
48 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
49 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
51 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * SUCH DAMAGE.
53 * ====================================================================
54 *
55 * This software consists of voluntary contributions made by many
56 * individuals on behalf of the Apache Software Foundation. For more
57 * information on the Apache Software Foundation, please see
58 * <http://www.apache.org/>.
59 *
60 * [Additional notices, if required by prior licensing conditions]
61 *
62 */
63
64 package org.apache.commons.httpclient.server;
65
66 import java.io.ByteArrayOutputStream;
67 import java.io.IOException;
68
69 import org.apache.commons.httpclient.Header;
70
71 /***
72 * A generic HTTP response.
73 *
74 * @author Christian Kohlschuetter
75 */
76 public class GenericResponse implements HttpRequestHandler {
77 private ByteArrayOutputStream bos = new ByteArrayOutputStream();
78 private String statusLine, contentType;
79 private String bodyString;
80 private byte[] bodyBytes;
81 private Header[] responseHeaders;
82
83 public GenericResponse() throws IOException {
84 this("HTTP/1.0 200 OK", "text/plain");
85 }
86 public GenericResponse(String statusLine, String contentType) {
87 this(statusLine, contentType, (Header[])null);
88 }
89
90 public GenericResponse(
91 String statusLine,
92 String contentType,
93 Header[] headers) {
94
95 this(statusLine, (String) null, contentType, headers);
96 }
97
98 public GenericResponse(
99 String statusLine,
100 String bodyString,
101 String contentType) {
102
103 this(statusLine, bodyString, contentType, null);
104 }
105
106 public GenericResponse(
107 String statusLine,
108 String bodyString,
109 String contentType,
110 Header[] headers) {
111
112 setStatusLine(statusLine);
113 setContentType(contentType);
114 setBodyString(bodyString);
115 setupBody();
116 }
117 public GenericResponse(
118 String statusLine,
119 byte[] bodyBytes,
120 String contentType,
121 Header[] headers) {
122 setStatusLine(statusLine);
123 setContentType(contentType);
124 setBodyBytes(bodyBytes);
125 setupBody();
126 }
127
128 public String getContentType() {
129 return contentType;
130 }
131 public void setContentType(String string) {
132 this.contentType = string;
133 }
134
135 public void setBodyString(String string) {
136 bodyString = string;
137 bodyBytes = null;
138 }
139 public void setBodyBytes(byte[] body) {
140 bodyBytes = body;
141 bodyString = null;
142 }
143
144 public String getStatusLine() {
145 return statusLine;
146 }
147
148 public void setStatusLine(String string) {
149 statusLine = string;
150 }
151
152 public Header[] getResponseHeaders() {
153 return responseHeaders;
154 }
155 public void setResponseHeaders(Header[] headers) {
156 responseHeaders = headers;
157 }
158
159 public void setupBody() {
160 try {
161 if (bodyString != null) {
162 ResponseWriter body = new ResponseWriter(bos);
163
164 if (bodyString != null) {
165 body.print(bodyString);
166 } else if (bodyBytes != null) {
167 body.write(bodyBytes);
168 }
169
170 body.close();
171 }
172 } catch (IOException e) {
173 e.printStackTrace(System.err);
174 }
175 }
176
177 public boolean processRequest(SimpleHttpServerConnection conn) throws IOException {
178
179 boolean haveContentLength = false;
180 boolean haveContentType = false;
181 ResponseWriter out = conn.getWriter();
182 out.println(getStatusLine());
183 if (responseHeaders != null) {
184 for (int i = 0; i < responseHeaders.length; i++) {
185 Header h = responseHeaders[i];
186 String name = h.getName();
187 if (name.equals("Content-Type")) {
188 haveContentType = true;
189 } else if (name.equals("Content-Length")) {
190 haveContentLength = true;
191 }
192
193 String value = h.getValue();
194 out.println(
195 ((null == name ? "" : name)
196 + ": "
197 + (null == value ? "" : value)));
198 }
199 }
200 if (!haveContentLength) {
201 out.print("Content-Length: ");
202 out.println(bos.size());
203 }
204 if (!haveContentType && getContentType() != null) {
205 out.print("Content-Type: ");
206 out.print(getContentType());
207 if (out.getEncoding() != null) {
208 out.print("; charset=");
209 out.println(out.getEncoding());
210 }
211 }
212 out.println();
213 out.write(bos.toByteArray());
214
215 bos.close();
216 return true;
217 }
218 }
This page was automatically generated by Maven