用ROME创建RSS服务

来源:互联网 发布:帆布鞋淘宝网 编辑:程序博客网 时间:2024/06/05 05:34

1.RSS标准

RSS标准比较混乱,主要有以下3个系列

  • RSS 0.9x / 2.0 : RSS技术诞生于1999年的网景公司(Netscape),其发布了一个0.9版本的规范。2001年,RSS技术标准的发展工作被Userland Software公司的戴夫 温那(Dave Winer)所接手。陆续发布了0.9x的系列版本。当W3C小组发布RSS 1.0后,Dave Winer不承认其有效性。并于2002年9月独自把RSS升级到了2.0版本(Really Simple Syndication),并交由哈佛大学Technology at Harvard Law进行维护。
  • RSS 1.0 : 在RSS发展过程中,为使RSS成为一个通用的规范,并进一步标准化。一个联合小组根据W3C新一代的Resource Description Framework  (RDF) 对RSS进行了重新定义,发布了RSS 1.0版,并把RSS定义为“RDF Site Summary”。现在RSS 1.0版由W3C联合小组维护。
  • Atom : Atom是一个项目的名字,主要是开发一个新的博客摘要格式以解决目前RSS存在的问题(混乱的版本号,不是一个真正的开放标准,表示方法的不一致,定义贫乏等等)。

2.如何实现RSS

RSS标准虽然混乱,但是其本质都是XML文档。你可以只使用notepad, 按照某个RSS标准, 手写一个xml, 并提供给客户端。

现在也有许多开源项目来提供RSS的解决方案。

Rome https://rome.dev.java.net/

RSSLibJ http://enigmastation.com/rsslibj/

RSSLib4J http://devzone.stealthp.org/cms/index.php?page=RSSLib4J

使用这些解决方案可以更方便的处理RSS.

3.用 Rome 实现 RSS 服务

目前Rome最新版本为rome-0.9. 本例是在Struts的Action中实现的RSS服务.

新建一个RssAction

java 代码

  1. import other classes...   
  2. import com.sun.syndication.feed.synd.SyndFeed;   
  3. import com.sun.syndication.io.SyndFeedOutput;   
  4. public class RssAction extends DispatchAction {   
  5. private static final String MIME_TYPE = "application/xml; charset=UTF-8";   
  6. // Rome中RSS的可选标准
  7. // rss_0.90, rss_0.91, rss_0.92, rss_0.93, rss_0.94, rss_1.0, rss_2.0, atom_0.3 
  8. private static final String RSS_TYPE = "rss_2.0";   
  9. public ActionForward newsFeed(ActionMapping mapping,   
  10.             ActionForm form, HttpServletRequest request,   
  11.             HttpServletResponse response) throws Exception {   
  12.         NewsFeedBA newsFeedBA = new NewsFeedBA();   
  13.         newsFeedBA.doExecute();   
  14.         outputRssFeed(response, newsFeedBA.getFeed());   
  15. return null;   
  16.     }   
  17. public ActionForward blogFeed(ActionMapping mapping,   
  18.             ActionForm form, HttpServletRequest request,   
  19.             HttpServletResponse response) throws Exception {   
  20.         String uid = request.getParameter("userId");   
  21.         BlogFeedBA blogFeedBA = new BlogFeedBA();   
  22.         blogFeedBA.setUserId(uid);   
  23.         blogFeedBA.doExecute();   
  24.         outputRssFeed(response, blogFeedBA.getFeed());   
  25. return null;   
  26.     }   
  27. //将SyndFeed写入HttpServletResponse
  28. private boolean outputRssFeed(HttpServletResponse response, SyndFeed feed) {   
  29. boolean result = false;   
  30.         feed.setFeedType(RSS_TYPE);   
  31.         response.setContentType(MIME_TYPE);   
  32.         SyndFeedOutput output = new SyndFeedOutput();   
  33. try {   
  34.             output.output(feed, response.getWriter());   
  35.             result = true;   
  36.         } catch (IOException e) {   
  37.             e.printStackTrace();   
  38.         } catch (FeedException e) {   
  39.             e.printStackTrace();   
  40.         }   
  41. return result;   
  42.     }   

然后在业务逻辑中,查询数据库,用返回的数据组织相应的Feed.

java 代码

  1. public SyndFeed createFeed(List news) {   
  2.     SyndFeed feed = new SyndFeedImpl();   
  3.     feed.setTitle("My RSS Service : news ");   
  4.     feed.setLink("http://www.myHomePage.com");   
  5.     feed.setDescription("My first RSS service .");   
  6.     feed.setEntries(getEntries(news));   
  7. return feed;   
  8. }   
  9. private List getEntries(List news) {   
  10.     List entries = new ArrayList();   
  11.        SyndEntry entry;   
  12.        SyndContent description;   
  13. for (NewsDTO dto : news) {   
  14.            entry = new SyndEntryImpl();   
  15.            entry.setTitle(dto.getTitle());   
  16.            entry.setLink(dto.getLink());   
  17.            entry.setPublishedDate(new Date());   
  18.            description = new SyndContentImpl();   
  19.            description.setType("text/html");   
  20.            description.setValue(dto.getContent());   
  21.            entry.setDescription(description);   
  22.            entries.add(entry);   
  23.        }   
  24. return entries;   

在struts-config.xml中配置RssAction

xml 代码

  1. <action path="/rss" type="com.web.action.RssAction"
  2. parameter="method"
  3. scope="request">
  4. action>

启动Tomcat,并访问/rss.do?method=newsFeed  就可以得到新闻的RSS

访问/rss.do?method=blogFeed&userId=123 就可以得到123的blog的RSS了。

现在IE7和Opera都集成了RSS功能。

<SCRIPT type=text/javascript>addthis_pub= 'wolfit';addthis_logo= 'http://download.rainbowsoft.org/image/common/zbloglogo.gif';addthis_brand= 'Z-Blog';addthis_options= 'favorites, delicious, twitter, digg, myspace, facebook, google, live, email, more';</SCRIPT>
原创粉丝点击