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