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 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
58
59
60
61
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
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
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
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 }