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