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