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.SyndEntry;
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.
25   * Some feeds to no update this value so {@link #setAcceptWithoutUpdateDate(boolean)}
26   * can be set to always consume the feed.
27   */
28  public class EntryLastUpdatedFilter implements Filter
29  {
30      /**
31       * logger used by this class
32       */
33      private final transient Log logger = LogFactory.getLog(EntryLastUpdatedFilter.class);
34  
35      private Date lastUpdate;
36  
37      private boolean acceptWithoutUpdateDate = true;
38  
39      public EntryLastUpdatedFilter()
40      {
41          //For Spring Xml
42      }
43  
44      public EntryLastUpdatedFilter(Date lastUpdate)
45      {
46          this.lastUpdate = lastUpdate;
47      }
48  
49      public boolean accept(MuleMessage message)
50      {
51          SyndEntry feed = transformToSyndEntry(message);
52  
53          Date updated = feed.getPublishedDate();
54          if (updated == null)
55          {
56              if (isAcceptWithoutUpdateDate())
57              {
58                  if (logger.isDebugEnabled())
59                  {
60                      logger.debug("Feed does not have a last updated or published date set, assuming the feed should be processed");
61                  }
62                  return true;
63              }
64              else
65              {
66                  if (logger.isWarnEnabled())
67                  {
68                      logger.warn("Feed does not have a last updated or published date set, not consuming the feed because 'acceptWithoutUpdateDate' is false");
69                  }
70                  return false;
71              }
72          }
73  
74          if (lastUpdate != null)
75          {
76              if (lastUpdate.after(updated) || lastUpdate.equals(updated))
77              {
78                  if (logger.isDebugEnabled())
79                  {
80                      logger.debug("Feed update is not newer than the last update, not processing");
81                  }
82                  return false;
83              }
84          }
85  
86          lastUpdate = updated;
87          return true;
88      }
89  
90      protected SyndEntry transformToSyndEntry(MuleMessage message)
91      {
92          try
93          {
94              return message.getPayload(DataTypeFactory.create(SyndEntry.class));
95          }
96          catch (TransformerException e)
97          {
98              throw new MuleRuntimeException(CoreMessages.failedToReadPayload(), e);
99          }
100     }
101 
102     public boolean isAcceptWithoutUpdateDate()
103     {
104         return acceptWithoutUpdateDate;
105     }
106 
107     public void setAcceptWithoutUpdateDate(boolean acceptWithoutUpdateDate)
108     {
109         this.acceptWithoutUpdateDate = acceptWithoutUpdateDate;
110     }
111 
112     public Date getLastUpdate()
113     {
114         return lastUpdate;
115     }
116 
117     public void setLastUpdate(Date lastUpdate)
118     {
119         this.lastUpdate = lastUpdate;
120     }
121 }