Archive for the ‘Java’ Category

Servlet Filter to Convert XML to JSON

In a recent project I was facing the problem that a JSON interface had to be added to an already existing XML interface. The XML was rendered using JSPs. That’s fine and I didn’t want to change that. I also didn’t want to duplicate the JSPs by writing additional JSPs that generate JSON. The easiest solution that came into my mind was to add a simple servlet filter that converts the generated XML to JSON on the fly when JSON format is requested.

In our web application the desired format (XML or JSON) was added as a request parameter to the HTTP request). All the servlet filter has to do is to check this parameter to decide whether the XML should be converted to JSON.

In order to compile and use the code you also need the following libraries:

  1. servlet-api 2.5
  2. dom4j 1.6.1 (http://dom4j.sourceforge.net/)
  3. jsonwriter (https://github.com/kkrugler/jsonwriter) A nice extension to dom4j written by Ken Krugler and Chris Schneider aka Schmed that allows to write a dom4j Document as json. Thanks to Ken and Schmed to make this library open source. I actually saved my blog.

Here is the code:

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

/**
 * Converts xml to json if query parameter format=json is available.
 */
public class JsonFilter implements Filter {

    public void destroy() {
        // do nothing
    }

    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
            throws IOException, ServletException {
        if (res instanceof HttpServletResponse) {
            HttpServletResponse response = (HttpServletResponse) res;
            if ("json".equals(req.getParameter("format"))) {
                JsonResponseWrapper wrappedResponse = new JsonResponseWrapper(response);
                chain.doFilter(req, wrappedResponse);
                wrappedResponse.finishResponse();
            } else {
                chain.doFilter(req, res);
            }
        }
    }

    public void init(FilterConfig filterConfig) throws ServletException {
        // do nothing
    }
}

The JsonResponseWrapper does the XML to JSON conversion. Please note that this implementation buffers the complete XML in memory. This approach is not appropriate for very large XML, but it was working very well for our application:

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.io.JSONFormat;
import org.dom4j.io.JSONWriter;

/**
 * Converts xml to json.
 */
public class JsonResponseWrapper extends HttpServletResponseWrapper {

    private ByteArrayServletOutputStream _servletOutputStream;
    private PrintWriter _printWriter;

    public JsonResponseWrapper(HttpServletResponse response) {
        super(response);
        response.setContentType("text/x-json");
    }

    @Override
    public void setContentLength(int len) {
        // ignore
    }

    @Override
    public void setContentType(String type) {
        // ignore
    }

    @Override
    public ServletOutputStream getOutputStream() throws IOException {
        if (_printWriter != null) {
            throw new IllegalStateException("Servlet already accessed print writer.");
        }

        if (_servletOutputStream == null) {
            _servletOutputStream = new ByteArrayServletOutputStream();
        }
        return _servletOutputStream;
    }

    @Override
    public PrintWriter getWriter() throws IOException {
        if (_printWriter == null && _servletOutputStream != null) {
            throw new IllegalStateException("Servlet already accessed output stream.");
        }

        if (_printWriter == null) {
            _servletOutputStream = new ByteArrayServletOutputStream();
            Writer writer = new OutputStreamWriter(_servletOutputStream, getResponse()
                    .getCharacterEncoding());
            _printWriter = new PrintWriter(writer);
        }
        return _printWriter;
    }

    public void finishResponse() throws IOException {
        if (_servletOutputStream != null) {
            if (_printWriter != null) {
                _printWriter.flush();
            }
            try {
                Document document = DocumentHelper.parseText(new String(
                        _servletOutputStream.getBytes(), getResponse()
                                .getCharacterEncoding()));
                JSONWriter writer = new JSONWriter(getResponse().getWriter(),
                        JSONFormat.RABBIT_FISH);
                writer.write(document);
                writer.flush();
                getResponse().getWriter().write("\n");
            } catch (DocumentException e) {
                throw new RuntimeException(e);
            }
        }
    }

    @Override
    public void resetBuffer() {
        _servletOutputStream = null;
        _printWriter = null;
        super.resetBuffer();
    }

    @Override
    public void reset() {
        _servletOutputStream = null;
        _printWriter = null;
        super.reset();
    }
}

The ByteArrayServletOutputStream just caches the XML in memory.

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import javax.servlet.ServletOutputStream;

public class ByteArrayServletOutputStream extends ServletOutputStream {

    private ByteArrayOutputStream _outputStream;

    public ByteArrayServletOutputStream() {
        _outputStream = new ByteArrayOutputStream();
    }

    @Override
    public void write(int b) throws IOException {
        _outputStream.write(b);
    }

    @Override
    public void write(byte[] b) throws IOException {
        _outputStream.write(b);
    }

    @Override
    public void write(byte[] b, int off, int len) throws IOException {
        _outputStream.write(b, off, len);
    }

    @Override
    public void close() throws IOException {
        _outputStream.close();
    }

    public byte[] getBytes() {
        return _outputStream.toByteArray();
    }
}

The code was pretty straight forward to write. But maybe it is still helpful for someone who has a similar problem. It can be optimized to use a streaming approach versus the in memory buffering of the XML. But that wasn’t needed for my purposes, so I leave this up to you. ;-)

Advertisements

Omit Unexpected XML Elements With XStream

XStream is a Java xml library, which nicely serializes Java objects to XML and vice versa. It can easily deal with missing (i.e. optional) XML elements. The corresponding Java fields will just be left blank.

<user>
    <name>Peter Voss<name>
</user>

can be read into the Java object:

public class User {
    private String name;
    private String role;

    // getter and setter methods are here
}

In this case the optional <role> field is missing in the XML and the corresponding field in the User Java object will be left null when deserializing the XML.

But once if you have decided on your XML API, you might want to question if it is flexible enough. Just consider you have built software based on this XML spec. Can you still add optional XML elements without breaking the applications that you have already shipped? Consider you want to add more information, like a <department> element. Will your clients be able to just ignore this piece of information? The short answer is: No. XStream will throw a ConversionException if it finds an element that has no corresponding Java field. The Jira ticket XSTR-30 is an improvement request related to this topic. But so far XStream has no simple switch to turn off complaining about unknown elements.

But you could easily tweak XStream to ignore additional elements by adding your own custom mapper that ignores the field. The following snippet creates an XStream instance that ignores additional fields. It is grabbed from the CustomMapperTest.testCanBeUsedToOmitUnexpectedElements() unit test that is part of the XStream source code:

XStream xstream = new XStream() {
  @Override
  protected MapperWrapper wrapMapper(MapperWrapper next) {
    return new MapperWrapper(next) {
      @Override
      public boolean shouldSerializeMember(Class definedIn,
              String fieldName) {
        if (definedIn == Object.class) {
          return false;
        }
        return super.shouldSerializeMember(definedIn, fieldName);
      }
    };
  }
};

I just wanted to write this down, because a solution for this common problem is somewhat difficult to find right now.