users@jersey.java.net

Re: [Jersey] JSONP Callback support

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Wed, 04 Feb 2009 12:08:41 +0100

On Feb 4, 2009, at 11:57 AM, Jakub Podlesak wrote:

> On Wed, Feb 04, 2009 at 11:30:21AM +0100, Paul Sandoz wrote:
>
> <snip/>
>
>>>
>>> The use of '@' sign in resulting JSON as described by BadgerFish is
>>> causing a downstream problem for me as the toolkit I am using to
>>> process
>>> JSON does not like it. So I need to fix my ResponseFilter to
>>> remove '@'
>>> sign in the JSON output.
>
> You do not need to filter '@' out using ResponseFilter,
> please see [1] for two options how to get rid of '@' and still
> retain possibility to de-serialize the JSON back to Java.
>

+1 the NATURAL configuration is the way to go.

Paul.


> Based on the proposed fix, i presume you do not actually use
> BadgerFish notation, and the suggested solutions should then work
> for you.
>
> HTH,
>
> ~Jakub
>
> [1]https://jersey.dev.java.net/issues/show_bug.cgi?id=201
>
>>>
>>> Here is what my adapter class looks like:
>>>
>>> /**
>>> *
>>> * See: http://n2.nabble.com/-Filter--how-to-customize-context-body---td2116754.html
>>> */
>>> private 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());
>>> }
>>> }
>>>
>>> I do not quite understand the above code originally from you.
>>> Where does
>>> the main body of the response get injected taht I need to filter?
>>>
>>
>> The main body of the response is set on the
>> ContainerResponse.getEntity().
>> This is then serialized using a message body writer.
>>
>> Your JSONCallbackResponseAdapter can adapt the output stream "out"
>> in it's
>> own output stream.
>>
>> Although such adaption could check for a byte that is of the value
>> '@' and
>> not write this value it is really not the right layer because you
>> do not
>> know what the character encoding is. One way is in the
>> writeStatusAndHeaders to buffer the content, then in the finish
>> method to
>> re-parse the JSON and using something like Jackson to filter out
>> the '@'
>> characters.
>>
>> Another alternative is of course to have a configuration option in
>> the JSON
>> serialization. But this may effect the ability to consume such JSON
>> back to
>> JAXB.
>>
>> Out of curiosity what JSON clients are you using?
>>
>> Paul.
>>
>>> What would I need to change to remove '@' sign globally from the
>>> response?
>>>
>>> Thanks for your help.
>>>
>>> --
>>> Regards,
>>> Farrukh
>>>
>>> Web: http://www.wellfleetsoftware.com
>>>
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>