KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > http > PollingHttpMessageReceiver


1 /*
2  * $Id: PollingHttpMessageReceiver.java 3798 2006-11-04 04:07:14Z aperepel $
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 MuleSource MPL
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.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 JavaDoc;
27 import java.net.HttpURLConnection JavaDoc;
28 import java.net.MalformedURLException JavaDoc;
29 import java.net.URL JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Map JavaDoc;
34
35 /**
36  * Will poll an http URL and use the response as the input for a service request.
37  *
38  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
39  * @version $Revision: 3798 $
40  */

41 public class PollingHttpMessageReceiver extends PollingMessageReceiver
42 {
43     private URL JavaDoc 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 JavaDoc(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 JavaDoc frequency) throws InitialisationException
64     {
65         super(connector, component, endpoint, frequency);
66         try
67         {
68             pollUrl = new URL JavaDoc(endpoint.getEndpointURI().getAddress());
69         }
70         catch (MalformedURLException JavaDoc 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 JavaDoc
78     {
79         HttpURLConnection JavaDoc connection = (HttpURLConnection JavaDoc)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         // TODO this is pretty dangerous for big payloads
91
byte[] buffer = new byte[contentLength];
92         ByteArrayOutputStream baos = new ByteArrayOutputStream(contentLength);
93         InputStream JavaDoc is = connection.getInputStream();
94         // Ensure we read all bytes, http connections may be slow
95
// to send all bytes in consistent stream. I've only seen
96
// this when using Axis...
97
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         // Truncate repetitive headers
117
Map JavaDoc respHeaders = new HashMap JavaDoc();
118         Iterator JavaDoc it = connection.getHeaderFields().entrySet().iterator();
119         while (it.hasNext())
120         {
121             Map.Entry JavaDoc msgHeader = (Map.Entry JavaDoc)it.next();
122             if (msgHeader.getValue() != null)
123             {
124                 respHeaders.put(msgHeader.getKey(), ((List JavaDoc)msgHeader.getValue()).get(0));
125             }
126         }
127
128         UMOMessageAdapter adapter = connector.getMessageAdapter(new Object JavaDoc[]{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 JavaDoc
136     {
137         URL JavaDoc url = null;
138         String JavaDoc connectUrl = (String JavaDoc)endpoint.getProperties().get("connectUrl");
139         if (connectUrl == null)
140         {
141             url = pollUrl;
142         }
143         else
144         {
145             url = new URL JavaDoc(connectUrl);
146         }
147         logger.debug("Using url to connect: " + pollUrl.toString());
148         HttpURLConnection JavaDoc connection = (HttpURLConnection JavaDoc)url.openConnection();
149         connection.disconnect();
150     }
151
152     public void doDisconnect() throws Exception JavaDoc
153     {
154         // nothing to do
155
}
156 }
157
Popular Tags