Coverage Report - org.mule.transport.http.HttpRequest
 
Classes in this File Line Coverage Branch Coverage Complexity
HttpRequest
0%
0/81
0%
0/44
0
 
 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  0
     private RequestLine requestLine = null;
 30  0
     private HeaderGroup headers = new HeaderGroup();
 31  0
     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  0
         super();
 38  0
         if (requestLine == null)
 39  
         {
 40  0
             throw new IllegalArgumentException("Request line may not be null");
 41  
         }
 42  0
         this.defaultEncoding = defaultEncoding;
 43  0
         this.requestLine = requestLine;
 44  0
         if (headers != null)
 45  
         {
 46  0
             this.headers.setHeaders(headers);
 47  
         }
 48  0
         if (content != null)
 49  
         {
 50  
             // only PUT and POST have content
 51  0
             String methodname = requestLine.getMethod();
 52  0
             if (HttpConstants.METHOD_POST.equalsIgnoreCase(methodname)
 53  
                 || HttpConstants.METHOD_PUT.equalsIgnoreCase(methodname))
 54  
             {
 55  0
                 Header contentLength = this.headers.getFirstHeader(HttpConstants.HEADER_CONTENT_LENGTH);
 56  0
                 Header transferEncoding = this.headers.getFirstHeader(HttpConstants.HEADER_TRANSFER_ENCODING);
 57  0
                 InputStream in = content;
 58  0
                 if (transferEncoding != null)
 59  
                 {
 60  0
                     if (transferEncoding.getValue().indexOf(HttpConstants.TRANSFER_ENCODING_CHUNKED) != -1)
 61  
                     {
 62  0
                         in = new ChunkedInputStream(in);
 63  
                     }
 64  
                 }
 65  0
                 else if (contentLength != null)
 66  
                 {
 67  0
                     long len = getContentLength();
 68  0
                     if (len >= 0)
 69  
                     {
 70  0
                         in = new ContentLengthInputStream(in, len);
 71  
                     }
 72  
                 }
 73  0
                 this.entity = in;
 74  
             }
 75  
         }
 76  0
     }
 77  
 
 78  
     public HttpRequest(final RequestLine requestLine, final Header[] headers, String defaultEncoding) throws IOException
 79  
     {
 80  0
         this(requestLine, headers, null, defaultEncoding);
 81  0
     }
 82  
 
 83  
     public RequestLine getRequestLine()
 84  
     {
 85  0
         return this.requestLine;
 86  
     }
 87  
 
 88  
     public void setRequestLine(final RequestLine requestline)
 89  
     {
 90  0
         if (requestline == null)
 91  
         {
 92  0
             throw new IllegalArgumentException("Request line may not be null");
 93  
         }
 94  0
         this.requestLine = requestline;
 95  0
     }
 96  
 
 97  
     public boolean containsHeader(final String name)
 98  
     {
 99  0
         return this.headers.containsHeader(name);
 100  
     }
 101  
 
 102  
     public Header[] getHeaders()
 103  
     {
 104  0
         return this.headers.getAllHeaders();
 105  
     }
 106  
 
 107  
     public Header getFirstHeader(final String s)
 108  
     {
 109  0
         return this.headers.getFirstHeader(s);
 110  
     }
 111  
 
 112  
     public void removeHeaders(final String s)
 113  
     {
 114  0
         if (s == null)
 115  
         {
 116  0
             return;
 117  
         }
 118  0
         Header[] headers = this.headers.getHeaders(s);
 119  0
         for (int i = 0; i < headers.length; i++)
 120  
         {
 121  0
             this.headers.removeHeader(headers[i]);
 122  
         }
 123  0
     }
 124  
 
 125  
     public void addHeader(final Header header)
 126  
     {
 127  0
         if (header == null)
 128  
         {
 129  0
             return;
 130  
         }
 131  0
         this.headers.addHeader(header);
 132  0
     }
 133  
 
 134  
     public void setHeader(final Header header)
 135  
     {
 136  0
         if (header == null)
 137  
         {
 138  0
             return;
 139  
         }
 140  0
         removeHeaders(header.getName());
 141  0
         addHeader(header);
 142  0
     }
 143  
 
 144  
     public Iterator<?> getHeaderIterator()
 145  
     {
 146  0
         return this.headers.getIterator();
 147  
     }
 148  
 
 149  
     public String getContentType()
 150  
     {
 151  0
         Header contenttype = this.headers.getFirstHeader(HttpConstants.HEADER_CONTENT_TYPE);
 152  0
         if (contenttype != null)
 153  
         {
 154  0
             return contenttype.getValue();
 155  
         }
 156  
         else
 157  
         {
 158  0
             return HttpConstants.DEFAULT_CONTENT_TYPE;
 159  
         }
 160  
     }
 161  
 
 162  
     public String getCharset()
 163  
     {
 164  0
         String charset = null;
 165  0
         Header contenttype = this.headers.getFirstHeader(HttpConstants.HEADER_CONTENT_TYPE);
 166  0
         if (contenttype != null)
 167  
         {
 168  0
             HeaderElement values[] = contenttype.getElements();
 169  0
             if (values.length == 1)
 170  
             {
 171  0
                 NameValuePair param = values[0].getParameterByName("charset");
 172  0
                 if (param != null)
 173  
                 {
 174  0
                     charset = param.getValue();
 175  
                 }
 176  
             }
 177  
         }
 178  0
         if (charset != null)
 179  
         {
 180  0
             return charset;
 181  
         }
 182  
         else
 183  
         {
 184  0
             return defaultEncoding;
 185  
         }
 186  
     }
 187  
 
 188  
     public long getContentLength()
 189  
     {
 190  0
         Header contentLength = this.headers.getFirstHeader(HttpConstants.HEADER_CONTENT_LENGTH);
 191  0
         if (contentLength != null)
 192  
         {
 193  
             try
 194  
             {
 195  0
                 return Long.parseLong(contentLength.getValue());
 196  
             }
 197  0
             catch (NumberFormatException e)
 198  
             {
 199  0
                 return -1;
 200  
             }
 201  
         }
 202  
         else
 203  
         {
 204  0
             return -1;
 205  
         }
 206  
     }
 207  
 
 208  
     public InputStream getBody()
 209  
     {
 210  0
         return this.entity;
 211  
     }
 212  
 
 213  
     public byte[] getBodyBytes() throws IOException
 214  
     {
 215  0
         InputStream in = getBody();
 216  0
         if (in != null)
 217  
         {
 218  0
             return IOUtils.toByteArray(in);
 219  
         }
 220  
         else
 221  
         {
 222  0
             return null;
 223  
         }
 224  
     }
 225  
 
 226  
     public String getBodyString() throws IOException
 227  
     {
 228  0
         byte[] raw = getBodyBytes();
 229  0
         if (raw != null)
 230  
         {
 231  0
             return new String(raw, getCharset());
 232  
         }
 233  
         else
 234  
         {
 235  0
             return null;
 236  
         }
 237  
     }
 238  
 
 239  
 }