Clover icon

Magnolia REST Integration 1.1

  1. Project Clover database Thu Aug 13 2015 19:08:49 CEST
  2. Package info.magnolia.rest

File RestDispatcherServlet.java

 

Coverage histogram

../../../img/srcFileCovDistChart0.png
60% of files have more coverage

Code metrics

4
38
13
1
189
122
15
0.39
2.92
13
1.15
9.8% of code in this file is excluded from these metrics.

Classes

Class Line # Actions
RestDispatcherServlet 67 38 9.8% 15 55
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /**
2    * This file Copyright (c) 2012-2015 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 final Logger log = LoggerFactory.getLogger(getClass());
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<String, Object>();
77    private Application application = new Application() {
78   
 
79    toggle @Override
80    public Set<Object> getSingletons() {
81    HashSet<Object> singletons = new HashSet<Object>();
82    singletons.addAll(endpoints.values());
83    return singletons;
84    }
85    };
86   
 
87  0 toggle @Inject
88    public RestDispatcherServlet(final RestIntegrationModule restIntegrationModule, EndpointDefinitionRegistry endpointDefinitionRegistry, @Named(SystemEventBus.NAME) EventBus systemEventBus) {
89  0 this.restIntegrationModule = restIntegrationModule;
90  0 this.systemEventBus = systemEventBus;
91  0 this.endpointDefinitionRegistry = endpointDefinitionRegistry;
92    }
93   
 
94  0 toggle @Override
95    public void init(ServletConfig servletConfig) throws ServletException {
96  0 this.servletConfig = servletConfig;
97   
98    // Initialise the ServletContainerDispatcher
99  0 servletContainerDispatcher = new ServletContainerDispatcher();
100  0 ConfigurationBootstrap bootstrap = createBootstrap(servletConfig);
101  0 servletContainerDispatcher.init(servletConfig.getServletContext(), bootstrap, this, this);
102  0 servletContainerDispatcher.getDispatcher().getDefaultContextObjects().put(ServletConfig.class, servletConfig);
103   
104    // Register additional providers that have been configured
105  0 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  0 for (EndpointDefinition endpointDefinition : endpointDefinitionRegistry.getAllEndpointDefinitions()) {
112  0 registerEndpoint(endpointDefinition);
113    }
114   
115    // Listen for changes to the registry to observe endpoints being added or removed
116  0 registerHandler = systemEventBus.addHandler(EndpointDefinitionRegistryEvent.class, this);
117    }
118   
 
119  0 toggle @Override
120    public void destroy() {
121  0 registerHandler.removeHandler();
122  0 super.destroy();
123  0 endpoints.clear();
124    }
125   
 
126  0 toggle @Override
127    public void onEndpointRegistered(EndpointDefinitionRegistryEvent event) {
128  0 registerEndpoint(event.getEndpointDefinition());
129    }
130   
 
131  0 toggle @Override
132    public void onEndpointReregistered(EndpointDefinitionRegistryEvent event) {
133  0 unregisterEndpoint(event.getEndpointName());
134  0 registerEndpoint(event.getEndpointDefinition());
135    }
136   
 
137  0 toggle @Override
138    public void onEndpointUnregistered(EndpointDefinitionRegistryEvent event) {
139  0 unregisterEndpoint(event.getEndpointName());
140    }
141   
142    // need to override this because we're not calling init(ServletConfig) on the super class
 
143    toggle @Override
144    public ServletConfig getServletConfig() {
145    return servletConfig;
146    }
147   
 
148  0 toggle protected Object registerEndpoint(EndpointDefinition endpointDefinition) {
149  0 if (!endpointDefinition.isEnabled()) {
150  0 return null;
151    }
152  0 Object endpoint = instantiateEndpoint(endpointDefinition);
153  0 endpoints.put(endpointDefinition.getName(), endpoint);
154  0 super.getDispatcher().getRegistry().addSingletonResource(endpoint);
155  0 return endpoint;
156    }
157   
 
158  0 toggle protected void unregisterEndpoint(String endpointName) {
159  0 Object endpoint = endpoints.remove(endpointName);
160  0 if (endpoint != null) {
161  0 Class<?> implementationClass = endpoint.getClass();
162  0 super.getDispatcher().getRegistry().removeRegistrations(implementationClass);
163    }
164    }
165   
 
166  0 toggle protected Object instantiateEndpoint(EndpointDefinition endpointDefinition) {
167  0 return Components.newInstance(endpointDefinition.getImplementationClass(), endpointDefinition);
168    }
169   
 
170  0 toggle protected Application getApplication() {
171  0 return application;
172    }
173   
 
174  0 toggle protected ConfigurationBootstrap createBootstrap(ServletConfig servletConfig) {
175  0 return new ServletBootstrap(servletConfig) {
176   
 
177  0 toggle @Override
178    public ResteasyDeployment createDeployment() {
179  0 return configureDeployment(super.createDeployment());
180    }
181    };
182    }
183   
 
184  0 toggle protected ResteasyDeployment configureDeployment(ResteasyDeployment deployment) {
185  0 deployment.setApplicationClass(null);
186  0 deployment.setApplication(getApplication());
187  0 return deployment;
188    }
189    }