View Javadoc

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