/*
 *  Copyright 2011 Eric F. Savage, code@efsavage.com
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */
package com.ajah.spring.mvc.util;

import java.util.EnumSet;

import javax.servlet.DispatcherType;
import javax.servlet.Filter;
import javax.servlet.FilterRegistration;
import javax.servlet.FilterRegistration.Dynamic;
import javax.servlet.Registration;
import javax.servlet.ServletContext;

import org.springframework.context.ApplicationContext;
import org.springframework.web.WebApplicationInitializer;

/**
 * Utilities for dealing with Filters. Currently just provides easier ways to
 * add filters to a context from within a {@link WebApplicationInitializer}.
 * 
 * @author <a href="http://efsavage.com">Eric F. Savage</a>, <a
 *         href="mailto:code@efsavage.com">code@efsavage.com</a>.
 * 
 */
public class FilterUtils {

	/**
	 * Convenience method for adding a filter that is available as a Spring
	 * bean/service to all requests.
	 * 
	 * @see ServletContext#addFilter(String, Class)
	 * @param filterClass
	 *            The class of Filter to instantiate.
	 * @param appContext
	 *            The application context to find the bean in.
	 * @param servletContext
	 * @return The Dynamic Mapping created by this method.
	 */
	public static Dynamic add(final Class<? extends Filter> filterClass, final ApplicationContext appContext, final ServletContext servletContext) {
		final Filter filter = appContext.getBean(filterClass);
		final FilterRegistration.Dynamic reg = servletContext.addFilter(filterClass.getName(), filter);
		reg.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, "/*");
		return reg;
	}

	/**
	 * Convenience method for adding a filter that is available as a Spring
	 * bean/service to a servlet.
	 * 
	 * @see ServletContext#addFilter(String, Class)
	 * @param filterClass
	 *            The class of Filter to instantiate.
	 * @param appContext
	 *            The application context to find the bean in.
	 * @param servletContext
	 * @param servletRegistration
	 *            The servlet to add this filter to.
	 * @return The Dynamic Mapping created by this method.
	 */
	public static Dynamic add(final Class<? extends Filter> filterClass, final ApplicationContext appContext, final ServletContext servletContext, final Registration servletRegistration) {
		final Filter filter = appContext.getBean(filterClass);
		final FilterRegistration.Dynamic reg = servletContext.addFilter(filterClass.getName(), filter);
		reg.addMappingForServletNames(EnumSet.of(DispatcherType.REQUEST), false, servletRegistration.getName());
		return reg;
	}

	/**
	 * Convenience method for adding a filter to all requests.
	 * 
	 * @see ServletContext#addFilter(String, Class)
	 * @param filterClass
	 *            The class of Filter to instantiate.
	 * @param servletContext
	 */
	public static void add(final Class<? extends Filter> filterClass, final ServletContext servletContext) {
		add(filterClass, servletContext, "/*");
	}

	/**
	 * Convenience method for adding a filter to certain requests.
	 * 
	 * @see ServletContext#addFilter(String, Class)
	 * @param filterClass
	 *            The class of Filter to instantiate, required.
	 * @param servletContext
	 *            The servlet context ofthe application, required.
	 * @param urlPattern
	 *            The pattern of the URL this filter should be applied to.
	 */
	public static void add(final Class<? extends Filter> filterClass, final ServletContext servletContext, String urlPattern) {
		final FilterRegistration.Dynamic reg = servletContext.addFilter(filterClass.getName(), filterClass);
		reg.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, urlPattern);
	}

}

@@ -3116,32 +3116,814 @@
 return reg;%0A%09%7D%0A%0A
+%09/**%0A%09 * Convenience method for adding a filter that is available as a Spring%0A%09 * bean/service to a servlet.%0A%09 * %0A%09 * @see ServletContext#addFilter(String, Class)%0A%09 * @param filterClass%0A%09 *            The class of Filter to instantiate.%0A%09 * @param servletContext%0A%09 * @param servletRegistration%0A%09 *            The servlet to add this filter to.%0A%09 * @return The Dynamic Mapping created by this method.%0A%09 */%0A%09public static Dynamic add(final Class%3C? extends Filter%3E filterClass, final ServletContext servletContext, final Registration servletRegistration) %7B%0A%09%09final FilterRegistration.Dynamic reg = servletContext.addFilter(filterClass.getName(), filterClass);%0A%09%09reg.addMappingForServletNames(EnumSet.of(DispatcherType.REQUEST), false, servletRegistration.getName());%0A%09%09return reg;%0A%09%7D%0A%0A
 %09/**%0A%09 * Conveni

Add spring-free filter to specific servlet