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.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   * Will filter a feed who's update date has changed since the last time it was read.  Some feeds to no update
23   * this value so {@link #setAcceptWithoutUpdateDate(boolean)} can be set to always consume the feed
24   */
25  public class FeedLastUpdatedFilter implements Filter
26  {
27      /**
28       * logger used by this class
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          //For Spring Xml
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 }