Coverage Report - org.mule.transport.http.HttpClientMessageRequester
 
Classes in this File Line Coverage Branch Coverage Complexity
HttpClientMessageRequester
0%
0/32
0%
0/8
3
 
 1  
 /*
 2  
  * $Id: HttpClientMessageRequester.java 19191 2010-08-25 21:05:23Z tcarlson $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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  
 import org.mule.api.MuleMessage;
 14  
 import org.mule.api.endpoint.InboundEndpoint;
 15  
 import org.mule.api.transformer.Transformer;
 16  
 import org.mule.api.transport.ReceiveException;
 17  
 import org.mule.transport.AbstractMessageRequester;
 18  
 import org.mule.transport.http.i18n.HttpMessages;
 19  
 import org.mule.transport.http.transformers.HttpClientMethodResponseToObject;
 20  
 import org.mule.util.StringUtils;
 21  
 
 22  
 import org.apache.commons.httpclient.HttpClient;
 23  
 import org.apache.commons.httpclient.HttpMethod;
 24  
 import org.apache.commons.httpclient.HttpStatus;
 25  
 import org.apache.commons.httpclient.methods.GetMethod;
 26  
 
 27  
 /**
 28  
  * Rquests Mule events over HTTP.
 29  
  */
 30  
 public class HttpClientMessageRequester extends AbstractMessageRequester
 31  
 {
 32  
 
 33  
     protected final HttpConnector connector;
 34  0
     protected volatile HttpClient client = null;
 35  
     protected final Transformer receiveTransformer;
 36  
 
 37  
     public HttpClientMessageRequester(InboundEndpoint endpoint)
 38  
     {
 39  0
         super(endpoint);
 40  0
         this.connector = (HttpConnector) endpoint.getConnector();
 41  0
         this.receiveTransformer = new HttpClientMethodResponseToObject();
 42  0
         this.receiveTransformer.setMuleContext(connector.getMuleContext());
 43  0
     }
 44  
 
 45  
     protected void doConnect() throws Exception
 46  
     {
 47  0
         if (client == null)
 48  
         {
 49  0
             client = connector.doClientConnect();
 50  
         }
 51  0
     }
 52  
 
 53  
     protected void doDisconnect() throws Exception
 54  
     {
 55  0
         client = null;
 56  0
     }
 57  
 
 58  
     /**
 59  
      * Make a specific request to the underlying transport
 60  
      *
 61  
      * @param timeout the maximum time the operation should block before returning.
 62  
      *            The call should return immediately if there is data available. If
 63  
      *            no data becomes available before the timeout elapses, null will be
 64  
      *            returned
 65  
      * @return the result of the request wrapped in a MuleMessage object. Null will be
 66  
      *         returned if no data was avaialable
 67  
      * @throws Exception if the call to the underlying protocal cuases an exception
 68  
      */
 69  
     protected MuleMessage doRequest(long timeout) throws Exception
 70  
     {
 71  0
         HttpMethod httpMethod = new GetMethod(endpoint.getEndpointURI().getAddress());
 72  0
         connector.setupClientAuthorization(null, httpMethod, client, endpoint);
 73  
 
 74  0
         boolean releaseConn = false;
 75  
         try
 76  
         {
 77  0
             HttpClient client = new HttpClient();
 78  0
             client.executeMethod(httpMethod);
 79  
 
 80  0
             if (httpMethod.getStatusCode() == HttpStatus.SC_OK)
 81  
             {
 82  0
                 MuleMessage res = (MuleMessage) receiveTransformer.transform(httpMethod);
 83  0
                 if (StringUtils.EMPTY.equals(res.getPayload()))
 84  
                 {
 85  0
                     releaseConn = true;
 86  
                 }
 87  0
                 return res;
 88  
             }
 89  
             else
 90  
             {
 91  0
                 releaseConn = true;
 92  0
                 throw new ReceiveException(
 93  
                     HttpMessages.requestFailedWithStatus(httpMethod.getStatusLine().toString()),
 94  
                     endpoint, timeout);
 95  
             }
 96  
         }
 97  0
         catch (ReceiveException e)
 98  
         {
 99  0
             releaseConn = true;
 100  0
             throw e;
 101  
         }
 102  0
         catch (Exception e)
 103  
         {
 104  0
             releaseConn = true;
 105  0
             throw new ReceiveException(endpoint, timeout, e);
 106  
         }
 107  
         finally
 108  
         {
 109  0
             if (releaseConn)
 110  
             {
 111  0
                 httpMethod.releaseConnection();
 112  
             }
 113  
         }
 114  
     }
 115  
 
 116  
     protected void doDispose()
 117  
     {
 118  
         // template method
 119  0
     }
 120  
 }