001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.servicemix.components.rss;
018:
019: import java.net.MalformedURLException;
020: import java.net.URL;
021: import java.util.ArrayList;
022: import java.util.Date;
023: import java.util.List;
024: import javax.jbi.JBIException;
025: import javax.jbi.messaging.InOnly;
026: import javax.jbi.messaging.NormalizedMessage;
027: import javax.xml.transform.Source;
028: import javax.xml.transform.dom.DOMSource;
029: import org.apache.commons.logging.Log;
030: import org.apache.commons.logging.LogFactory;
031: import org.apache.servicemix.components.util.PollingComponentSupport;
032:
033: import com.sun.syndication.feed.synd.SyndEntry;
034: import com.sun.syndication.feed.synd.SyndFeed;
035: import com.sun.syndication.feed.synd.SyndFeedImpl;
036: import com.sun.syndication.io.SyndFeedInput;
037: import com.sun.syndication.io.SyndFeedOutput;
038: import com.sun.syndication.io.XmlReader;
039:
040: /**
041: * The RssPollingComponent polls for updates to RSS feeds
042: *
043: * @version $Revision: 426415 $
044: */
045: public class RssPollingComponent extends PollingComponentSupport {
046: private static final Log log = LogFactory
047: .getLog(RssPollingComponent.class);
048: private List urlStrings = new ArrayList();
049: private List urls = new ArrayList();
050: private Date lastPolledDate = new Date();
051: private String outputType = "rss_2.0";
052:
053: /**
054: * @return Returns the urlStrings.
055: */
056: public List getUrlStrings() {
057: return urlStrings;
058: }
059:
060: /**
061: * @param urlStrings The urlStrings to set.
062: */
063: public void setUrlStrings(List urlStrings) {
064: this .urlStrings = urlStrings;
065: }
066:
067: /**
068: * @return Returns the outputType.
069: */
070: public String getOutputType() {
071: return outputType;
072: }
073:
074: /**
075: * @param outputType The outputType to set.
076: */
077: public void setOutputType(String outputType) {
078: this .outputType = outputType;
079: }
080:
081: /**
082: * @return Returns the lastPolledDate.
083: */
084: public Date getLastPolledDate() {
085: return lastPolledDate;
086: }
087:
088: /**
089: * @param lastPolledDate The lastPolledDate to set.
090: */
091: public void setLastPolledDate(Date lastPolledDate) {
092: this .lastPolledDate = lastPolledDate;
093: }
094:
095: protected void init() throws JBIException {
096: urls.clear();
097: if (urlStrings != null) {
098: for (int i = 0; i < urlStrings.size(); i++) {
099: try {
100: urls.add(new URL(urlStrings.get(i).toString()));
101: } catch (MalformedURLException e) {
102: log.warn("URL: " + urlStrings.get(i)
103: + " is badly formed", e);
104: }
105: }
106: }
107: super .init();
108:
109: }
110:
111: /**
112: * Poll for updates
113: */
114: public void poll() {
115: List list = getLastesEntries();
116: if (list != null && !list.isEmpty()) {
117: SyndFeed feed = new SyndFeedImpl();
118: feed.setFeedType(outputType);
119: feed.setTitle("Aggregated Feed");
120: feed.setDescription("Anonymous Aggregated Feed");
121: feed.setAuthor("servicemix");
122: feed.setLink("http://www.servicemix.org");
123: feed.setEntries(list);
124: // send on to the nmr ...
125: SyndFeedOutput output = new SyndFeedOutput();
126: try {
127: Source source = new DOMSource(output.outputW3CDom(feed));
128: InOnly exchange = getExchangeFactory()
129: .createInOnlyExchange();
130: NormalizedMessage message = exchange.createMessage();
131: message.setContent(source);
132: exchange.setInMessage(message);
133: send(exchange);
134: } catch (Exception e) {
135: log.error("Failed to send RSS message to the NMR");
136: } finally {
137: lastPolledDate = new Date();
138: }
139: }
140: }
141:
142: protected List getLastesEntries() {
143: List result = new ArrayList();
144: SyndFeedInput input = new SyndFeedInput();
145: for (int i = 0; i < urls.size(); i++) {
146: URL inputUrl = (URL) urls.get(i);
147: SyndFeed inFeed;
148: try {
149: inFeed = input.build(new XmlReader(inputUrl));
150: List entries = inFeed.getEntries();
151: for (int k = 0; k < entries.size(); k++) {
152: SyndEntry entry = (SyndEntry) entries.get(k);
153: if (entry.getPublishedDate().after(
154: getLastPolledDate())) {
155: result.add(entry);
156: }
157: }
158: } catch (Exception e) {
159: log.warn("Failed to process feed from: " + inputUrl, e);
160: }
161: }
162: return result;
163: }
164: }
|