Coverage Report - org.mule.transport.http.PollingHttpMessageReceiver
 
Classes in this File Line Coverage Branch Coverage Complexity
PollingHttpMessageReceiver
0%
0/43
0%
0/22
0
 
 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  0
     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  0
         super(connector, flowConstruct, endpoint);
 53  
 
 54  
         HttpPollingConnector pollingConnector;
 55  
 
 56  0
         if (connector instanceof HttpPollingConnector)
 57  
         {
 58  0
             pollingConnector = (HttpPollingConnector) connector;
 59  
         }
 60  
         else
 61  
         {
 62  0
             throw new CreateException(HttpMessages.pollingReciverCannotbeUsed(), this);
 63  
         }
 64  
 
 65  0
         long pollingFrequency = MapUtils.getLongValue(endpoint.getProperties(), "pollingFrequency",
 66  
                 pollingConnector.getPollingFrequency());
 67  0
         if (pollingFrequency > 0)
 68  
         {
 69  0
             this.setFrequency(pollingFrequency);
 70  
         }
 71  
 
 72  0
         checkEtag = MapUtils.getBooleanValue(endpoint.getProperties(), "checkEtag", pollingConnector.isCheckEtag());
 73  0
         discardEmptyContent = MapUtils.getBooleanValue(endpoint.getProperties(), "discardEmptyContent", pollingConnector.isDiscardEmptyContent());
 74  0
     }
 75  
 
 76  
     @Override
 77  
     protected void doDispose()
 78  
     {
 79  
         // nothing to do
 80  0
     }
 81  
 
 82  
     @Override
 83  
     protected void doConnect() throws Exception
 84  
     {
 85  
         // nothing to do
 86  0
     }
 87  
 
 88  
     @Override
 89  
     public void doDisconnect() throws Exception
 90  
     {
 91  
         // nothing to do
 92  0
     }
 93  
 
 94  
     @Override
 95  
     public void poll() throws Exception
 96  
     {
 97  0
         MuleContext muleContext = connector.getMuleContext();
 98  
 
 99  0
         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  0
             EndpointBuilder endpointBuilder = new EndpointURIEndpointBuilder(endpoint);
 104  
             // Must not use inbound endpoint processors
 105  0
             endpointBuilder.setMessageProcessors(Collections.<MessageProcessor>emptyList());
 106  0
             endpointBuilder.setResponseMessageProcessors(Collections.<MessageProcessor>emptyList());
 107  0
             endpointBuilder.setMessageProcessors(Collections.<MessageProcessor>emptyList());
 108  0
             endpointBuilder.setResponseMessageProcessors(Collections.<MessageProcessor>emptyList());
 109  0
             endpointBuilder.setExchangePattern(MessageExchangePattern.REQUEST_RESPONSE);
 110  
 
 111  0
             outboundEndpoint = muleContext.getEndpointFactory().getOutboundEndpoint(
 112  
                     endpointBuilder);
 113  
         }
 114  
 
 115  0
         MuleMessage request = new DefaultMuleMessage(StringUtils.EMPTY, outboundEndpoint.getProperties(), muleContext);
 116  0
         if (etag != null && checkEtag)
 117  
         {
 118  0
             request.setOutboundProperty(HttpConstants.HEADER_IF_NONE_MATCH, etag);
 119  
         }
 120  0
         request.setOutboundProperty(HttpConnector.HTTP_METHOD_PROPERTY, "GET");
 121  
 
 122  0
         MuleSession session = new DefaultMuleSession(flowConstruct, connector.getMuleContext());
 123  
 
 124  
 
 125  0
         MuleEvent event = new DefaultMuleEvent(request, outboundEndpoint, session);
 126  
 
 127  0
         MuleEvent result = outboundEndpoint.process(event);
 128  0
         MuleMessage message = null;
 129  0
         if (result != null)
 130  
         {
 131  0
             message = result.getMessage();
 132  
         }
 133  
 
 134  0
         final int contentLength = message.getOutboundProperty(HttpConstants.HEADER_CONTENT_LENGTH, -1);
 135  0
         if (contentLength == 0 && discardEmptyContent)
 136  
         {
 137  0
             if (logger.isDebugEnabled())
 138  
             {
 139  0
                 logger.debug("Received empty message and ignoring from: " + endpoint.getEndpointURI());
 140  
             }
 141  0
             return;
 142  
         }
 143  0
         int status = message.getOutboundProperty(HttpConnector.HTTP_STATUS_PROPERTY, 0);
 144  0
         etag = message.getOutboundProperty(HttpConstants.HEADER_ETAG);
 145  
 
 146  0
         if ((status != HttpConstants.SC_NOT_MODIFIED || !checkEtag))
 147  
         {
 148  0
             routeMessage(message);
 149  
         }
 150  0
     }
 151  
 }