View Javadoc

1   /*
2    * $Id: FeedLastUpdatedFilter.java 20321 2010-11-24 15:21:24Z dfeist $
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.MuleRuntimeException;
14  import org.mule.api.routing.filter.Filter;
15  import org.mule.api.transformer.TransformerException;
16  import org.mule.config.i18n.CoreMessages;
17  
18  import java.util.Date;
19  
20  import org.apache.abdera.model.Feed;
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  
24  /**
25   * Will filter a feed who's update date has changed since the last time it was read.  Some feeds to no update
26   * this value so {@link #setAcceptWithoutUpdateDate(boolean)} can be set to always consume the feed
27   */
28  public class FeedLastUpdatedFilter implements Filter
29  {
30      /**
31       * logger used by this class
32       */
33      private final transient Log logger = LogFactory.getLog(FeedLastUpdatedFilter.class);
34      private Date lastUpdate;
35  
36      private boolean acceptWithoutUpdateDate = true;
37  
38      public FeedLastUpdatedFilter()
39      {
40          //For Spring Xml
41      }
42  
43      public FeedLastUpdatedFilter(Date lastUpdate)
44      {
45          this.lastUpdate = lastUpdate;
46      }
47  
48      public boolean accept(MuleMessage message)
49      {
50          Feed feed;
51          try
52          {
53              feed = message.getPayload(Feed.class);
54          }
55          catch (TransformerException e)
56          {
57              throw new MuleRuntimeException(CoreMessages.failedToReadPayload(), e);
58          }
59  
60          Date updated = feed.getUpdated();
61          if (updated == null)
62          {
63              if (isAcceptWithoutUpdateDate())
64              {
65                  if (logger.isDebugEnabled())
66                  {
67                      logger.debug("Feed does not have a last updated or published date set, assuming the feed should be processed");
68                  }
69                  return true;
70              }
71              else
72              {
73                  if (logger.isWarnEnabled())
74                  {
75                      logger.warn("Feed does not have a last updated or published date set, not consuming the feed because 'acceptWithoutUpdateDate' is false");
76                  }
77                  return false;
78              }
79          }
80  
81          if (lastUpdate != null)
82          {
83              if (lastUpdate.after(updated) || lastUpdate.equals(updated))
84              {
85                  if (logger.isDebugEnabled())
86                  {
87                      logger.debug("Feed update is not newer than the last update, not processing");
88                  }
89                  return false;
90              }
91          }
92          lastUpdate = updated;
93          return true;
94  
95      }
96  
97      public boolean isAcceptWithoutUpdateDate()
98      {
99          return acceptWithoutUpdateDate;
100     }
101 
102     public void setAcceptWithoutUpdateDate(boolean acceptWithoutUpdateDate)
103     {
104         this.acceptWithoutUpdateDate = acceptWithoutUpdateDate;
105     }
106 
107     public Date getLastUpdate()
108     {
109         return lastUpdate;
110     }
111 
112     public void setLastUpdate(Date lastUpdate)
113     {
114         this.lastUpdate = lastUpdate;
115     }
116 }