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