Clover icon

Magnolia REST Tools 1.1

  1. Project Clover database Thu Aug 13 2015 19:12:59 CEST
  2. Package info.magnolia.rest.tools

File SwaggerRestDispatcherServlet.java

 

Coverage histogram

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

Code metrics

2
22
6
1
120
65
7
0.32
3.67
6
1.17

Classes

Class Line # Actions
SwaggerRestDispatcherServlet 62 22 0% 7 30
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /**
2    * This file Copyright (c) 2013-2015 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.event.EventBus;
38    import info.magnolia.event.SystemEventBus;
39    import info.magnolia.rest.RestDispatcherServlet;
40    import info.magnolia.rest.RestIntegrationModule;
41    import info.magnolia.rest.registry.ConfiguredEndpointDefinition;
42    import info.magnolia.rest.registry.EndpointDefinitionRegistry;
43    import info.magnolia.rest.registry.EndpointDefinitionRegistryEvent;
44   
45    import java.util.HashMap;
46   
47    import javax.inject.Inject;
48    import javax.inject.Named;
49    import javax.servlet.ServletConfig;
50    import javax.servlet.ServletException;
51   
52    import com.wordnik.swagger.jaxrs.config.DefaultJaxrsConfig;
53    import com.wordnik.swagger.jaxrs.listing.ApiListingResource;
54    import com.wordnik.swagger.jaxrs.listing.ApiListingResourceJSON;
55   
56    /**
57    * API-aware dispatcher of the rest requests to the dynamically registered endpoints.
58    *
59    * Additional providers needed for the API documentation are loaded in {@link info.magnolia.rest.RestDispatcherServlet}.
60    * More providers can be loaded by adding them to the configuration of {@link info.magnolia.rest.RestIntegrationModule}.
61    */
 
62    public class SwaggerRestDispatcherServlet extends RestDispatcherServlet {
63   
64    private final static String PARAMETER_API_VERSION = "api.version";
65    private final static String PARAMETER_API_BASEPATH = "swagger.api.basepath";
66   
67    private final RestToolsModule restToolsModule;
68    private ApiListingResource apiListingResource;
69    private DefaultJaxrsConfig config;
70   
 
71  0 toggle @Inject
72    public SwaggerRestDispatcherServlet(final RestIntegrationModule restIntegrationModule, final EndpointDefinitionRegistry endpointDefinitionRegistry, @Named(SystemEventBus.NAME) EventBus systemEventBus, final RestToolsModule restToolsModule) {
73  0 super(restIntegrationModule, endpointDefinitionRegistry, systemEventBus);
74  0 this.restToolsModule = restToolsModule;
75    }
76   
 
77  0 toggle @Override
78    public void init(ServletConfig servletConfig) throws ServletException {
79  0 super.init(servletConfig);
80   
81    // Register API listing resource
82  0 ConfiguredEndpointDefinition endpointDefinition = new ConfiguredEndpointDefinition();
83  0 endpointDefinition.setImplementationClass(ApiListingResourceJSON.class);
84  0 endpointDefinition.setName("api-listing-resource-json");
85  0 this.apiListingResource = (ApiListingResourceJSON) super.registerEndpoint(endpointDefinition);
86   
87  0 config = new DefaultJaxrsConfig();
88  0 HashMap<String, String> initParameters = new HashMap<String, String>();
89  0 initParameters.put(PARAMETER_API_VERSION, restToolsModule.getApiVersion());
90  0 initParameters.put(PARAMETER_API_BASEPATH, restToolsModule.getApiBasepath());
91  0 config.init(new CustomServletConfig("DefaultJaxrsConfig", super.getServletContext(), initParameters));
92    }
93   
 
94  0 toggle @Override
95    public void destroy() {
96  0 unregisterEndpoint("api-listing-resource-json");
97  0 super.destroy();
98  0 if (config != null) {
99  0 config.destroy();
100    }
101    }
102   
 
103  0 toggle @Override
104    public void onEndpointRegistered(EndpointDefinitionRegistryEvent event) {
105  0 super.onEndpointRegistered(event);
106  0 apiListingResource.invalidateCache();
107    }
108   
 
109  0 toggle @Override
110    public void onEndpointReregistered(EndpointDefinitionRegistryEvent event) {
111  0 super.onEndpointReregistered(event);
112  0 apiListingResource.invalidateCache();
113    }
114   
 
115  0 toggle @Override
116    public void onEndpointUnregistered(EndpointDefinitionRegistryEvent event) {
117  0 super.onEndpointUnregistered(event);
118  0 apiListingResource.invalidateCache();
119    }
120    }