22
2006
Generating RSS Feeds with Grails and Rome
I’m currently working on a little Grails bug tracking application which should see the light of day on Sourceforge sometime in the new year. One of the features I just love about Jira is the RSS support – I’m always watching the Grails recently closed issues to see what’s been fixed in the SVN.
So I decided to see what’s involved in generating a feed. I could use the standard Groovy MarkupBuilder for sure, but then I’d have to work out the details for each of the XML formats in the common RSS and Atom formats. There has to be an easier way.
Enter ROME, a fantastic little library for generating ALL the common RSS feed formats. My goal was to have a controller action that takes a parameter for feed type so I could set up a bunch of links for the common cases. Turns out there’s a great tutorial to get you started. Using it from Grails makes it even simpler…
Copy your rome.jar file to your project/lib directory, create a new FeedController, and you’re off and running. Here’s a sample controller I’ve whipped up that generates a ton of standard feeds:
import com.sun.syndication.feed.synd.*;
import com.sun.syndication.io.SyndFeedOutput;
class FeedController {
def supportedFormats = [ "rss_0.90", "rss_0.91", "rss_0.92", "rss_0.93", "rss_0.94", "rss_1.0", "rss_2.0", "atom_0.3"]
def rss = {
render(text: getFeed("rss_1.0"), contentType:"text/xml", encoding:"UTF-8")
}
def atom = {
render(text: getFeed("atom_1.0"), contentType:"text/xml", encoding:"UTF-8")
}
// or specify your own feed type
def all = {
def format = params.id
if (supportedFormats.contains(format)) {
render(text: getFeed(format), contentType:"text/xml", encoding:"UTF-8")
} else {
response.sendError(response.SC_FORBIDDEN);
}
}
def getFeed(feedType) {
def issues = Bug.list(max: 5, sort: "created", order: "desc")
def entries = []
issues.each { issue ->
def desc = new SyndContentImpl(type: "text/plain", value: issue.description);
def entry = new SyndEntryImpl(title: issue.name + " - " + issue.summary,
link: 'http://bugga.sf.net/issues/show/' + issue.name,
publishedDate: issue.created, description: desc);
entries.add(entry);
}
SyndFeed feed = new SyndFeedImpl(feedType: feedType, title: 'Recently Closed Bugga Issues',
link: 'http://bugga.sf.net', description: 'Bugga issues closed in the last few days',
entries: entries);
StringWriter writer = new StringWriter();
SyndFeedOutput output = new SyndFeedOutput();
output.output(feed,writer);
writer.close();
return writer.toString();
}
}
Then you just need to point your aggregator to http://localhost/bugga/feed/atom and you’re in business. Or if you want to use any of the supported feeds, head on over to http://localhost:8080/bugga/feed/all/rss_2.0 or whatever you fancy.
Here’s a grab from NewzCrawler:

Props to the ROME team for a first class little library that does one thing very well.
6 Comments + Add Comment
Leave a comment
Glen Smith
Archives
- April 2012
- March 2012
- January 2012
- November 2011
- October 2011
- September 2011
- August 2011
- July 2011
- June 2011
- April 2011
- March 2011
- January 2011
- November 2010
- October 2010
- September 2010
- August 2010
- July 2010
- June 2010
- May 2010
- April 2010
- February 2010
- January 2010
- December 2009
- November 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- May 2007
- April 2007
- March 2007
- February 2007
- January 2007
- December 2006
- November 2006
- October 2006
- September 2006
- August 2006
- July 2006
- June 2006
- May 2006
- April 2006
- March 2006
- February 2006
- January 2006
- December 2005
- November 2005
- October 2005
- September 2005
- August 2005
- July 2005
- June 2005
- May 2005
- April 2005
- March 2005
- February 2005
- January 2005
- December 2004
- November 2004
- October 2004
- September 2004
- August 2004
- July 2004
- June 2004
- May 2004
- March 2004
- February 2004
- January 2004
- December 2003
- November 2003
- October 2003
- September 2003

An article by Glen





nice posting.. very useful. Wanted to point out that when I first tried it I got a class not found for org/jdom/Document. I was able to resolve it with the jar from http://www.jdom.org/. Would any XML DOM represention package work though?
Thanks very much – exactly what I was looking for and even easier than Rome from Java!
really a nice article. but when i try your example i get the following error:
groovy.lang.MissingPropertyException: No such property: Bug for class: EntryController
the problem seems to be the render method:
render( text : getFeed(‘rss_2.0′) , contentType : “text/xml” , encoding : “UTF-8″ )
Hi. Looks like you need to change that Bug.list(…) call to be whatever domain class you are using in your grails app.
For example, if it was a list of Books in your database that you were generating a feed for, change it to Book.list(…).
Excellent article, only took a few minutes to get an rss feed up and running. iGoogle and Google Reader have trouble with the feeds klipfolio, Firefox, Sharpreader all take the feeds. Am I missing something or is this something others have seen?
Google Reader doesn’t support reading feeds from localhost – maybe that is your problem.