View Javadoc

1   /*
2    * $Id: EntryLastUpdatedFilter.java 19980 2010-10-21 11:27:14Z rossmason $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  package org.mule.module.atom.routing;
11  
12  import org.mule.api.MuleMessage;
13  import org.mule.api.routing.filter.Filter;
14  
15  import java.util.Date;
16  
17  import org.apache.abdera.model.Entry;
18  import org.apache.commons.logging.Log;
19  import org.apache.commons.logging.LogFactory;
20  
21  /**
22   * Will filter entries in an atom feed based on the lasted Edited date, falling back to the
23   * published date if the edit date was not set
24   */
25  public class EntryLastUpdatedFilter implements Filter
26  {
27      /**
28       * logger used by this class
29       */
30      private final transient Log logger = LogFactory.getLog(EntryLastUpdatedFilter.class);
31      private Date lastUpdate;
32  
33      private boolean acceptWithoutUpdateDate = true;
34  
35      public EntryLastUpdatedFilter()
36      {
37      }
38  
39      public EntryLastUpdatedFilter(Date lastUpdate)
40      {
41          this.lastUpdate = lastUpdate;
42      }
43  
44      public boolean accept(MuleMessage message)
45      {
46          Entry entry = (Entry) message.getPayload();
47          Date updated = entry.getEdited();
48          if (updated == null)
49          {
50              updated = entry.getPublished();
51              if (updated == null)
52              {
53                  if (isAcceptWithoutUpdateDate())
54                  {
55                      if (logger.isDebugEnabled())
56                      {
57                          logger.debug("Entry does not have a last updated or published date set, assuming the feed should be processed");
58                      }
59                      return true;
60                  }
61                  else
62                  {
63                      if (logger.isWarnEnabled())
64                      {
65                          logger.warn("Entry does not have a last updated or published date set, not consuming the feed because 'acceptWithoutUpdateDate' is false");
66                      }
67                      return false;
68                  }
69              }
70          }
71          if (lastUpdate != null)
72          {
73              if (lastUpdate.after(updated) || lastUpdate.equals(updated))
74              {
75                  if (logger.isDebugEnabled())
76                  {
77                      logger.debug("Feed update is not newer than the last update, not processing");
78                  }
79                  return false;
80              }
81          }
82          lastUpdate = updated;
83          return true;
84      }
85  
86      public boolean isAcceptWithoutUpdateDate()
87      {
88          return acceptWithoutUpdateDate;
89      }
90  
91      public void setAcceptWithoutUpdateDate(boolean acceptWithoutUpdateDate)
92      {
93          this.acceptWithoutUpdateDate = acceptWithoutUpdateDate;
94      }
95  
96      public Date getLastUpdate()
97      {
98          return lastUpdate;
99      }
100 
101     public void setLastUpdate(Date lastUpdate)
102     {
103         this.lastUpdate = lastUpdate;
104     }
105 }