Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 /*
  * Copyright (c) SiteWhere, LLC. All rights reserved. http://www.sitewhere.com
  *
  * The software in this package is published under the terms of the CPAL v1.0
  * license, a copy of which has been included with this distribution in the
  * LICENSE.txt file.
  */
 package com.sitewhere.web;
 
Wraps API calls that request a JSONP response.

Author(s):
Derek
public class JsonpFilter implements Filter {

Parameter containing function name for callback function
	private static final String CALLBACK_PARAMETER = "callback";

The default padding to use if the specified padding contains invalid characters
	public static final String DEFAULT_CALLBACK = "jsonpDataResult";

Regular expression that determines if callback is a valid function name
	public static final Pattern SAFE_PATTERN = Pattern.compile("[a-zA-Z0-9_\\.]+");
	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
	 */
	public void init(FilterConfig fConfigthrows ServletException {
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest,
	 * javax.servlet.ServletResponse, javax.servlet.FilterChain)
	 */
	@SuppressWarnings("unchecked")
	public void doFilter(ServletRequest requestServletResponse responseFilterChain chain)
		HttpServletRequest httpRequest = (HttpServletRequestrequest;
		HttpServletResponse httpResponse = (HttpServletResponseresponse;
		Map<StringString[]> parms = httpRequest.getParameterMap();
			// Make sure that callback value is a function name and not code.
			String callback = parms.get()[0];
			if (!.matcher(callback).matches()) {
				callback = ;
			}
			OutputStream out = httpResponse.getOutputStream();
			GenericResponseWrapper wrapper = new GenericResponseWrapper(httpResponse);
			chain.doFilter(requestwrapper);
			out.write(new String(callback + "(").getBytes());
			out.write(wrapper.getData());
			out.write(new String(");").getBytes());
			wrapper.setContentType("text/javascript;charset=UTF-8");
			out.close();
else {
			chain.doFilter(requestresponse);
		}
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.Filter#destroy()
	 */
	public void destroy() {
	}
New to GrepCode? Check out our FAQ X