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.routing.filter.Filter;
11  
12  import java.util.Date;
13  
14  import org.apache.abdera.model.Entry;
15  import org.apache.commons.logging.Log;
16  import org.apache.commons.logging.LogFactory;
17  
18  /**
19   * Will filter entries in an atom feed based on the lasted Edited date, falling back to the
20   * published date if the edit date was not set
21   */
22  public class EntryLastUpdatedFilter implements Filter
23  {
24      /**
25       * logger used by this class
26       */
27      private final transient Log logger = LogFactory.getLog(EntryLastUpdatedFilter.class);
28      private Date lastUpdate;
29  
30      private boolean acceptWithoutUpdateDate = true;
31  
32      public EntryLastUpdatedFilter()
33      {
34      }
35  
36      public EntryLastUpdatedFilter(Date lastUpdate)
37      {
38          this.lastUpdate = lastUpdate;
39      }
40  
41      public boolean accept(MuleMessage message)
42      {
43          Entry entry = (Entry) message.getPayload();
44          Date updated = entry.getEdited();
45          if (updated == null)
46          {
47              updated = entry.getPublished();
48              if (updated == null)
49              {
50                  if (isAcceptWithoutUpdateDate())
51                  {
52                      if (logger.isDebugEnabled())
53                      {
54                          logger.debug("Entry does not have a last updated or published date set, assuming the feed should be processed");
55                      }
56                      return true;
57                  }
58                  else
59                  {
60                      if (logger.isWarnEnabled())
61                      {
62                          logger.warn("Entry does not have a last updated or published date set, not consuming the feed because 'acceptWithoutUpdateDate' is false");
63                      }
64                      return false;
65                  }
66              }
67          }
68          if (lastUpdate != null)
69          {
70              if (lastUpdate.after(updated) || lastUpdate.equals(updated))
71              {
72                  if (logger.isDebugEnabled())
73                  {
74                      logger.debug("Feed update is not newer than the last update, not processing");
75                  }
76                  return false;
77              }
78          }
79          lastUpdate = updated;
80          return true;
81      }
82  
83      public boolean isAcceptWithoutUpdateDate()
84      {
85          return acceptWithoutUpdateDate;
86      }
87  
88      public void setAcceptWithoutUpdateDate(boolean acceptWithoutUpdateDate)
89      {
90          this.acceptWithoutUpdateDate = acceptWithoutUpdateDate;
91      }
92  
93      public Date getLastUpdate()
94      {
95          return lastUpdate;
96      }
97  
98      public void setLastUpdate(Date lastUpdate)
99      {
100         this.lastUpdate = lastUpdate;
101     }
102 }