/*
 *  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.ServletContext;

import org.springframework.context.ApplicationContext;

/**
 * @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 to all requests.
	 * 
	 * @see ServletContext#addFilter(String, Class)
	 * @param filterClass
	 *            The class of Filter to instantiate.
	 * @param servletContext
	 */
	public static void add(Class<? extends Filter> filterClass, ServletContext servletContext) {
		FilterRegistration.Dynamic mdcInsertingServletFilterReg = servletContext.addFilter(filterClass.getName(), filterClass);
		mdcInsertingServletFilterReg.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, "/*");
	}

	/**
	 * 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
	 */
	public static void add(Class<? extends Filter> filterClass, ApplicationContext appContext, ServletContext servletContext) {
		Filter filter = appContext.getBean(filterClass);
		FilterRegistration.Dynamic mdcInsertingServletFilterReg = servletContext.addFilter(filterClass.getName(), filter);
		mdcInsertingServletFilterReg.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, "/*");
	}

}

@@ -798,16 +798,100 @@
 ration;%0A
+import javax.servlet.FilterRegistration.Dynamic;%0Aimport javax.servlet.Registration;%0A
 import j
@@ -1497,34 +1497,9 @@
 mic 
-mdcInsertingServletFilterR
+r
 eg =
@@ -1567,34 +1567,9 @@
 ;%0A%09%09
-mdcInsertingServletFilterR
+r
 eg.a
@@ -1985,32 +1985,88 @@
  servletContext%0A
+%09 * @return The Dynamic Mapping created by this method.%0A
 %09 */%0A%09public sta
@@ -2061,36 +2061,39 @@
 %0A%09public static 
-void
+Dynamic
  add(Class%3C? ext
@@ -2266,40 +2266,293 @@
 mic 
-mdcInsertingServletFilterReg = s
+reg = servletContext.addFilter(filterClass.getName(), filter);%0A%09%09reg.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, %22/*%22);%0A%09%09return 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 S
 ervl
@@ -2552,33 +2552,33 @@
 e ServletContext
-.
+#
 addFilter(filter
@@ -2575,68 +2575,640 @@
 ter(
-filterClass.getName(), filter);%0A%09%09mdcInsertingServletFilterR
+String, Class)%0A%09 * @param filterClass%0A%09 *            The class of Filter to instantiate.%0A%09 * @param appContext%0A%09 *            The application context to find the bean in.%0A%09 * @param servletContext%0A%09 * @param servletRegistration The servlet to add this filter to.%0A%09 * @return The Dynamic Mapping created by this method.%0A%09 */%0A%09public static Dynamic add(Class%3C? extends Filter%3E filterClass, ApplicationContext appContext, ServletContext servletContext, Registration servletRegistration) %7B%0A%09%09Filter filter = appContext.getBean(filterClass);%0A%09%09FilterRegistration.Dynamic reg = servletContext.addFilter(filterClass.getName(), filter);%0A%09%09r
 eg.a
@@ -3215,34 +3215,35 @@
 ddMappingFor
-UrlPattern
+ServletName
 s(EnumSet.of
@@ -3268,27 +3268,66 @@
 QUEST), 
-true, %22/*%22);%0A%09%7D%0A
+false, servletRegistration.getName());%0A%09%09return reg;%0A%09%7D
 %0A%7D%0A

Add filter for a servlet