1   /*
2    * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/Attic/GenericResponse.java,v 1.1.2.3 2004/02/22 18:21:18 olegk Exp $
3    * $Revision: 1.1.2.3 $
4    * $Date: 2004/02/22 18:21:18 $
5    *
6    * ====================================================================
7    *
8    *  Copyright 1999-2004 The Apache Software Foundation
9    *
10   *  Licensed under the Apache License, Version 2.0 (the "License");
11   *  you may not use this file except in compliance with the License.
12   *  You may obtain a copy of the License at
13   *
14   *      http://www.apache.org/licenses/LICENSE-2.0
15   *
16   *  Unless required by applicable law or agreed to in writing, software
17   *  distributed under the License is distributed on an "AS IS" BASIS,
18   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   *  See the License for the specific language governing permissions and
20   *  limitations under the License.
21   * ====================================================================
22   *
23   * This software consists of voluntary contributions made by many
24   * individuals on behalf of the Apache Software Foundation.  For more
25   * information on the Apache Software Foundation, please see
26   * <http://www.apache.org/>.
27   *
28   * [Additional notices, if required by prior licensing conditions]
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 }