Coverage Report - org.mule.transport.http.HttpClientMessageRequester
 
Classes in this File Line Coverage Branch Coverage Complexity
HttpClientMessageRequester
0%
0/31
0%
0/8
3
 
 1  
 /*
 2  
  * $Id: HttpClientMessageRequester.java 10961 2008-02-22 19:01:02Z 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  
 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  
     private final HttpConnector connector;
 34  0
     private volatile HttpClient client = null;
 35  
     private 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
     }
 43  
 
 44  
     protected void doConnect() throws Exception
 45  
     {
 46  0
         if (client == null)
 47  
         {
 48  0
             client = connector.doClientConnect();
 49  
         }
 50  0
     }
 51  
 
 52  
     protected void doDisconnect() throws Exception
 53  
     {
 54  0
         client = null;
 55  0
     }
 56  
 
 57  
     /**
 58  
      * Make a specific request to the underlying transport
 59  
      *
 60  
      * @param timeout the maximum time the operation should block before returning.
 61  
      *            The call should return immediately if there is data available. If
 62  
      *            no data becomes available before the timeout elapses, null will be
 63  
      *            returned
 64  
      * @return the result of the request wrapped in a MuleMessage object. Null will be
 65  
      *         returned if no data was avaialable
 66  
      * @throws Exception if the call to the underlying protocal cuases an exception
 67  
      */
 68  
     protected MuleMessage doRequest(long timeout) throws Exception
 69  
     {
 70  0
         HttpMethod httpMethod = new GetMethod(endpoint.getEndpointURI().getAddress());
 71  0
         connector.setupClientAuthorization(null, httpMethod, client, endpoint);
 72  
 
 73  0
         boolean releaseConn = false;
 74  
         try
 75  
         {
 76  0
             HttpClient client = new HttpClient();
 77  0
             client.executeMethod(httpMethod);
 78  
 
 79  0
             if (httpMethod.getStatusCode() == HttpStatus.SC_OK)
 80  
             {
 81  0
                 MuleMessage res = (MuleMessage) receiveTransformer.transform(httpMethod);
 82  0
                 if (StringUtils.EMPTY.equals(res.getPayload()))
 83  
                 {
 84  0
                     releaseConn = true;
 85  
                 }
 86  0
                 return res;
 87  
             }
 88  
             else
 89  
             {
 90  0
                 releaseConn = true;
 91  0
                 throw new ReceiveException(
 92  
                     HttpMessages.requestFailedWithStatus(httpMethod.getStatusLine().toString()),
 93  
                     endpoint, timeout);
 94  
             }
 95  
         }
 96  0
         catch (ReceiveException e)
 97  
         {
 98  0
             releaseConn = true;
 99  0
             throw e;
 100  
         }
 101  0
         catch (Exception e)
 102  
         {
 103  0
             releaseConn = true;
 104  0
             throw new ReceiveException(endpoint, timeout, e);
 105  
         }
 106  
         finally
 107  
         {
 108  0
             if (releaseConn)
 109  
             {
 110  0
                 httpMethod.releaseConnection();
 111  
             }
 112  
         }
 113  
     }
 114  
 
 115  
     protected void doDispose()
 116  
     {
 117  
         // template method
 118  0
     }
 119  
 }