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