View Javadoc

1   /**
2    * This file Copyright (c) 2003-2010 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.cms.filters;
35  
36  import java.io.IOException;
37  import java.util.Enumeration;
38  import java.util.HashMap;
39  import java.util.Hashtable;
40  import java.util.Map;
41  
42  import javax.servlet.Filter;
43  import javax.servlet.FilterChain;
44  import javax.servlet.FilterConfig;
45  import javax.servlet.ServletContext;
46  import javax.servlet.ServletException;
47  import javax.servlet.http.HttpServletRequest;
48  import javax.servlet.http.HttpServletResponse;
49  
50  import org.slf4j.Logger;
51  import org.slf4j.LoggerFactory;
52  
53  
54  /**
55   * Used to decorate a normal (not magnolia) filter.
56   * @author philipp
57   * @version $Id: FilterDecorator.java 32667 2010-03-13 00:37:06Z gjoseph $
58   */
59  public class FilterDecorator extends AbstractMgnlFilter {
60  
61      /**
62       * Decorated Filter
63       */
64      private Filter decoratedFilter;
65  
66      /**
67       * Parameters passed in the config
68       */
69      private Map config;
70  
71      /**
72       * Logger.
73       */
74      private static Logger log = LoggerFactory.getLogger(FilterDecorator.class);
75  
76      public void init(FilterConfig filterConfig) throws ServletException {
77          this.decoratedFilter.init(new CustomFilterConfig(filterConfig, config));
78      }
79  
80      public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException{
81          this.decoratedFilter.doFilter(request, response, chain);
82      }
83  
84      public void destroy() {
85          this.decoratedFilter.destroy();
86      }
87  
88      public Map getConfig() {
89          return this.config;
90      }
91  
92      public void setConfig(Map config) {
93          this.config = config;
94      }
95  
96      public Filter getDecoratedFilter() {
97          return this.decoratedFilter;
98      }
99  
100     public void setDecoratedFilter(Filter decoratedFilter) {
101         this.decoratedFilter = decoratedFilter;
102     }
103 
104     public static class CustomFilterConfig implements FilterConfig {
105 
106         private Map parameters;
107 
108         private FilterConfig parent;
109 
110         /**
111          * @param parameters
112          */
113         public CustomFilterConfig(FilterConfig parent, Map parameters) {
114             super();
115             this.parent = parent;
116             if (parameters != null) {
117                 this.parameters = parameters;
118             }
119             else {
120                 this.parameters = new HashMap();
121             }
122         }
123 
124         /**
125          * @see javax.servlet.FilterConfig#getFilterName()
126          */
127         public String getFilterName() {
128             return parent.getFilterName();
129         }
130 
131         /**
132          * @see javax.servlet.FilterConfig#getInitParameter(java.lang.String)
133          */
134         public String getInitParameter(String name) {
135             return (String) parameters.get(name);
136         }
137 
138         /**
139          * @see javax.servlet.FilterConfig#getInitParameterNames()
140          */
141         public Enumeration getInitParameterNames() {
142             return new Hashtable(parameters).keys();
143         }
144 
145         /**
146          * @see javax.servlet.FilterConfig#getServletContext()
147          */
148         public ServletContext getServletContext() {
149             return parent.getServletContext();
150         }
151 
152         /**
153          * This is a custom method to get recursive configuration parameters like maps, list
154          */
155         public Object getInitParameterObject(String name) {
156             return parameters.get(name);
157         }
158     }
159 }