Coverage Report - org.mule.providers.http.PollingHttpMessageReceiver
 
Classes in this File Line Coverage Branch Coverage Complexity
PollingHttpMessageReceiver
87%
48/55
70%
14/20
3.2
 
 1  
 /*
 2  
  * $Id: PollingHttpMessageReceiver.java 7963 2007-08-21 08:53:15Z dirk.olmes $
 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.providers.http;
 12  
 
 13  
 import org.mule.config.i18n.CoreMessages;
 14  
 import org.mule.impl.MuleMessage;
 15  
 import org.mule.providers.AbstractPollingMessageReceiver;
 16  
 import org.mule.umo.UMOComponent;
 17  
 import org.mule.umo.UMOMessage;
 18  
 import org.mule.umo.endpoint.UMOEndpoint;
 19  
 import org.mule.umo.lifecycle.InitialisationException;
 20  
 import org.mule.umo.provider.UMOConnector;
 21  
 import org.mule.umo.provider.UMOMessageAdapter;
 22  
 import org.mule.util.Base64;
 23  
 import org.mule.util.MapUtils;
 24  
 
 25  
 import java.io.InputStream;
 26  
 import java.net.HttpURLConnection;
 27  
 import java.net.MalformedURLException;
 28  
 import java.net.URL;
 29  
 import java.util.HashMap;
 30  
 import java.util.Iterator;
 31  
 import java.util.List;
 32  
 import java.util.Map;
 33  
 
 34  
 import org.apache.commons.io.output.ByteArrayOutputStream;
 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  
     private URL pollUrl;
 42  
 
 43  
     private static final int DEFAULT_BUFFER_SIZE = 1024 * 32;
 44  
 
 45  
     public PollingHttpMessageReceiver(UMOConnector connector,
 46  
                                       UMOComponent component,
 47  
                                       final UMOEndpoint endpoint) throws InitialisationException
 48  
     {
 49  6
         super(connector, component, endpoint);
 50  
 
 51  6
         long pollingFrequency = MapUtils.getLongValue(endpoint.getProperties(), "pollingFrequency",
 52  
             -1);
 53  6
         if (pollingFrequency > 0)
 54  
         {
 55  2
             this.setFrequency(pollingFrequency);
 56  
         }
 57  
 
 58  
         try
 59  
         {
 60  6
             pollUrl = new URL(endpoint.getEndpointURI().getAddress());
 61  
         }
 62  0
         catch (MalformedURLException e)
 63  
         {
 64  0
             throw new InitialisationException(
 65  
                 CoreMessages.valueIsInvalidFor(endpoint.getEndpointURI().getAddress(), "uri"), 
 66  
                 e, this);
 67  6
         }
 68  6
     }
 69  
 
 70  
     protected void doDispose()
 71  
     {
 72  
         // template method
 73  6
     }
 74  
 
 75  
     protected void doConnect() throws Exception
 76  
     {
 77  
         URL url;
 78  6
         String connectUrl = (String)endpoint.getProperties().get("connectUrl");
 79  6
         if (connectUrl == null)
 80  
         {
 81  6
             url = pollUrl;
 82  
         }
 83  
         else
 84  
         {
 85  0
             url = new URL(connectUrl);
 86  
         }
 87  6
         logger.debug("Using url to connect: " + pollUrl.toString());
 88  6
         HttpURLConnection connection = (HttpURLConnection)url.openConnection();
 89  6
         connection.disconnect();
 90  6
     }
 91  
 
 92  
     public void doDisconnect() throws Exception
 93  
     {
 94  
         // nothing to do
 95  6
     }
 96  
 
 97  
     public void poll() throws Exception
 98  
     {
 99  14
         HttpURLConnection connection = (HttpURLConnection)pollUrl.openConnection();
 100  14
         String authentication = endpoint.getEndpointURI().getUserInfo();
 101  14
         if (authentication != null)
 102  
         {
 103  2
             connection.setRequestProperty("Authorization", "Basic "
 104  
                                                            + Base64.encodeBytes(authentication
 105  
                                                                .getBytes()));
 106  
         }
 107  
 
 108  
         int len;
 109  14
         int bytesWritten = 0;
 110  
 
 111  14
         int contentLength = connection.getContentLength();
 112  14
         boolean contentLengthNotSet = false;
 113  14
         if (contentLength < 0)
 114  
         {
 115  0
             contentLength = DEFAULT_BUFFER_SIZE;
 116  0
             contentLengthNotSet = true;
 117  
         }
 118  
 
 119  
         // TODO this is pretty dangerous for big payloads
 120  14
         byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
 121  14
         ByteArrayOutputStream baos = new ByteArrayOutputStream(contentLength);
 122  14
         InputStream is = connection.getInputStream();
 123  
 
 124  
         // Ensure we read all bytes, http connections may be slow
 125  
         // to send all bytes in consistent stream. I've only seen
 126  
         // this when using Axis...
 127  26
         while (bytesWritten != contentLength)
 128  
         {
 129  12
             len = is.read(buffer);
 130  12
             if (len != -1)
 131  
             {
 132  12
                 baos.write(buffer, 0, len);
 133  12
                 bytesWritten += len;
 134  
             }
 135  
             else
 136  
             {
 137  0
                 if (contentLengthNotSet)
 138  
                 {
 139  0
                     contentLength = bytesWritten;
 140  
                 }
 141  
             }
 142  
         }
 143  14
         buffer = baos.toByteArray();
 144  14
         baos.close();
 145  
 
 146  
         // Truncate repetitive headers
 147  14
         Map respHeaders = new HashMap();
 148  14
         Iterator it = connection.getHeaderFields().entrySet().iterator();
 149  112
         while (it.hasNext())
 150  
         {
 151  98
             Map.Entry msgHeader = (Map.Entry)it.next();
 152  98
             Object key = msgHeader.getKey();
 153  98
             Object value = msgHeader.getValue();
 154  98
             if (key != null && value != null)
 155  
             {
 156  84
                 respHeaders.put(key, ((List)value).get(0));
 157  
             }
 158  98
         }
 159  
 
 160  14
         UMOMessageAdapter adapter = connector.getMessageAdapter(new Object[]{buffer, respHeaders});
 161  
 
 162  14
         connection.disconnect();
 163  14
         UMOMessage message = new MuleMessage(adapter);
 164  14
         routeMessage(message, endpoint.isSynchronous());
 165  14
     }
 166  
 }