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