1   /*
2    * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/SimpleResponse.java,v 1.2 2004/09/14 15:50:41 olegk Exp $
3    * $Revision: 1.2 $
4    * $Date: 2004/09/14 15:50:41 $
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.util.Iterator;
35  
36  import org.apache.commons.httpclient.Header;
37  import org.apache.commons.httpclient.HeaderGroup;
38  import org.apache.commons.httpclient.HttpStatus;
39  import org.apache.commons.httpclient.HttpVersion;
40  
41  /***
42   * A generic HTTP response.
43   * 
44   * @author Christian Kohlschuetter
45   * @author Oleg Kalnichevski
46   */
47  public class SimpleResponse {
48      
49      private String statusLine = "HTTP/1.0 200 OK";
50      private String contentType = "text/plain";
51      private String bodyString = null;
52      private HeaderGroup headers = new HeaderGroup();
53  
54      public SimpleResponse() {
55          super();
56      }
57  
58      public SimpleResponse(final String statusLine) {
59          super();
60          this.statusLine = statusLine;
61      }
62  
63      public String getContentType() {
64          return this.contentType;
65      }
66      
67      public void setContentType(String string) {
68          this.contentType = string;
69      }
70  
71      public void setBodyString(String string) {
72          this.bodyString = string;
73      }
74      
75      public String getBodyString() {
76          return this.bodyString;
77      }
78  
79      public String getStatusLine() {
80          return this.statusLine;
81      }
82  
83      public void setStatusLine(final String string) {
84          this.statusLine = string;
85      }
86  
87      public void setStatusLine(final HttpVersion version, int statuscode) {
88      	if (version == null) {
89      		throw new IllegalArgumentException("HTTP version may not be null");
90      	}
91      	StringBuffer buffer = new StringBuffer();
92      	buffer.append(version);
93      	buffer.append(' ');
94      	buffer.append(statuscode);
95      	String statustext = HttpStatus.getStatusText(statuscode);
96      	if (statustext != null) {
97          	buffer.append(' ');
98          	buffer.append(statustext);
99      	}
100     	this.statusLine = buffer.toString();
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 void setHeader(final Header header) {
112         if (header == null) {
113             return;
114         }
115         Header[] headers = this.headers.getHeaders(header.getName());
116         for (int i = 0; i < headers.length; i++) {
117             this.headers.removeHeader(headers[i]);
118         }
119         this.headers.addHeader(header);
120     }
121 
122     public void addHeader(final Header header) {
123         if (header == null) {
124             return;
125         }
126         this.headers.addHeader(header);
127     }
128 
129     public void setHeaders(final Header[] headers) {
130         if (headers == null) {
131             return;
132         }
133         this.headers.setHeaders(headers);
134     }
135 
136     public Iterator getHeaderIterator() {
137         return this.headers.getIterator();
138     }
139 }