View Javadoc

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