1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.resource.servlet

File ResourceReferenceHandlerServlet.java

 

Coverage histogram

../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

0
47
7
1
188
118
15
0.32
6.71
7
2.14

Classes

Class Line # Actions
ResourceReferenceHandlerServlet 54 47 0% 15 8
0.851851985.2%
 

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.resource.servlet;
21   
22    import java.io.IOException;
23    import java.lang.reflect.Type;
24    import java.util.Collections;
25   
26    import javax.servlet.ServletException;
27    import javax.servlet.http.HttpServlet;
28    import javax.servlet.http.HttpServletRequest;
29    import javax.servlet.http.HttpServletResponse;
30   
31    import org.xwiki.component.manager.ComponentLookupException;
32    import org.xwiki.component.manager.ComponentManager;
33    import org.xwiki.component.util.DefaultParameterizedType;
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.resource.ResourceReference;
39    import org.xwiki.resource.ResourceReferenceHandlerException;
40    import org.xwiki.resource.ResourceReferenceHandlerManager;
41    import org.xwiki.resource.ResourceReferenceResolver;
42    import org.xwiki.resource.ResourceType;
43    import org.xwiki.url.ExtendedURL;
44   
45    /**
46    * Handles any Resource Reference discovered by the Routing Filter and put in the HTTP Request. Any module who wish to
47    * add a new Resource Type in the XWiki URL simply needs to register a Handler component (of role
48    * {@link org.xwiki.resource.ResourceReferenceHandler}) and any URL matching the corresponding {@link ResourceType} will
49    * be handled.
50    *
51    * @version $Id: 3f67dbcd50fe3b40a9353a0d75ec0e5fc6501fea $
52    * @since 7.1M1
53    */
 
