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