View Javadoc

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