View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
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.SyndFeed;
17  
18  import java.util.Date;
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  
23  /**
24   * Will filter a feed who's update date has changed since the last time it was read.  Some feeds to no update
25   * this value so {@link #setAcceptWithoutUpdateDate(boolean)} can be set to always consume the feed
26   */
27  public class FeedLastUpdatedFilter implements Filter
28  {
29      /**
30       * logger used by this class
31       */
32      private final transient Log logger = LogFactory.getLog(FeedLastUpdatedFilter.class);
33      private Date lastUpdate;
34  
35      private boolean acceptWithoutUpdateDate = true;
36  
37      public FeedLastUpdatedFilter()
38      {
39          //For Spring Xml
40      }
41  
42      public FeedLastUpdatedFilter(Date lastUpdate)
43      {
44          this.lastUpdate = lastUpdate;
45      }
46  
47      public boolean accept(MuleMessage message)
48      {
49          SyndFeed feed;
50          try
51          {
52              feed = message.getPayload(DataTypeFactory.create(SyndFeed.class));
53          }
54          catch (TransformerException e)
55          {
56              throw new MuleRuntimeException(CoreMessages.failedToReadPayload(), e);
57          }
58  
59          Date updated = feed.getPublishedDate();
60          if (updated == null)
61          {
62              if (isAcceptWithoutUpdateDate())
63              {
64                  if (logger.isDebugEnabled())
65                  {
66                      logger.debug("Feed does not have a last updated or published date set, assuming the feed should be processed");
67                  }
68                  return true;
69              }
70              else
71              {
72                  if (logger.isWarnEnabled())
73                  {
74                      logger.warn("Feed does not have a last updated or published date set, not consuming the feed because 'acceptWithoutUpdateDate' is false");
75                  }
76                  return false;
77              }
78          }
79  
80          if (lastUpdate != null)
81          {
82              if (lastUpdate.after(updated) || lastUpdate.equals(updated))
83              {
84                  if (logger.isDebugEnabled())
85                  {
86                      logger.debug("Feed update is not newer than the last update, not processing");
87                  }
88                  return false;
89              }
90          }
91          lastUpdate = updated;
92          return true;
93  
94      }
95  
96      public boolean isAcceptWithoutUpdateDate()
97      {
98          return acceptWithoutUpdateDate;
99      }
100 
101     public void setAcceptWithoutUpdateDate(boolean acceptWithoutUpdateDate)
102     {
103         this.acceptWithoutUpdateDate = acceptWithoutUpdateDate;
104     }
105 
106     public Date getLastUpdate()
107     {
108         return lastUpdate;
109     }
110 
111     public void setLastUpdate(Date lastUpdate)
112     {
113         this.lastUpdate = lastUpdate;
114     }
115 }