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