DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
11 Monitoring and Observability Tools for 2023
Learn more
  1. DZone
  2. Coding
  3. Languages
  4. Creating an ATOMPub server in Java that can return JSON (Using Apache Abdera)

Creating an ATOMPub server in Java that can return JSON (Using Apache Abdera)

Chad Lung user avatar by
Chad Lung
·
Oct. 24, 11 · Interview
Like (0)
Save
Tweet
Share
6.20K Views

Join the DZone community and get the full member experience.

Join For Free

A 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.

 

 

From http://www.giantflyingsaucer.com/blog/?p=3092

JSON Apache Abdera Java (programming language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Is DevOps Dead?
  • The 5 Books You Absolutely Must Read as an Engineering Manager
  • Introduction to Automation Testing Strategies for Microservices
  • Key Elements of Site Reliability Engineering (SRE)

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: