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