Coverage Report - org.mule.transport.http.PollingHttpMessageReceiver
 
Classes in this File Line Coverage Branch Coverage Complexity
PollingHttpMessageReceiver
83%
30/36
45%
9/20
2.8
 
 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  10
     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  10
         super(connector, service, endpoint);
 52  
 
 53  
         HttpPollingConnector pollingConnector;
 54  
 
 55  10
         if (connector instanceof HttpPollingConnector)
 56  
         {
 57  10
             pollingConnector = (HttpPollingConnector) connector;
 58  
         }
 59  
         else
 60  
         {
 61  0
             throw new CreateException(HttpMessages.pollingReciverCannotbeUsed(), this);
 62  
         }
 63  
 
 64  10
         long pollingFrequency = MapUtils.getLongValue(endpoint.getProperties(), "pollingFrequency",
 65  
                 pollingConnector.getPollingFrequency());
 66  10
         if (pollingFrequency > 0)
 67  
         {
 68  10
             this.setFrequency(pollingFrequency);
 69  
         }
 70  
 
 71  10
         checkEtag = MapUtils.getBooleanValue(endpoint.getProperties(), "checkEtag", pollingConnector.isCheckEtag());
 72  10
         discardEmptyContent = MapUtils.getBooleanValue(endpoint.getProperties(), "discardEmptyContent", pollingConnector.isDiscardEmptyContent());
 73  10
     }
 74  
 
 75  
     protected void doDispose()
 76  
     {
 77  
         // nothing to do
 78  20
     }
 79  
 
 80  
     protected void doConnect() throws Exception
 81  
     {
 82  
         // nothing to do
 83  10
     }
 84  
 
 85  
     public void doDisconnect() throws Exception
 86  
     {
 87  
         // nothing to do
 88  10
     }
 89  
 
 90  
     public void poll() throws Exception
 91  
     {
 92  14
         MuleMessage req = new DefaultMuleMessage(new DefaultMessageAdapter(""));
 93  14
         if (etag != null && checkEtag)
 94  
         {
 95  0
             Map customHeaders = Collections.singletonMap(HttpConstants.HEADER_IF_NONE_MATCH, etag);
 96  0
             req.setProperty(HttpConnector.HTTP_CUSTOM_HEADERS_MAP_PROPERTY, customHeaders);
 97  
         }
 98  14
         req.setProperty(HttpConnector.HTTP_METHOD_PROPERTY, "GET");
 99  
 
 100  14
         MuleSession session = new DefaultMuleSession(service, connector.getMuleContext());
 101  
 
 102  14
         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  10
             MuleContext muleContext = endpoint.getMuleContext();
 107  10
             EndpointBuilder endpointBuilder = new EndpointURIEndpointBuilder(endpoint, muleContext);
 108  
             //Must not use inbound transformers for the outbound request
 109  10
             endpointBuilder.setTransformers(Collections.EMPTY_LIST);
 110  10
             outboundEndpoint = muleContext.getRegistry().lookupEndpointFactory().getOutboundEndpoint(
 111  
                     endpointBuilder);
 112  
         }
 113  14
         MuleEvent event = new DefaultMuleEvent(req, outboundEndpoint, session, true);
 114  
 
 115  14
         MuleMessage message = connector.send(outboundEndpoint, event);
 116  
 
 117  14
         if (message.getIntProperty(HttpConstants.HEADER_CONTENT_LENGTH, 0) == 0 && discardEmptyContent)
 118  
         {
 119  0
             if (logger.isDebugEnabled())
 120  
             {
 121  0
                 logger.debug("Received empty message and ignoring from: " + endpoint.getEndpointURI());
 122  
             }
 123  0
             return;
 124  
         }
 125  14
         int status = message.getIntProperty(HttpConnector.HTTP_STATUS_PROPERTY, 0);
 126  14
         etag = message.getStringProperty(HttpConstants.HEADER_ETAG, null);
 127  
 
 128  14
         if ((status != 304 || !checkEtag))
 129  
         {
 130  14
             routeMessage(message, endpoint.isSynchronous());
 131  
         }
 132  14
     }
 133  
 }