rome-rss

原创 2007年09月27日 10:51:00

This sample consists of a servlet that serves a feed as response. The feed type (RSS in any of its variants or Atom) can be specified as a URL parameter when requesting the feed. The returned feed is hardwired in the sample and it would be straight forward to modify the sample to generate the feed dynamically (i.e. from data stored in a database).

The core logic of the FeedServlet is in the following fragment of code:

 

public class FeedServlet extends HttpServlet {
    ...

    public void doGet(HttpServletRequest req,HttpServletResponse res) throws IOException {
            ...
            SyndFeed feed = getFeed(req);

            String feedType = req.getParameter(FEED_TYPE);
            feedType = (feedType!=null) ? feedType : _defaultFeedType;
            feed.setFeedType(feedType);

            res.setContentType(MIME_TYPE);            
            SyndFeedOutput output = new SyndFeedOutput();
            output.output(feed,res.getWriter());
            ...
    }

    protected SyndFeed getFeed(HttpServletRequest req) throws IOException,FeedException {
        SyndFeed feed = new SyndFeedImpl();
        feed = ...
        return feed;
    }

}

The servlet returns a feed upon HTTP GET requests with the doGet() method.

First the SyndFeed bean is obtained by invoking the getFeed() method, the request object is passed as it could be used to obtain request contextual information to create the feed. How to create a feed using SyndFeed bean is explained in detail in the Using ROME to create and write a feed Tutorial.

Then the feed type of the response is determined, first looking at the request parameters and falling back to a default feed type (specified by a servlet init parameter) if the type is not indicated in the request parameters. Setting the feed type in the feed bean will indicate the SyndFeedOutput the feed type to output.

Finally, the response is set with the proper content type (the MIME_TYPE constant is 'application/xml; charset=UTF-8') and the feed is written to response writer using the SyndFeedOutput output classes.

Following is the full code for the servlet.

 

package com.sun.syndication.samples.servlet;

import com.sun.syndication.feed.synd.*;
import com.sun.syndication.io.FeedException;
import com.sun.syndication.io.SyndFeedOutput;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Sample Servlet that serves a feed created with ROME.
 * <p>
 * The feed type is determined by the 'type' request parameter, if the parameter is missing it defaults
 * to the 'default.feed.type' servlet init parameter, if the init parameter is missing it defaults to 'atom_0.3'
 * <p>
 * @author Alejandro Abdelnur
 *
 */
public class FeedServlet extends HttpServlet {
    private static final String DEFAULT_FEED_TYPE = "default.feed.type";
    private static final String FEED_TYPE = "type";
    private static final String MIME_TYPE = "application/xml; charset=UTF-8";
    private static final String COULD_NOT_GENERATE_FEED_ERROR = "Could not generate feed";

    private static final DateFormat DATE_PARSER = new SimpleDateFormat("yyyy-MM-dd");

    private String _defaultFeedType;

    public void init() {
        _defaultFeedType = getServletConfig().getInitParameter(DEFAULT_FEED_TYPE);
        _defaultFeedType = (_defaultFeedType!=null) ? _defaultFeedType : "atom_0.3";
    }

