View Javadoc

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