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

File XWikiSetupCleanupFilter.java

 

Coverage histogram

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

Code metrics

6
17
4
1
121
59
9
0.53
4.25
4
2.25

Classes

Class Line # Actions
XWikiSetupCleanupFilter 47 17 0% 9 5
0.814814881.5%
 

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.rest.internal;
21   
22    import java.util.Collections;
23    import java.util.List;
24    import java.util.logging.Level;
25   
26    import javax.servlet.http.HttpServletRequest;
27   
28    import org.restlet.Request;
29    import org.restlet.Response;
30    import org.restlet.ext.servlet.ServletUtils;
31    import org.restlet.routing.Filter;
32    import org.xwiki.component.manager.ComponentLifecycleException;
33    import org.xwiki.component.manager.ComponentManager;
34    import org.xwiki.context.Execution;
35    import org.xwiki.context.ExecutionContext;
36    import org.xwiki.rest.XWikiRestComponent;
37   
38    /**
39    * <p>
40    * The Setup cleanup filter is used to populate the Restlet context with relevant variables that are used by JAX-RS
41    * resources. It is also used to release the JAX-RS resources that are instantiated by using the component manager, in
42    * order to avoid memory leaks.
43    * </p>
44    *
45    * @version $Id: 6b47763940f9aa697f2a55132998eb04db99e745 $
46    */
 
47    public class XWikiSetupCleanupFilter extends Filter
48    {
 
49  1709 toggle @Override
50    protected int beforeHandle(Request request, Response response)
51    {
52    /*
53    * We put the original HTTP request in context attributes because this is needed for reading
54    * application/www-form-urlencoded POSTs. In fact servlet filters might call getParameters() which invalidates
55    * the request body, making Restlet unable to process it. In this case we need to use getParameters as well
56    * instead of reading form data from the input stream, and in order to do this we need the original HTTP request
57    * object. This is basically a hack that should be removed as soon as the Restlet JAX-RS extension will support
58    * the injection of the request object via the @Context annotation.
59    */
60  1709 getContext().getAttributes().put(Constants.HTTP_REQUEST, getHttpRequest(request));
61   
62  1709 return Filter.CONTINUE;
63    }
64   
 
65  1709 toggle @Override
66    protected void afterHandle(Request request, Response response)
67    {
68    // Release all the JAX-RS resources that are implemented as components with per-lookup policy and that have been
69    // instantiated during this request.
70  1709 ComponentManager componentManager =
71    (ComponentManager) getApplication().getContext().getAttributes().get(Constants.XWIKI_COMPONENT_MANAGER);
72  1709 for (XWikiRestComponent component : getReleasableComponents(componentManager)) {
73  1562 try {
74  1562 componentManager.release(component);
75    } catch (ComponentLifecycleException e) {
76  0 getLogger().log(Level.WARNING, "Unable to release component [{0}]. ({1})",
77    new Object[] {component.getClass().getName(), e.getMessage()});
78    }
79    }
80   
81    /* Avoid that empty entities make the engine forward the response creation to the XWiki servlet. */
82  1709 if (response.getEntity() != null) {
83  1709 if (!response.getEntity().isAvailable()) {
84  327 response.setEntity(null);
85    }
86    }
87    }
88   
89    /**
90    * @param componentManager the component manager
91    * @return the list of JAX-RS resources that are implemented as components with per-lookup policy and that have been
92    * instantiated during this request
93    */
 
94  1709 toggle private List<XWikiRestComponent> getReleasableComponents(ComponentManager componentManager)
95    {
96  1709 try {
97  1709 ExecutionContext executionContext = componentManager.<Execution> getInstance(Execution.class).getContext();
98  1709 @SuppressWarnings("unchecked")
99    List<XWikiRestComponent> releasableComponents =
100    (List<XWikiRestComponent>) executionContext.getProperty(Constants.RELEASABLE_COMPONENT_REFERENCES);
101  1709 return releasableComponents != null ? releasableComponents : Collections.<XWikiRestComponent> emptyList();
102    } catch (Exception e) {
103  0 getLogger().log(Level.WARNING, "Failed to retrieve the list of releasable components.", e);
104  0 return Collections.emptyList();
105    }
106    }
107   
108    /**
109    * <p>
110    * Retrieves the original servlet request.
111    * </p>
112    *
113    * @param req The Restlet request to handle.
114    * @return httpServletRequest The original HTTP servlet request.
115    */
 
116  1709 toggle protected static HttpServletRequest getHttpRequest(Request req)
117    {
118  1709 return ServletUtils.getRequest(req);
119    }
120   
121    }