1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.module.rss.routing; |
8 | |
|
9 | |
import org.mule.api.MuleMessage; |
10 | |
import org.mule.api.MuleRuntimeException; |
11 | |
import org.mule.api.routing.filter.Filter; |
12 | |
import org.mule.api.transformer.TransformerException; |
13 | |
import org.mule.config.i18n.CoreMessages; |
14 | |
import org.mule.transformer.types.DataTypeFactory; |
15 | |
|
16 | |
import com.sun.syndication.feed.synd.SyndEntry; |
17 | |
|
18 | |
import java.util.Date; |
19 | |
|
20 | |
import org.apache.commons.logging.Log; |
21 | |
import org.apache.commons.logging.LogFactory; |
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
public class EntryLastUpdatedFilter implements Filter |
29 | |
{ |
30 | |
|
31 | |
|
32 | |
|
33 | 0 | private final transient Log logger = LogFactory.getLog(EntryLastUpdatedFilter.class); |
34 | |
|
35 | |
private Date lastUpdate; |
36 | |
|
37 | 0 | private boolean acceptWithoutUpdateDate = true; |
38 | |
|
39 | |
public EntryLastUpdatedFilter() |
40 | 0 | { |
41 | |
|
42 | 0 | } |
43 | |
|
44 | |
public EntryLastUpdatedFilter(Date lastUpdate) |
45 | 0 | { |
46 | 0 | this.lastUpdate = lastUpdate; |
47 | 0 | } |
48 | |
|
49 | |
public boolean accept(MuleMessage message) |
50 | |
{ |
51 | 0 | SyndEntry feed = transformToSyndEntry(message); |
52 | |
|
53 | 0 | Date updated = feed.getPublishedDate(); |
54 | 0 | if (updated == null) |
55 | |
{ |
56 | 0 | if (isAcceptWithoutUpdateDate()) |
57 | |
{ |
58 | 0 | if (logger.isDebugEnabled()) |
59 | |
{ |
60 | 0 | logger.debug("Feed does not have a last updated or published date set, assuming the feed should be processed"); |
61 | |
} |
62 | 0 | return true; |
63 | |
} |
64 | |
else |
65 | |
{ |
66 | 0 | if (logger.isWarnEnabled()) |
67 | |
{ |
68 | 0 | logger.warn("Feed does not have a last updated or published date set, not consuming the feed because 'acceptWithoutUpdateDate' is false"); |
69 | |
} |
70 | 0 | return false; |
71 | |
} |
72 | |
} |
73 | |
|
74 | 0 | if (lastUpdate != null) |
75 | |
{ |
76 | 0 | if (lastUpdate.after(updated) || lastUpdate.equals(updated)) |
77 | |
{ |
78 | 0 | if (logger.isDebugEnabled()) |
79 | |
{ |
80 | 0 | logger.debug("Feed update is not newer than the last update, not processing"); |
81 | |
} |
82 | 0 | return false; |
83 | |
} |
84 | |
} |
85 | |
|
86 | 0 | lastUpdate = updated; |
87 | 0 | return true; |
88 | |
} |
89 | |
|
90 | |
protected SyndEntry transformToSyndEntry(MuleMessage message) |
91 | |
{ |
92 | |
try |
93 | |
{ |
94 | 0 | return message.getPayload(DataTypeFactory.create(SyndEntry.class)); |
95 | |
} |
96 | 0 | catch (TransformerException e) |
97 | |
{ |
98 | 0 | throw new MuleRuntimeException(CoreMessages.failedToReadPayload(), e); |
99 | |
} |
100 | |
} |
101 | |
|
102 | |
public boolean isAcceptWithoutUpdateDate() |
103 | |
{ |
104 | 0 | return acceptWithoutUpdateDate; |
105 | |
} |
106 | |
|
107 | |
public void setAcceptWithoutUpdateDate(boolean acceptWithoutUpdateDate) |
108 | |
{ |
109 | 0 | this.acceptWithoutUpdateDate = acceptWithoutUpdateDate; |
110 | 0 | } |
111 | |
|
112 | |
public Date getLastUpdate() |
113 | |
{ |
114 | 0 | return lastUpdate; |
115 | |
} |
116 | |
|
117 | |
public void setLastUpdate(Date lastUpdate) |
118 | |
{ |
119 | 0 | this.lastUpdate = lastUpdate; |
120 | 0 | } |
121 | |
} |