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