View Javadoc
1   /**
2    * This file Copyright (c) 2013-2018 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.setup;
35  
36  import static info.magnolia.repository.RepositoryConstants.CONFIG;
37  
38  import info.magnolia.cms.security.Permission;
39  import info.magnolia.module.DefaultModuleVersionHandler;
40  import info.magnolia.module.InstallContext;
41  import info.magnolia.module.delta.AddPermissionTask;
42  import info.magnolia.module.delta.CheckAndModifyPropertyValueTask;
43  import info.magnolia.module.delta.DeltaBuilder;
44  import info.magnolia.module.delta.RemoveNodesTask;
45  import info.magnolia.module.delta.RemovePermissionTask;
46  import info.magnolia.module.delta.RemovePropertyTask;
47  import info.magnolia.module.delta.RenameNodeTask;
48  import info.magnolia.module.delta.SetPropertyTask;
49  import info.magnolia.module.delta.Task;
50  import info.magnolia.module.delta.ValueOfPropertyDelegateTask;
51  import info.magnolia.repository.RepositoryConstants;
52  import info.magnolia.rest.RestDispatcherServlet;
53  import info.magnolia.rest.tools.SwaggerRestDispatcherServlet;
54  
55  import java.util.ArrayList;
56  import java.util.Arrays;
57  import java.util.List;
58  
59  /**
60   * Module version handler.
61   */
62  public class RestToolsModuleVersionHandler extends DefaultModuleVersionHandler {
63  
64      private final static String REST_DISPATCHER_SERVLET_PATH = "/server/filters/servlets/RestDispatcherServlet";
65      private final static String PATH_TO_REST_INTEGRATION_MODULE = "/modules/rest-integration";
66  
67      public RestToolsModuleVersionHandler() {
68          super();
69  
70          register(DeltaBuilder.update("1.2.0", "Update to 1.2.0")
71                  .addTask(new SetPropertyTask("Update apiDeclarationProvider class for new Swagger version",
72                          CONFIG, PATH_TO_REST_INTEGRATION_MODULE + "/config/additionalProviders/apiDeclarationProvider", "providerClass", "io.swagger.jaxrs.listing.ApiListingResource"))
73                  .addTask(new SetPropertyTask("Update resourceListingProvider class for new Swagger version",
74                          CONFIG, PATH_TO_REST_INTEGRATION_MODULE + "/config/additionalProviders/resourceListingProvider", "providerClass", "io.swagger.jaxrs.listing.SwaggerSerializers"))
75                  .addTask(new RenameNodeTask("Rename additional provider node 'apiDeclarationProvider' to 'apiListingResource'",
76                          CONFIG, PATH_TO_REST_INTEGRATION_MODULE + "/config/additionalProviders", "apiDeclarationProvider", "apiListingResource", true))
77                  .addTask(new RenameNodeTask("Rename additional provider node 'resourceListingProvider' to 'swaggerSerializers'",
78                          CONFIG, PATH_TO_REST_INTEGRATION_MODULE + "/config/additionalProviders", "resourceListingProvider", "swaggerSerializers", true))
79                  .addTask(new ValueOfPropertyDelegateTask("Update Swagger API version in configuration", "/modules/rest-tools/config", "apiVersion", "1.0.0", true,
80                          new SetPropertyTask(RepositoryConstants.CONFIG, "/modules/rest-tools/config", "apiVersion", "1.5.4")))
81                  .addTask(new RemovePermissionTask("Remove old swagger doc access in 'rest' role (/.rest/api-docs*)", "rest", "uri", "/.rest/api-docs*", Permission.ALL))
82                  .addTask(new AddPermissionTask("Enable swagger doc access in 'rest' role (/.rest/swagger*)", "rest", "uri", "/.rest/swagger*", Permission.ALL, false))
83                  .addTask(new SetPropertyTask(RepositoryConstants.CONFIG, "/modules/rest-tools/apps/restTools/subApps/apiDocs", "url", "/.resources/rest-tools/webresources/swagger-ui/index.html")));
84  
85          register(DeltaBuilder.update("2.2.7", "Update to 2.2.7")
86                  .addTask(new RemoveNodesTask("Remove obsolete additionalProviders", CONFIG, Arrays.asList(
87                          PATH_TO_REST_INTEGRATION_MODULE + "/config/additionalProviders/apiListingResource",
88                          PATH_TO_REST_INTEGRATION_MODULE + "/config/additionalProviders/swaggerSerializers"), false))
89                  .addTask(new RemovePropertyTask("Remove obsolete apiVersion", "/modules/rest-tools/config", "apiVersion")));
90      }
91  
92      @Override
93      protected List<Task> getExtraInstallTasks(InstallContext installContext) {
94          List<Task> list = new ArrayList<Task>();
95  
96          list.add(new CheckAndModifyPropertyValueTask("Replace dispatcher servlet", "Replace default dispatcher servlet with API-aware dispatcher servlet", RepositoryConstants.CONFIG,
97                  REST_DISPATCHER_SERVLET_PATH, "servletClass", RestDispatcherServlet.class.getName(), SwaggerRestDispatcherServlet.class.getName()));
98  
99          // Allow web access to '/.rest/api-docs' in role 'rest'
100         list.add(new AddPermissionTask("Update 'rest-editor' role", "Allows access to the REST API documentation interface residing under '/.rest/swagger' in role 'rest-editor'.", "rest-editor", "uri", "/.rest/swagger*", Permission.ALL, false));
101 
102         return list;
103     }
104 }