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.objectfactory.Components;
39 import info.magnolia.voting.Voter;
40 import info.magnolia.voting.Voting;
41
42 import java.io.IOException;
43 import java.util.ArrayList;
44 import java.util.Collection;
45 import java.util.regex.Pattern;
46
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 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 dispatchRules = new DispatchRules();
76
77 private Mapping mapping = new Mapping();
78
79 private WebContainerResources webContainerResources = Components.getSingleton(WebContainerResources.class);
80
81 protected AbstractMgnlFilter() {
82 }
83
84 public void init(FilterConfig filterConfig) throws ServletException {
85 }
86
87 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
88 doFilter((HttpServletRequest) request, (HttpServletResponse) response, chain);
89 }
90
91 public abstract void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException;
92
93 public boolean matches(HttpServletRequest request) {
94 return isEnabled() && matchesDispatching(request) && mapsTo(request) && !bypasses(request);
95 }
96
97 protected boolean mapsTo(HttpServletRequest request) {
98 if(getMapping().getMappings().isEmpty()){
99 return true;
100 }
101 return getMapping().match(request).isMatching();
102 }
103
104 protected boolean matchesDispatching(HttpServletRequest request) {
105 if(webContainerResources == null){
106 return true;
107 }
108 boolean toWebContainerResource = webContainerResources.isWebContainerResource(request);
109 boolean toMagnoliaResource = !toWebContainerResource;
110
111 DispatchRule dispatchRule = getDispatchRules().getDispatchRule(ServletUtils.getDispatcherType(request));
112 if (toMagnoliaResource && dispatchRule.isToMagnoliaResources())
113 return true;
114 if (toWebContainerResource && dispatchRule.isToWebContainerResources())
115 return true;
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 return false;
125 }
126
127 public void destroy() {
128
129 }
130
131 public Voter[] getBypasses() {
132 return this.bypasses;
133 }
134
135 public void addBypass(Voter voter) {
136 this.bypasses = (Voter[]) ArrayUtils.add(this.bypasses, voter);
137 }
138
139 public String getName() {
140 return this.name;
141 }
142
143 public void setName(String name) {
144 this.name = name;
145 }
146
147
148 public boolean isEnabled() {
149 return this.enabled;
150 }
151
152 public void setEnabled(boolean enabled) {
153 this.enabled = enabled;
154 }
155
156 public DispatchRules getDispatchRules() {
157 return dispatchRules;
158 }
159
160 public void setDispatchRules(DispatchRules dispatching) {
161 this.dispatchRules = dispatching;
162 }
163
164 public Collection<String> getMappings() {
165 ArrayList<String> result = new ArrayList<String>();
166 for (Pattern pattern : getMapping().getMappings()) {
167 result.add(pattern.pattern());
168 }
169 return result;
170 }
171
172 protected Mapping getMapping() {
173 return mapping;
174 }
175
176 public void addMapping(String mapping){
177 this.getMapping().addMapping(mapping);
178 }
179
180
181 protected boolean acceptsGzipEncoding(HttpServletRequest request) {
182 return RequestHeaderUtil.acceptsGzipEncoding(request);
183 }
184
185 protected boolean acceptsEncoding(final HttpServletRequest request, final String name) {
186 return RequestHeaderUtil.acceptsEncoding(request, name);
187 }
188
189 protected boolean headerContains(final HttpServletRequest request, final String header, final String value) {
190 return RequestHeaderUtil.headerContains(request, header, value);
191 }
192
193 protected void addAndVerifyHeader(HttpServletResponse response, String name, String value) {
194 RequestHeaderUtil.addAndVerifyHeader(response, name, value);
195 }
196
197
198
199 }