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.lang.ArrayUtils;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
58
59
60
61
62
63
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
137 }
138
139 public Voter[] getBypasses() {
140 return this.bypasses;
141 }
142
143 public void setBypasses(Voter[] bypasses) {
144 this.bypasses = (Voter[]) ArrayUtils.addAll(this.bypasses, bypasses);
145 }
146
147 public void addBypass(Voter voter) {
148 this.bypasses = (Voter[]) ArrayUtils.add(this.bypasses, voter);
149 }
150
151 @Override
152 public String getName() {
153 return this.name;
154 }
155
156 @Override
157 public void setName(String name) {
158 this.name = name;
159 }
160
161 public boolean isEnabled() {
162 return this.enabled;
163 }
164
165 public void setEnabled(boolean enabled) {
166 this.enabled = enabled;
167 }
168
169 public DispatchRules getDispatching() {
170 return dispatching;
171 }
172
173 public void setDispatching(DispatchRules dispatching) {
174 this.dispatching = dispatching;
175 }
176
177 public Collection<String> getMappings() {
178 ArrayList<String> result = new ArrayList<String>();
179 for (Pattern pattern : getMapping().getMappings()) {
180 result.add(pattern.pattern());
181 }
182 return result;
183 }
184
185 protected Mapping getMapping() {
186 return mapping;
187 }
188
189 public void setMappings(Collection<String> mappings) {
190 this.getMapping().setMappings(mappings);
191 }
192
193 public void addMapping(String mapping){
194 this.getMapping().addMapping(mapping);
195 }
196
197
198 protected boolean acceptsGzipEncoding(HttpServletRequest request) {
199 return RequestHeaderUtil.acceptsGzipEncoding(request);
200 }
201
202 protected boolean acceptsEncoding(final HttpServletRequest request, final String name) {
203 return RequestHeaderUtil.acceptsEncoding(request, name);
204 }
205
206 protected boolean headerContains(final HttpServletRequest request, final String header, final String value) {
207 return RequestHeaderUtil.headerContains(request, header, value);
208 }
209
210 protected void addAndVerifyHeader(HttpServletResponse response, String name, String value) {
211 RequestHeaderUtil.addAndVerifyHeader(response, name, value);
212 }
213
214
215
216 }