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