View Javadoc
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      protected volatile HttpClient client = null;
31      protected final Transformer receiveTransformer;
32  
33      public HttpClientMessageRequester(InboundEndpoint endpoint)
34      {
35          super(endpoint);
36          this.connector = (HttpConnector) endpoint.getConnector();
37          this.receiveTransformer = new HttpClientMethodResponseToObject();
38          this.receiveTransformer.setMuleContext(connector.getMuleContext());
39      }
40  
41      protected void doConnect() throws Exception
42      {
43          if (client == null)
44          {
45              client = connector.doClientConnect();
46          }
47      }
48  
49      protected void doDisconnect() throws Exception
50      {
51          client = null;
52      }
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          HttpMethod httpMethod = new GetMethod(endpoint.getEndpointURI().getAddress());
68          connector.setupClientAuthorization(null, httpMethod, client, endpoint);
69  
70          boolean releaseConn = false;
71          try
72          {
73              HttpClient client = new HttpClient();
74              client.executeMethod(httpMethod);
75  
76              if (httpMethod.getStatusCode() == HttpStatus.SC_OK)
77              {
78                  MuleMessage res = (MuleMessage) receiveTransformer.transform(httpMethod);
79                  if (StringUtils.EMPTY.equals(res.getPayload()))
80                  {
81                      releaseConn = true;
82                  }
83                  return res;
84              }
85              else
86              {
87                  releaseConn = true;
88                  throw new ReceiveException(
89                      HttpMessages.requestFailedWithStatus(httpMethod.getStatusLine().toString()),
90                      endpoint, timeout);
91              }
92          }
93          catch (ReceiveException e)
94          {
95              releaseConn = true;
96              throw e;
97          }
98          catch (Exception e)
99          {
100             releaseConn = true;
101             throw new ReceiveException(endpoint, timeout, e);
102         }
103         finally
104         {
105             if (releaseConn)
106             {
107                 httpMethod.releaseConnection();
108             }
109         }
110     }
111 
112     protected void doDispose()
113     {
114         // template method
115     }
116 }