Feed Icon Bl-Or

Rome framework is one of the most popular APIs for creating and reading rss feeds.In this tutorial we will see how to implement an rss feed stream with feeds created from database entries. As an example for this tutorial the rss stream will consist of the tutorials of java-only.comThe first step will be to create a servlet where the RSS stream and the feed entries will be created and initialized.In this case the feed entries will the result set from the database.

package com.javaonly.rss;

import com.javaonly.Daos.TutorialDao;
import com.javaonly.hibernate.HibernateUtil;
import com.javaonly.model.Tutorial;
import com.sun.syndication.feed.synd.SyndContent;
import com.sun.syndication.feed.synd.SyndContentImpl;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndEntryImpl;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.feed.synd.SyndFeedImpl;
import com.sun.syndication.io.FeedException;
import com.sun.syndication.io.SyndFeedOutput;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class RssServlet extends HttpServlet {

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

    public void init() {
        defaultType = getServletConfig().getInitParameter(default.feed.type);
        defaultType = (defaultType != null) ? defaultType : "atom_0.3";
    }
protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
            throws ServletException, IOException {
        try {
            SyndFeed feed = getFeed(request);

            String feedType = request.getParameter("type");
            feedType = (feedType!=null) ? feedType : _defaultFeedType;
            feed.setFeedType(feedType);

            response.setContentType("application/xml; charset=UTF-8");
            SyndFeedOutput output = new SyndFeedOutput();
            output.output(feed,response.getWriter());
        }
        catch (FeedException ex) {
            String msg ="Could not generate feed";
            log(msg,ex);
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,msg);
        }
    }

 @Override
 protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request,
 HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }

    protected SyndFeed getFeed(HttpServletRequest request) {
        TutorialDao tutorialDao = new TutorialDao();
        tutorialDao.setSez(HibernateUtil.getSessionFactory().getCurrentSession());
        HibernateUtil.beginViewTransaction();
        List<Tutorial> tutorials = tutorialDao.getAll();
        HibernateUtil.commitTransaction();
    //create the rss feed and set the Title the link to the site and a small descreption
        SyndFeed feed = new SyndFeedImpl();
        feed.setTitle("JAVA ONLY: tutorials and resources for Java");
        feed.setLink("http://www.java-only.com/Index.javaonly");
        feed.setDescription("Tutorials and resources for Java");

        //create the feeds.Each tutorial will be a feed entry
        List entries = new ArrayList();
        for (Tutorial tutorial : tutorials) {
            SyndEntry entry = entry = new SyndEntryImpl();
            SyndContent description;
            String title = tutorial.getTitle();
 String link ="";
 link ="http://http://latest-tutorial.pakifreelancer.com/?p=" + tutorial.getId();
            entry.setTitle(title);
            entry.setLink(link);
            try {
                //set published Date
  entry.setPublishedDate(DATE_PARSER.parse(tutorial.getDateAdded().toString()));
            } catch (ParseException ex) {
                ex.printStackTrace();
            }
            //Create the description of the feed entry
            description = new SyndContentImpl();
            description.setType("text/plain");
            description.setValue(tutorial.getSummary());
            entry.setDescription(description);
            entries.add(entry);
        }
        feed.setEntries(entries);
        return feed;
    }
}

Finally we have to configure Rome in the web.xml of the application.So in the web xml we will add the following snippet:

 <servlet>
        <servlet-name>RssServlet</servlet-name>
        <servlet-class>com.javaonly.rss.RssServlet</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>RssServlet</servlet-name>
        <url-pattern>/feed</url-pattern>
    </servlet-mapping>

 

One thought on “How to make RSS Feed in Java

Leave a Reply

Your email address will not be published. Required fields are marked *