1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.wysiwyg.server.filter

File XWikiContextInitializationFilter.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

10
42
6
1
193
112
14
0.33
7
6
2.33

Classes

Class Line # Actions
XWikiContextInitializationFilter 58 42 0% 14 5
0.913793191.4%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.wysiwyg.server.filter;
21   
22    import java.io.IOException;
23    import java.lang.reflect.Type;
24   
25    import javax.servlet.Filter;
26    import javax.servlet.FilterChain;
27    import javax.servlet.FilterConfig;
28    import javax.servlet.ServletException;
29    import javax.servlet.ServletRequest;
30    import javax.servlet.ServletResponse;
31    import javax.servlet.http.HttpServletRequest;
32    import javax.servlet.http.HttpServletResponse;
33   
34    import org.xwiki.container.Container;
35    import org.xwiki.container.servlet.ServletContainerException;
36    import org.xwiki.container.servlet.ServletContainerInitializer;
37    import org.xwiki.context.Execution;
38    import org.xwiki.model.reference.DocumentReference;
39    import org.xwiki.model.reference.DocumentReferenceResolver;
40    import org.xwiki.model.reference.SpaceReference;
41    import org.xwiki.model.reference.WikiReference;
42   
43    import com.xpn.xwiki.XWiki;
44    import com.xpn.xwiki.XWikiContext;
45    import com.xpn.xwiki.XWikiException;
46    import com.xpn.xwiki.user.api.XWikiRightService;
47    import com.xpn.xwiki.user.api.XWikiUser;
48    import com.xpn.xwiki.web.Utils;
49    import com.xpn.xwiki.web.XWikiServletContext;
50    import com.xpn.xwiki.web.XWikiServletRequest;
51    import com.xpn.xwiki.web.XWikiServletResponse;
52   
53    /**
54    * This filter can be used to initialize the XWiki context before processing a request.
55    *
56    * @version $Id: 28b28ff3be809a86b3e59b7ed2d83e5212ce77b5 $
57    */
 
58    public class XWikiContextInitializationFilter implements Filter
59    {
60    /**
61    * The filter configuration object.
62    */
63    private FilterConfig filterConfig;
64   
65    /**
66    * XWiki context mode.
67    */
68    private int mode;
69   
 
70  64 toggle @Override
71    public void destroy()
72    {
73  64 this.filterConfig = null;
74    }
75   
 
76  1712 toggle @Override
77    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
78    ServletException
79    {
80  1712 try {
81    // Only HTTP requests are supported.
82  1712 if (request instanceof HttpServletRequest) {
83  1712 initializeXWikiContext(request, response);
84    }
85  1712 chain.doFilter(request, response);
86    } finally {
87  1712 if (request instanceof HttpServletRequest) {
88  1712 cleanupComponents();
89    }
90    }
91    }
92   
 
93  64 toggle @Override
94    public void init(FilterConfig filterConfig) throws ServletException
95    {
96  64 this.filterConfig = filterConfig;
97   
98  64 try {
99  64 this.mode = Integer.parseInt(filterConfig.getInitParameter("mode"));
100    } catch (Exception e) {
101  32 this.mode = -1;
102    }
103    }
104   
105    /**
106    * Initializes the XWiki context.
107    *
108    * @param request the request being processed
109    * @param response the response
110    * @throws ServletException if the initialization fails
111    */
 
112  1712 toggle protected void initializeXWikiContext(ServletRequest request, ServletResponse response) throws ServletException
113    {
114  1712 try {
115    // Not all request types specify an action (e.g. GWT-RPC) so we default to the empty string.
116  1712 String action = "";
117  1712 XWikiServletContext xwikiEngine = new XWikiServletContext(this.filterConfig.getServletContext());
118  1712 XWikiServletRequest xwikiRequest = new XWikiServletRequest((HttpServletRequest) request);
119  1712 XWikiServletResponse xwikiResponse = new XWikiServletResponse((HttpServletResponse) response);
120   
121    // Create the XWiki context.
122  1712 XWikiContext context = Utils.prepareContext(action, xwikiRequest, xwikiResponse, xwikiEngine);
123   
124    // Overwrite the context mode set in the prepareContext() call just above if the mode filter initialization
125    // parameter is specified.
126  1712 if (this.mode >= 0) {
127  1709 context.setMode(this.mode);
128    }
129   
130    // Initialize the Container component which is the new way of transporting the Context in the new component
131    // architecture. Further initialization might require the Container component.
132  1712 initializeContainerComponent(context);
133   
134    // Initialize the XWiki database. XWiki#getXWiki(XWikiContext) calls XWikiContext.setWiki(XWiki).
135  1712 XWiki xwiki = XWiki.getXWiki(context);
136   
137    // Initialize the URL factory.
138  1712 context.setURLFactory(xwiki.getURLFactoryService().createURLFactory(context.getMode(), context));
139   
140    // Prepare the localized resources, according to the selected language.
141  1712 xwiki.prepareResources(context);
142   
143    // Initialize the current user.
144  1712 XWikiUser user = context.getWiki().checkAuth(context);
145  1712 if (user != null) {
146  773 DocumentReferenceResolver<String> documentReferenceResolver =
147    Utils.getComponent(DocumentReferenceResolver.TYPE_STRING, "explicit");
148  773 SpaceReference defaultUserSpace =
149    new SpaceReference(XWiki.SYSTEM_SPACE, new WikiReference(context.getWikiId()));
150  773 DocumentReference userReference = documentReferenceResolver.resolve(user.getUser(), defaultUserSpace);
151  773 context.setUserReference(XWikiRightService.GUEST_USER.equals(userReference.getName()) ? null
152    : userReference);
153    }
154    } catch (XWikiException e) {
155  0 throw new ServletException("Failed to initialize the XWiki context.", e);
156    }
157    }
158   
159    /**
160    * @param context the XWiki context
161    * @throws ServletException if the container component initialization fails
162    */
 
163  1712 toggle protected void initializeContainerComponent(XWikiContext context) throws ServletException
164    {
165    // Initialize the Container fields (request, response, session). Note that this is a bridge between the old core
166    // and the component architecture. In the new component architecture we use ThreadLocal to transport the
167    // request, response and session to components which require them.
168  1712 ServletContainerInitializer containerInitializer = Utils.getComponent((Type) ServletContainerInitializer.class);
169   
170  1712 try {
171  1712 containerInitializer.initializeRequest(context.getRequest().getHttpServletRequest(), context);
172  1712 containerInitializer.initializeResponse(context.getResponse());
173  1712 containerInitializer.initializeSession(context.getRequest().getHttpServletRequest());
174    } catch (ServletContainerException e) {
175  0 throw new ServletException("Failed to initialize Request/Response or Session", e);
176    }
177    }
178   
179    /**
180    * We must ensure we clean the ThreadLocal variables located in the Container and Execution components as otherwise
181    * we will have a potential memory leak.
182    */
 
183  1712 toggle protected void cleanupComponents()
184    {
185  1712 Container container = Utils.getComponent((Type) Container.class);
186  1712 container.removeRequest();
187  1712 container.removeResponse();
188  1712 container.removeSession();
189   
190  1712 Execution execution = Utils.getComponent((Type) Execution.class);
191  1712 execution.removeContext();
192    }
193    }