1   /*
2    * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/test/org/apache/commons/httpclient/server/SimpleRequest.java,v 1.3 2004/11/13 12:21:28 olegk Exp $
3    * $Revision: 155418 $
4    * $Date: 2005-02-26 08:01:52 -0500 (Sat, 26 Feb 2005) $
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   */
29  
30  package org.apache.commons.httpclient.server;
31  
32  import java.io.ByteArrayOutputStream;
33  import java.io.IOException;
34  import java.io.InputStream;
35  import java.util.Iterator;
36  
37  import org.apache.commons.httpclient.ChunkedInputStream;
38  import org.apache.commons.httpclient.ContentLengthInputStream;
39  import org.apache.commons.httpclient.Header;
40  import org.apache.commons.httpclient.HeaderElement;
41  import org.apache.commons.httpclient.HeaderGroup;
42  import org.apache.commons.httpclient.NameValuePair;
43  
44  /***
45   * A generic HTTP request.
46   * 
47   * @author Oleg Kalnichevski
48   */
49  public class SimpleRequest {
50      
51      public static final String DEFAULT_CONTENT_CHARSET = "ISO-8859-1";
52      
53      private RequestLine requestLine = null;
54      private HeaderGroup headers = new HeaderGroup();
55      private InputStream entity = null;
56  
57      public SimpleRequest() {
58          super();
59      }
60  
61      public SimpleRequest(
62          final RequestLine requestLine,
63          final Header[] headers,
64          final InputStream content) throws IOException
65      {
66          super();
67          if (requestLine == null) {
68              throw new IllegalArgumentException("Request line may not be null");
69          }
70          this.requestLine = requestLine;
71          if (headers != null) {
72              this.headers.setHeaders(headers);
73          }
74          if (content != null) {
75              // only PUT and POST have content
76              String methodname = requestLine.getMethod(); 
77              if ("POST".equalsIgnoreCase(methodname) || "PUT".equalsIgnoreCase(methodname)) {
78                  Header contentLength = this.headers.getFirstHeader("Content-Length");
79                  Header transferEncoding = this.headers.getFirstHeader("Transfer-Encoding");
80                  InputStream in = content;
81                  if (transferEncoding != null) {
82                      if (transferEncoding.getValue().indexOf("chunked") != -1) {
83                          in = new ChunkedInputStream(in);
84                      }
85                  } else if (contentLength != null) {
86                      long len = getContentLength();
87                      if (len >= 0) {
88                          in = new ContentLengthInputStream(in, len);
89                      }
90                  }
91                  this.entity = in;
92              }
93          }
94      }
95  
96      public SimpleRequest(final RequestLine requestLine, final Header[] headers)
97          throws IOException {
98          this(requestLine, headers, null);
99      }
100     
101     public RequestLine getRequestLine() {
102         return this.requestLine;
103     }
104 
105     public void setRequestLine(final RequestLine requestline) {
106         if (requestline == null) {
107             throw new IllegalArgumentException("Request line may not be null");
108         }
109         this.requestLine = requestline;
110     }
111 
112     public boolean containsHeader(final String name) {
113         return this.headers.containsHeader(name);
114     }
115 
116     public Header[] getHeaders() {
117         return this.headers.getAllHeaders();
118     }
119 
120     public Header getFirstHeader(final String s) {
121         return this.headers.getFirstHeader(s);
122     }
123 
124     public void removeHeaders(final String s) {
125         if (s == null) {
126             return;
127         }
128         Header[] headers = this.headers.getHeaders(s);
129         for (int i = 0; i < headers.length; i++) {
130             this.headers.removeHeader(headers[i]);
131         }
132     }
133 
134     public void addHeader(final Header header) {
135         if (header == null) {
136             return;
137         }
138         this.headers.addHeader(header);
139     }
140 
141     public void setHeader(final Header header) {
142         if (header == null) {
143             return;
144         }
145         removeHeaders(header.getName());
146         addHeader(header);
147     }
148 
149     public Iterator getHeaderIterator() {
150         return this.headers.getIterator();
151     }
152 
153     public String getContentType() {
154         Header contenttype = this.headers.getFirstHeader("Content-Type");
155         if (contenttype != null) {
156             return contenttype.getValue(); 
157         } else {
158             return "text/plain"; 
159         }
160     }
161     
162     public String getCharset() {
163         String charset = null;
164         Header contenttype = this.headers.getFirstHeader("Content-Type");
165         if (contenttype != null) {
166             HeaderElement values[] = contenttype.getElements();
167             if (values.length == 1) {
168                 NameValuePair param = values[0].getParameterByName("charset");
169                 if (param != null) {
170                     charset = param.getValue();
171                 }
172             }
173         }
174         if (charset != null) {
175             return charset;
176         } else {
177             return DEFAULT_CONTENT_CHARSET;
178         }
179     }
180     
181     public long getContentLength() {
182         Header contentLength = this.headers.getFirstHeader("Content-Length");
183         if (contentLength != null) {
184             try {
185                 return Long.parseLong(contentLength.getValue());
186             } catch (NumberFormatException e) {
187                 return -1;
188             }
189         } else {
190             return -1;
191         }
192     }
193     
194     public InputStream getBody() {
195         return this.entity;
196     }
197     
198     public byte[] getBodyBytes() throws IOException {
199         InputStream in = getBody();
200         if (in != null) {
201             byte[] tmp = new byte[4096];
202             int bytesRead = 0;
203             ByteArrayOutputStream buffer = new ByteArrayOutputStream(1024);
204             while ((bytesRead = in.read(tmp)) != -1) {
205                 buffer.write(tmp, 0, bytesRead);
206             }
207             return buffer.toByteArray();
208         } else {
209             return null;
210         }
211     }
212     
213     public String getBodyString() throws IOException {
214         byte[] raw = getBodyBytes();
215         if (raw != null) {
216             return new String(raw, getCharset());
217         } else {
218             return null;
219         }
220     }
221 }