54    public class ResourceReferenceHandlerServlet extends HttpServlet
55    {
56    /**
57    * Needed for serialization.
58    */
59    private static final long serialVersionUID = 1L;
60   
61    private ComponentManager rootComponentManager;
62   
 
63  29 toggle @Override
64    public void init() throws ServletException
65    {
66  29 super.init();
67   
68    // Get the Component Manager which has been initialized first in a Servlet Context Listener.
69  29 this.rootComponentManager =
70    (ComponentManager) getServletContext().getAttribute(ComponentManager.class.getName());
71    }
72   
 
73  1715 toggle @Override
74    protected void service(HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws ServletException,
75    IOException
76    {
77  1714 try {
78    // Before handling the Resource Reference we need to setup the Request/Response so that it's available to
79    // the Handlers (for writing content to the response for example!)
80    // Note that we don't initialize other things such as the XWiki Contexts for example since we assume that
81    // not all Resource Handlers require the XWiki Contexts (the WebJar Resource Handler doesn't need it for
82    // example). Thus it's up for the specific Resource Handlers to initialize anything else they need to work.
83    // We just initialize the Request/Response/Session here as we consider them to be basic needs for all
84    // Resource Handlers.
85  1712 initializeContainerComponent(httpRequest, httpResponse);
86   
87  1716 handleResourceReference(getResourceReference(httpRequest));
88    } finally {
89  1716 cleanupComponents();
90    }
91    }
92   
 
93  1700 toggle private ResourceReference getResourceReference(HttpServletRequest httpRequest) throws ServletException
94    {
95    // Get the Resource Type from the request's attribute, where it's been put by the RoutingFilter.
96  1704 ResourceType resourceType = (ResourceType) httpRequest.getAttribute(RoutingFilter.RESOURCE_TYPE_NAME);
97   
98    // Get the ExtendedURL from the request's attribute too (so that we don't have to compute it again).
99  1713 ExtendedURL extendedURL = (ExtendedURL) httpRequest.getAttribute(RoutingFilter.RESOURCE_EXTENDEDURL);
100   
101    // Extract the Resource Reference, passing the already extracted Resource Type
102  1713 ResourceReferenceResolver<ExtendedURL> urlResolver = getResourceReferenceResolver();
103  1713 try {
104    // Note that before this code executes a valid Execution Context must be available as it's required to
105    // resolve the wiki referenced by the URL (since this means looking for Wiki Descriptors and do queries on
106    // the store.
107  1715 return urlResolver.resolve(extendedURL, resourceType, Collections.<String, Object>emptyMap());
108    } catch (Exception e) {
109    // This shouldn't happen, raise an exception
110  0 throw new ServletException(String.format("Failed to extract the Resource Reference from the URL [%s]",
111    extendedURL.getWrappedURL()), e);
112    }
113    }
114   
 
115  1700 toggle private ResourceReferenceResolver<ExtendedURL> getResourceReferenceResolver() throws ServletException
116    {
117  1707 ResourceReferenceResolver<ExtendedURL> urlResolver;
118  1708 try {
119  1710 Type role = new DefaultParameterizedType(null, ResourceReferenceResolver.class, ExtendedURL.class);
120  1714 urlResolver = this.rootComponentManager.getInstance(role);
121    } catch (ComponentLookupException e) {
122    // Should not happen since a URL provider should exist on the system.
123  0 throw new ServletException("Failed to locate an ExtendedURL Resource Reference Resolver component", e);
124    }
125  1716 return urlResolver;
126    }
127   
 
128  1712 toggle private void initializeContainerComponent(HttpServletRequest httpRequest, HttpServletResponse httpResponse)
129    throws ServletException
130    {
131  1714 ServletContainerInitializer containerInitializer;
132  1713 try {
133  1713 containerInitializer = this.rootComponentManager.getInstance(ServletContainerInitializer.class);
134    } catch (Exception e) {
135    // This shouldn't happen, raise an exception
136  0 throw new ServletException("Failed to locate a ServletContainerInitializer component", e);
137    }
138  1714 try {
139  1716 containerInitializer.initializeRequest(httpRequest);
140  1715 containerInitializer.initializeResponse(httpResponse);
141  1713 containerInitializer.initializeSession(httpRequest);
142    } catch (ServletContainerException e) {
143  0 throw new ServletException("Failed to initialize Request/Response or Session", e);
144    }
145    }
146   
 
147  1702 toggle private void handleResourceReference(ResourceReference resourceReference) throws ServletException
148    {
149  1705 ResourceReferenceHandlerManager<?> resourceReferenceHandlerManager;
150  1707 try {
151  1712 Type role = new DefaultParameterizedType(null, ResourceReferenceHandlerManager.class, ResourceType.class);
152  1715 resourceReferenceHandlerManager = this.rootComponentManager.getInstance(role);
153    } catch (ComponentLookupException e) {
154    // Should not happen since a Resource Reference Handler should always exist on the system.
155  0 throw new ServletException("Failed to locate a Resource Reference Handler Manager component", e);
156    }
157   
158  1715 try {
159  1716 resourceReferenceHandlerManager.handle(resourceReference);
160    } catch (ResourceReferenceHandlerException e) {
161  0 throw new ServletException(String.format("Failed to handle Resource Reference [%s]", resourceReference), e);
162    }
163    }
164   
 
165  1716 toggle private void cleanupComponents() throws ServletException
166    {
167  1716 Container container;
168  1716 try {
169  1716 container = this.rootComponentManager.getInstance(Container.class);
170    } catch (ComponentLookupException e) {
171  0 throw new ServletException("Failed to locate a Container component", e);
172    }
173   
174  1716 Execution execution;
175  1716 try {
176  1716 execution = this.rootComponentManager.getInstance(Execution.class);
177    } catch (ComponentLookupException e) {
178  0 throw new ServletException("Failed to locate a Execution component", e);
179    }
180   
181    // We must ensure we clean the ThreadLocal variables located in the Container and Execution components as
182    // otherwise we will have a potential memory leak.
183  1716 container.removeRequest();
184  1715 container.removeResponse();
185  1716 container.removeSession();
186  1716 execution.removeContext();
187    }
188    }