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