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