View Javadoc

1   /*
2    * $Id: RssEndpointBuilder.java 19359 2010-09-03 20:03:08Z rossmason $
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.rss.endpoint;
11  
12  import org.mule.api.MuleContext;
13  import org.mule.api.endpoint.EndpointException;
14  import org.mule.api.endpoint.EndpointURI;
15  import org.mule.api.endpoint.InboundEndpoint;
16  import org.mule.api.endpoint.OutboundEndpoint;
17  import org.mule.api.lifecycle.InitialisationException;
18  import org.mule.api.routing.filter.Filter;
19  import org.mule.endpoint.AbstractMetaEndpointBuilder;
20  import org.mule.endpoint.EndpointURIEndpointBuilder;
21  import org.mule.endpoint.URIBuilder;
22  import org.mule.module.rss.routing.EntryLastUpdatedFilter;
23  import org.mule.module.rss.routing.FeedSplitter;
24  import org.mule.transport.http.HttpPollingConnector;
25  import org.mule.util.StringUtils;
26  
27  import java.text.ParseException;
28  import java.text.SimpleDateFormat;
29  import java.util.Date;
30  
31  /**
32   * Creates RSS endpoints. Right now only inbound endpoints are supported, i.e. poll an RSS URL
33   */
34  public class RssEndpointBuilder extends AbstractMetaEndpointBuilder
35  {
36      public static final String DATE_FORMAT = "yyyy-MM-dd hh:mm:ss";
37      public static final String SHORT_DATE_FORMAT = "yyyy-MM-dd";
38  
39      private boolean splitFeed = true;
40  
41      private String lastUpdate = null;
42  
43      private long pollingFrequency = 1000;
44  
45      private final SimpleDateFormat dateFormatter = new SimpleDateFormat(DATE_FORMAT);
46      private final SimpleDateFormat shortDateFormatter = new SimpleDateFormat(SHORT_DATE_FORMAT);
47  
48      public RssEndpointBuilder()
49      {
50          super();
51          init();
52      }
53  
54      public RssEndpointBuilder(EndpointURIEndpointBuilder global) throws EndpointException
55      {
56          super(global);
57          init();
58      }
59  
60      public RssEndpointBuilder(URIBuilder URIBuilder)
61      {
62          super(URIBuilder);
63          init();
64      }
65  
66      public RssEndpointBuilder(String address, MuleContext muleContext)
67      {
68          super(address, muleContext);
69          init();
70      }
71  
72      protected RssEndpointBuilder(EndpointURI endpointURI)
73      {
74          super(endpointURI);
75          init();
76      }
77  
78      protected void init()
79      {
80          shortDateFormatter.setLenient(false);
81          dateFormatter.setLenient(false);
82      }
83  
84      @Override
85      public InboundEndpoint buildInboundEndpoint() throws EndpointException, InitialisationException
86      {
87          try
88          {
89              Date date = formatDate(getLastUpdate());
90              if (isSplitFeed())
91              {
92                  Filter filter = new EntryLastUpdatedFilter(date);
93                  FeedSplitter splitter = new FeedSplitter();
94                  splitter.setEntryFilter(filter);
95                  addMessageProcessor(splitter);
96  
97              }
98              RssInboundEndpoint in = new RssInboundEndpoint(isSplitFeed(), date, super.buildInboundEndpoint());
99              in.registerSupportedProtocol("http");
100             in.registerSupportedProtocol("https");
101             in.registerSupportedProtocol("vm");
102             if (in.getConnector() instanceof HttpPollingConnector)
103             {
104                 ((HttpPollingConnector) in.getConnector()).setPollingFrequency(pollingFrequency);
105             }
106             return in;
107         }
108         catch (ParseException e)
109         {
110             throw new EndpointException(e);
111         }
112     }
113 
114     @Override
115     public OutboundEndpoint buildOutboundEndpoint() throws EndpointException, InitialisationException
116     {
117         throw new UnsupportedOperationException("Outbound RSS endpoints not supported");
118     }
119 
120     @Override
121     protected boolean isAlwaysCreateConnector()
122     {
123         return true;
124     }
125 
126     public String getLastUpdate()
127     {
128         return lastUpdate;
129     }
130 
131     public void setLastUpdate(String lastUpdate)
132     {
133         this.lastUpdate = lastUpdate;
134     }
135 
136     public boolean isSplitFeed()
137     {
138         return splitFeed;
139     }
140 
141     public void setSplitFeed(boolean splitFeed)
142     {
143         this.splitFeed = splitFeed;
144     }
145 
146     public long getPollingFrequency()
147     {
148         return pollingFrequency;
149     }
150 
151     public void setPollingFrequency(long pollingFrequency)
152     {
153         this.pollingFrequency = pollingFrequency;
154     }
155 
156     protected Date formatDate(String date) throws ParseException
157     {
158         Date lastUpdateDate = null;
159         if (StringUtils.isNotBlank(date))
160         {
161             if (lastUpdate.length() == 10)
162             {
163                 lastUpdateDate = shortDateFormatter.parse(date);
164             }
165             else
166             {
167                 lastUpdateDate = dateFormatter.parse(date);
168             }
169         }
170         return lastUpdateDate;
171     }
172 }