1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
package org.mule.module.atom.endpoint; |
11 | |
|
12 | |
import org.mule.api.MuleContext; |
13 | |
import org.mule.api.endpoint.EndpointException; |
14 | |
import org.mule.api.endpoint.EndpointURI; |
15 | |
import org.mule.api.endpoint.InboundEndpoint; |
16 | |
import org.mule.api.endpoint.OutboundEndpoint; |
17 | |
import org.mule.api.lifecycle.InitialisationException; |
18 | |
import org.mule.api.routing.filter.Filter; |
19 | |
import org.mule.endpoint.AbstractMetaEndpointBuilder; |
20 | |
import org.mule.endpoint.EndpointURIEndpointBuilder; |
21 | |
import org.mule.endpoint.URIBuilder; |
22 | |
import org.mule.module.atom.routing.EntryLastUpdatedFilter; |
23 | |
import org.mule.module.atom.routing.FeedSplitter; |
24 | |
import org.mule.transport.http.HttpPollingConnector; |
25 | |
import org.mule.util.StringUtils; |
26 | |
|
27 | |
import java.text.ParseException; |
28 | |
import java.text.SimpleDateFormat; |
29 | |
import java.util.Date; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
public class AtomEndpointBuilder extends AbstractMetaEndpointBuilder |
35 | |
{ |
36 | |
public static final String DATE_FORMAT = "yyyy-MM-dd hh:mm:ss"; |
37 | |
public static final String SHORT_DATE_FORMAT = "yyyy-MM-dd"; |
38 | |
|
39 | 0 | private boolean splitFeed = true; |
40 | |
|
41 | 0 | private String lastUpdate = null; |
42 | |
|
43 | 0 | private long pollingFrequency = 1000; |
44 | |
|
45 | 0 | private final SimpleDateFormat dateFormatter = new SimpleDateFormat(DATE_FORMAT); |
46 | 0 | private final SimpleDateFormat shortDateFormatter = new SimpleDateFormat(SHORT_DATE_FORMAT); |
47 | |
|
48 | |
public AtomEndpointBuilder() |
49 | |
{ |
50 | 0 | super(); |
51 | 0 | } |
52 | |
|
53 | |
public AtomEndpointBuilder(EndpointURIEndpointBuilder global) throws EndpointException |
54 | |
{ |
55 | 0 | super(global); |
56 | 0 | } |
57 | |
|
58 | |
public AtomEndpointBuilder(URIBuilder uriBuilder) |
59 | |
{ |
60 | 0 | super(uriBuilder); |
61 | 0 | } |
62 | |
|
63 | |
public AtomEndpointBuilder(String address, MuleContext muleContext) |
64 | |
{ |
65 | 0 | super(address, muleContext); |
66 | 0 | } |
67 | |
|
68 | |
protected AtomEndpointBuilder(EndpointURI endpointURI) |
69 | |
{ |
70 | 0 | super(endpointURI); |
71 | 0 | } |
72 | |
|
73 | |
@Override |
74 | |
public InboundEndpoint buildInboundEndpoint() throws EndpointException, InitialisationException |
75 | |
{ |
76 | |
try |
77 | |
{ |
78 | 0 | Date date = formatDate(getLastUpdate()); |
79 | 0 | if (isSplitFeed()) |
80 | |
{ |
81 | 0 | Filter filter = new EntryLastUpdatedFilter(date); |
82 | 0 | FeedSplitter splitter = new FeedSplitter(); |
83 | 0 | splitter.setEntryFilter(filter); |
84 | 0 | addMessageProcessor(splitter); |
85 | |
} |
86 | 0 | AtomInboundEndpoint in = new AtomInboundEndpoint(isSplitFeed(), date, super.buildInboundEndpoint()); |
87 | 0 | in.registerSupportedProtocol("http"); |
88 | 0 | in.registerSupportedProtocol("https"); |
89 | 0 | in.registerSupportedProtocol("vm"); |
90 | 0 | if (in.getConnector() instanceof HttpPollingConnector) |
91 | |
{ |
92 | 0 | ((HttpPollingConnector) in.getConnector()).setPollingFrequency(pollingFrequency); |
93 | |
} |
94 | 0 | return in; |
95 | |
} |
96 | 0 | catch (ParseException e) |
97 | |
{ |
98 | 0 | throw new EndpointException(e); |
99 | |
} |
100 | |
} |
101 | |
|
102 | |
@Override |
103 | |
public OutboundEndpoint buildOutboundEndpoint() throws EndpointException, InitialisationException |
104 | |
{ |
105 | 0 | throw new UnsupportedOperationException("Outbound ATOM endpoints not supported"); |
106 | |
} |
107 | |
|
108 | |
public String getLastUpdate() |
109 | |
{ |
110 | 0 | return lastUpdate; |
111 | |
} |
112 | |
|
113 | |
public void setLastUpdate(String lastUpdate) |
114 | |
{ |
115 | 0 | this.lastUpdate = lastUpdate; |
116 | 0 | } |
117 | |
|
118 | |
public boolean isSplitFeed() |
119 | |
{ |
120 | 0 | return splitFeed; |
121 | |
} |
122 | |
|
123 | |
public void setSplitFeed(boolean splitFeed) |
124 | |
{ |
125 | 0 | this.splitFeed = splitFeed; |
126 | 0 | } |
127 | |
|
128 | |
public long getPollingFrequency() |
129 | |
{ |
130 | 0 | return pollingFrequency; |
131 | |
} |
132 | |
|
133 | |
public void setPollingFrequency(long pollingFrequency) |
134 | |
{ |
135 | 0 | this.pollingFrequency = pollingFrequency; |
136 | 0 | } |
137 | |
|
138 | |
protected Date formatDate(String date) throws ParseException |
139 | |
{ |
140 | 0 | Date lastUpdateDate = null; |
141 | 0 | if (StringUtils.isNotBlank(date)) |
142 | |
{ |
143 | 0 | if (lastUpdate.length() == 10) |
144 | |
{ |
145 | 0 | lastUpdateDate = shortDateFormatter.parse(date); |
146 | |
} |
147 | |
else |
148 | |
{ |
149 | 0 | lastUpdateDate = dateFormatter.parse(date); |
150 | |
} |
151 | |
} |
152 | 0 | return lastUpdateDate; |
153 | |
} |
154 | |
} |