View Javadoc

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