/**
 * 
 */
package com.trendrr.strest.contrib.filters;

import java.io.UnsupportedEncodingException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.netty.handler.codec.http.HttpHeaders;
import org.jboss.netty.handler.codec.http.HttpResponse;

import com.trendrr.strest.ContentTypes;
import com.trendrr.strest.StrestException;
import com.trendrr.strest.StrestHttpException;
import com.trendrr.strest.StrestUtil.HEADERS;
import com.trendrr.strest.server.ResponseBuilder;
import com.trendrr.strest.server.StrestController;
import com.trendrr.strest.server.StrestControllerFilter;


/**
 * 
 * Simple filter to handle jsonp requests.
 * 
 * 
 * @author Dustin Norlander
 * @created Apr 12, 2011
 * 
 */
public class JsonpFilter implements StrestControllerFilter {

	protected Log log = LogFactory.getLog(JsonpFilter.class);

	String jsonpParam = "jsonp";
	
	/* (non-Javadoc)
	 * @see com.trendrr.strest.server.StrestControllerFilter#before(com.trendrr.strest.server.StrestController)
	 */
	@Override
	public void before(StrestController controller) throws StrestException {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see com.trendrr.strest.server.StrestControllerFilter#after(com.trendrr.strest.server.StrestController)
	 */
	@Override
	public void after(StrestController controller) throws StrestException {
		HttpResponse response = controller.getResponse();
		if (!ContentTypes.JSON.equals(response.getHeader(HttpHeaders.Names.CONTENT_TYPE))) {
			return;
		}
		String param = controller.getParams().getString(this.jsonpParam);
		if (param == null)
			return;
		try {
			String jsonp = param +"(" + new String(response.getContent().array(), "utf8") + ");";
			ResponseBuilder.instance(response).content(ContentTypes.JAVASCRIPT, jsonp.getBytes("utf8"));
		} catch (UnsupportedEncodingException e) {
			throw StrestHttpException.INTERNAL_SERVER_ERROR(e.getMessage());
		}
	}

	/* (non-Javadoc)
	 * @see com.trendrr.strest.server.StrestControllerFilter#error(com.trendrr.strest.server.StrestController, org.jboss.netty.handler.codec.http.HttpResponse, java.lang.Exception)
	 */
	@Override
	public void error(StrestController controller, HttpResponse response,
			Exception exception) {
		// TODO Auto-generated method stub

	}
}

@@ -94,16 +94,45 @@
 ception;
+%0Aimport java.util.Collection;
 %0A%0Aimport
@@ -322,16 +322,84 @@
 ponse;%0A%0A
+import com.trendrr.oss.ListHelper;%0Aimport com.trendrr.oss.TypeCast;%0A
 import c
@@ -971,38 +971,297 @@
 s);%0A
+%09
 %0A%09
-String jsonpParam = %22jsonp%22;
+private static Collection%3CString%3E params = ListHelper.toTypedList(String.class, %22jsonp,callback%22, %22,%22);%0A%09%0A%09/**%0A%09 * returns the possible names for the jsonp param.  %0A%09 * defaults to %5B'jsonp','callback'%5D%0A%09 * @return%0A%09 */%0A%09protected Collection%3CString%3E getParamNames() %7B%0A%09%09return params;%0A%09%7D
 %0A%09%0A%09
@@ -1908,58 +1908,140 @@
 m = 
-controller.getParams().getString(this.jsonpParam);
+null;%0A%09%09for (String p : this.getParamNames()) %7B%0A%09%09%09param = controller.getParams().getString(p);%0A%09%09%09if (param != null)%0A%09%09%09%09break;%0A%09%09%7D
 %0A%09%09i

add callback as a possible jsonp param