users@jersey.java.net

JSONP Outputfilter

From: tarjei <tarjei_at_nu.no>
Date: Fri, 24 Jul 2009 11:47:08 +0200

Hi, I'm trying to implement JSONP on top of a solution that allready
produces JSON and XML. As such, I do not want to rewrite my classes to
return JSONWithPadding objects.

I found some information [1] saying that I can do this by implementing
an extra filter that adds wraps the JSON with a callback.

I've tried implementing the filter, but somehow I'm missing an important
line as the filter only returns callback=( and no content when I try to
use it. The filter is below. I think I'm missing just something crucial,
I just do not know what :)

Is there a transparent way to do this in Jersey that I do not know about?


/**
  * See: http://n2.nabble.com/JSONP-Callback-support-tc2260544.html
  */
public class JSONCallbackResponseFilter implements ContainerResponseFilter {
        Logger log = Logger.getLogger(getClass());
        public ContainerResponse filter(ContainerRequest request,
                        ContainerResponse response) {
                if (!request.getAcceptableMediaTypes()
.contains("application/x-javascript")) {
                        log.info("Request does not contain ok mediatype. returning normal
info.");
                        return response;
                }
                
                MultivaluedMap<String, String> queryParamsMapMulti =
request.getQueryParameters();
                String callback = queryParamsMapMulti.getFirst("callback");
                log.debug("callback=" + callback);
                if (callback != null) {
                        response.setContainerResponseWriter(
                                new JSONCallbackResponseAdapter(
                        response.getContainerResponseWriter(),
                        callback));
                }
                return response;
        }
        
        /**
         * * * See:
         *
http://n2.nabble.com/-Filter--how-to-customize-context-body---td2116754.html
         */
        public static final class JSONCallbackResponseAdapter implements
                        ContainerResponseWriter {
                private final ContainerResponseWriter crw;

                private OutputStream out;
                private String callback;

                JSONCallbackResponseAdapter(ContainerResponseWriter crw, String
callback) {
                        this.crw = crw;
                        this.callback = callback;
                }

                public OutputStream writeStatusAndHeaders(
                        long contentLength,
                        ContainerResponse response) throws IOException {
                        out = crw.writeStatusAndHeaders(-1, response);

                        out.write((this.callback + "(").getBytes());
                        return out;
                }

                public void finish() throws IOException {
                        out.write(")".getBytes());
                }
        }
}


1. http://n2.nabble.com/JSONP-Callback-support-tc2260544.html


Kind regards,
Tarjei Huse
Mobil: 920 63 413