Clover icon

Magnolia REST Integration 1.2.2

  1. Project Clover database Thu Jun 29 2017 13:57:28 CEST
  2. Package info.magnolia.rest

File RestDispatcherServlet.java

 

Coverage histogram

../../../img/srcFileCovDistChart7.png
40% of files have more coverage

Code metrics

4
40
13
1
194
126
16
0.4
3.08
13
1.23
9.5% of code in this file is excluded from these metrics.

Classes

Class Line # Actions
RestDispatcherServlet 67 40 9.5% 16 22
0.6140350761.4%
 

Contributing tests

This file is covered by 1 test. .

Source view

1    /**
2    * This file Copyright (c) 2012-2017 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.rest;
35   
36    import info.magnolia.event.EventBus;
37    import info.magnolia.event.HandlerRegistration;
38    import info.magnolia.event.SystemEventBus;
39    import info.magnolia.objectfactory.Components;
40    import info.magnolia.rest.provider.AdditionalProviderDefinition;
41    import info.magnolia.rest.registry.EndpointDefinitionRegistry;
42    import info.magnolia.rest.registry.EndpointDefinitionRegistryEvent;
43    import info.magnolia.rest.registry.EndpointDefinitionRegistryEventHandler;
44   
45    import java.util.HashSet;
46    import java.util.Map;
47    import java.util.Set;
48    import java.util.concurrent.ConcurrentHashMap;
49   
50    import javax.inject.Inject;
51    import javax.inject.Named;
52    import javax.servlet.ServletConfig;
53    import javax.servlet.ServletException;
54    import javax.ws.rs.core.Application;
55   
56    import org.jboss.resteasy.plugins.server.servlet.ConfigurationBootstrap;
57    import org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher;
58    import org.jboss.resteasy.plugins.server.servlet.ServletBootstrap;
59    import org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher;
60    import org.jboss.resteasy.spi.ResteasyDeployment;
61    import org.slf4j.Logger;
62    import org.slf4j.LoggerFactory;
63   
64    /**
65    * Dispatcher of the rest requests to the dynamically registered endpoints.
66    */
 
67    public class RestDispatcherServlet extends HttpServletDispatcher implements EndpointDefinitionRegistryEventHandler {
68   
69    private static final Logger log = LoggerFactory.getLogger(RestDispatcherServlet.class);
70   
71    private ServletConfig servletConfig;
72    private final RestIntegrationModule restIntegrationModule;
73    private final EventBus systemEventBus;
74    private HandlerRegistration registerHandler;
75    private EndpointDefinitionRegistry endpointDefinitionRegistry;
76    private final Map<String, Object> endpoints = new ConcurrentHashMap<>();
77    private Application application = new Application() {
78   
 
79    toggle @Override
80    public Set<Object> getSingletons() {
81    Set<Object> singletons = new HashSet<>();
82    singletons.addAll(endpoints.values());
83    return singletons;
84    }
85    };
86   
 
87  1 toggle @Inject
88    public RestDispatcherServlet(final RestIntegrationModule restIntegrationModule, EndpointDefinitionRegistry endpointDefinitionRegistry, @Named(SystemEventBus.NAME) EventBus systemEventBus) {
89  1 this.restIntegrationModule = restIntegrationModule;
90  1 this.systemEventBus = systemEventBus;
91  1 this.endpointDefinitionRegistry = endpointDefinitionRegistry;
92    }
93   
 
94  1 toggle @Override
95    public void init(ServletConfig servletConfig) throws ServletException {
96  1 this.servletConfig = servletConfig;
97   
98    // Initialise the ServletContainerDispatcher
99  1 servletContainerDispatcher = new ServletContainerDispatcher();
100  1 ConfigurationBootstrap bootstrap = createBootstrap(servletConfig);
101  1 servletContainerDispatcher.init(servletConfig.getServletContext(), bootstrap, this, this);
102  1 servletContainerDispatcher.getDispatcher().getDefaultContextObjects().put(ServletConfig.class, servletConfig);
103   
104    // Register additional providers that have been configured
105  1 for (AdditionalProviderDefinition provider : restIntegrationModule.getAdditionalProviders()) {
106  0 log.debug("Registering additional provider [{}]", provider.getProviderClass());
107  0 super.getDispatcher().getProviderFactory().registerProvider(provider.getProviderClass());
108    }
109   
110    // Register all currently registered endpoints
111  1 for (EndpointDefinition endpointDefinition : endpointDefinitionRegistry.getAllEndpointDefinitions()) {
112  3 try {
113  3 registerEndpoint(endpointDefinition);
114    } catch (Exception e) {
115  1 log.error("Failed to register endpoint [{}]", endpointDefinition.getName(), e);
116    // Others should continue to be registered.
117    }
118    }
119   
120    // Listen for changes to the registry to observe endpoints being added or removed
121  1 registerHandler = systemEventBus.addHandler(EndpointDefinitionRegistryEvent.class, this);
122    }
123   
 
124  0 toggle @Override
125    public void destroy() {
126  0 registerHandler.removeHandler();
127  0 super.destroy();
128  0 endpoints.clear();
129    }
130   
 
131  0 toggle @Override
132    public void onEndpointRegistered(EndpointDefinitionRegistryEvent event) {
133  0 registerEndpoint(event.getEndpointDefinition());
134    }
135   
 
136  0 toggle @Override
137    public void onEndpointReregistered(EndpointDefinitionRegistryEvent event) {
138  0 unregisterEndpoint(event.getEndpointName());
139  0 registerEndpoint(event.getEndpointDefinition());
140    }
141   
 
142  0 toggle @Override
143    public void onEndpointUnregistered(EndpointDefinitionRegistryEvent event) {
144  0 unregisterEndpoint(event.getEndpointName());
145    }
146   
147    // need to override this because we're not calling init(ServletConfig) on the super class
 
148    toggle @Override
149    public ServletConfig getServletConfig() {
150    return servletConfig;
151    }
152   
 
153  3 toggle protected Object registerEndpoint(EndpointDefinition endpointDefinition) {
154  3 if (!endpointDefinition.isEnabled()) {
155  0 return null;
156    }
157  3 Object endpoint = instantiateEndpoint(endpointDefinition);
158  3 endpoints.put(endpointDefinition.getName(), endpoint);
159  2 super.getDispatcher().getRegistry().addSingletonResource(endpoint);
160  2 return endpoint;
161    }
162   
 
163  0 toggle protected void unregisterEndpoint(String endpointName) {
164  0 Object endpoint = endpoints.remove(endpointName);
165  0 if (endpoint != null) {
166  0 Class<?> implementationClass = endpoint.getClass();
167  0 super.getDispatcher().getRegistry().removeRegistrations(implementationClass);
168    }
169    }
170   
 
171  3 toggle protected Object instantiateEndpoint(EndpointDefinition endpointDefinition) {
172  3 return Components.newInstance(endpointDefinition.getImplementationClass(), endpointDefinition);
173    }
174   
 
175  2 toggle protected Application getApplication() {
176  2 return application;
177    }
178   
 
179  1 toggle protected ConfigurationBootstrap createBootstrap(ServletConfig servletConfig) {
180  1 return new ServletBootstrap(servletConfig) {
181   
 
182  1 toggle @Override
183    public ResteasyDeployment createDeployment() {
184  1 return configureDeployment(super.createDeployment());
185    }
186    };
187    }
188   
 
189  1 toggle protected ResteasyDeployment configureDeployment(ResteasyDeployment deployment) {
190  1 deployment.setApplicationClass(null);
191  1 deployment.setApplication(getApplication());
192  1 return deployment;
193    }
194    }