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

File XWikiRestletServlet.java

 

Coverage histogram

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

Code metrics

6
23
3
1
127
62
8
0.35
7.67
3
2.67

Classes

Class Line # Actions
XWikiRestletServlet 52 23 0% 8 5
0.8437584.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.rest.internal;
21   
22    import java.io.InputStream;
23    import java.util.logging.LogManager;
24   
25    import javax.servlet.ServletException;
26   
27    import org.apache.commons.io.IOUtils;
28    import org.restlet.Application;
29    import org.restlet.Context;
30    import org.xwiki.component.manager.ComponentLookupException;
31    import org.xwiki.component.manager.ComponentManager;
32    import org.xwiki.rest.internal.Constants;
33   
34    import org.restlet.ext.servlet.ServerServlet;
35   
36    /**
37    * <p>
38    * The XWiki Restlet servlet is used to provide additional initialization logic to the base Restlet servlet. This
39    * servlet does three things:
40    * </p>
41    * <ul>
42    * <li>Creates the Restlet application.</li>
43    * <li>Initialize the logging system by reading the configuration from well-defined locations.</li>
44    * <li>Injects the component manager in the Restlet application context so that it will be accessible by all the other
45    * Restlet components.</li>
46    * <li>Set the object factory for the JAX-RS application to a factory that will use the component manager in order to
47    * create instances (this will allow us to declare JAX-RS resources as XWiki components)</li>
48    * </ul>
49    *
50    * @version $Id: 6727b89021bc05678f4ad94a4f82e2d8e5da60d9 $
51    */
 
52    public class XWikiRestletServlet extends ServerServlet
53    {
54    private static final String JAVA_LOGGING_PROPERTY_FILE = "java-logging.properties";
55   
56    private static final long serialVersionUID = 9148448182654390153L;
57   
 
58  18 toggle @Override
59    protected Application createApplication(Context context)
60    {
61  18 Application application = super.createApplication(context);
62   
63    /* Retrieve the application context in order to populate it with relevant variables. */
64  18 Context applicationContext = application.getContext();
65   
66    /* Retrieve the component manager and make it available in the restlet application context. */
67  18 ComponentManager componentManager = getComponentManager(context);
68  18 applicationContext.getAttributes().put(Constants.XWIKI_COMPONENT_MANAGER, componentManager);
69   
70    /* Set the object factory for instantiating components. */
71  18 if (application instanceof XWikiRestletJaxRsApplication) {
72  18 XWikiRestletJaxRsApplication jaxrsApplication = (XWikiRestletJaxRsApplication) application;
73  18 jaxrsApplication.setObjectFactory(new ComponentsObjectFactory(componentManager));
74    } else {
75  0 log("The Restlet application is not an instance of XWikiRestletJaxRsApplication. Please check your web.xml");
76    }
77   
78  18 return application;
79    }
80   
 
81  18 toggle @Override
82    public void init() throws ServletException
83    {
84  18 super.init();
85   
86  18 try {
87    /* Try first in WEB-INF */
88  18 InputStream is =
89    getServletContext().getResourceAsStream(String.format("/WEB-INF/%s", JAVA_LOGGING_PROPERTY_FILE));
90   
91    /* If nothing is there then try in the current jar */
92  18 if (is == null) {
93  18 is = getClass().getClassLoader().getResourceAsStream(JAVA_LOGGING_PROPERTY_FILE);
94    }
95   
96  18 if (is != null) {
97  18 try {
98  18 LogManager.getLogManager().readConfiguration(is);
99    } finally {
100  18 IOUtils.closeQuietly(is);
101    }
102    }
103    } catch (Exception e) {
104  0 log("Unable to initialize Java logging framework. Using defaults", e);
105    }
106    }
107   
108    /**
109    * Finds the correct Component Manager to use to find REST Resource components. This is important so that
110    * components registered in a children Component Manager are found (for example a REST Resource Component added
111    * in a subwiki).
112    *
113    * @param context the RESTlet context
114    * @return the Context Component Manager or if it doesn't exist the Root Component Manager
115    */
 
116  18 toggle private ComponentManager getComponentManager(Context context)
117    {
118  18 ComponentManager result =
119    (ComponentManager) getServletContext().getAttribute("org.xwiki.component.manager.ComponentManager");
120  18 try {
121  18 result = result.getInstance(ComponentManager.class, "context");
122    } catch (ComponentLookupException e) {
123    // Return the root CM since there's no Context CM!
124    }
125  18 return result;
126    }
127    }