View Javadoc

1   /*
2    * $Id: PollingHttpMessageReceiver.java 11983 2008-06-10 15:16:36Z rossmason $
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.DefaultMuleEvent;
14  import org.mule.DefaultMuleMessage;
15  import org.mule.DefaultMuleSession;
16  import org.mule.api.MuleContext;
17  import org.mule.api.MuleEvent;
18  import org.mule.api.MuleMessage;
19  import org.mule.api.MuleSession;
20  import org.mule.api.endpoint.EndpointBuilder;
21  import org.mule.api.endpoint.InboundEndpoint;
22  import org.mule.api.endpoint.OutboundEndpoint;
23  import org.mule.api.lifecycle.CreateException;
24  import org.mule.api.service.Service;
25  import org.mule.api.transport.Connector;
26  import org.mule.endpoint.EndpointURIEndpointBuilder;
27  import org.mule.transport.AbstractPollingMessageReceiver;
28  import org.mule.transport.DefaultMessageAdapter;
29  import org.mule.transport.http.i18n.HttpMessages;
30  import org.mule.util.MapUtils;
31  
32  import java.util.Collections;
33  import java.util.Map;
34  
35  /**
36   * Will poll an http URL and use the response as the input for a service request.
37   */
38  public class PollingHttpMessageReceiver extends AbstractPollingMessageReceiver
39  {
40      protected String etag = null;
41      private boolean checkEtag;
42      private boolean discardEmptyContent;
43      //The outbound endpoint to poll
44      private OutboundEndpoint outboundEndpoint;
45  
46      public PollingHttpMessageReceiver(Connector connector,
47                                        Service service,
48                                        final InboundEndpoint endpoint) throws CreateException
49      {
50  
51          super(connector, service, endpoint);
52  
53          HttpPollingConnector pollingConnector;
54  
55          if (connector instanceof HttpPollingConnector)
56          {
57              pollingConnector = (HttpPollingConnector) connector;
58          }
59          else
60          {
61              throw new CreateException(HttpMessages.pollingReciverCannotbeUsed(), this);
62          }
63  
64          long pollingFrequency = MapUtils.getLongValue(endpoint.getProperties(), "pollingFrequency",
65                  pollingConnector.getPollingFrequency());
66          if (pollingFrequency > 0)
67          {
68              this.setFrequency(pollingFrequency);
69          }
70  
71          checkEtag = MapUtils.getBooleanValue(endpoint.getProperties(), "checkEtag", pollingConnector.isCheckEtag());
72          discardEmptyContent = MapUtils.getBooleanValue(endpoint.getProperties(), "discardEmptyContent", pollingConnector.isDiscardEmptyContent());
73      }
74  
75      protected void doDispose()
76      {
77          // nothing to do
78      }
79  
80      protected void doConnect() throws Exception
81      {
82          // nothing to do
83      }
84  
85      public void doDisconnect() throws Exception
86      {
87          // nothing to do
88      }
89  
90      public void poll() throws Exception
91      {
92          MuleMessage req = new DefaultMuleMessage(new DefaultMessageAdapter(""));
93          if (etag != null && checkEtag)
94          {
95              Map customHeaders = Collections.singletonMap(HttpConstants.HEADER_IF_NONE_MATCH, etag);
96              req.setProperty(HttpConnector.HTTP_CUSTOM_HEADERS_MAP_PROPERTY, customHeaders);
97          }
98          req.setProperty(HttpConnector.HTTP_METHOD_PROPERTY, "GET");
99  
100         MuleSession session = new DefaultMuleSession(service, connector.getMuleContext());
101 
102         if (outboundEndpoint == null)
103         {
104             // We need to create an outbound endpoint to do the polled request using
105             // send() as thats the only way we can customize headers and use eTags
106             MuleContext muleContext = endpoint.getMuleContext();
107             EndpointBuilder endpointBuilder = new EndpointURIEndpointBuilder(endpoint, muleContext);
108             //Must not use inbound transformers for the outbound request
109             endpointBuilder.setTransformers(Collections.EMPTY_LIST);
110             outboundEndpoint = muleContext.getRegistry().lookupEndpointFactory().getOutboundEndpoint(
111                     endpointBuilder);
112         }
113         MuleEvent event = new DefaultMuleEvent(req, outboundEndpoint, session, true);
114 
115         MuleMessage message = connector.send(outboundEndpoint, event);
116 
117         if (message.getIntProperty(HttpConstants.HEADER_CONTENT_LENGTH, 0) == 0 && discardEmptyContent)
118         {
119             if (logger.isDebugEnabled())
120             {
121                 logger.debug("Received empty message and ignoring from: " + endpoint.getEndpointURI());
122             }
123             return;
124         }
125         int status = message.getIntProperty(HttpConnector.HTTP_STATUS_PROPERTY, 0);
126         etag = message.getStringProperty(HttpConstants.HEADER_ETAG, null);
127 
128         if ((status != 304 || !checkEtag))
129         {
130             routeMessage(message, endpoint.isSynchronous());
131         }
132     }
133 }