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.config.registry.DefinitionProvider; |
37 |
|
import info.magnolia.event.EventBus; |
38 |
|
import info.magnolia.event.HandlerRegistration; |
39 |
|
import info.magnolia.event.SystemEventBus; |
40 |
|
import info.magnolia.objectfactory.ComponentProvider; |
41 |
|
import info.magnolia.objectfactory.Components; |
42 |
|
import info.magnolia.rest.provider.AdditionalProviderDefinition; |
43 |
|
import info.magnolia.rest.registry.EndpointDefinitionRegistry; |
44 |
|
import info.magnolia.rest.registry.EndpointDefinitionRegistryEvent; |
45 |
|
import info.magnolia.rest.registry.EndpointDefinitionRegistryEventHandler; |
46 |
|
|
47 |
|
import java.util.HashSet; |
48 |
|
import java.util.Map; |
49 |
|
import java.util.Set; |
50 |
|
import java.util.concurrent.ConcurrentHashMap; |
51 |
|
|
52 |
|
import javax.inject.Inject; |
53 |
|
import javax.inject.Named; |
54 |
|
import javax.servlet.ServletConfig; |
55 |
|
import javax.servlet.ServletException; |
56 |
|
import javax.ws.rs.core.Application; |
57 |
|
|
58 |
|
import org.jboss.resteasy.plugins.server.servlet.ConfigurationBootstrap; |
59 |
|
import org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher; |
60 |
|
import org.jboss.resteasy.plugins.server.servlet.ServletBootstrap; |
61 |
|
import org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher; |
62 |
|
import org.jboss.resteasy.spi.ResteasyDeployment; |
63 |
|
import org.slf4j.Logger; |
64 |
|
import org.slf4j.LoggerFactory; |
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
|
|
| 64.2% |
Uncovered Elements: 29 (81) |
Complexity: 21 |
Complexity Density: 0.38 |
|
69 |
|
public class RestDispatcherServlet extends HttpServletDispatcher implements EndpointDefinitionRegistryEventHandler { |
70 |
|
|
71 |
|
private static final Logger log = LoggerFactory.getLogger(RestDispatcherServlet.class); |
72 |
|
|
73 |
|
private final RestIntegrationModule restIntegrationModule; |
74 |
|
private final EndpointDefinitionRegistry endpointRegistry; |
75 |
|
private final EventBus systemEventBus; |
76 |
|
private final Map<String, Object> endpoints = new ConcurrentHashMap<>(); |
77 |
|
private final ComponentProvider componentProvider; |
78 |
|
|
79 |
|
private Application application = new Application() { |
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
80 |
|
@Override... |
81 |
|
public Set<Object> getSingletons() { |
82 |
|
HashSet<Object> singletons = new HashSet<>(); |
83 |
|
singletons.addAll(endpoints.values()); |
84 |
|
return singletons; |
85 |
|
} |
86 |
|
}; |
87 |
|
private ServletConfig servletConfig; |
88 |
|
private HandlerRegistration registerHandler; |
89 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
90 |
3 |
@Inject... |
91 |
|
public RestDispatcherServlet(final RestIntegrationModule restIntegrationModule, EndpointDefinitionRegistry endpointRegistry, @Named(SystemEventBus.NAME) EventBus systemEventBus, ComponentProvider componentProvider) { |
92 |
3 |
this.restIntegrationModule = restIntegrationModule; |
93 |
3 |
this.endpointRegistry = endpointRegistry; |
94 |
3 |
this.systemEventBus = systemEventBus; |
95 |
3 |
this.componentProvider = componentProvider; |
96 |
|
} |
97 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
98 |
0 |
@Deprecated... |
99 |
|
public RestDispatcherServlet(final RestIntegrationModule restIntegrationModule, EndpointDefinitionRegistry endpointRegistry, @Named(SystemEventBus.NAME) EventBus systemEventBus) { |
100 |
0 |
this(restIntegrationModule, endpointRegistry, systemEventBus, Components.getComponentProvider()); |
101 |
|
} |
102 |
|
|
|
|
| 84.6% |
Uncovered Elements: 2 (13) |
Complexity: 2 |
Complexity Density: 0.15 |
|
103 |
2 |
@Override... |
104 |
|
public void init(ServletConfig servletConfig) throws ServletException { |
105 |
2 |
this.servletConfig = servletConfig; |
106 |
|
|
107 |
|
|
108 |
2 |
servletContainerDispatcher = new ServletContainerDispatcher(); |
109 |
2 |
ConfigurationBootstrap bootstrap = createBootstrap(servletConfig); |
110 |
2 |
servletContainerDispatcher.init(servletConfig.getServletContext(), bootstrap, this, this); |
111 |
2 |
servletContainerDispatcher.getDispatcher().getDefaultContextObjects().put(ServletConfig.class, servletConfig); |
112 |
|
|
113 |
|
|
114 |
2 |
for (AdditionalProviderDefinition provider : restIntegrationModule.getAdditionalProviders()) { |
115 |
0 |
log.debug("Registering additional provider [{}]", provider.getProviderClass()); |
116 |
0 |
super.getDispatcher().getProviderFactory().registerProvider(provider.getProviderClass()); |
117 |
|
} |
118 |
|
|
119 |
|
|
120 |
2 |
for (DefinitionProvider<EndpointDefinition> provider : endpointRegistry.getAllProviders()) { |
121 |
4 |
try { |
122 |
4 |
registerEndpoint(provider); |
123 |
|
} catch (Exception e) { |
124 |
1 |
log.error("Failed to register endpoint [{}]", provider.getMetadata().getReferenceId(), e); |
125 |
|
|
126 |
|
} |
127 |
|
} |
128 |
|
|
129 |
|
|
130 |
2 |
registerHandler = systemEventBus.addHandler(EndpointDefinitionRegistryEvent.class, this); |
131 |
|
} |
132 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
133 |
0 |
@Override... |
134 |
|
public void destroy() { |
135 |
0 |
registerHandler.removeHandler(); |
136 |
0 |
super.destroy(); |
137 |
0 |
endpoints.clear(); |
138 |
|
} |
139 |
|
|
140 |
|
|
141 |
|
|
|
|
| 83.3% |
Uncovered Elements: 1 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
142 |
1 |
@Override... |
143 |
|
public void onEndpointRegistered(EndpointDefinitionRegistryEvent event) { |
144 |
1 |
DefinitionProvider<EndpointDefinition> provider = event.getEndpointDefinitionProvider(); |
145 |
1 |
if (endpoints.containsKey(provider.getMetadata().getName())) { |
146 |
1 |
unregisterEndpoint(provider.getMetadata().getReferenceId()); |
147 |
|
} |
148 |
1 |
registerEndpoint(provider); |
149 |
|
} |
150 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
151 |
0 |
@Override... |
152 |
|
public void onEndpointReregistered(EndpointDefinitionRegistryEvent event) { |
153 |
0 |
DefinitionProvider<EndpointDefinition> provider = event.getEndpointDefinitionProvider(); |
154 |
0 |
unregisterEndpoint(provider.getMetadata().getReferenceId()); |
155 |
0 |
registerEndpoint(provider); |
156 |
|
} |
157 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
158 |
0 |
@Override... |
159 |
|
public void onEndpointUnregistered(EndpointDefinitionRegistryEvent event) { |
160 |
0 |
unregisterEndpoint(event.getEndpointName()); |
161 |
|
} |
162 |
|
|
163 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
164 |
|
@Override... |
165 |
|
public ServletConfig getServletConfig() { |
166 |
|
return servletConfig; |
167 |
|
} |
168 |
|
|
169 |
|
|
170 |
|
|
171 |
|
@link |
172 |
|
|
|
|
| 0% |
Uncovered Elements: 8 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
173 |
0 |
protected Object registerEndpoint(EndpointDefinition endpointDefinition) {... |
174 |
0 |
if (!endpointDefinition.isEnabled()) { |
175 |
0 |
return null; |
176 |
|
} |
177 |
0 |
Object endpoint = instantiateEndpoint(endpointDefinition); |
178 |
0 |
endpoints.put(endpointDefinition.getName(), endpoint); |
179 |
0 |
super.getDispatcher().getRegistry().addSingletonResource(endpoint); |
180 |
0 |
return endpoint; |
181 |
|
} |
182 |
|
|
|
|
| 71.4% |
Uncovered Elements: 4 (14) |
Complexity: 3 |
Complexity Density: 0.3 |
|
183 |
5 |
protected Object registerEndpoint(DefinitionProvider<EndpointDefinition> provider) {... |
184 |
5 |
if (provider.isValid()) { |
185 |
5 |
EndpointDefinition endpointDefinition = provider.get(); |
186 |
5 |
String endpointReferenceId = provider.getMetadata().getReferenceId(); |
187 |
|
|
188 |
5 |
if (endpointDefinition.isEnabled()) { |
189 |
5 |
Object endpoint = instantiateEndpoint(endpointDefinition); |
190 |
5 |
endpoints.put(endpointReferenceId, endpoint); |
191 |
4 |
super.getDispatcher().getRegistry().addSingletonResource(endpoint); |
192 |
4 |
return endpoint; |
193 |
|
|
194 |
|
} else { |
195 |
0 |
log.info("Endpoint %s is disabled, skipping registration", endpointReferenceId); |
196 |
|
} |
197 |
|
} |
198 |
|
|
199 |
0 |
return null; |
200 |
|
} |
201 |
|
|
|
|
| 83.3% |
Uncovered Elements: 1 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
202 |
1 |
protected void unregisterEndpoint(String endpointReferenceId) {... |
203 |
1 |
Object endpoint = endpoints.remove(endpointReferenceId); |
204 |
1 |
if (endpoint != null) { |
205 |
1 |
Class<?> endpointClass = endpoint.getClass(); |
206 |
1 |
super.getDispatcher().getRegistry().removeRegistrations(endpointClass); |
207 |
|
} |
208 |
|
} |
209 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
210 |
5 |
protected Object instantiateEndpoint(EndpointDefinition endpointDefinition) {... |
211 |
5 |
return componentProvider.newInstance(endpointDefinition.getImplementationClass(), endpointDefinition); |
212 |
|
} |
213 |
|
|
214 |
|
|
215 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
216 |
5 |
protected Application getApplication() {... |
217 |
5 |
return application; |
218 |
|
} |
219 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
220 |
2 |
protected ConfigurationBootstrap createBootstrap(ServletConfig servletConfig) {... |
221 |
2 |
return new ServletBootstrap(servletConfig) { |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
222 |
2 |
@Override... |
223 |
|
public ResteasyDeployment createDeployment() { |
224 |
2 |
return configureDeployment(super.createDeployment()); |
225 |
|
} |
226 |
|
}; |
227 |
|
} |
228 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
229 |
2 |
protected ResteasyDeployment configureDeployment(ResteasyDeployment deployment) {... |
230 |
2 |
deployment.setApplicationClass(null); |
231 |
2 |
deployment.setApplication(getApplication()); |
232 |
2 |
return deployment; |
233 |
|
} |
234 |
|
} |