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 36522 2010-08-18 15:43:44Z pbaerfuss $
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     /**
105      * Exposes the parameters configured using content2bean as {@link FilterConfig} to the servlet API.
106      */
107     public static class CustomFilterConfig implements FilterConfig {
108 
109         private Map parameters;
110 
111         private FilterConfig parent;
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         public String getFilterName() {
125             return parent.getFilterName();
126         }
127 
128         public String getInitParameter(String name) {
129             return (String) parameters.get(name);
130         }
131 
132         public Enumeration getInitParameterNames() {
133             return new Hashtable(parameters).keys();
134         }
135 
136         public ServletContext getServletContext() {
137             return parent.getServletContext();
138         }
139 
140         /**
141          * This is a custom method to get recursive configuration parameters like maps or list.
142          */
143         public Object getInitParameterObject(String name) {
144             return parameters.get(name);
145         }
146     }
147 }