1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
package org.mule.module.rss.routing; |
11 | |
|
12 | |
import org.mule.DefaultMuleMessage; |
13 | |
import org.mule.api.MessagingException; |
14 | |
import org.mule.api.MuleEvent; |
15 | |
import org.mule.api.MuleException; |
16 | |
import org.mule.api.MuleMessage; |
17 | |
import org.mule.api.routing.filter.Filter; |
18 | |
import org.mule.module.rss.transformers.ObjectToRssFeed; |
19 | |
import org.mule.routing.AbstractSplitter; |
20 | |
|
21 | |
import com.sun.syndication.feed.synd.SyndEntry; |
22 | |
import com.sun.syndication.feed.synd.SyndFeed; |
23 | |
|
24 | |
import java.util.ArrayList; |
25 | |
import java.util.Comparator; |
26 | |
import java.util.List; |
27 | |
import java.util.Set; |
28 | |
import java.util.TreeSet; |
29 | |
|
30 | |
import org.apache.commons.logging.Log; |
31 | |
import org.apache.commons.logging.LogFactory; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
public class FeedSplitter extends AbstractSplitter |
38 | |
{ |
39 | |
|
40 | |
|
41 | |
|
42 | 0 | protected transient final Log logger = LogFactory.getLog(FeedSplitter.class); |
43 | |
|
44 | |
public static final String FEED_PROPERTY = "feed.object"; |
45 | |
private Filter entryFilter; |
46 | 0 | private ObjectToRssFeed objectToFeed = new ObjectToRssFeed(); |
47 | |
|
48 | |
public FeedSplitter() |
49 | 0 | { |
50 | |
|
51 | 0 | entryFilter = new EntryLastUpdatedFilter(null); |
52 | 0 | } |
53 | |
|
54 | |
@Override |
55 | |
protected List<MuleMessage> splitMessage(MuleEvent event) throws MuleException |
56 | |
{ |
57 | |
|
58 | 0 | setMuleContext(event.getMuleContext()); |
59 | |
|
60 | 0 | List<MuleMessage> messages = new ArrayList<MuleMessage>(); |
61 | 0 | if(event.getMessage().getInboundProperty("Content-Length", -1) == 0) |
62 | |
{ |
63 | 0 | logger.info("Feed has no content, ignoring"); |
64 | 0 | return messages; |
65 | |
} |
66 | |
|
67 | |
try |
68 | |
{ |
69 | 0 | Object payload = event.getMessage().getPayload(); |
70 | |
|
71 | |
SyndFeed feed; |
72 | 0 | if (payload instanceof SyndFeed) |
73 | |
{ |
74 | 0 | feed = (SyndFeed) payload; |
75 | |
} |
76 | |
else |
77 | |
{ |
78 | 0 | feed = (SyndFeed) objectToFeed.transform(event.getMessage().getPayload()); |
79 | |
} |
80 | |
|
81 | 0 | Set<SyndEntry> entries = new TreeSet<SyndEntry>(new EntryComparator()); |
82 | 0 | entries.addAll(feed.getEntries()); |
83 | |
|
84 | 0 | for (SyndEntry entry : entries) |
85 | |
{ |
86 | 0 | MuleMessage m = new DefaultMuleMessage(entry, event.getMuleContext()); |
87 | 0 | if (entryFilter != null && !entryFilter.accept(m)) |
88 | |
{ |
89 | 0 | continue; |
90 | |
} |
91 | 0 | m.setInvocationProperty(FEED_PROPERTY, feed); |
92 | 0 | messages.add(m); |
93 | 0 | } |
94 | 0 | return messages; |
95 | |
|
96 | |
} |
97 | 0 | catch (MuleException e) |
98 | |
{ |
99 | 0 | throw new MessagingException(e.getI18nMessage(), event, e); |
100 | |
} |
101 | |
} |
102 | |
|
103 | |
public Filter getEntryFilter() |
104 | |
{ |
105 | 0 | return entryFilter; |
106 | |
} |
107 | |
|
108 | |
public void setEntryFilter(Filter entryFilter) |
109 | |
{ |
110 | 0 | this.entryFilter = entryFilter; |
111 | 0 | } |
112 | |
|
113 | 0 | class EntryComparator implements Comparator<SyndEntry> |
114 | |
{ |
115 | |
public int compare(SyndEntry e1, SyndEntry e2) |
116 | |
{ |
117 | 0 | if (e1.getPublishedDate().before(e2.getPublishedDate())) |
118 | |
{ |
119 | 0 | return -1; |
120 | |
} |
121 | 0 | else if (e1.equals(e2)) |
122 | |
{ |
123 | 0 | return 0; |
124 | |
} |
125 | |
else |
126 | |
{ |
127 | 0 | return 1; |
128 | |
} |
129 | |
} |
130 | |
} |
131 | |
} |