001: /*
002: * $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 $
003: * $Revision: 480424 $
004: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005: *
006: * ====================================================================
007: *
008: * Licensed to the Apache Software Foundation (ASF) under one or more
009: * contributor license agreements. See the NOTICE file distributed with
010: * this work for additional information regarding copyright ownership.
011: * The ASF licenses this file to You under the Apache License, Version 2.0
012: * (the "License"); you may not use this file except in compliance with
013: * the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing, software
018: * distributed under the License is distributed on an "AS IS" BASIS,
019: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
020: * See the License for the specific language governing permissions and
021: * limitations under the License.
022: * ====================================================================
023: *
024: * This software consists of voluntary contributions made by many
025: * individuals on behalf of the Apache Software Foundation. For more
026: * information on the Apache Software Foundation, please see
027: * <http://www.apache.org/>.
028: *
029: */
030:
031: package org.apache.commons.httpclient.server;
032:
033: import java.io.ByteArrayOutputStream;
034: import java.io.IOException;
035: import java.io.InputStream;
036: import java.util.Iterator;
037:
038: import org.apache.commons.httpclient.ChunkedInputStream;
039: import org.apache.commons.httpclient.ContentLengthInputStream;
040: import org.apache.commons.httpclient.Header;
041: import org.apache.commons.httpclient.HeaderElement;
042: import org.apache.commons.httpclient.HeaderGroup;
043: import org.apache.commons.httpclient.NameValuePair;
044:
045: /**
046: * A generic HTTP request.
047: *
048: * @author Oleg Kalnichevski
049: */
050: public class SimpleRequest {
051:
052: public static final String DEFAULT_CONTENT_CHARSET = "ISO-8859-1";
053:
054: private RequestLine requestLine = null;
055: private HeaderGroup headers = new HeaderGroup();
056: private InputStream entity = null;
057:
058: public SimpleRequest() {
059: super ();
060: }
061:
062: public SimpleRequest(final RequestLine requestLine,
063: final Header[] headers, final InputStream content)
064: throws IOException {
065: super ();
066: if (requestLine == null) {
067: throw new IllegalArgumentException(
068: "Request line may not be null");
069: }
070: this .requestLine = requestLine;
071: if (headers != null) {
072: this .headers.setHeaders(headers);
073: }
074: if (content != null) {
075: // only PUT and POST have content
076: String methodname = requestLine.getMethod();
077: if ("POST".equalsIgnoreCase(methodname)
078: || "PUT".equalsIgnoreCase(methodname)) {
079: Header contentLength = this .headers
080: .getFirstHeader("Content-Length");
081: Header transferEncoding = this .headers
082: .getFirstHeader("Transfer-Encoding");
083: InputStream in = content;
084: if (transferEncoding != null) {
085: if (transferEncoding.getValue().indexOf("chunked") != -1) {
086: in = new ChunkedInputStream(in);
087: }
088: } else if (contentLength != null) {
089: long len = getContentLength();
090: if (len >= 0) {
091: in = new ContentLengthInputStream(in, len);
092: }
093: }
094: this .entity = in;
095: }
096: }
097: }
098:
099: public SimpleRequest(final RequestLine requestLine,
100: final Header[] headers) throws IOException {
101: this (requestLine, headers, null);
102: }
103:
104: public RequestLine getRequestLine() {
105: return this .requestLine;
106: }
107:
108: public void setRequestLine(final RequestLine requestline) {
109: if (requestline == null) {
110: throw new IllegalArgumentException(
111: "Request line may not be null");
112: }
113: this .requestLine = requestline;
114: }
115:
116: public boolean containsHeader(final String name) {
117: return this .headers.containsHeader(name);
118: }
119:
120: public Header[] getHeaders() {
121: return this .headers.getAllHeaders();
122: }
123:
124: public Header getFirstHeader(final String s) {
125: return this .headers.getFirstHeader(s);
126: }
127:
128: public void removeHeaders(final String s) {
129: if (s == null) {
130: return;
131: }
132: Header[] headers = this .headers.getHeaders(s);
133: for (int i = 0; i < headers.length; i++) {
134: this .headers.removeHeader(headers[i]);
135: }
136: }
137:
138: public void addHeader(final Header header) {
139: if (header == null) {
140: return;
141: }
142: this .headers.addHeader(header);
143: }
144:
145: public void setHeader(final Header header) {
146: if (header == null) {
147: return;
148: }
149: removeHeaders(header.getName());
150: addHeader(header);
151: }
152:
153: public Iterator getHeaderIterator() {
154: return this .headers.getIterator();
155: }
156:
157: public String getContentType() {
158: Header contenttype = this .headers
159: .getFirstHeader("Content-Type");
160: if (contenttype != null) {
161: return contenttype.getValue();
162: } else {
163: return "text/plain";
164: }
165: }
166:
167: public String getCharset() {
168: String charset = null;
169: Header contenttype = this .headers
170: .getFirstHeader("Content-Type");
171: if (contenttype != null) {
172: HeaderElement values[] = contenttype.getElements();
173: if (values.length == 1) {
174: NameValuePair param = values[0]
175: .getParameterByName("charset");
176: if (param != null) {
177: charset = param.getValue();
178: }
179: }
180: }
181: if (charset != null) {
182: return charset;
183: } else {
184: return DEFAULT_CONTENT_CHARSET;
185: }
186: }
187:
188: public long getContentLength() {
189: Header contentLength = this .headers
190: .getFirstHeader("Content-Length");
191: if (contentLength != null) {
192: try {
193: return Long.parseLong(contentLength.getValue());
194: } catch (NumberFormatException e) {
195: return -1;
196: }
197: } else {
198: return -1;
199: }
200: }
201:
202: public InputStream getBody() {
203: return this .entity;
204: }
205:
206: public byte[] getBodyBytes() throws IOException {
207: InputStream in = getBody();
208: if (in != null) {
209: byte[] tmp = new byte[4096];
210: int bytesRead = 0;
211: ByteArrayOutputStream buffer = new ByteArrayOutputStream(
212: 1024);
213: while ((bytesRead = in.read(tmp)) != -1) {
214: buffer.write(tmp, 0, bytesRead);
215: }
216: return buffer.toByteArray();
217: } else {
218: return null;
219: }
220: }
221:
222: public String getBodyString() throws IOException {
223: byte[] raw = getBodyBytes();
224: if (raw != null) {
225: return new String(raw, getCharset());
226: } else {
227: return null;
228: }
229: }
230: }
|