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