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