    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: 480424 $
    4    * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
    5    *
    6    * ====================================================================
    7    *
    8    *  Licensed to the Apache Software Foundation (ASF) under one or more
    9    *  contributor license agreements.  See the NOTICE file distributed with
    10   *  this work for additional information regarding copyright ownership.
    11   *  The ASF licenses this file to You under the Apache License, Version 2.0
    12   *  (the "License"); you may not use this file except in compliance with
    13   *  the License.  You may obtain a copy of the License at
    14   *
    15   *      http://www.apache.org/licenses/LICENSE-2.0
    16   *
    17   *  Unless required by applicable law or agreed to in writing, software
    18   *  distributed under the License is distributed on an "AS IS" BASIS,
    19   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    20   *  See the License for the specific language governing permissions and
    21   *  limitations under the License.
    22   * ====================================================================
    23   *
    24   * This software consists of voluntary contributions made by many
    25   * individuals on behalf of the Apache Software Foundation.  For more
    26   * information on the Apache Software Foundation, please see
    27   * <http://www.apache.org/>.
    28   *
    29   */
    30
    31  package org.apache.commons.httpclient.server;
    32
    33  import java.io.ByteArrayOutputStream;
    34  import java.io.IOException;
    35  import java.io.InputStream;
    36  import java.util.Iterator;
    37
    38  import org.apache.commons.httpclient.ChunkedInputStream;
    39  import org.apache.commons.httpclient.ContentLengthInputStream;
    40  import org.apache.commons.httpclient.Header;
    41  import org.apache.commons.httpclient.HeaderElement;
    42  import org.apache.commons.httpclient.HeaderGroup;
    43  import org.apache.commons.httpclient.NameValuePair;
    44
    45  /**
    46   * A generic HTTP request.
    47   *
    48   * @author Oleg Kalnichevski
    49   */
    50  public class SimpleRequest {
    51
    52      public static final String DEFAULT_CONTENT_CHARSET = "ISO-8859-1";
    53
    54      private RequestLine requestLine = null;
    55      private HeaderGroup headers = new HeaderGroup();
    56      private InputStream entity = null;
    57
    58      public SimpleRequest() {
    59          super();
    60      }
    61
    62      public SimpleRequest(
    63          final RequestLine requestLine,
    64          final Header[] headers,
    65          final InputStream content) throws IOException
    66      {
    67          super();
    68          if (requestLine == null) {
    69              throw new IllegalArgumentException("Request line may not be null");
    70          }
    71          this.requestLine = requestLine;
    72          if (headers != null) {
    73              this.headers.setHeaders(headers);
    74          }
    75          if (content != null) {
    76              // only PUT and POST have content
    77              String methodname = requestLine.getMethod();
    78              if ("POST".equalsIgnoreCase(methodname) || "PUT".equalsIgnoreCase(methodname)) {
    79                  Header contentLength = this.headers.getFirstHeader("Content-Length");
    80                  Header transferEncoding = this.headers.getFirstHeader("Transfer-Encoding");
    81                  InputStream in = content;
    82                  if (transferEncoding != null) {
    83                      if (transferEncoding.getValue().indexOf("chunked") != -1) {
    84                          in = new ChunkedInputStream(in);
    85                      }
    86                  } else if (contentLength != null) {
    87                      long len = getContentLength();
    88                      if (len >= 0) {
    89                          in = new ContentLengthInputStream(in, len);
    90                      }
    91                  }
    92                  this.entity = in;
    93              }
    94          }
    95      }
    96
    97      public SimpleRequest(final RequestLine requestLine, final Header[] headers)
    98          throws IOException {
    99          this(requestLine, headers, null);
    100     }
    101
    102     public RequestLine getRequestLine() {
    103         return this.requestLine;
    104     }
    105
    106     public void setRequestLine(final RequestLine requestline) {
    107         if (requestline == null) {
    108             throw new IllegalArgumentException("Request line may not be null");
    109         }
    110         this.requestLine = requestline;
    111     }
    112
    113     public boolean containsHeader(final String name) {
    114         return this.headers.containsHeader(name);
    115     }
    116
    117     public Header[] getHeaders() {
    118         return this.headers.getAllHeaders();
    119     }
    120
    121     public Header getFirstHeader(final String s) {
    122         return this.headers.getFirstHeader(s);
    123     }
    124
    125     public void removeHeaders(final String s) {
    126         if (s == null) {
    127             return;
    128         }
    129         Header[] headers = this.headers.getHeaders(s);
    130         for (int i = 0; i < headers.length; i++) {
    131             this.headers.removeHeader(headers[i]);
    132         }
    133     }
    134
    135     public void addHeader(final Header header) {
    136         if (header == null) {
    137             return;
    138         }
    139         this.headers.addHeader(header);
    140     }
    141
    142     public void setHeader(final Header header) {
    143         if (header == null) {
    144             return;
    145         }
    146         removeHeaders(header.getName());
    147         addHeader(header);
    148     }
    149
    150     public Iterator getHeaderIterator() {
    151         return this.headers.getIterator();
    152     }
    153
    154     public String getContentType() {
    155         Header contenttype = this.headers.getFirstHeader("Content-Type");
    156         if (contenttype != null) {
    157             return contenttype.getValue();
    158         } else {
    159             return "text/plain";
    160         }
    161     }
    162
    163     public String getCharset() {
    164         String charset = null;
    165         Header contenttype = this.headers.getFirstHeader("Content-Type");
    166         if (contenttype != null) {
    167             HeaderElement values[] = contenttype.getElements();
    168             if (values.length == 1) {
    169                 NameValuePair param = values[0].getParameterByName("charset");
    170                 if (param != null) {
    171                     charset = param.getValue();
    172                 }
    173             }
    174         }
    175         if (charset != null) {
    176             return charset;
    177         } else {
    178             return DEFAULT_CONTENT_CHARSET;
    179         }
    180     }
    181
    182     public long getContentLength() {
    183         Header contentLength = this.headers.getFirstHeader("Content-Length");
    184         if (contentLength != null) {
    185             try {
    186                 return Long.parseLong(contentLength.getValue());
    187             } catch (NumberFormatException e) {
    188                 return -1;
    189             }
    190         } else {
    191             return -1;
    192         }
    193     }
    194
    195     public InputStream getBody() {
    196         return this.entity;
    197     }
    198
    199     public byte[] getBodyBytes() throws IOException {
    200         InputStream in = getBody();
    201         if (in != null) {
    202             byte[] tmp = new byte[4096];
    203             int bytesRead = 0;
    204             ByteArrayOutputStream buffer = new ByteArrayOutputStream(1024);
    205             while ((bytesRead = in.read(tmp)) != -1) {
    206                 buffer.write(tmp, 0, bytesRead);
    207             }
    208             return buffer.toByteArray();
    209         } else {
    210             return null;
    211         }
    212     }
    213
    214     public String getBodyString() throws IOException {
    215         byte[] raw = getBodyBytes();
    216         if (raw != null) {
    217             return new String(raw, getCharset());
    218         } else {
    219             return null;
    220         }
    221     }
    222 }