View Javadoc

1   /**
2    * This file Copyright (c) 2003-2014 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.voting.Voter;
39  import info.magnolia.voting.Voting;
40  
41  import java.io.IOException;
42  import java.util.ArrayList;
43  import java.util.Collection;
44  import java.util.regex.Pattern;
45  
46  import javax.inject.Inject;
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$
64   */
65  public abstract class AbstractMgnlFilter implements MgnlFilter {
66  
67      private static final 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 dispatching = new DispatchRules();
76  
77      private Mapping mapping = new Mapping();
78  
79      @Inject
80      private WebContainerResources webContainerResources;
81  
82      protected AbstractMgnlFilter() {
83      }
84  
85      @Override
86      public void init(FilterConfig filterConfig) throws ServletException {
87      }
88  
89      @Override
90      public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
91          doFilter((HttpServletRequest) request, (HttpServletResponse) response, chain);
92      }
93  
94      public abstract void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException;
95  
96      @Override
97      public boolean matches(HttpServletRequest request) {
98          return isEnabled() && matchesDispatching(request) && mapsTo(request) && !bypasses(request);
99      }
100 
101     protected boolean mapsTo(HttpServletRequest request) {
102         if(getMapping().getMappings().isEmpty()){
103             return true;
104         }
105         return getMapping().match(request).isMatching();
106     }
107 
108     protected boolean matchesDispatching(HttpServletRequest request) {
109         if(webContainerResources == null){
110             return true;
111         }
112         boolean toWebContainerResource = webContainerResources.isWebContainerResource(request);
113         boolean toMagnoliaResource = !toWebContainerResource;
114 
115         DispatchRule dispatchRule = getDispatching().getDispatchRule(ServletUtils.getDispatcherType(request));
116         if (toMagnoliaResource && dispatchRule.isToMagnoliaResources()) {
117             return true;
118         }
119         if (toWebContainerResource && dispatchRule.isToWebContainerResources()) {
120             return true;
121         }
122         return false;
123     }
124 
125     protected boolean bypasses(HttpServletRequest request) {
126         Voting voting = Voting.HIGHEST_LEVEL;
127         if (voting.vote(bypasses, request) > 0) {
128             return true;
129         }
130 
131         return false;
132     }
133 
134     @Override
135     public void destroy() {
136         // nothing to do here
137     }
138 
139     public Voter[] getBypasses() {
140         return this.bypasses;
141     }
142 
143     public void addBypass(Voter voter) {
144         this.bypasses = (Voter[]) ArrayUtils.add(this.bypasses, voter);
145     }
146 
147     @Override
148     public String getName() {
149         return this.name;
150     }
151 
152     @Override
153     public void setName(String name) {
154         this.name = name;
155     }
156 
157     public boolean isEnabled() {
158         return this.enabled;
159     }
160 
161     public void setEnabled(boolean enabled) {
162         this.enabled = enabled;
163     }
164 
165     public DispatchRules getDispatching() {
166         return dispatching;
167     }
168 
169     public void setDispatching(DispatchRules dispatching) {
170         this.dispatching = dispatching;
171     }
172 
173     public Collection<String> getMappings() {
174         ArrayList<String> result = new ArrayList<String>();
175         for (Pattern pattern : getMapping().getMappings()) {
176             result.add(pattern.pattern());
177         }
178         return result;
179     }
180 
181     protected Mapping getMapping() {
182         return mapping;
183     }
184 
185     public void addMapping(String mapping){
186         this.getMapping().addMapping(mapping);
187     }
188 
189     //---- utility methods -----
190     protected boolean acceptsGzipEncoding(HttpServletRequest request) {
191         return RequestHeaderUtil.acceptsGzipEncoding(request);
192     }
193 
194     protected boolean acceptsEncoding(final HttpServletRequest request, final String name) {
195         return RequestHeaderUtil.acceptsEncoding(request, name);
196     }
197 
198     protected boolean headerContains(final HttpServletRequest request, final String header, final String value) {
199         return RequestHeaderUtil.headerContains(request, header, value);
200     }
201 
202     protected void addAndVerifyHeader(HttpServletResponse response, String name, String value) {
203         RequestHeaderUtil.addAndVerifyHeader(response, name, value);
204     }
205 
206 
207 
208 }