1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
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 |
|
|
66 |
|
|
|
|
| 0% |
Uncovered Elements: 55 (55) |
Complexity: 15 |
Complexity Density: 0.39 |
|
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 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
79 |
|
@Override... |
80 |
|
public Set<Object> getSingletons() { |
81 |
|
HashSet<Object> singletons = new HashSet<Object>(); |
82 |
|
singletons.addAll(endpoints.values()); |
83 |
|
return singletons; |
84 |
|
} |
85 |
|
}; |
86 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
87 |
0 |
@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 |
|
|
|
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
|
94 |
0 |
@Override... |
95 |
|
public void init(ServletConfig servletConfig) throws ServletException { |
96 |
0 |
this.servletConfig = servletConfig; |
97 |
|
|
98 |
|
|
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 |
|
|
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 |
|
|
111 |
0 |
for (EndpointDefinition endpointDefinition : endpointDefinitionRegistry.getAllEndpointDefinitions()) { |
112 |
0 |
registerEndpoint(endpointDefinition); |
113 |
|
} |
114 |
|
|
115 |
|
|
116 |
0 |
registerHandler = systemEventBus.addHandler(EndpointDefinitionRegistryEvent.class, this); |
117 |
|
} |
118 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
119 |
0 |
@Override... |
120 |
|
public void destroy() { |
121 |
0 |
registerHandler.removeHandler(); |
122 |
0 |
super.destroy(); |
123 |
0 |
endpoints.clear(); |
124 |
|
} |
125 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
126 |
0 |
@Override... |
127 |
|
public void onEndpointRegistered(EndpointDefinitionRegistryEvent event) { |
128 |
0 |
registerEndpoint(event.getEndpointDefinition()); |
129 |
|
} |
130 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
131 |
0 |
@Override... |
132 |
|
public void onEndpointReregistered(EndpointDefinitionRegistryEvent event) { |
133 |
0 |
unregisterEndpoint(event.getEndpointName()); |
134 |
0 |
registerEndpoint(event.getEndpointDefinition()); |
135 |
|
} |
136 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
137 |
0 |
@Override... |
138 |
|
public void onEndpointUnregistered(EndpointDefinitionRegistryEvent event) { |
139 |
0 |
unregisterEndpoint(event.getEndpointName()); |
140 |
|
} |
141 |
|
|
142 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
143 |
|
@Override... |
144 |
|
public ServletConfig getServletConfig() { |
145 |
|
return servletConfig; |
146 |
|
} |
147 |
|
|
|
|
| 0% |
Uncovered Elements: 8 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
148 |
0 |
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 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
158 |
0 |
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 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
166 |
0 |
protected Object instantiateEndpoint(EndpointDefinition endpointDefinition) {... |
167 |
0 |
return Components.newInstance(endpointDefinition.getImplementationClass(), endpointDefinition); |
168 |
|
} |
169 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
170 |
0 |
protected Application getApplication() {... |
171 |
0 |
return application; |
172 |
|
} |
173 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
174 |
0 |
protected ConfigurationBootstrap createBootstrap(ServletConfig servletConfig) {... |
175 |
0 |
return new ServletBootstrap(servletConfig) { |
176 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
177 |
0 |
@Override... |
178 |
|
public ResteasyDeployment createDeployment() { |
179 |
0 |
return configureDeployment(super.createDeployment()); |
180 |
|
} |
181 |
|
}; |
182 |
|
} |
183 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
184 |
0 |
protected ResteasyDeployment configureDeployment(ResteasyDeployment deployment) {... |
185 |
0 |
deployment.setApplicationClass(null); |
186 |
0 |
deployment.setApplication(getApplication()); |
187 |
0 |
return deployment; |
188 |
|
} |
189 |
|
} |