1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
60
61 public abstract class AbstractMgnlFilter implements MgnlFilter {
62
63 private String name;
64
65 private Voter[] bypasses = new Voter[0];
66
67 private boolean enabled = true;
68
69 private DispatchRules dispatching = new DispatchRules();
70
71 private 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
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
173
174 public Collection<String> getMappings() {
175 ArrayList<String> result = new ArrayList<String>();
176 for (Pattern pattern : getMapping().getMappings()) {
177 result.add(pattern.pattern());
178 }
179 return result;
180 }
181
182 protected Mapping getMapping() {
183 return mapping;
184 }
185
186 public void setMappings(Collection<String> mappings) {
187 this.getMapping().setMappings(mappings);
188 }
189
190 public void addMapping(String mapping) {
191 this.getMapping().addMapping(mapping);
192 }
193
194
195 protected boolean acceptsGzipEncoding(HttpServletRequest request) {
196 return RequestHeaderUtil.acceptsGzipEncoding(request);
197 }
198
199 protected boolean acceptsEncoding(final HttpServletRequest request, final String name) {
200 return RequestHeaderUtil.acceptsEncoding(request, name);
201 }
202
203 protected boolean headerContains(final HttpServletRequest request, final String header, final String value) {
204 return RequestHeaderUtil.headerContains(request, header, value);
205 }
206
207 protected void addAndVerifyHeader(HttpServletResponse response, String name, String value) {
208 RequestHeaderUtil.addAndVerifyHeader(response, name, value);
209 }
210
211
212 }