Coverage Report - org.mule.module.rss.routing.FeedLastUpdatedFilter
 
Classes in this File Line Coverage Branch Coverage Complexity
FeedLastUpdatedFilter
0%
0/33
0%
0/16
2.714
 
 1  
 /*
 2  
  * $Id: FeedLastUpdatedFilter.java 19191 2010-08-25 21:05:23Z tcarlson $
 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  
 
 18  
 import com.sun.syndication.feed.synd.SyndFeed;
 19  
 
 20  
 import java.util.Date;
 21  
 
 22  
 import org.apache.commons.logging.Log;
 23  
 import org.apache.commons.logging.LogFactory;
 24  
 import org.mule.transformer.types.DataTypeFactory;
 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 FeedLastUpdatedFilter implements Filter
 31  
 {
 32  
     /**
 33  
      * logger used by this class
 34  
      */
 35  0
     private final transient Log logger = LogFactory.getLog(FeedLastUpdatedFilter.class);
 36  
     private Date lastUpdate;
 37  
 
 38  0
     private boolean acceptWithoutUpdateDate = true;
 39  
 
 40  
     public FeedLastUpdatedFilter()
 41  0
     {
 42  
         //For Spring Xml
 43  0
     }
 44  
 
 45  
     public FeedLastUpdatedFilter(Date lastUpdate)
 46  0
     {
 47  0
         this.lastUpdate = lastUpdate;
 48  0
     }
 49  
 
 50  
     public boolean accept(MuleMessage message)
 51  
     {
 52  
         SyndFeed feed;
 53  
         try
 54  
         {
 55  0
             feed = (SyndFeed) message.getPayload(DataTypeFactory.create(SyndFeed.class));
 56  
         }
 57  0
         catch (TransformerException e)
 58  
         {
 59  0
             throw new MuleRuntimeException(CoreMessages.failedToReadPayload(), e);
 60  0
         }
 61  
 
 62  0
         Date updated = feed.getPublishedDate();
 63  0
         if (updated == null)
 64  
         {
 65  0
             if (isAcceptWithoutUpdateDate())
 66  
             {
 67  0
                 if (logger.isDebugEnabled())
 68  
                 {
 69  0
                     logger.debug("Feed does not have a last updated or published date set, assuming the feed should be processed");
 70  
                 }
 71  0
                 return true;
 72  
             }
 73  
             else
 74  
             {
 75  0
                 if (logger.isWarnEnabled())
 76  
                 {
 77  0
                     logger.warn("Feed does not have a last updated or published date set, not consuming the feed because 'acceptWithoutUpdateDate' is false");
 78  
                 }
 79  0
                 return false;
 80  
             }
 81  
         }
 82  
 
 83  0
         if (lastUpdate != null)
 84  
         {
 85  0
             if (lastUpdate.after(updated) || lastUpdate.equals(updated))
 86  
             {
 87  0
                 if (logger.isDebugEnabled())
 88  
                 {
 89  0
                     logger.debug("Feed update is not newer than the last update, not processing");
 90  
                 }
 91  0
                 return false;
 92  
             }
 93  
         }
 94  0
         lastUpdate = updated;
 95  0
         return true;
 96  
 
 97  
     }
 98  
 
 99  
     public boolean isAcceptWithoutUpdateDate()
 100  
     {
 101  0
         return acceptWithoutUpdateDate;
 102  
     }
 103  
 
 104  
     public void setAcceptWithoutUpdateDate(boolean acceptWithoutUpdateDate)
 105  
     {
 106  0
         this.acceptWithoutUpdateDate = acceptWithoutUpdateDate;
 107  0
     }
 108  
 
 109  
     public Date getLastUpdate()
 110  
     {
 111  0
         return lastUpdate;
 112  
     }
 113  
 
 114  
     public void setLastUpdate(Date lastUpdate)
 115  
     {
 116  0
         this.lastUpdate = lastUpdate;
 117  0
     }
 118  
 }