View Javadoc

1   /*
2    * $Id: HttpRequest.java 7976 2007-08-21 14:26:13Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.providers.http;
12  
13  import org.mule.MuleManager;
14  
15  import java.io.IOException;
16  import java.io.InputStream;
17  import java.util.Iterator;
18  
19  import org.apache.commons.httpclient.ChunkedInputStream;
20  import org.apache.commons.httpclient.ContentLengthInputStream;
21  import org.apache.commons.httpclient.Header;
22  import org.apache.commons.httpclient.HeaderElement;
23  import org.apache.commons.httpclient.HeaderGroup;
24  import org.apache.commons.httpclient.NameValuePair;
25  import org.apache.commons.io.IOUtils;
26  import org.apache.commons.io.output.ByteArrayOutputStream;
27  
28  /**
29   * A http request wrapper
30   */
31  public class HttpRequest
32  {
33  
34      private RequestLine requestLine = null;
35      private HeaderGroup headers = new HeaderGroup();
36      private InputStream entity = null;
37  
38      public HttpRequest()
39      {
40          super();
41      }
42  
43      public HttpRequest(final RequestLine requestLine, final Header[] headers, final InputStream content)
44          throws IOException
45      {
46          super();
47          if (requestLine == null)
48          {
49              throw new IllegalArgumentException("Request line may not be null");
50          }
51          this.requestLine = requestLine;
52          if (headers != null)
53          {
54              this.headers.setHeaders(headers);
55          }
56          if (content != null)
57          {
58              // only PUT and POST have content
59              String methodname = requestLine.getMethod();
60              if (HttpConstants.METHOD_POST.equalsIgnoreCase(methodname)
61                  || HttpConstants.METHOD_PUT.equalsIgnoreCase(methodname))
62              {
63                  Header contentLength = this.headers.getFirstHeader(HttpConstants.HEADER_CONTENT_LENGTH);
64                  Header transferEncoding = this.headers.getFirstHeader(HttpConstants.HEADER_TRANSFER_ENCODING);
65                  InputStream in = content;
66                  if (transferEncoding != null)
67                  {
68                      if (transferEncoding.getValue().indexOf(HttpConstants.TRANSFER_ENCODING_CHUNKED) != -1)
69                      {
70                          in = new ChunkedInputStream(in);
71                      }
72                  }
73                  else if (contentLength != null)
74                  {
75                      long len = getContentLength();
76                      if (len >= 0)
77                      {
78                          in = new ContentLengthInputStream(in, len);
79                      }
80                  }
81                  this.entity = in;
82              }
83          }
84      }
85  
86      public HttpRequest(final RequestLine requestLine, final Header[] headers) throws IOException
87      {
88          this(requestLine, headers, null);
89      }
90  
91      public RequestLine getRequestLine()
92      {
93          return this.requestLine;
94      }
95  
96      public void setRequestLine(final RequestLine requestline)
97      {
98          if (requestline == null)
99          {
100             throw new IllegalArgumentException("Request line may not be null");
101         }
102         this.requestLine = requestline;
103     }
104 
105     public boolean containsHeader(final String name)
106     {
107         return this.headers.containsHeader(name);
108     }
109 
110     public Header[] getHeaders()
111     {
112         return this.headers.getAllHeaders();
113     }
114 
115     public Header getFirstHeader(final String s)
116     {
117         return this.headers.getFirstHeader(s);
118     }
119 
120     public void removeHeaders(final String s)
121     {
122         if (s == null)
123         {
124             return;
125         }
126         Header[] headers = this.headers.getHeaders(s);
127         for (int i = 0; i < headers.length; i++)
128         {
129             this.headers.removeHeader(headers[i]);
130         }
131     }
132 
133     public void addHeader(final Header header)
134     {
135         if (header == null)
136         {
137             return;
138         }
139         this.headers.addHeader(header);
140     }
141 
142     public void setHeader(final Header header)
143     {
144         if (header == null)
145         {
146             return;
147         }
148         removeHeaders(header.getName());
149         addHeader(header);
150     }
151 
152     public Iterator getHeaderIterator()
153     {
154         return this.headers.getIterator();
155     }
156 
157     public String getContentType()
158     {
159         Header contenttype = this.headers.getFirstHeader(HttpConstants.HEADER_CONTENT_TYPE);
160         if (contenttype != null)
161         {
162             return contenttype.getValue();
163         }
164         else
165         {
166             return HttpConstants.DEFAULT_CONTENT_TYPE;
167         }
168     }
169 
170     public String getCharset()
171     {
172         String charset = null;
173         Header contenttype = this.headers.getFirstHeader(HttpConstants.HEADER_CONTENT_TYPE);
174         if (contenttype != null)
175         {
176             HeaderElement values[] = contenttype.getElements();
177             if (values.length == 1)
178             {
179                 NameValuePair param = values[0].getParameterByName("charset");
180                 if (param != null)
181                 {
182                     charset = param.getValue();
183                 }
184             }
185         }
186         if (charset != null)
187         {
188             return charset;
189         }
190         else
191         {
192             return MuleManager.getConfiguration().getEncoding();
193         }
194     }
195 
196     public long getContentLength()
197     {
198         Header contentLength = this.headers.getFirstHeader(HttpConstants.HEADER_CONTENT_LENGTH);
199         if (contentLength != null)
200         {
201             try
202             {
203                 return Long.parseLong(contentLength.getValue());
204             }
205             catch (NumberFormatException e)
206             {
207                 return -1;
208             }
209         }
210         else
211         {
212             return -1;
213         }
214     }
215 
216     public InputStream getBody()
217     {
218         return this.entity;
219     }
220 
221     public byte[] getBodyBytes() throws IOException
222     {
223         InputStream in = getBody();
224         if (in != null)
225         {
226             ByteArrayOutputStream buffer = new ByteArrayOutputStream();
227             IOUtils.copy(in, buffer);
228             return buffer.toByteArray();
229         }
230         else
231         {
232             return null;
233         }
234     }
235 
236     public String getBodyString() throws IOException
237     {
238         byte[] raw = getBodyBytes();
239         if (raw != null)
240         {
241             return new String(raw, getCharset());
242         }
243         else
244         {
245             return null;
246         }
247     }
248 
249 }