1 17 package org.apache.servicemix.components.rss; 18 19 import java.net.MalformedURLException ; 20 import java.net.URL ; 21 import java.util.ArrayList ; 22 import java.util.Date ; 23 import java.util.List ; 24 import javax.jbi.JBIException; 25 import javax.jbi.messaging.InOnly; 26 import javax.jbi.messaging.NormalizedMessage; 27 import javax.xml.transform.Source ; 28 import javax.xml.transform.dom.DOMSource ; 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 import org.apache.servicemix.components.util.PollingComponentSupport; 32 33 import com.sun.syndication.feed.synd.SyndEntry; 34 import com.sun.syndication.feed.synd.SyndFeed; 35 import com.sun.syndication.feed.synd.SyndFeedImpl; 36 import com.sun.syndication.io.SyndFeedInput; 37 import com.sun.syndication.io.SyndFeedOutput; 38 import com.sun.syndication.io.XmlReader; 39 40 45 public class RssPollingComponent extends PollingComponentSupport { 46 private static final Log log = LogFactory.getLog(RssPollingComponent.class); 47 private List urlStrings = new ArrayList (); 48 private List urls = new ArrayList (); 49 private Date lastPolledDate = new Date (); 50 private String outputType = "rss_2.0"; 51 52 53 54 57 public List getUrlStrings() { 58 return urlStrings; 59 } 60 61 64 public void setUrlStrings(List urlStrings) { 65 this.urlStrings = urlStrings; 66 } 67 68 71 public String getOutputType() { 72 return outputType; 73 } 74 75 78 public void setOutputType(String outputType) { 79 this.outputType = outputType; 80 } 81 82 85 public Date getLastPolledDate() { 86 return lastPolledDate; 87 } 88 89 92 public void setLastPolledDate(Date lastPolledDate) { 93 this.lastPolledDate = lastPolledDate; 94 } 95 96 97 98 protected void init() throws JBIException { 99 urls.clear(); 100 if (urlStrings != null) { 101 for (int i = 0;i < urlStrings.size();i++) { 102 try { 103 urls.add(new URL (urlStrings.get(i).toString())); 104 } 105 catch (MalformedURLException e) { 106 log.warn("URL: " + urlStrings.get(i) + " is badly formed", e); 107 } 108 } 109 } 110 super.init(); 111 112 } 113 114 117 public void poll() { 118 List list = getLastesEntries(); 119 if (list != null && !list.isEmpty()) { 120 SyndFeed feed = new SyndFeedImpl(); 121 feed.setFeedType(outputType); 122 feed.setTitle("Aggregated Feed"); 123 feed.setDescription("Anonymous Aggregated Feed"); 124 feed.setAuthor("servicemix"); 125 feed.setLink("http://www.servicemix.org"); 126 feed.setEntries(list); 127 SyndFeedOutput output = new SyndFeedOutput(); 129 try { 130 Source source = new DOMSource (output.outputW3CDom(feed)); 131 InOnly exchange = getExchangeFactory().createInOnlyExchange(); 132 NormalizedMessage message = exchange.createMessage(); 133 message.setContent(source); 134 exchange.setInMessage(message); 135 send(exchange); 136 } 137 catch (Exception e) { 138 log.error("Failed to send RSS message to the NMR"); 139 } 140 finally { 141 lastPolledDate = new Date (); 142 } 143 } 144 } 145 146 protected List getLastesEntries() { 147 List result = new ArrayList (); 148 SyndFeedInput input = new SyndFeedInput(); 149 for (int i = 0;i < urls.size();i++) { 150 URL inputUrl = (URL ) urls.get(i); 151 SyndFeed inFeed; 152 try { 153 inFeed = input.build(new XmlReader(inputUrl)); 154 List entries = inFeed.getEntries(); 155 for (int k = 0;k < entries.size();k++) { 156 SyndEntry entry = (SyndEntry) entries.get(k); 157 if (entry.getPublishedDate().after(getLastPolledDate())) { 158 result.add(entry); 159 } 160 } 161 } 162 catch (Exception e) { 163 log.warn("Failed to process feed from: " + inputUrl, e); 164 } 165 } 166 return result; 167 } 168 } 169 | Popular Tags |