I‘ve been really busy lately and have been neglecting my website and a bunch of other stuff besides. So this post is going to be brief.

... quite a lot more brief than that.
Formats made easy using SeemoreJ
You could also say, Rails-style.
In this post I‘m going to be talking about how SeemoreJ deals with deciding what format to present a resource in and how easy it is to display the same resource in multiple different formats.
The need for this was driven by my desire to provide a new feed for the articles on my website. That was straight forward and I just implemented a special case, which just spat out some XML and didn‘t forward on to a JSP view. This resulted in the following code for the list action.
BlogController.java (before)//...
public class BlogController {
//...
ArticlePager pager;
//...
@BeforeFilter(include = {"home", "list", "byCategory", "byMonth"})
public void createPager() {
pager = new ArticlePager();
pager.setResultsPerPage(10);
try {
pager.setCurrentPageNumber(Integer.valueOf(request().getParameter("p")));
} catch (NumberFormatException e) {
pager.setCurrentPageNumber(1);
}
request().setAttribute("pager", pager);
}
@AfterFilter(include = {"home", "list", "byCategory", "byMonth"})
public void initialisePager() {
try {
pager.initialise();
} catch (Exception e) {
log.error("Pager failed to initialise", e);
}
}
//...
public void list() {
if ("html".equalsIgnoreCase(request().getParameter(FORMAT))) {
request().setAttribute("sectionTitle", "All Articles");
} else {
try {
Syndicate syndicate = new Syndicate(RequestUtil.getAppURL(request()));
syndicate.syndicateBlog("atom_1.0", response().getWriter());
renderNothing();
} catch (IOException e) {
log.error("Couldn't produce ATOM feed", e);
renderView("404");
}
}
}
//...
}
A quick note on the BeforeFilter and AfterFilter annotated methods. These are a SeemoreJ feature that allow any arbitrary method to be invoked before and after any action. There'll be more on this in future posts.
Anyway, back to the subject at hand. The code above does the job but is not elegant. It did not please me to write it and it does not please me to look at it. It worked well enough and so this was the code for my site for a few months despite the fact that it did not really sit comfortably with me.
A better solution
I recently added SeemoreJ to ohloh. I'm not really sure what ohloh is or whether its going to be useful to me yet, or whether it has or will gain the critical mass of users to become a success. However, one of the features on there was to provide a news feed specifically for a project. So I figured I could just publish all the posts in the SeemoreJ category.
This article is in danger of becoming something far more verbose and – crucially – not at all brief. So I'll cut to the chase.
I have added a feature to SeemoreJ that allows easy customisation of what format to deliver a resource in. Navigate to http://www.malethan.com/all.html, you get a HTML. However, navigate to http://www.malethan.com/all.atom, you get XML. Atom XML to be precise.
This is achieved by having the view JSPs named in such a way that allows SeemoreJ to determine what format they represent. So I have a JSP called list.html.jsp for HTML and list.atom.jsp for Atom XML. Fans of Rails reading this will be familiar with the convention.
First, lets have a look at the relevant code in my controller class now. List is now a one–liner, specifying the title for the page/feed only.
BlogController.java (after)//...
public class BlogController {
//...
public void list() {
request().setAttribute("sectionTitle", "All Articles");
}
//...
}
To deliver any resource with Atom I just need to create an appropriately named JSP and let it deliver the content. So I have the following view JSP for my Atom feed…
list.atom.jsp<%@ page import="com.malethan.blog.RequestUtil" %>
<%@ page import="com.malethan.blog.feed.Syndicate" %>
<%@ page import="com.malethan.blog.models.ArticlePager" %>
<%
String title = request.getAttribute("sectionTitle") + " | implements Elegance { // Elwyn Malethan's Blog";
ArticlePager pager = (ArticlePager) request.getAttribute("pager");
Syndicate syndicate = new Syndicate(RequestUtil.getAppURL(request));
syndicate.syndicateArticles("atom_1.0", response.getWriter(), pager.getResults(), title);
%>
... and this for the HTML …
list.html.jsp<%@ include file="/WEB-INF/common/taglibs.jsp" %>
<html>
<head>
<title><%@ include file="/WEB-INF/app/blog/_title.jsp" %></title>
</head>
<body>
<div id="content" class="wide">
<c:if test="${empty(pager.results)}">
<p>Your selection has resulted in a page with no articles. Perhaps you would like to look at
<a href="/all.html">all of them</a>.</p>
</c:if>
<c:if test="${!empty(pager.results)}">
<c:set var="blogPosts" value="${pager.results}" />
<pager:pages pager="${pager}" />
<%@ include file="_list.jsp" %>
<pager:pages pager="${pager}" />
</c:if>
</div>
</body>
</html>
So, as only somewhat briefly described, I can now syndicate SeemoreJ posts alone to ohloh. It also means that all the other categories and the archive is similarly published.
First published on Jul 6, 2009. Last updated on: Dec 29, 2009.
In my