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.tools; |
35 |
|
|
36 |
|
import info.magnolia.cms.util.CustomServletConfig; |
37 |
|
import info.magnolia.context.MgnlContext; |
38 |
|
import info.magnolia.event.EventBus; |
39 |
|
import info.magnolia.event.SystemEventBus; |
40 |
|
import info.magnolia.objectfactory.ComponentProvider; |
41 |
|
import info.magnolia.objectfactory.Components; |
42 |
|
import info.magnolia.rest.RestDispatcherServlet; |
43 |
|
import info.magnolia.rest.RestIntegrationModule; |
44 |
|
import info.magnolia.rest.registry.ConfiguredEndpointDefinition; |
45 |
|
import info.magnolia.rest.registry.EndpointDefinitionRegistry; |
46 |
|
import info.magnolia.rest.registry.EndpointDefinitionRegistryEvent; |
47 |
|
|
48 |
|
import java.io.IOException; |
49 |
|
import java.util.HashMap; |
50 |
|
|
51 |
|
import javax.inject.Inject; |
52 |
|
import javax.inject.Named; |
53 |
|
import javax.servlet.ServletConfig; |
54 |
|
import javax.servlet.ServletException; |
55 |
|
import javax.servlet.http.HttpServletRequest; |
56 |
|
import javax.servlet.http.HttpServletResponse; |
57 |
|
|
58 |
|
import io.swagger.jaxrs.config.DefaultJaxrsConfig; |
59 |
|
import io.swagger.jaxrs.listing.ApiListingResource; |
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
@link |
65 |
|
@link |
66 |
|
|
|
|
| 86.7% |
Uncovered Elements: 6 (45) |
Complexity: 12 |
Complexity Density: 0.39 |
|
67 |
|
public class SwaggerRestDispatcherServlet extends RestDispatcherServlet { |
68 |
|
|
69 |
|
private final static String PARAMETER_API_VERSION = "api.version"; |
70 |
|
private final static String PARAMETER_API_BASEPATH = "swagger.api.basepath"; |
71 |
|
private static final String ENDPOINT_NAME = "api-listing-resource-json"; |
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
static final String SWAGGER = "swagger"; |
76 |
|
|
77 |
|
private final RestToolsModule restToolsModule; |
78 |
|
private DefaultJaxrsConfig config; |
79 |
|
|
80 |
|
|
81 |
|
@link |
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
private boolean swaggerCacheIsStale = false; |
86 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
87 |
1 |
@Inject... |
88 |
|
public SwaggerRestDispatcherServlet(final RestIntegrationModule restIntegrationModule, final EndpointDefinitionRegistry endpointDefinitionRegistry, @Named(SystemEventBus.NAME) EventBus systemEventBus, final RestToolsModule restToolsModule, ComponentProvider componentProvider) { |
89 |
1 |
super(restIntegrationModule, endpointDefinitionRegistry, systemEventBus, componentProvider); |
90 |
1 |
this.restToolsModule = restToolsModule; |
91 |
|
} |
92 |
|
|
93 |
|
|
94 |
|
@deprecated@link |
95 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
96 |
0 |
@Deprecated... |
97 |
|
public SwaggerRestDispatcherServlet(final RestIntegrationModule restIntegrationModule, final EndpointDefinitionRegistry endpointDefinitionRegistry, @Named(SystemEventBus.NAME) EventBus systemEventBus, final RestToolsModule restToolsModule) { |
98 |
0 |
this(restIntegrationModule, endpointDefinitionRegistry, systemEventBus, restToolsModule, Components.getComponentProvider()); |
99 |
|
} |
100 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
|
101 |
1 |
@Override... |
102 |
|
public void init(ServletConfig servletConfig) throws ServletException { |
103 |
1 |
super.init(servletConfig); |
104 |
|
|
105 |
1 |
initApiListing(); |
106 |
|
|
107 |
1 |
config = new DefaultJaxrsConfig(); |
108 |
1 |
HashMap<String, String> initParameters = new HashMap<String, String>(); |
109 |
1 |
initParameters.put(PARAMETER_API_VERSION, restToolsModule.getApiVersion()); |
110 |
1 |
initParameters.put(PARAMETER_API_BASEPATH, restToolsModule.getApiBasepath()); |
111 |
1 |
config.init(new CustomServletConfig("DefaultJaxrsConfig", super.getServletContext(), initParameters)); |
112 |
|
} |
113 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
114 |
3 |
private void initApiListing() {... |
115 |
|
|
116 |
3 |
ConfiguredEndpointDefinition endpointDefinition = new ConfiguredEndpointDefinition(); |
117 |
3 |
endpointDefinition.setImplementationClass(ApiListingResource.class); |
118 |
3 |
endpointDefinition.setName(ENDPOINT_NAME); |
119 |
3 |
super.registerEndpoint(endpointDefinition); |
120 |
|
} |
121 |
|
|
122 |
|
|
123 |
|
|
124 |
|
|
125 |
|
@link |
126 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
127 |
2 |
private void refreshApiListing() {... |
128 |
2 |
unregisterEndpoint(ENDPOINT_NAME); |
129 |
2 |
initApiListing(); |
130 |
2 |
swaggerCacheIsStale = true; |
131 |
|
} |
132 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
133 |
3 |
@Override... |
134 |
|
protected void service(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException { |
135 |
|
|
136 |
3 |
if (swaggerCacheIsStale) { |
137 |
2 |
MgnlContext.getWebContext().getServletContext().removeAttribute(SWAGGER); |
138 |
2 |
swaggerCacheIsStale = false; |
139 |
|
} |
140 |
|
|
141 |
3 |
super.service(httpServletRequest, httpServletResponse); |
142 |
|
} |
143 |
|
|
|
|
| 83.3% |
Uncovered Elements: 1 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
144 |
1 |
@Override... |
145 |
|
public void destroy() { |
146 |
1 |
unregisterEndpoint(ENDPOINT_NAME); |
147 |
1 |
super.destroy(); |
148 |
1 |
if (config != null) { |
149 |
1 |
config.destroy(); |
150 |
|
} |
151 |
|
} |
152 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
153 |
1 |
@Override... |
154 |
|
public void onEndpointRegistered(EndpointDefinitionRegistryEvent event) { |
155 |
1 |
super.onEndpointRegistered(event); |
156 |
1 |
refreshApiListing(); |
157 |
|
} |
158 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
159 |
1 |
@Override... |
160 |
|
public void onEndpointReregistered(EndpointDefinitionRegistryEvent event) { |
161 |
1 |
super.onEndpointReregistered(event); |
162 |
1 |
refreshApiListing(); |
163 |
|
} |
164 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
165 |
0 |
@Override... |
166 |
|
public void onEndpointUnregistered(EndpointDefinitionRegistryEvent event) { |
167 |
0 |
super.onEndpointUnregistered(event); |
168 |
0 |
refreshApiListing(); |
169 |
|
} |
170 |
|
} |