001: package org.claros.intouch.rss.controllers;
002:
003: import java.net.URL;
004: import java.util.ArrayList;
005: import java.util.LinkedList;
006: import java.util.List;
007:
008: import org.apache.commons.logging.Log;
009: import org.apache.commons.logging.LogFactory;
010: import org.claros.commons.auth.models.AuthProfile;
011: import org.claros.commons.cache.Cache;
012: import org.claros.commons.cache.CacheManager;
013: import org.claros.commons.configuration.PropertyFile;
014: import org.claros.intouch.preferences.controllers.UserPrefsController;
015: import org.claros.intouch.rss.models.NewsItem;
016: import org.gnu.stealthp.rsslib.RSSChannel;
017: import org.gnu.stealthp.rsslib.RSSException;
018: import org.gnu.stealthp.rsslib.RSSHandler;
019: import org.gnu.stealthp.rsslib.RSSItem;
020: import org.gnu.stealthp.rsslib.RSSParser;
021:
022: /**
023: * @author Umut Gokbayrak
024: */
025: public class NewsController {
026: private static Log log = LogFactory.getLog(NewsController.class);
027:
028: /**
029: * Rss reader
030: * @return
031: */
032: private static List fetchRssItems(String newsUrl) {
033: ArrayList allNews = new ArrayList();
034: try {
035: RSSHandler hand = new RSSHandler();
036: try {
037: URL u = new URL(newsUrl);
038: RSSParser.parseXmlFile(u, hand, false);
039: } catch (RSSException e) {
040: e.printStackTrace();
041: }
042: RSSChannel ch = hand.getRSSChannel();
043:
044: String channelDescription = ch.getDescription();
045: String channelUrl = ch.getLink();
046: String channelTitle = ch.getTitle();
047:
048: LinkedList lst = hand.getRSSChannel().getItems();
049:
050: for (int i = 0; i < lst.size(); i++) {
051: RSSItem itm = (RSSItem) lst.get(i);
052: try {
053: NewsItem news = new NewsItem();
054: news.setChannelDescription(channelDescription);
055: news.setChannelTitle(channelTitle);
056: news.setChannelUrl(channelUrl);
057: news.setTitle(itm.getTitle().replace('\n', ' '));
058: news.setDate(itm.getDate());
059: news.setDescription(itm.getDescription().replace(
060: '\n', ' '));
061: news.setLink(itm.getLink());
062: allNews.add(news);
063: } catch (Exception e1) {
064: log.debug("Unable to add news item: "
065: + itm.toString(), e1);
066: }
067: }
068: } catch (Exception e) {
069: log.debug("RSS Feed cannot be fetched and parsed.", e);
070: }
071: return allNews;
072: }
073:
074: /**
075: * RSS feeds are cached...
076: *
077: * @param user
078: * @return
079: */
080: public static List getRssItems(AuthProfile auth) throws Exception {
081: String newsUrl = UserPrefsController.getUserSetting(auth,
082: "newsUrl");
083: // if user has no value then get the system default
084: if (newsUrl == null) {
085: newsUrl = PropertyFile.getConfiguration(
086: "/config/config.xml").getString(
087: "common-params.rss-feed");
088: }
089: // if still there is no rss setting, fetch CNN.
090: if (newsUrl == null) {
091: newsUrl = "http://rss.cnn.com/rss/cnn_topstories.rss";
092: // newsUrl = "http://rss.hurriyet.com.tr/rss.aspx?sectionId=1";
093: // newsUrl = "http://rss.e-kolay.net/pages/haber.aspx";
094: }
095:
096: String key = newsUrl;
097: Cache c = CacheManager.getContent(key);
098: List result = null;
099: if (c == null || c.isExpired()) {
100: long ttl = 1000 * 60 * 5;
101: result = fetchRssItems(newsUrl);
102: CacheManager.putContent(key, result, ttl);
103: } else {
104: result = (List) c.getValue();
105: }
106: return result;
107: }
108: }
|