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.lang3.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   */
63  public abstract class AbstractMgnlFilter implements MgnlFilter {
64  
65      private static final Logger log = LoggerFactory.getLogger(AbstractMgnlFilter.class);
66  
67      private String name;
68  
69      private Voter[] bypasses = new Voter[0];
70  
71      private boolean enabled = true;
72  
73      private DispatchRules dispatching = new DispatchRules();
74  
75      private Mapping mapping = new Mapping();
76  
77      @Inject
78      private WebContainerResources webContainerResources;
79  
80      protected AbstractMgnlFilter() {
81      }
82  
83      @Override
84      public void init(FilterConfig filterConfig) throws ServletException {
85      }
86  
87      @Override
88      public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
89          doFilter((HttpServletRequest) request, (HttpServletResponse) response, chain);
90      }
91  
92      public abstract void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException;
93  
94      @Override
95      public boolean matches(HttpServletRequest request) {
96          return isEnabled() && matchesDispatching(request) && mapsTo(request) && !bypasses(request);
97      }
98  
99      protected boolean mapsTo(HttpServletRequest request) {
100         if(getMapping().getMappings().isEmpty()){
101             return true;
102         }
103         return getMapping().match(request).isMatching();
104     }
105 
106     protected boolean matchesDispatching(HttpServletRequest request) {
107         if(webContainerResources == null){
108             return true;
109         }
110         boolean toWebContainerResource = webContainerResources.isWebContainerResource(request);
111         boolean toMagnoliaResource = !toWebContainerResource;
112 
113         DispatchRule dispatchRule = getDispatching().getDispatchRule(ServletUtils.getDispatcherType(request));
114         if (toMagnoliaResource && dispatchRule.isToMagnoliaResources()) {
115             return true;
116         }
117         if (toWebContainerResource && dispatchRule.isToWebContainerResources()) {
118             return true;
119         }
120         return false;
121     }
122 
123     protected boolean bypasses(HttpServletRequest request) {
124         Voting voting = Voting.HIGHEST_LEVEL;
125         if (voting.vote(bypasses, request) > 0) {
126             return true;
127         }
128 
129         return false;
130     }
131 
132     @Override
133     public void destroy() {
134         // nothing to do here
135     }
136 
137     public Voter[] getBypasses() {
138         return this.bypasses;
139     }
140 
141     public void setBypasses(Voter[] bypasses) {
142         this.bypasses = (Voter[]) ArrayUtils.addAll(this.bypasses, bypasses);
143     }
144 
145     public void addBypass(Voter voter) {
146         this.bypasses = (Voter[]) ArrayUtils.add(this.bypasses, voter);
147     }
148 
149     @Override
150     public String getName() {
151         return this.name;
152     }
153 
154     @Override
155     public void setName(String name) {
156         this.name = name;
157     }
158 
159     public boolean isEnabled() {
160         return this.enabled;
161     }
162 
163     public void setEnabled(boolean enabled) {
164         this.enabled = enabled;
165     }
166 
167     public DispatchRules getDispatching() {
168         return dispatching;
169     }
170 
171     public void setDispatching(DispatchRules dispatching) {
172         this.dispatching = dispatching;
173     }
174 
175     /**
176      * @deprecated since 5.3 - was here to make node2bean think it's dealing with a Collection called mappings. Not needed anymore.
177      */
178     public Collection<String> getMappings() {
179         ArrayList<String> result = new ArrayList<String>();
180         for (Pattern pattern : getMapping().getMappings()) {
181             result.add(pattern.pattern());
182         }
183         return result;
184     }
185 
186     protected Mapping getMapping() {
187         return mapping;
188     }
189 
190     public void setMappings(Collection<String> mappings) {
191         this.getMapping().setMappings(mappings);
192     }
193 
194     public void addMapping(String mapping){
195         this.getMapping().addMapping(mapping);
196     }
197 
198     //---- utility methods -----
199     protected boolean acceptsGzipEncoding(HttpServletRequest request) {
200         return RequestHeaderUtil.acceptsGzipEncoding(request);
201     }
202 
203     protected boolean acceptsEncoding(final HttpServletRequest request, final String name) {
204         return RequestHeaderUtil.acceptsEncoding(request, name);
205     }
206 
207     protected boolean headerContains(final HttpServletRequest request, final String header, final String value) {
208         return RequestHeaderUtil.headerContains(request, header, value);
209     }
210 
211     protected void addAndVerifyHeader(HttpServletResponse response, String name, String value) {
212         RequestHeaderUtil.addAndVerifyHeader(response, name, value);
213     }
214 
215 
216 
217 }