View Javadoc

1   /*
2    * $Id: FeedSplitter.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.atom.routing;
11  
12  import org.mule.DefaultMuleMessage;
13  import org.mule.api.MessagingException;
14  import org.mule.api.MuleEvent;
15  import org.mule.api.MuleException;
16  import org.mule.api.MuleMessage;
17  import org.mule.api.routing.filter.Filter;
18  import org.mule.api.transformer.TransformerException;
19  import org.mule.module.atom.transformers.ObjectToFeed;
20  import org.mule.routing.AbstractSplitter;
21  
22  import java.util.ArrayList;
23  import java.util.Comparator;
24  import java.util.List;
25  import java.util.Set;
26  import java.util.TreeSet;
27  
28  import org.apache.abdera.model.Entry;
29  import org.apache.abdera.model.Feed;
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  
33  /**
34   * An inbound router that will split a Feed into entries. A filter can be applied to the entries to omit
35   * certain entries, the most common use of this would be to filter out entries that have already been read
36   * by using the {@link org.mule.module.atom.routing.EntryLastUpdatedFilter} filter.
37   */
38  public class FeedSplitter extends AbstractSplitter
39  {
40      /**
41       * logger used by this class
42       */
43      protected transient final Log logger = LogFactory.getLog(FeedSplitter.class);
44  
45      public static final String FEED_PROPERTY = "feed.object";
46      private Filter entryFilter;
47      private ObjectToFeed objectToFeed = new ObjectToFeed();
48  
49      public FeedSplitter()
50      {
51          //By default set the filter so that entries are only read once
52          entryFilter = new EntryLastUpdatedFilter(null);
53      }
54  
55      @Override
56      protected List<MuleMessage> splitMessage(MuleEvent event) throws MuleException
57      {
58          List<MuleMessage> messages = new ArrayList<MuleMessage>();        
59          if(event.getMessage().getInboundProperty("Content-Length", -1) == 0)
60          {
61              logger.info("Feed has no content, ignoring");
62              return messages;
63          }
64  
65          try
66          {
67              Object payload = event.getMessage().getPayload();
68              
69              Feed feed;
70              if (payload instanceof Feed)
71              {
72                  feed = (Feed) payload;
73              }
74              else
75              {
76                  feed = (Feed) objectToFeed.transform(event.getMessage().getPayload());
77              }
78  
79              Set<Entry> entries = new TreeSet<Entry>(new EntryComparator());
80              entries.addAll(feed.getEntries());
81              for (Entry entry : entries)
82              {
83                  MuleMessage m = new DefaultMuleMessage(entry, event.getMuleContext());
84                  if (entryFilter != null && !entryFilter.accept(m))
85                  {
86                      continue;
87                  }
88                  m.setInvocationProperty(FEED_PROPERTY, feed);
89                  messages.add(m);
90              }
91              return messages;
92          }
93          catch (TransformerException e)
94          {
95              throw new MessagingException(e.getI18nMessage(), event, e);
96          }
97      }
98  
99      public Filter getEntryFilter()
100     {
101         return entryFilter;
102     }
103 
104     public void setEntryFilter(Filter entryFilter)
105     {
106         this.entryFilter = entryFilter;
107     }
108 
109     class EntryComparator implements Comparator<Entry>
110     {
111         public int compare(Entry e1, Entry e2)
112         {
113             if(e1==null && e2 !=null)
114             {
115                 return -1;
116             }
117             else if(e1!=null && e2 ==null)
118             {
119                 return 1;
120             }
121             else if(e1==null && e2 ==null)
122             {
123                 return 0;
124             }
125             else if (e1.getPublished() !=null && e1.getPublished().before(e2.getPublished()))
126             {
127                 return -1;
128             }
129             else if (e1.equals(e2))
130             {
131                 return 0;
132             }
133             else
134             {
135                 return 1;
136             }
137         }
138     }
139 }