Creating an ATOMPub server in Java that can return JSON (Using Apache Abdera)
Join the DZone community and get the full member experience.
Join For FreeA few days ago I showed you how you can build an ATOM client that can convert ATOM XML to JSON. Today I’ll show you how to take Apache Abdera and create an ATOMPub server that can return ATOM XML as well as JSON. For the client part (to demo) I’m going to use the Firefox plug-in Poster.
Take a look at my previous ATOMPub server article since that will be the basis of this new server. In order to add support to return JSON there are a few steps:
1. Make sure Maven is setup to add the Apache Abdera JSON Extension
2. Modify my previous article’s code to return JSON if requested by the client
Going with the original ATOMPub server code you only need to add two lines of code to add in the JSON support:
The import for the Apache Abdera JSON Extension:
import org.apache.abdera.ext.json.JSONFilter;
Add support for the JSONFilter:
provider.addFilter(new JSONFilter());
The final code looks like this:
/* * Original code created by the Apache Abdera team * http://abdera.apache.org/ */ package com.giantflyingsaucer.atompubserver; import org.apache.abdera.protocol.server.Provider; import org.apache.abdera.protocol.server.impl.DefaultProvider; import org.apache.abdera.protocol.server.impl.SimpleWorkspaceInfo; import org.apache.abdera.protocol.server.servlet.AbderaServlet; import org.apache.abdera.ext.json.JSONFilter; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletHolder; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class App { public static void main(String... args) throws Exception { int port = 9002; try { port = args.length > 0 ? Integer.parseInt(args[0]) : 9002; } catch (Exception e) { } Server server = new Server(port); ServletContextHandler context = new ServletContextHandler(server, "/", ServletContextHandler.SESSIONS); ServletHolder servletHolder = new ServletHolder(new EmployeeProviderServlet()); context.addServlet(servletHolder, "/*"); server.start(); server.join(); } public static final class EmployeeProviderServlet extends AbderaServlet { @Override protected Provider createProvider() { EmployeeCollectionAdapter ca = new EmployeeCollectionAdapter(); ca.setHref("employee"); SimpleWorkspaceInfo wi = new SimpleWorkspaceInfo(); wi.setTitle("Employee Directory Workspace"); wi.addCollection(ca); DefaultProvider provider = new DefaultProvider("/"); provider.addWorkspace(wi); provider.init(getAbdera(), null); provider.addFilter(new JSONFilter()); return provider; } @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { super.service(request, response); } } }
For Maven, make sure you have this dependency (and the keep the others I already have in the prior project):
<dependency> <groupId>org.apache.abdera</groupId> <artifactId>abdera-extensions-json</artifactId> <version>1.1.2</version> </dependency>
Using Poster I can populate the Abdera server with some data:
<?xml version="1.0"?> <entry xmlns="http://www.w3.org/2005/Atom"> <id>tag:example.org,2011:foo</id> <title type="text">This is the title</title> <updated>2011-09-18T18:50:22.356Z</updated> <content type="text">Hello World</content> </entry>
Insert the data with Poster via an HTTP POST action:
Get the data back using Poster via an HTTP GET action and a querystring argument of: format=json
Results:
"feed":{ "id":"tag:acme.com,2007:employee:feed", "title":"Acme Employee Database", "updated":"2011-09-18T18:52:59.359Z", "authors":[{ "name":"Acme Industries" } ], "links":[{ "href":"" },{ "href":"", "rel":"self" } ], "entries":[{ "id":"tag:giantflyingsaucer.com,2011:employee:entry:1000", "title":"Hello World", "content":"Hello World", "authors":[{ "name":"Acme Industries" } ], "links":[{ "href":"/employee/1000-Hello_World", "rel":"edit" } ] } ] } }
Of course you can take my ATOM Client project and modify that to work with this ATOMPub server rather than have to use Poster.
Opinions expressed by DZone contributors are their own.
Comments