users@jersey.java.net

Re: [Jersey] [Filter] how to customize context body ?

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Tue, 06 Jan 2009 17:52:14 +0100

On Jan 6, 2009, at 5:40 PM, Sam Pullara wrote:

> Hi Paul,
>
> This is a very common practice so you can use the service from a
> <script> include within a web page. I am actually surprised that
> JAX-RS doesn't support JSON callbacks out of the box because it is
> such a widespread practice. For instance, all Yahoo web service
> APIs support this as one of the result formats. It is generally
> referred to as JSONP. There should likely be a provider that
> supports it:
>
> http://en.wikipedia.org/wiki/JSON#JSONP
> http://developer.yahoo.com/common/json.html#callbackparam
>

Thanks that helped me understand better. I agree it should be a
provider, could you log an issue?

Such a provider can reuse the JAXB provider for JSON and it can look
at the callback query parameter so it is not necessary for the
application to do that.

Paul.

> Sam
>
> On Jan 6, 2009, at 1:58 AM, Paul Sandoz wrote:
>
>> Hi,
>>
>> See the source code of the GZIP and logging filter:
>>
>> https://jersey.dev.java.net/source/browse/jersey/trunk/jersey/jersey-server/src/main/java/com/sun/jersey/api/container/filter/GZIPContentEncodingFilter.java?view=markup
>>
>> https://jersey.dev.java.net/source/browse/jersey/trunk/jersey/jersey-server/src/main/java/com/sun/jersey/api/container/filter/GZIPContentEncodingFilter.java?view=markup
>>
>> Your adapter could do something like this:
>>
>> private static final class Adapter implements
>> ContainerResponseWriter {
>> private final ContainerResponseWriter crw;
>>
>> private OutputStream out;
>>
>> Adapter(ContainerResponseWriter crw) {
>> this.crw = crw;
>> }
>>
>> public OutputStream writeStatusAndHeaders(long
>> contentLength, ContainerResponse response) throws IOException {
>> out = crw.writeStatusAndHeaders(-1, response);
>>
>> out.write("zzz(".getBytes());
>> return out;
>> }
>>
>> public void finish() throws IOException {
>> out.write(")".getBytes());
>> }
>> }
>>
>> And you register it like this:
>>
>> if(query string 'callback' is set) {
>> response.setContainerResponseWriter(
>> new
>> Adapter(response.getContainerResponseWriter()));
>> }
>>
>> But. given that the client states what the function should be in
>> the query parameter why can't it do the work itself and wrap the
>> JSON around the function? why it is necessary to do this on the
>> server side?
>>
>> Paul.
>>
>> On Jan 6, 2009, at 7:25 AM, À¯Àç±Õ wrote:
>>
>>> Thank you Sandoz.
>>>
>>> https://jersey.dev.java.net/servlets/ReadMsg?list=users&msgNo=3746
>>> this question is related above url.
>>>
>>> I have make Filter class.
>>> --------------------------------------------------------------
>>> public class JsonCallbackFilter implements ContainerResponseFilter{
>>> public ContainerResponse filter(ContainerRequest request,
>>> ContainerResponse response) {
>>> if(query string 'callback' is set) {
>>> ContainerResponseWriter crw =
>>> response.getContainerResponseWriter();
>>>
>>> // TODO how to add 'callback' value ???
>>>
>>> response.setContainerResponseWriter(crw);
>>> }
>>> return response;
>>> }
>>> --------------------------------------------------------------
>>>
>>> the request url is like this.
>>> http://xxxxxxxxxx/member/info?name=yyy&callback=zzz&type=json
>>>
>>> and expected result is
>>> zzz({"code":"12312","name":"yyy"})
>>>
>>>
>>> please help me.
>>>
>>> °³ÀοëÀÚ·á - Á¢±Ù±ÝÁö
>>>
>>> Ãâó°¡ ¸íÈ®ÇÏÁö ¾ÊÀº °Ô½Ã¹°ÀÌ ¸¹ÀÌ Æ÷ÇԵǾîÀÖ½À´Ï´Ù.
>>> °ü°èÀÚ°¡ ¾Æ´ÑºÐÀº ¹æ¹®À» »ï°¡ÇØ ÁֽʽÿÀ.
>>>
>>
>