[ 
https://issues.apache.org/jira/browse/CXF-3005?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12930896#action_12930896
 ] 

Dobes Vandermeer edited comment on CXF-3005 at 11/10/10 9:17 PM:
-----------------------------------------------------------------

Here's a somewhat simpler solution; create a subclass of the JSONProvider that 
handles JSONP:

{code}
        /**
         * Add jsonp support to the JSONProvider.
         * 
         * Basically, check for the appropriate content type
         * and the parameter specifying the prefix.
         */
        @Produces("application/javascript")
        static class JsonpProvider extends JSONProvider {
                @Context HttpServletRequest request; 
                
                @Override
                protected void marshal(Marshaller ms, Object actualObject, 
Class<?> actualClass, Type genericType, String enc, OutputStream os,
                                boolean isCollection) throws Exception {
                        String prefix = request.getParameter("_jsonp");
                        boolean hasPrefix = !isEmpty(prefix);
                        if(hasPrefix) {
                                os.write(prefix.getBytes(enc));
                                os.write('(');
                        }
                        super.marshal(ms, actualObject, actualClass, 
genericType, enc, os, isCollection);
                        if(hasPrefix) {
                                os.write(')');
                        }
                }
        }

{code}

      was (Author: dobes_vandermeer):
    Here's a somewhat simpler solution; create a subclass of the JSONProvider 
that handles JSONP:

{{{
        /**
         * Add jsonp support to the JSONProvider.
         * 
         * Basically, check for the appropriate content type
         * and the parameter specifying the prefix.
         */
        @Produces("application/javascript")
        static class JsonpProvider extends JSONProvider {
                @Context HttpServletRequest request; 
                
                @Override
                protected void marshal(Marshaller ms, Object actualObject, 
Class<?> actualClass, Type genericType, String enc, OutputStream os,
                                boolean isCollection) throws Exception {
                        String prefix = request.getParameter("_jsonp");
                        boolean hasPrefix = !isEmpty(prefix);
                        if(hasPrefix) {
                                os.write(prefix.getBytes(enc));
                                os.write('(');
                        }
                        super.marshal(ms, actualObject, actualClass, 
genericType, enc, os, isCollection);
                        if(hasPrefix) {
                                os.write(')');
                        }
                }
        }

}}}
  
> Add support for jsonp in CXF JAX-RS
> -----------------------------------
>
>                 Key: CXF-3005
>                 URL: https://issues.apache.org/jira/browse/CXF-3005
>             Project: CXF
>          Issue Type: New Feature
>          Components: JAX-RS
>            Reporter: Josh Holtzman
>         Attachments: cxf_jsonp.diff
>
>
> JAX-RS endpoints that produce JSON can be wrapped by a callback to enable 
> JSONP, or JSON with padding.  The attached patch adds JSONP interceptors that 
> may be added to a JAXRSServerFactoryBean to support JSONP.
>     JAXRSServerFactoryBean factory = new JAXRSServerFactoryBean();
>     factory.getInInterceptors().add(new JsonpInvokeInterceptor());
>     factory.getOutInterceptors().add(new JsonpPreStreamInterceptor());
>     factory.getOutInterceptors().add(new JsonpPostStreamInterceptor());
>     ...

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply via email to