ManyDesigns logo

How to create a Blog in Portofino 4 - part 5

In this fifth and last tutorial we create RSS feeds for our blog. The final goal is shown in the following picture.

If you haven't, read tutorial part 1, part 2, part 3 and part 4.

 
 

Step 1. Create the RSS feed

In our last customization, we are going to create the rss feed for our posts.
 
We create a method "rss" that returns the file with the "post" rss. The method returns a Resolution, this make the metod accessible via web (see the code below).
 
To call this page, an user has to write the name of the method as a parameter, e.g. http://localhost:8080/post?rss.  
 
class MyCrudAction extends CrudAction {

...
    //***************************************
    // RSS
    //***************************************
    public Resolution rss() {
        setupSearchForm();
        loadObjects();
        DateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy kk:mm:ss +S");
        Date lastDate = objects.size()>0?objects.get(0).date:new Date();
        XmlBuffer buffer = new XmlBuffer();
        buffer.writeXmlHeader("UTF-8");
        buffer.openElement("rss");
        buffer.addAttribute("version", "2.0");
        buffer.openElement("channel");
        buffer.openElement("title");
        buffer.write("My Blog");
        buffer.closeElement("title");
        buffer.openElement("description");
        buffer.write("My first blog with Portofino 4");
        buffer.closeElement("description");
        buffer.openElement("lastBuildDate");
        buffer.write(df.format(lastDate));
        buffer.closeElement("lastBuildDate");
        buffer.openElement("pubDate");
        buffer.write(df.format(lastDate));
        buffer.closeElement("pubDate");
        for (HashMap blog : objects){
            buffer.openElement("item");
            buffer.openElement("title");
            buffer.write((String) blog.title);
            buffer.closeElement("title");
            buffer.openElement("description");
            buffer.write((String) blog.summary);
            buffer.closeElement("description");
            buffer.openElement("link");
            String serverName = context.getRequest().getServerName();
            serverName+= (80!=context.getRequest().getServerPort()?"
:"+context.getRequest().getServerPort():"");
            buffer.write(serverName+"/posts/"+blog.id);
            buffer.closeElement("link");
            buffer.openElement("guid");
            buffer.write((String) blog.id);
            buffer.closeElement("guid");
            buffer.openElement("pubDate");
            buffer.write(df.format(blog.date));
            buffer.closeElement("pubDate");
            buffer.closeElement("item");
        }
        buffer.closeElement("channel");
        buffer.closeElement("rss");
        return new StreamingResolution("application/rss+xml",
           buffer.toString());
    }
…
}
This method creates the xml following feed rss format, we use our library XMLBuffer to create the XML.
The returned result has  "application/rss+xml" as mimetype (see below).
return new StreamingResolution("application/rss+xml",
 buffer.toString());
Notice that the Resolution doen't not make a forward to a jsp, instead it returns a stream with the XML document.
Open your browser to http://localhost:8080/post?rss to see the rss with your posts.

 

 

comments powered by Disqus