1 10 11 package org.mule.providers.http; 12 13 import org.apache.commons.collections.MapUtils; 14 import org.apache.commons.io.output.ByteArrayOutputStream; 15 import org.mule.config.i18n.Message; 16 import org.mule.config.i18n.Messages; 17 import org.mule.impl.MuleMessage; 18 import org.mule.providers.PollingMessageReceiver; 19 import org.mule.umo.UMOComponent; 20 import org.mule.umo.UMOMessage; 21 import org.mule.umo.endpoint.UMOEndpoint; 22 import org.mule.umo.lifecycle.InitialisationException; 23 import org.mule.umo.provider.UMOConnector; 24 import org.mule.umo.provider.UMOMessageAdapter; 25 26 import java.io.InputStream ; 27 import java.net.HttpURLConnection ; 28 import java.net.MalformedURLException ; 29 import java.net.URL ; 30 import java.util.HashMap ; 31 import java.util.Iterator ; 32 import java.util.List ; 33 import java.util.Map ; 34 35 41 public class PollingHttpMessageReceiver extends PollingMessageReceiver 42 { 43 private URL pollUrl; 44 45 private int defaultBufferSize = 1024 * 32; 46 47 public PollingHttpMessageReceiver(UMOConnector connector, 48 UMOComponent component, 49 final UMOEndpoint endpoint) throws InitialisationException 50 { 51 this(connector, component, endpoint, new Long (1000)); 52 53 long pollingFrequency = MapUtils.getLongValue(endpoint.getProperties(), "pollingFrequency", -1); 54 if (pollingFrequency > 0) 55 { 56 setFrequency(pollingFrequency); 57 } 58 } 59 60 public PollingHttpMessageReceiver(UMOConnector connector, 61 UMOComponent component, 62 final UMOEndpoint endpoint, 63 Long frequency) throws InitialisationException 64 { 65 super(connector, component, endpoint, frequency); 66 try 67 { 68 pollUrl = new URL (endpoint.getEndpointURI().getAddress()); 69 } 70 catch (MalformedURLException e) 71 { 72 throw new InitialisationException(new Message(Messages.VALUE_X_IS_INVALID_FOR_X, 73 endpoint.getEndpointURI().getAddress(), "uri"), e, this); 74 } 75 } 76 77 public void poll() throws Exception 78 { 79 HttpURLConnection connection = (HttpURLConnection )pollUrl.openConnection(); 80 81 int len = 0; 82 int bytesWritten = 0; 83 int contentLength = connection.getContentLength(); 84 boolean contentLengthNotSet = false; 85 if (contentLength < 0) 86 { 87 contentLength = defaultBufferSize; 88 contentLengthNotSet = true; 89 } 90 byte[] buffer = new byte[contentLength]; 92 ByteArrayOutputStream baos = new ByteArrayOutputStream(contentLength); 93 InputStream is = connection.getInputStream(); 94 while (bytesWritten != contentLength) 98 { 99 len = is.read(buffer); 100 if (len != -1) 101 { 102 baos.write(buffer, 0, len); 103 bytesWritten += len; 104 } 105 else 106 { 107 if (contentLengthNotSet) 108 { 109 contentLength = bytesWritten; 110 } 111 } 112 } 113 buffer = baos.toByteArray(); 114 baos.close(); 115 116 Map respHeaders = new HashMap (); 118 Iterator it = connection.getHeaderFields().entrySet().iterator(); 119 while (it.hasNext()) 120 { 121 Map.Entry msgHeader = (Map.Entry )it.next(); 122 if (msgHeader.getValue() != null) 123 { 124 respHeaders.put(msgHeader.getKey(), ((List )msgHeader.getValue()).get(0)); 125 } 126 } 127 128 UMOMessageAdapter adapter = connector.getMessageAdapter(new Object []{buffer, respHeaders}); 129 130 connection.disconnect(); 131 UMOMessage message = new MuleMessage(adapter); 132 routeMessage(message, endpoint.isSynchronous()); 133 } 134 135 public void doConnect() throws Exception 136 { 137 URL url = null; 138 String connectUrl = (String )endpoint.getProperties().get("connectUrl"); 139 if (connectUrl == null) 140 { 141 url = pollUrl; 142 } 143 else 144 { 145 url = new URL (connectUrl); 146 } 147 logger.debug("Using url to connect: " + pollUrl.toString()); 148 HttpURLConnection connection = (HttpURLConnection )url.openConnection(); 149 connection.disconnect(); 150 } 151 152 public void doDisconnect() throws Exception 153 { 154 } 156 } 157 | Popular Tags |