    public void doGet(HttpServletRequest req,HttpServletResponse res) throws IOException {
        try {
            SyndFeed feed = getFeed(req);

            String feedType = req.getParameter(FEED_TYPE);
            feedType = (feedType!=null) ? feedType : _defaultFeedType;
            feed.setFeedType(feedType);

            res.setContentType(MIME_TYPE);
            SyndFeedOutput output = new SyndFeedOutput();
            output.output(feed,res.getWriter());
        }
        catch (FeedException ex) {
            String msg = COULD_NOT_GENERATE_FEED_ERROR;
            log(msg,ex);
            res.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,msg);
        }
    }

    protected SyndFeed getFeed(HttpServletRequest req) throws IOException,FeedException {
        SyndFeed feed = new SyndFeedImpl();

        feed.setTitle("Sample Feed (created with ROME)");
        feed.setLink("http://rome.dev.java.net");
        feed.setDescription("This feed has been created using ROME (Java syndication utilities");

        List entries = new ArrayList();
        SyndEntry entry;
        SyndContent description;

        entry = new SyndEntryImpl();
        entry.setTitle("ROME v0.1");
        entry.setLink("http://wiki.java.net/bin/view/Javawsxml/rome01");
        try {
            entry.setPublishedDate(DATE_PARSER.parse("2004-06-08"));
        }
        catch (ParseException ex) {
            // IT CANNOT HAPPEN WITH THIS SAMPLE
        }
        description = new SyndContentImpl();
        description.setType("text/plain");
        description.setValue("Initial release of ROME");
        entry.setDescription(description);
        entries.add(entry);

        entry = new SyndEntryImpl();
        entry.setTitle("Rome v0.2");
        entry.setLink("http://wiki.java.net/bin/view/Javawsxml/rome02");
        try {
            entry.setPublishedDate(DATE_PARSER.parse("2004-06-16"));
        }
        catch (ParseException ex) {
            // IT CANNOT HAPPEN WITH THIS SAMPLE
        }
        description = new SyndContentImpl();
        description.setType("text/plain");
        description.setValue("Bug fixes, minor API changes and some new features"+
                             "<p>For details check the <a href=/"http://wiki.java.net/bin/view/Javawsxml/RomeChangesLog#RomeV02/">Changes Log for 0.2</a></p>");
        entry.setDescription(description);
        entries.add(entry);

        entry = new SyndEntryImpl();
        entry.setTitle("ROME v0.3");
        entry.setLink("http://wiki.java.net/bin/view/Javawsxml/rome03");
        try {
            entry.setPublishedDate(DATE_PARSER.parse("2004-07-27"));
        }
        catch (ParseException ex) {
            // IT CANNOT HAPPEN WITH THIS SAMPLE
        }
        description = new SyndContentImpl();
        description.setType("text/html");
        description.setValue("<p>Bug fixes, API changes, some new features and some Unit testing</p>"+
                             "<p>For details check the <a href=/"http://wiki.java.net/bin/view/Javawsxml/RomeChangesLog#RomeV03/">Changes Log for 0.3</a></p>");
        entry.setDescription(description);
        entries.add(entry);

        entry = new SyndEntryImpl();
        entry.setTitle("ROME v0.4");
        entry.setLink("http://wiki.java.net/bin/view/Javawsxml/rome04");
        try {
            entry.setPublishedDate(DATE_PARSER.parse("2004-09-24"));
        }
        catch (ParseException ex) {
            // IT CANNOT HAPPEN WITH THIS SAMPLE
        }
        description = new SyndContentImpl();
        description.setType("text/html");
        description.setValue("<p>Bug fixes, API changes, some new features, Unit testing completed</p>"+
                             "<p>For details check the <a href=/"http://wiki.java.net/bin/view/Javawsxml/RomeChangesLog#RomeV04/">Changes Log for 0.4</a></p>");
        entry.setDescription(description);
        entries.add(entry);

        feed.setEntries(entries);

        return feed;
    }

}

To use the FeedServlet we need to create a Web Application. For the Web Application we need a deployment descriptor, the web.xml file:

 

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
    <display-name>ROME Samples</display-name>

    <servlet>
        <servlet-name>FeedServlet</servlet-name>
        <servlet-class>com.sun.syndication.samples.servlet.FeedServlet</servlet-class>
        <init-param>
            <param-name>default.feed.type</param-name>
            <param-value>rss_2.0</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
         <servlet-name>FeedServlet</servlet-name>
         <url-pattern>/feed</url-pattern>
    </servlet-mapping>

</web-app>
 

相关文章推荐

java使用Rome解析Rss的实例

Rome简介 Rome是为RSS聚合而开发的开源包,它可以支持0.91、0.92、0.93、0.94、1.0、2.0,可以说rss的版本基本上都支持了。 Rss简介 RSS是站点用来...
  • earbao
  • earbao
  • 2014年06月23日 17:25
  • 5164

rome-1.0RC2rss解析

  • 2013年06月15日 09:37
  • 753KB
  • 下载

rome-1.0 jdom 第一个RSS发布实例

  • 2011年08月17日 16:12
  • 343KB
  • 下载

浅谈Android中用rome解析RSS

随着信息的发展,各种各样的信息格式相继出现。而RSS这种聚合信息的技术也是在稳步发展,各大网站都有提供订阅RSS服务,好用也实用。一般解析RSS其实就是解析xml文件,只是这种xml文件是符合RSS标...

rome实现rss订阅与发布

1. 什么是RSS RSS也叫聚合RSS,是在线共享内容的一种简易方式(也叫聚合内容, 简易供稿,Really Simple Syndication(真正简单的聚合 ))。通常在时效性比较强的内容上...

通过rome实现RSS订阅

首先需要准备下环境(需要的jar包): rome-1.0.jar (下载地址:http://download.csdn.net/detail/y397126710/4323863) jdom-1....

jdom+rome JAR包

  • 2008年08月25日 10:09
  • 269KB
  • 下载

Rome0.9

  • 2008年04月18日 14:32
  • 1.37MB
  • 下载

1087.All Roads Lead to Rome (30)

1087.All Roads Lead to Rome (30)pat-al-10872017-02-03 典型的dijkstra,无须dfs就可以 使用map来进行城市与序号的映射,求单源最短路径就...

Rome OS 自举部分代码框架

  • 2009年08月06日 23:03
  • 541KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:rome-rss
举报原因:
原因补充:

(最多只允许输入30个字)