View Javadoc
1   /**
2    * This file Copyright (c) 2017-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.servlet;
35  
36  import java.io.InputStream;
37  import java.net.MalformedURLException;
38  import java.net.URL;
39  import java.util.Enumeration;
40  import java.util.EventListener;
41  import java.util.Map;
42  import java.util.Set;
43  
44  import javax.servlet.Filter;
45  import javax.servlet.FilterRegistration;
46  import javax.servlet.RequestDispatcher;
47  import javax.servlet.Servlet;
48  import javax.servlet.ServletContext;
49  import javax.servlet.ServletException;
50  import javax.servlet.ServletRegistration;
51  import javax.servlet.SessionCookieConfig;
52  import javax.servlet.SessionTrackingMode;
53  import javax.servlet.descriptor.JspConfigDescriptor;
54  
55  /**
56   * A general-purpose wrapper around ServletContext to help reducing boilerplate for servlet decorators.
57   */
58  public class ServletContextWrapper implements ServletContext {
59  
60      private final ServletContext parentContext;
61  
62      protected ServletContextWrapper(ServletContext parentServletContext) {
63          // allow also a null parent context for unit tests
64          this.parentContext = parentServletContext;
65      }
66  
67      @Override
68      public String getContextPath() {
69          return parentContext.getContextPath();
70      }
71  
72      @Override
73      public ServletContext getContext(String uripath) {
74          return parentContext.getContext(uripath);
75      }
76  
77      @Override
78      public int getMajorVersion() {
79          return parentContext.getMajorVersion();
80      }
81  
82      @Override
83      public int getMinorVersion() {
84          return parentContext.getMinorVersion();
85      }
86  
87      @Override
88      public int getEffectiveMajorVersion() {
89          return parentContext.getEffectiveMajorVersion();
90      }
91  
92      @Override
93      public int getEffectiveMinorVersion() {
94          return parentContext.getEffectiveMinorVersion();
95      }
96  
97      @Override
98      public String getMimeType(String file) {
99          return parentContext.getMimeType(file);
100     }
101 
102     @Override
103     public Set<String> getResourcePaths(String path) {
104         return parentContext.getResourcePaths(path);
105     }
106 
107     @Override
108     public URL getResource(String path) throws MalformedURLException {
109         return parentContext.getResource(path);
110     }
111 
112     @Override
113     public InputStream getResourceAsStream(String path) {
114         return parentContext.getResourceAsStream(path);
115     }
116 
117     @Override
118     public RequestDispatcher getRequestDispatcher(String path) {
119         return parentContext.getRequestDispatcher(path);
120     }
121 
122     @Override
123     public RequestDispatcher getNamedDispatcher(String name) {
124         return parentContext.getNamedDispatcher(name);
125     }
126 
127     @Override
128     @SuppressWarnings("deprecation")
129     public Servlet getServlet(String name) throws ServletException {
130         return parentContext.getServlet(name);
131     }
132 
133     @Override
134     @SuppressWarnings("deprecation")
135     public Enumeration<Servlet> getServlets() {
136         return parentContext.getServlets();
137     }
138 
139     @Override
140     @SuppressWarnings("deprecation")
141     public Enumeration<String> getServletNames() {
142         return parentContext.getServletNames();
143     }
144 
145     @Override
146     public void log(String msg) {
147         parentContext.log(msg);
148     }
149 
150     @Override
151     @SuppressWarnings("deprecation")
152     public void log(Exception exception, String msg) {
153         parentContext.log(exception, msg);
154     }
155 
156     @Override
157     public void log(String message, Throwable throwable) {
158         parentContext.log(message, throwable);
159     }
160 
161     @Override
162     public String getRealPath(String path) {
163         return parentContext.getRealPath(path);
164     }
165 
166     @Override
167     public String getServerInfo() {
168         return parentContext.getServerInfo();
169     }
170 
171     @Override
172     public String getInitParameter(String name) {
173         return parentContext.getInitParameter(name);
174     }
175 
176     @Override
177     public Enumeration<String> getInitParameterNames() {
178         return parentContext.getInitParameterNames();
179     }
180 
181     @Override
182     public boolean setInitParameter(String name, String value) {
183         return parentContext.setInitParameter(name, value);
184     }
185 
186     @Override
187     public Object getAttribute(String name) {
188         return parentContext.getAttribute(name);
189     }
190 
191     @Override
192     public Enumeration<String> getAttributeNames() {
193         return parentContext.getAttributeNames();
194     }
195 
196     @Override
197     public void setAttribute(String name, Object object) {
198         parentContext.setAttribute(name, object);
199     }
200 
201     @Override
202     public void removeAttribute(String name) {
203         parentContext.removeAttribute(name);
204     }
205 
206     @Override
207     public String getServletContextName() {
208         return parentContext.getServletContextName();
209     }
210 
211     @Override
212     public ServletRegistration.Dynamic addServlet(String servletName, String className) {
213         return parentContext.addServlet(servletName, className);
214     }
215 
216     @Override
217     public ServletRegistration.Dynamic addServlet(String servletName, Servlet servlet) {
218         return parentContext.addServlet(servletName, servlet);
219     }
220 
221     @Override
222     public ServletRegistration.Dynamic addServlet(String servletName, Class<? extends Servlet> servletClass) {
223         return parentContext.addServlet(servletName, servletClass);
224     }
225 
226     @Override
227     public ServletRegistration.Dynamic addJspFile(String servletName, String jspFile) {
228         return parentContext.addJspFile(servletName, jspFile);
229     }
230 
231     @Override
232     public <T extends Servlet> T createServlet(Class<T> clazz) throws ServletException {
233         return parentContext.createServlet(clazz);
234     }
235 
236     @Override
237     public ServletRegistration getServletRegistration(String servletName) {
238         return parentContext.getServletRegistration(servletName);
239     }
240 
241     @Override
242     public Map<String, ? extends ServletRegistration> getServletRegistrations() {
243         return parentContext.getServletRegistrations();
244     }
245 
246     @Override
247     public FilterRegistration.Dynamic addFilter(String filterName, String className) {
248         return parentContext.addFilter(filterName, className);
249     }
250 
251     @Override
252     public FilterRegistration.Dynamic addFilter(String filterName, Filter filter) {
253         return parentContext.addFilter(filterName, filter);
254     }
255 
256     @Override
257     public FilterRegistration.Dynamic addFilter(String filterName, Class<? extends Filter> filterClass) {
258         return parentContext.addFilter(filterName, filterClass);
259     }
260 
261     @Override
262     public <T extends Filter> T createFilter(Class<T> clazz) throws ServletException {
263         return parentContext.createFilter(clazz);
264     }
265 
266     @Override
267     public FilterRegistration getFilterRegistration(String filterName) {
268         return parentContext.getFilterRegistration(filterName);
269     }
270 
271     @Override
272     public Map<String, ? extends FilterRegistration> getFilterRegistrations() {
273         return parentContext.getFilterRegistrations();
274     }
275 
276     @Override
277     public SessionCookieConfig getSessionCookieConfig() {
278         return parentContext.getSessionCookieConfig();
279     }
280 
281     @Override
282     public void setSessionTrackingModes(Set<SessionTrackingMode> sessionTrackingModes) {
283         parentContext.setSessionTrackingModes(sessionTrackingModes);
284     }
285 
286     @Override
287     public Set<SessionTrackingMode> getDefaultSessionTrackingModes() {
288         return parentContext.getDefaultSessionTrackingModes();
289     }
290 
291     @Override
292     public Set<SessionTrackingMode> getEffectiveSessionTrackingModes() {
293         return parentContext.getEffectiveSessionTrackingModes();
294     }
295 
296     @Override
297     public void addListener(String className) {
298         parentContext.addListener(className);
299     }
300 
301     @Override
302     public <T extends EventListener> void addListener(T t) {
303         parentContext.addListener(t);
304     }
305 
306     @Override
307     public void addListener(Class<? extends EventListener> listenerClass) {
308         parentContext.addListener(listenerClass);
309     }
310 
311     @Override
312     public <T extends EventListener> T createListener(Class<T> clazz) throws ServletException {
313         return parentContext.createListener(clazz);
314     }
315 
316     @Override
317     public JspConfigDescriptor getJspConfigDescriptor() {
318         return parentContext.getJspConfigDescriptor();
319     }
320 
321     @Override
322     public ClassLoader getClassLoader() {
323         return parentContext.getClassLoader();
324     }
325 
326     @Override
327     public void declareRoles(String... roleNames) {
328         parentContext.declareRoles(roleNames);
329     }
330 
331     @Override
332     public String getVirtualServerName() {
333         return parentContext.getVirtualServerName();
334     }
335 
336     @Override
337     public int getSessionTimeout() {
338         return parentContext.getSessionTimeout();
339     }
340 
341     @Override
342     public void setSessionTimeout(int sessionTimeout) {
343         parentContext.setSessionTimeout(sessionTimeout);
344     }
345 
346     @Override
347     public String getRequestCharacterEncoding() {
348         return parentContext.getRequestCharacterEncoding();
349     }
350 
351     @Override
352     public void setRequestCharacterEncoding(String encoding) {
353         parentContext.setRequestCharacterEncoding(encoding);
354     }
355 
356     @Override
357     public String getResponseCharacterEncoding() {
358         return parentContext.getResponseCharacterEncoding();
359     }
360 
361     @Override
362     public void setResponseCharacterEncoding(String encoding) {
363         parentContext.setRequestCharacterEncoding(encoding);
364     }
365 }