Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package com.wadpam.open.jsonp;
 
 
public class JsonpCallbackFilter implements Filter {
    public static final String  CALLBACK_METHOD_NAME = "callbackMethodName";
    private static final Logger LOG                  = LoggerFactory.getLogger(JsonpCallbackFilter.class);
    private String              callbackParam        = "callback";
    public void init(FilterConfig fConfigthrows ServletException {
        final String callbackMethodName = fConfig.getInitParameter();
        if (null != callbackMethodName) {
             = callbackMethodName;
        }
    }
    public void doFilter(ServletRequest requestServletResponse responseFilterChain chainthrows IOException,
            ServletException {
        HttpServletRequest httpRequest = (HttpServletRequestrequest;
        HttpServletResponse httpResponse = (HttpServletResponseresponse;
        final String callback = httpRequest.getParameter();
        if (null != callback && callback.length() > 0) {
            .debug("Wrapping response with JSONP callback '" + callback + "'");
            GenericResponseWrapper wrapper = new GenericResponseWrapper(httpResponse);
            chain.doFilter(requestwrapper);
            httpResponse.setContentType("text/javascript;charset=UTF-8");
            final OutputStream out = httpResponse.getOutputStream();
            out.write(callback.getBytes());
            out.write('(');
            final byte data[] = wrapper.getData();
            .debug("Wrapped data size is " + data.length);
            out.write(data);
            out.write(')');
            out.write(';');
            out.close();
        }
        else {
            chain.doFilter(requestresponse);
        }
    }
    public void destroy() {
    }
New to GrepCode? Check out our FAQ X