Coverage Report - org.mule.module.rss.endpoint.RssEndpointBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
RssEndpointBuilder
0%
0/55
0%
0/8
0
 
 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  0
     private boolean splitFeed = true;
 40  
 
 41  0
     private String lastUpdate = null;
 42  
 
 43  0
     private long pollingFrequency = 1000;
 44  
 
 45  0
     private final SimpleDateFormat dateFormatter = new SimpleDateFormat(DATE_FORMAT);
 46  0
     private final SimpleDateFormat shortDateFormatter = new SimpleDateFormat(SHORT_DATE_FORMAT);
 47  
 
 48  
     public RssEndpointBuilder()
 49  
     {
 50  0
         super();
 51  0
         init();
 52  0
     }
 53  
 
 54  
     public RssEndpointBuilder(EndpointURIEndpointBuilder global) throws EndpointException
 55  
     {
 56  0
         super(global);
 57  0
         init();
 58  0
     }
 59  
 
 60  
     public RssEndpointBuilder(URIBuilder URIBuilder)
 61  
     {
 62  0
         super(URIBuilder);
 63  0
         init();
 64  0
     }
 65  
 
 66  
     public RssEndpointBuilder(String address, MuleContext muleContext)
 67  
     {
 68  0
         super(address, muleContext);
 69  0
         init();
 70  0
     }
 71  
 
 72  
     protected RssEndpointBuilder(EndpointURI endpointURI)
 73  
     {
 74  0
         super(endpointURI);
 75  0
         init();
 76  0
     }
 77  
 
 78  
     protected void init()
 79  
     {
 80  0
         shortDateFormatter.setLenient(false);
 81  0
         dateFormatter.setLenient(false);
 82  0
     }
 83  
 
 84  
     @Override
 85  
     public InboundEndpoint buildInboundEndpoint() throws EndpointException, InitialisationException
 86  
     {
 87  
         try
 88  
         {
 89  0
             Date date = formatDate(getLastUpdate());
 90  0
             if (isSplitFeed())
 91  
             {
 92  0
                 Filter filter = new EntryLastUpdatedFilter(date);
 93  0
                 FeedSplitter splitter = new FeedSplitter();
 94  0
                 splitter.setEntryFilter(filter);
 95  0
                 addMessageProcessor(splitter);
 96  
 
 97  
             }
 98  0
             RssInboundEndpoint in = new RssInboundEndpoint(isSplitFeed(), date, super.buildInboundEndpoint());
 99  0
             in.registerSupportedProtocol("http");
 100  0
             in.registerSupportedProtocol("https");
 101  0
             in.registerSupportedProtocol("vm");
 102  0
             if (in.getConnector() instanceof HttpPollingConnector)
 103  
             {
 104  0
                 ((HttpPollingConnector) in.getConnector()).setPollingFrequency(pollingFrequency);
 105  
             }
 106  0
             return in;
 107  
         }
 108  0
         catch (ParseException e)
 109  
         {
 110  0
             throw new EndpointException(e);
 111  
         }
 112  
     }
 113  
 
 114  
     @Override
 115  
     public OutboundEndpoint buildOutboundEndpoint() throws EndpointException, InitialisationException
 116  
     {
 117  0
         throw new UnsupportedOperationException("Outbound RSS endpoints not supported");
 118  
     }
 119  
 
 120  
     @Override
 121  
     protected boolean isAlwaysCreateConnector()
 122  
     {
 123  0
         return true;
 124  
     }
 125  
 
 126  
     public String getLastUpdate()
 127  
     {
 128  0
         return lastUpdate;
 129  
     }
 130  
 
 131  
     public void setLastUpdate(String lastUpdate)
 132  
     {
 133  0
         this.lastUpdate = lastUpdate;
 134  0
     }
 135  
 
 136  
     public boolean isSplitFeed()
 137  
     {
 138  0
         return splitFeed;
 139  
     }
 140  
 
 141  
     public void setSplitFeed(boolean splitFeed)
 142  
     {
 143  0
         this.splitFeed = splitFeed;
 144  0
     }
 145  
 
 146  
     public long getPollingFrequency()
 147  
     {
 148  0
         return pollingFrequency;
 149  
     }
 150  
 
 151  
     public void setPollingFrequency(long pollingFrequency)
 152  
     {
 153  0
         this.pollingFrequency = pollingFrequency;
 154  0
     }
 155  
 
 156  
     protected Date formatDate(String date) throws ParseException
 157  
     {
 158  0
         Date lastUpdateDate = null;
 159  0
         if (StringUtils.isNotBlank(date))
 160  
         {
 161  0
             if (lastUpdate.length() == 10)
 162  
             {
 163  0
                 lastUpdateDate = shortDateFormatter.parse(date);
 164  
             }
 165  
             else
 166  
             {
 167  0
                 lastUpdateDate = dateFormatter.parse(date);
 168  
             }
 169  
         }
 170  0
         return lastUpdateDate;
 171  
     }
 172  
 }