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