001/* 002 * Copyright 2012 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.core.servletwrapper.filter; 017 018import java.io.IOException; 019import java.util.Map; 020 021import javax.servlet.Filter; 022import javax.servlet.FilterChain; 023import javax.servlet.ServletContext; 024import javax.servlet.ServletException; 025import javax.servlet.ServletRequest; 026import javax.servlet.ServletResponse; 027import javax.servlet.http.HttpServletRequest; 028import javax.servlet.http.HttpServletResponse; 029 030import org.apache.cocoon.environment.Redirector; 031import org.apache.cocoon.environment.http.HttpEnvironment; 032 033import org.ametys.core.servletwrapper.servlet.ServletWrapperResponse; 034 035 036/** 037 * This class is a generic filter wrapping an actual {@link Filter}. 038 * {@link ServletFilterWrapper} are used to filter requests from inside the application, instead of inside the servlet engine. 039 */ 040public class ServletFilterWrapper 041{ 042 private Filter _filter; 043 044 /** 045 * Constructor 046 * @param filter A filter to apply 047 */ 048 public ServletFilterWrapper(Filter filter) 049 { 050 _filter = filter; 051 } 052 053 /** 054 * Enables to init config parameters using a FilterConfig. 055 * @param parameters a map containing all filter parameters. 056 * @param servletContext the servlet context. 057 * @throws ServletException if the underlying Filter fails to initialize. 058 */ 059 public void init(Map<String, String> parameters, ServletContext servletContext) throws ServletException 060 { 061 try 062 { 063 _filter.init(new ServletFilterWrapperConfig(servletContext, parameters)); 064 } 065 catch (Exception e) 066 { 067 throw new ServletException("Impossible to initialize the filter.", e); 068 } 069 } 070 071 /** 072 * Applies the filter. 073 * @param objectModel the Cocoon's object model. 074 * @param redirect the Cocoon's redirector. 075 * @throws ServletException if the underlying Filter fails to process the request. 076 * @throws IOException if the underlying Filter fails to process the request. 077 */ 078 public void doFilter(Map objectModel, Redirector redirect) throws ServletException, IOException 079 { 080 HttpServletRequest request = (HttpServletRequest) objectModel.get(HttpEnvironment.HTTP_REQUEST_OBJECT); 081 HttpServletResponse response = (HttpServletResponse) objectModel.get(HttpEnvironment.HTTP_RESPONSE_OBJECT); 082 ServletWrapperResponse filteredResponse = new ServletWrapperResponse(response, redirect); 083 084 FilterChain chain = new FilterChain() 085 { 086 @Override 087 public void doFilter(ServletRequest req, ServletResponse res) throws IOException, ServletException 088 { 089 // does nothing 090 } 091 }; 092 093 _filter.doFilter(request, filteredResponse, chain); 094 } 095 096 /** 097 * Destroys the filter. 098 */ 099 public void destroy() 100 { 101 _filter.destroy(); 102 } 103}