View Javadoc

1   /*
2    * $Id: PollingHttpMessageReceiver.java 20385 2010-11-29 20:25:26Z dfeist $
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.DefaultMuleEvent;
14  import org.mule.DefaultMuleMessage;
15  import org.mule.MessageExchangePattern;
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.construct.FlowConstruct;
21  import org.mule.api.endpoint.EndpointBuilder;
22  import org.mule.api.endpoint.InboundEndpoint;
23  import org.mule.api.endpoint.OutboundEndpoint;
24  import org.mule.api.lifecycle.CreateException;
25  import org.mule.api.processor.MessageProcessor;
26  import org.mule.api.transport.Connector;
27  import org.mule.endpoint.EndpointURIEndpointBuilder;
28  import org.mule.session.DefaultMuleSession;
29  import org.mule.transport.AbstractPollingMessageReceiver;
30  import org.mule.transport.http.i18n.HttpMessages;
31  import org.mule.util.MapUtils;
32  import org.mule.util.StringUtils;
33  
34  import java.util.Collections;
35  
36  /**
37   * Will poll an http URL and use the response as the input for a service request.
38   */
39  public class PollingHttpMessageReceiver extends AbstractPollingMessageReceiver
40  {
41      protected String etag = null;
42      private boolean checkEtag;
43      private boolean discardEmptyContent;
44      //The outbound endpoint to poll
45      private OutboundEndpoint outboundEndpoint;
46  
47      public PollingHttpMessageReceiver(Connector connector,
48                                        FlowConstruct flowConstruct,
49                                        final InboundEndpoint endpoint) throws CreateException
50      {
51  
52          super(connector, flowConstruct, endpoint);
53  
54          HttpPollingConnector pollingConnector;
55  
56          if (connector instanceof HttpPollingConnector)
57          {
58              pollingConnector = (HttpPollingConnector) connector;
59          }
60          else
61          {
62              throw new CreateException(HttpMessages.pollingReciverCannotbeUsed(), this);
63          }
64  
65          long pollingFrequency = MapUtils.getLongValue(endpoint.getProperties(), "pollingFrequency",
66                  pollingConnector.getPollingFrequency());
67          if (pollingFrequency > 0)
68          {
69              this.setFrequency(pollingFrequency);
70          }
71  
72          checkEtag = MapUtils.getBooleanValue(endpoint.getProperties(), "checkEtag", pollingConnector.isCheckEtag());
73          discardEmptyContent = MapUtils.getBooleanValue(endpoint.getProperties(), "discardEmptyContent", pollingConnector.isDiscardEmptyContent());
74      }
75  
76      @Override
77      protected void doDispose()
78      {
79          // nothing to do
80      }
81  
82      @Override
83      protected void doConnect() throws Exception
84      {
85          // nothing to do
86      }
87  
88      @Override
89      public void doDisconnect() throws Exception
90      {
91          // nothing to do
92      }
93  
94      @Override
95      public void poll() throws Exception
96      {
97          MuleContext muleContext = connector.getMuleContext();
98  
99          if (outboundEndpoint == null)
100         {
101             // We need to create an outbound endpoint to do the polled request using
102             // send() as thats the only way we can customize headers and use eTags
103             EndpointBuilder endpointBuilder = new EndpointURIEndpointBuilder(endpoint);
104             // Must not use inbound endpoint processors
105             endpointBuilder.setMessageProcessors(Collections.<MessageProcessor>emptyList());
106             endpointBuilder.setResponseMessageProcessors(Collections.<MessageProcessor>emptyList());
107             endpointBuilder.setMessageProcessors(Collections.<MessageProcessor>emptyList());
108             endpointBuilder.setResponseMessageProcessors(Collections.<MessageProcessor>emptyList());
109             endpointBuilder.setExchangePattern(MessageExchangePattern.REQUEST_RESPONSE);
110 
111             outboundEndpoint = muleContext.getEndpointFactory().getOutboundEndpoint(
112                     endpointBuilder);
113         }
114 
115         MuleMessage request = new DefaultMuleMessage(StringUtils.EMPTY, outboundEndpoint.getProperties(), muleContext);
116         if (etag != null && checkEtag)
117         {
118             request.setOutboundProperty(HttpConstants.HEADER_IF_NONE_MATCH, etag);
119         }
120         request.setOutboundProperty(HttpConnector.HTTP_METHOD_PROPERTY, "GET");
121 
122         MuleSession session = new DefaultMuleSession(flowConstruct, connector.getMuleContext());
123 
124 
125         MuleEvent event = new DefaultMuleEvent(request, outboundEndpoint, session);
126 
127         MuleEvent result = outboundEndpoint.process(event);
128         MuleMessage message = null;
129         if (result != null)
130         {
131             message = result.getMessage();
132         }
133 
134         final int contentLength = message.getOutboundProperty(HttpConstants.HEADER_CONTENT_LENGTH, -1);
135         if (contentLength == 0 && discardEmptyContent)
136         {
137             if (logger.isDebugEnabled())
138             {
139                 logger.debug("Received empty message and ignoring from: " + endpoint.getEndpointURI());
140             }
141             return;
142         }
143         int status = message.getOutboundProperty(HttpConnector.HTTP_STATUS_PROPERTY, 0);
144         etag = message.getOutboundProperty(HttpConstants.HEADER_ETAG);
145 
146         if ((status != HttpConstants.SC_NOT_MODIFIED || !checkEtag))
147         {
148             routeMessage(message);
149         }
150     }
151 }