Clover icon

Magnolia REST Tools 1.2.2

  1. Project Clover database Thu Jun 29 2017 13:59:54 CEST
  2. Package info.magnolia.rest.tools

File SwaggerRestDispatcherServlet.java

 

Coverage histogram

../../../../img/srcFileCovDistChart9.png
66% of files have more coverage

Code metrics

4
30
9
1
160
86
11
0.37
3.33
9
1.22

Classes

Class Line # Actions
SwaggerRestDispatcherServlet 65 30 0% 11 4
0.9069767690.7%
 

Contributing tests

This file is covered by 1 test. .

Source view

1    /**
2    * This file Copyright (c) 2013-2017 Magnolia International
3    * Ltd. (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10    * This file is distributed in the hope that it will be
11    * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12    * implied warranty of MERCHANTABILITY or FITNESS FOR A
13    * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14    * Redistribution, except as permitted by whichever of the GPL
15    * or MNA you select, is prohibited.
16    *
17    * 1. For the GPL license (GPL), you can redistribute and/or
18    * modify this file under the terms of the GNU General
19    * Public License, Version 3, as published by the Free Software
20    * Foundation. You should have received a copy of the GNU
21    * General Public License, Version 3 along with this program;
22    * if not, write to the Free Software Foundation, Inc., 51
23    * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24    *
25    * 2. For the Magnolia Network Agreement (MNA), this file
26    * and the accompanying materials are made available under the
27    * terms of the MNA which accompanies this distribution, and
28    * is available at http://www.magnolia-cms.com/mna.html
29    *
30    * Any modifications to this file must keep this entire header
31    * intact.
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    * API-aware dispatcher of the rest requests to the dynamically registered endpoints.
61    *
62    * Additional providers needed for the API documentation are loaded in {@link info.magnolia.rest.RestDispatcherServlet}.
63    * More providers can be loaded by adding them to the configuration of {@link info.magnolia.rest.RestIntegrationModule}.
64    */
 
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    * Key of object in servlet context where swagger caches data.
72    */
73    static final String SWAGGER = "swagger";
74   
75    private final RestToolsModule restToolsModule;
76    private DefaultJaxrsConfig config;
77    /**
78    * Indicates that endpoint definitions have changed. Swagger caches some information in the servlet context
79    * (see {@link ApiListingResource}), which we can only access (and flush) during a request. However, since notifications
80    * about changed endpoint definitions arrive independently of requests, we use this flag to keep track of 'staleness'
81    * and flush on the next request we encounter.
82    */
83    private boolean swaggerCacheIsStale = false;
84   
 
85  1 toggle @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   
 
91  1 toggle @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   
 
104  3 toggle private void initApiListing() {
105    // Register API listing resource
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    * Refresh doc listings after changes in endpoint config.
114    * In particular, un- and re-register the doc endpoint to create new instances in order to get rid of internal cachings,
115    * like in {@link ApiListingResource}. Then set a flag to flush additional cached data in servlet context on next opportunity.
116    */
 
117  2 toggle private void refreshApiListing() {
118  2 unregisterEndpoint(ENDPOINT_NAME);
119  2 initApiListing();
120  2 swaggerCacheIsStale = true;
121    }
122   
 
123  3 toggle @Override
124    protected void service(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
125    // Swagger caches data in context -> hack it away. Sometimes you gotta turn your swag off.
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   
 
134  1 toggle @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   
 
143  1 toggle @Override
144    public void onEndpointRegistered(EndpointDefinitionRegistryEvent event) {
145  1 super.onEndpointRegistered(event);
146  1 refreshApiListing();
147    }
148   
 
149  1 toggle @Override
150    public void onEndpointReregistered(EndpointDefinitionRegistryEvent event) {
151  1 super.onEndpointReregistered(event);
152  1 refreshApiListing();
153    }
154   
 
155  0 toggle @Override
156    public void onEndpointUnregistered(EndpointDefinitionRegistryEvent event) {
157  0 super.onEndpointUnregistered(event);
158  0 refreshApiListing();
159    }
160    }