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.io.File; |
48 |
|
import java.util.Arrays; |
49 |
|
import java.util.HashSet; |
50 |
|
import java.util.Map; |
51 |
|
import java.util.Set; |
52 |
|
import java.util.concurrent.ConcurrentHashMap; |
53 |
|
import java.util.stream.Collectors; |
54 |
|
|
55 |
|
import javax.inject.Inject; |
56 |
|
import javax.inject.Named; |
57 |
|
import javax.servlet.ServletConfig; |
58 |
|
import javax.servlet.ServletException; |
59 |
|
import javax.ws.rs.core.Application; |
60 |
|
|
61 |
|
import org.apache.commons.lang3.StringUtils; |
62 |
|
import org.jboss.resteasy.plugins.server.servlet.ConfigurationBootstrap; |
63 |
|
import org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher; |
64 |
|
import org.jboss.resteasy.plugins.server.servlet.ServletBootstrap; |
65 |
|
import org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher; |
66 |
|
import org.jboss.resteasy.spi.ResteasyDeployment; |
67 |
|
import org.slf4j.Logger; |
68 |
|
import org.slf4j.LoggerFactory; |
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
|
|
| 67.8% |
Uncovered Elements: 37 (115) |
Complexity: 29 |
Complexity Density: 0.37 |
|
73 |
|
public class RestDispatcherServlet extends HttpServletDispatcher implements EndpointDefinitionRegistryEventHandler { |
74 |
|
|
75 |
|
private static final Logger log = LoggerFactory.getLogger(RestDispatcherServlet.class); |
76 |
|
|
77 |
|
private final RestIntegrationModule restIntegrationModule; |
78 |
|
private final EndpointDefinitionRegistry endpointRegistry; |
79 |
|
private final EventBus systemEventBus; |
80 |
|
private final Map<String, Object> endpoints = new ConcurrentHashMap<>(); |
81 |
|
private final ComponentProvider componentProvider; |
82 |
|
|
83 |
|
private Application application = new Application() { |
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
84 |
|
@Override... |
85 |
|
public Set<Object> getSingletons() { |
86 |
|
HashSet<Object> singletons = new HashSet<>(); |
87 |
|
singletons.addAll(endpoints.values()); |
88 |
|
return singletons; |
89 |
|
} |
90 |
|
}; |
91 |
|
private ServletConfig servletConfig; |
92 |
|
private HandlerRegistration registerHandler; |
93 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
94 |
5 |
@Inject... |
95 |
|
public RestDispatcherServlet(final RestIntegrationModule restIntegrationModule, EndpointDefinitionRegistry endpointRegistry, @Named(SystemEventBus.NAME) EventBus systemEventBus, ComponentProvider componentProvider) { |
96 |
5 |
this.restIntegrationModule = restIntegrationModule; |
97 |
5 |
this.endpointRegistry = endpointRegistry; |
98 |
5 |
this.systemEventBus = systemEventBus; |
99 |
5 |
this.componentProvider = componentProvider; |
100 |
|
} |
101 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
102 |
0 |
@Deprecated... |
103 |
|
public RestDispatcherServlet(final RestIntegrationModule restIntegrationModule, EndpointDefinitionRegistry endpointRegistry, @Named(SystemEventBus.NAME) EventBus systemEventBus) { |
104 |
0 |
this(restIntegrationModule, endpointRegistry, systemEventBus, Components.getComponentProvider()); |
105 |
|
} |
106 |
|
|
|
|
| 84.6% |
Uncovered Elements: 2 (13) |
Complexity: 2 |
Complexity Density: 0.15 |
|
107 |
4 |
@Override... |
108 |
|
public void init(ServletConfig servletConfig) throws ServletException { |
109 |
4 |
this.servletConfig = servletConfig; |
110 |
|
|
111 |
|
|
112 |
4 |
servletContainerDispatcher = new ServletContainerDispatcher(); |
113 |
4 |
ConfigurationBootstrap bootstrap = createBootstrap(servletConfig); |
114 |
4 |
servletContainerDispatcher.init(servletConfig.getServletContext(), bootstrap, this, this); |
115 |
4 |
servletContainerDispatcher.getDispatcher().getDefaultContextObjects().put(ServletConfig.class, servletConfig); |
116 |
|
|
117 |
|
|
118 |
4 |
for (AdditionalProviderDefinition provider : restIntegrationModule.getAdditionalProviders()) { |
119 |
0 |
log.debug("Registering additional provider [{}]", provider.getProviderClass()); |
120 |
0 |
super.getDispatcher().getProviderFactory().registerProvider(provider.getProviderClass()); |
121 |
|
} |
122 |
|
|
123 |
|
|
124 |
4 |
for (DefinitionProvider<EndpointDefinition> provider : endpointRegistry.getAllProviders()) { |
125 |
10 |
try { |
126 |
10 |
registerEndpoint(provider); |
127 |
|
} catch (Exception e) { |
128 |
3 |
log.error("Failed to register endpoint [{}]", provider.getMetadata().getReferenceId(), e); |
129 |
|
|
130 |
|
} |
131 |
|
} |
132 |
|
|
133 |
|
|
134 |
4 |
registerHandler = systemEventBus.addHandler(EndpointDefinitionRegistryEvent.class, this); |
135 |
|
} |
136 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
137 |
0 |
@Override... |
138 |
|
public void destroy() { |
139 |
0 |
registerHandler.removeHandler(); |
140 |
0 |
super.destroy(); |
141 |
0 |
endpoints.clear(); |
142 |
|
} |
143 |
|
|
144 |
|
|
145 |
|
|
|
|
| 85.7% |
Uncovered Elements: 1 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
146 |
1 |
@Override... |
147 |
|
public void onEndpointRegistered(EndpointDefinitionRegistryEvent event) { |
148 |
1 |
DefinitionProvider<EndpointDefinition> provider = event.getEndpointDefinitionProvider(); |
149 |
1 |
final String referenceId = provider.getMetadata().getReferenceId(); |
150 |
1 |
if (endpoints.containsKey(referenceId)) { |
151 |
1 |
unregisterEndpoint(referenceId); |
152 |
|
} |
153 |
1 |
registerEndpoint(provider); |
154 |
|
} |
155 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
156 |
0 |
@Override... |
157 |
|
public void onEndpointReregistered(EndpointDefinitionRegistryEvent event) { |
158 |
0 |
DefinitionProvider<EndpointDefinition> provider = event.getEndpointDefinitionProvider(); |
159 |
0 |
unregisterEndpoint(provider.getMetadata().getReferenceId()); |
160 |
0 |
registerEndpoint(provider); |
161 |
|
} |
162 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
163 |
0 |
@Override... |
164 |
|
public void onEndpointUnregistered(EndpointDefinitionRegistryEvent event) { |
165 |
0 |
unregisterEndpoint(event.getEndpointName()); |
166 |
|
} |
167 |
|
|
168 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
169 |
|
@Override... |
170 |
|
public ServletConfig getServletConfig() { |
171 |
|
return servletConfig; |
172 |
|
} |
173 |
|
|
174 |
|
|
175 |
|
|
176 |
|
@link |
177 |
|
|
|
|
| 0% |
Uncovered Elements: 8 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
178 |
0 |
protected Object registerEndpoint(EndpointDefinition endpointDefinition) {... |
179 |
0 |
if (!endpointDefinition.isEnabled()) { |
180 |
0 |
return null; |
181 |
|
} |
182 |
0 |
Object endpoint = instantiateEndpoint(endpointDefinition); |
183 |
0 |
endpoints.put(endpointDefinition.getName(), endpoint); |
184 |
0 |
super.getDispatcher().getRegistry().addSingletonResource(endpoint); |
185 |
0 |
return endpoint; |
186 |
|
} |
187 |
|
|
|
|
| 81.5% |
Uncovered Elements: 5 (27) |
Complexity: 5 |
Complexity Density: 0.26 |
|
188 |
14 |
protected Object registerEndpoint(DefinitionProvider<EndpointDefinition> provider) {... |
189 |
14 |
if (!provider.isValid()) { |
190 |
0 |
return null; |
191 |
|
} |
192 |
|
|
193 |
14 |
EndpointDefinition endpointDefinition = provider.get(); |
194 |
14 |
String endpointReferenceId = provider.getMetadata().getReferenceId(); |
195 |
|
|
196 |
14 |
if (!endpointDefinition.isEnabled()) { |
197 |
0 |
log.info("Endpoint {} is disabled, skipping registration", endpointReferenceId); |
198 |
0 |
return null; |
199 |
|
} |
200 |
|
|
201 |
14 |
Object endpoint = instantiateEndpoint(endpointDefinition); |
202 |
|
|
203 |
14 |
if (supportDynamicPath(endpointDefinition.getImplementationClass())) { |
204 |
3 |
String path; |
205 |
3 |
if (StringUtils.isNotEmpty(endpointDefinition.getEndpointPath())) { |
206 |
1 |
path = truncatePath(endpointDefinition.getEndpointPath()); |
207 |
|
} else { |
208 |
2 |
path = getBasePath(endpointReferenceId); |
209 |
|
} |
210 |
3 |
super.getDispatcher().getRegistry().addSingletonResource(endpoint, path); |
211 |
3 |
log.info("Endpoint {} is registered with base path: [context]/{}", endpointReferenceId, path); |
212 |
3 |
endpoints.put(endpointReferenceId, endpoint); |
213 |
|
} else { |
214 |
11 |
super.getDispatcher().getRegistry().addSingletonResource(endpoint); |
215 |
8 |
endpoints.put(endpointReferenceId, endpoint); |
216 |
|
} |
217 |
|
|
218 |
11 |
return endpoint; |
219 |
|
} |
220 |
|
|
|
|
| 50% |
Uncovered Elements: 8 (16) |
Complexity: 5 |
Complexity Density: 0.5 |
|
221 |
1 |
protected void unregisterEndpoint(String endpointReferenceId) {... |
222 |
1 |
Object endpoint = endpoints.remove(endpointReferenceId); |
223 |
1 |
if (endpoint != null) { |
224 |
1 |
Class<?> endpointClass = endpoint.getClass(); |
225 |
1 |
if (supportDynamicPath(endpointClass) && endpoint instanceof AbstractEndpoint) { |
226 |
0 |
String configuredPath = ((AbstractEndpoint) endpoint).getEndpointDefinition().getEndpointPath(); |
227 |
0 |
String path = StringUtils.isEmpty(configuredPath) ? getBasePath(endpointReferenceId) : truncatePath(configuredPath); |
228 |
0 |
super.getDispatcher().getRegistry().removeRegistrations(endpointClass, path); |
229 |
0 |
log.debug("Unregister endpoint {} with base path {} from registry.", endpointReferenceId, path); |
230 |
|
} else { |
231 |
1 |
super.getDispatcher().getRegistry().removeRegistrations(endpointClass); |
232 |
1 |
log.debug("Unregister endpoint which has reference id: {} from registry.", endpointReferenceId); |
233 |
|
} |
234 |
|
|
235 |
|
} |
236 |
|
} |
237 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
238 |
14 |
protected Object instantiateEndpoint(EndpointDefinition endpointDefinition) {... |
239 |
14 |
return componentProvider.newInstance(endpointDefinition.getImplementationClass(), endpointDefinition); |
240 |
|
} |
241 |
|
|
242 |
|
|
243 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
244 |
9 |
protected Application getApplication() {... |
245 |
9 |
return application; |
246 |
|
} |
247 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
248 |
4 |
protected ConfigurationBootstrap createBootstrap(ServletConfig servletConfig) {... |
249 |
4 |
return new ServletBootstrap(servletConfig) { |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
250 |
4 |
@Override... |
251 |
|
public ResteasyDeployment createDeployment() { |
252 |
4 |
return configureDeployment(super.createDeployment()); |
253 |
|
} |
254 |
|
}; |
255 |
|
} |
256 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
257 |
4 |
protected ResteasyDeployment configureDeployment(ResteasyDeployment deployment) {... |
258 |
4 |
deployment.setApplicationClass(null); |
259 |
4 |
deployment.setApplication(getApplication()); |
260 |
4 |
return deployment; |
261 |
|
} |
262 |
|
|
263 |
|
|
264 |
|
|
265 |
|
@linkplain |
266 |
|
|
267 |
|
|
268 |
|
|
269 |
|
|
270 |
|
|
271 |
|
|
272 |
|
@return |
273 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
274 |
2 |
protected String getBasePath(String endpointReferenceId) {... |
275 |
2 |
final String[] endpointPaths = StringUtils.split(endpointReferenceId, File.separator); |
276 |
2 |
endpointPaths[endpointPaths.length - 1] = endpointPaths[endpointPaths.length - 1].replace('_', '/'); |
277 |
2 |
return Arrays.stream(endpointPaths).collect(Collectors.joining("/")); |
278 |
|
} |
279 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
280 |
15 |
private boolean supportDynamicPath(final Class<?> implementationClass) {... |
281 |
15 |
return implementationClass != null && implementationClass.getAnnotation(DynamicPath.class) != null; |
282 |
|
} |
283 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
284 |
1 |
private String truncatePath(String configuredPath) {... |
285 |
1 |
String path = StringUtils.removeEnd(configuredPath, "/"); |
286 |
1 |
path = StringUtils.removeStart(path, "/"); |
287 |
1 |
return path; |
288 |
|
} |
289 |
|
} |