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.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.atom.routing.EntryLastUpdatedFilter;
20  import org.mule.module.atom.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   * An endpoint factory used for creating an ATOM endpoint
30   */
31  public class AtomEndpointBuilder 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 AtomEndpointBuilder()
46      {
47          super();
48      }
49  
50      public AtomEndpointBuilder(EndpointURIEndpointBuilder global) throws EndpointException
51      {
52          super(global);
53      }
54  
55      public AtomEndpointBuilder(URIBuilder uriBuilder)
56      {
57          super(uriBuilder);
58      }
59  
60      public AtomEndpointBuilder(String address, MuleContext muleContext)
61      {
62          super(address, muleContext);
63      }
64  
65      protected AtomEndpointBuilder(EndpointURI endpointURI)
66      {
67          super(endpointURI);
68      }
69  
70      @Override
71      public InboundEndpoint buildInboundEndpoint() throws EndpointException, InitialisationException
72      {
73          try
74          {
75              Date date = formatDate(getLastUpdate());
76              if (isSplitFeed())
77              {
78                  Filter filter = new EntryLastUpdatedFilter(date);
79                  FeedSplitter splitter = new FeedSplitter();
80                  splitter.setEntryFilter(filter);
81                  addMessageProcessor(splitter);
82              }
83              AtomInboundEndpoint in = new AtomInboundEndpoint(isSplitFeed(), date, super.buildInboundEndpoint());
84              in.registerSupportedProtocol("http");
85              in.registerSupportedProtocol("https");
86              in.registerSupportedProtocol("vm");
87              if (in.getConnector() instanceof HttpPollingConnector)
88              {
89                  ((HttpPollingConnector) in.getConnector()).setPollingFrequency(pollingFrequency);
90              }
91              return in;
92          }
93          catch (ParseException e)
94          {
95              throw new EndpointException(e);
96          }
97      }
98  
99      @Override
100     public OutboundEndpoint buildOutboundEndpoint() throws EndpointException, InitialisationException
101     {
102         throw new UnsupportedOperationException("Outbound ATOM endpoints not supported");
103     }
104 
105     public String getLastUpdate()
106     {
107         return lastUpdate;
108     }
109 
110     public void setLastUpdate(String lastUpdate)
111     {
112         this.lastUpdate = lastUpdate;
113     }
114 
115     public boolean isSplitFeed()
116     {
117         return splitFeed;
118     }
119 
120     public void setSplitFeed(boolean splitFeed)
121     {
122         this.splitFeed = splitFeed;
123     }
124 
125     public long getPollingFrequency()
126     {
127         return pollingFrequency;
128     }
129 
130     public void setPollingFrequency(long pollingFrequency)
131     {
132         this.pollingFrequency = pollingFrequency;
133     }
134 
135     protected Date formatDate(String date) throws ParseException
136     {
137         Date lastUpdateDate = null;
138         if (StringUtils.isNotBlank(date))
139         {
140             if (lastUpdate.length() == 10)
141             {
142                 lastUpdateDate = shortDateFormatter.parse(date);
143             }
144             else
145             {
146                 lastUpdateDate = dateFormatter.parse(date);
147             }
148         }
149         return lastUpdateDate;
150     }
151 }