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 info.magnolia.cms.util.RequestHeaderUtil;
37  import info.magnolia.cms.util.ServletUtils;
38  import info.magnolia.objectfactory.Components;
39  import info.magnolia.voting.Voter;
40  import info.magnolia.voting.Voting;
41  
42  import java.io.IOException;
43  import java.util.ArrayList;
44  import java.util.Collection;
45  import java.util.regex.Pattern;
46  
47  import javax.servlet.FilterChain;
48  import javax.servlet.FilterConfig;
49  import javax.servlet.ServletException;
50  import javax.servlet.ServletRequest;
51  import javax.servlet.ServletResponse;
52  import javax.servlet.http.HttpServletRequest;
53  import javax.servlet.http.HttpServletResponse;
54  
55  import org.apache.commons.lang.ArrayUtils;
56  import org.slf4j.Logger;
57  import org.slf4j.LoggerFactory;
58  
59  
60  /**
61   * A magnolia filter configured in the config repository. This filter is bypassable.
62   * @author philipp
63   * @version $Id: AbstractMgnlFilter.java 39880 2010-11-28 19:59:52Z pbaerfuss $
64   */
65  public abstract class AbstractMgnlFilter implements MgnlFilter {
66  
67      private static Logger log =  LoggerFactory.getLogger(AbstractMgnlFilter.class);
68  
69      private String name;
70  
71      private Voter[] bypasses = new Voter[0];
72  
73      private boolean enabled = true;
74  
75      private DispatchRules dispatchRules = new DispatchRules();
76  
77      private Mapping mapping = new Mapping();
78  
79      private WebContainerResources webContainerResources = Components.getSingleton(WebContainerResources.class);
80  
81      protected AbstractMgnlFilter() {
82      }
83  
84      public void init(FilterConfig filterConfig) throws ServletException {
85      }
86  
87      public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
88          doFilter((HttpServletRequest) request, (HttpServletResponse) response, chain);
89      }
90  
91      public abstract void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException;
92  
93      public boolean matches(HttpServletRequest request) {
94          return isEnabled() && matchesDispatching(request) && mapsTo(request) && !bypasses(request);
95      }
96  
97      protected boolean mapsTo(HttpServletRequest request) {
98          if(getMapping().getMappings().isEmpty()){
99              return true;
100         }
101         return getMapping().match(request).isMatching();
102     }
103 
104     protected boolean matchesDispatching(HttpServletRequest request) {
105         if(webContainerResources == null){
106             return true;
107         }
108         boolean toWebContainerResource = webContainerResources.isWebContainerResource(request);
109         boolean toMagnoliaResource = !toWebContainerResource;
110 
111         DispatchRule dispatchRule = getDispatchRules().getDispatchRule(ServletUtils.getDispatcherType(request));
112         if (toMagnoliaResource && dispatchRule.isToMagnoliaResources())
113             return true;
114         if (toWebContainerResource && dispatchRule.isToWebContainerResources())
115             return true;
116         return false;
117     }
118 
119     protected boolean bypasses(HttpServletRequest request) {
120         Voting voting = Voting.HIGHEST_LEVEL;
121         if (voting.vote(bypasses, request) > 0)
122             return true;
123 
124         return false;
125     }
126 
127     public void destroy() {
128         // nothing to do here
129     }
130 
131     public Voter[] getBypasses() {
132         return this.bypasses;
133     }
134 
135     public void addBypass(Voter voter) {
136         this.bypasses = (Voter[]) ArrayUtils.add(this.bypasses, voter);
137     }
138 
139     public String getName() {
140         return this.name;
141     }
142 
143     public void setName(String name) {
144         this.name = name;
145     }
146 
147 
148     public boolean isEnabled() {
149         return this.enabled;
150     }
151 
152     public void setEnabled(boolean enabled) {
153         this.enabled = enabled;
154     }
155 
156     public DispatchRules getDispatchRules() {
157         return dispatchRules;
158     }
159 
160     public void setDispatchRules(DispatchRules dispatching) {
161         this.dispatchRules = dispatching;
162     }
163 
164     public Collection<String> getMappings() {
165         ArrayList<String> result = new ArrayList<String>();
166         for (Pattern pattern : getMapping().getMappings()) {
167             result.add(pattern.pattern());
168         }
169         return result;
170     }
171 
172     protected Mapping getMapping() {
173         return mapping;
174     }
175 
176     public void addMapping(String mapping){
177         this.getMapping().addMapping(mapping);
178     }
179 
180     //---- utility methods -----
181     protected boolean acceptsGzipEncoding(HttpServletRequest request) {
182         return RequestHeaderUtil.acceptsGzipEncoding(request);
183     }
184 
185     protected boolean acceptsEncoding(final HttpServletRequest request, final String name) {
186         return RequestHeaderUtil.acceptsEncoding(request, name);
187     }
188 
189     protected boolean headerContains(final HttpServletRequest request, final String header, final String value) {
190         return RequestHeaderUtil.headerContains(request, header, value);
191     }
192 
193     protected void addAndVerifyHeader(HttpServletResponse response, String name, String value) {
194         RequestHeaderUtil.addAndVerifyHeader(response, name, value);
195     }
196 
197 
198 
199 }