1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.transport.http; |
8 | |
|
9 | |
import org.mule.DefaultMuleEvent; |
10 | |
import org.mule.DefaultMuleMessage; |
11 | |
import org.mule.MessageExchangePattern; |
12 | |
import org.mule.api.MuleContext; |
13 | |
import org.mule.api.MuleEvent; |
14 | |
import org.mule.api.MuleMessage; |
15 | |
import org.mule.api.MuleSession; |
16 | |
import org.mule.api.construct.FlowConstruct; |
17 | |
import org.mule.api.endpoint.EndpointBuilder; |
18 | |
import org.mule.api.endpoint.InboundEndpoint; |
19 | |
import org.mule.api.endpoint.OutboundEndpoint; |
20 | |
import org.mule.api.lifecycle.CreateException; |
21 | |
import org.mule.api.processor.MessageProcessor; |
22 | |
import org.mule.api.transport.Connector; |
23 | |
import org.mule.endpoint.EndpointURIEndpointBuilder; |
24 | |
import org.mule.session.DefaultMuleSession; |
25 | |
import org.mule.transport.AbstractPollingMessageReceiver; |
26 | |
import org.mule.transport.http.i18n.HttpMessages; |
27 | |
import org.mule.util.MapUtils; |
28 | |
import org.mule.util.StringUtils; |
29 | |
|
30 | |
import java.util.Collections; |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
public class PollingHttpMessageReceiver extends AbstractPollingMessageReceiver |
36 | |
{ |
37 | 0 | protected String etag = null; |
38 | |
private boolean checkEtag; |
39 | |
private boolean discardEmptyContent; |
40 | |
|
41 | |
private OutboundEndpoint outboundEndpoint; |
42 | |
|
43 | |
public PollingHttpMessageReceiver(Connector connector, |
44 | |
FlowConstruct flowConstruct, |
45 | |
final InboundEndpoint endpoint) throws CreateException |
46 | |
{ |
47 | |
|
48 | 0 | super(connector, flowConstruct, endpoint); |
49 | |
|
50 | |
HttpPollingConnector pollingConnector; |
51 | |
|
52 | 0 | if (connector instanceof HttpPollingConnector) |
53 | |
{ |
54 | 0 | pollingConnector = (HttpPollingConnector) connector; |
55 | |
} |
56 | |
else |
57 | |
{ |
58 | 0 | throw new CreateException(HttpMessages.pollingReciverCannotbeUsed(), this); |
59 | |
} |
60 | |
|
61 | 0 | long pollingFrequency = MapUtils.getLongValue(endpoint.getProperties(), "pollingFrequency", |
62 | |
pollingConnector.getPollingFrequency()); |
63 | 0 | if (pollingFrequency > 0) |
64 | |
{ |
65 | 0 | this.setFrequency(pollingFrequency); |
66 | |
} |
67 | |
|
68 | 0 | checkEtag = MapUtils.getBooleanValue(endpoint.getProperties(), "checkEtag", pollingConnector.isCheckEtag()); |
69 | 0 | discardEmptyContent = MapUtils.getBooleanValue(endpoint.getProperties(), "discardEmptyContent", pollingConnector.isDiscardEmptyContent()); |
70 | 0 | } |
71 | |
|
72 | |
@Override |
73 | |
protected void doDispose() |
74 | |
{ |
75 | |
|
76 | 0 | } |
77 | |
|
78 | |
@Override |
79 | |
protected void doConnect() throws Exception |
80 | |
{ |
81 | |
|
82 | 0 | } |
83 | |
|
84 | |
@Override |
85 | |
public void doDisconnect() throws Exception |
86 | |
{ |
87 | |
|
88 | 0 | } |
89 | |
|
90 | |
@Override |
91 | |
public void poll() throws Exception |
92 | |
{ |
93 | 0 | MuleContext muleContext = connector.getMuleContext(); |
94 | |
|
95 | 0 | if (outboundEndpoint == null) |
96 | |
{ |
97 | |
|
98 | |
|
99 | 0 | EndpointBuilder endpointBuilder = new EndpointURIEndpointBuilder(endpoint); |
100 | |
|
101 | 0 | endpointBuilder.setMessageProcessors(Collections.<MessageProcessor>emptyList()); |
102 | 0 | endpointBuilder.setResponseMessageProcessors(Collections.<MessageProcessor>emptyList()); |
103 | 0 | endpointBuilder.setMessageProcessors(Collections.<MessageProcessor>emptyList()); |
104 | 0 | endpointBuilder.setResponseMessageProcessors(Collections.<MessageProcessor>emptyList()); |
105 | 0 | endpointBuilder.setExchangePattern(MessageExchangePattern.REQUEST_RESPONSE); |
106 | |
|
107 | 0 | outboundEndpoint = muleContext.getEndpointFactory().getOutboundEndpoint( |
108 | |
endpointBuilder); |
109 | |
} |
110 | |
|
111 | 0 | MuleMessage request = new DefaultMuleMessage(StringUtils.EMPTY, outboundEndpoint.getProperties(), muleContext); |
112 | 0 | if (etag != null && checkEtag) |
113 | |
{ |
114 | 0 | request.setOutboundProperty(HttpConstants.HEADER_IF_NONE_MATCH, etag); |
115 | |
} |
116 | 0 | request.setOutboundProperty(HttpConnector.HTTP_METHOD_PROPERTY, "GET"); |
117 | |
|
118 | 0 | MuleSession session = new DefaultMuleSession(flowConstruct, connector.getMuleContext()); |
119 | |
|
120 | |
|
121 | 0 | MuleEvent event = new DefaultMuleEvent(request, outboundEndpoint, session); |
122 | |
|
123 | 0 | MuleEvent result = outboundEndpoint.process(event); |
124 | 0 | MuleMessage message = null; |
125 | 0 | if (result != null) |
126 | |
{ |
127 | 0 | message = result.getMessage(); |
128 | |
} |
129 | |
|
130 | 0 | final int contentLength = message.getOutboundProperty(HttpConstants.HEADER_CONTENT_LENGTH, -1); |
131 | 0 | if (contentLength == 0 && discardEmptyContent) |
132 | |
{ |
133 | 0 | if (logger.isDebugEnabled()) |
134 | |
{ |
135 | 0 | logger.debug("Received empty message and ignoring from: " + endpoint.getEndpointURI()); |
136 | |
} |
137 | 0 | return; |
138 | |
} |
139 | 0 | int status = message.getOutboundProperty(HttpConnector.HTTP_STATUS_PROPERTY, 0); |
140 | 0 | etag = message.getOutboundProperty(HttpConstants.HEADER_ETAG); |
141 | |
|
142 | 0 | if ((status != HttpConstants.SC_NOT_MODIFIED || !checkEtag)) |
143 | |
{ |
144 | 0 | routeMessage(message); |
145 | |
} |
146 | 0 | } |
147 | |
} |