Code Optimization.
[sdc/sdc-workflow-designer.git] / sdc-workflow-designer-server / src / main / java / org / onap / sdc / workflowdesigner / WorkflowDesignerApp.java
1 /**
2  * Copyright (c) 2017-2018 ZTE Corporation.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the Apache License, Version 2.0
5  * and the Eclipse Public License v1.0 which both accompany this distribution,
6  * and are available at http://www.eclipse.org/legal/epl-v10.html
7  * and http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Contributors:
10  *     ZTE - initial API and implementation and/or initial documentation
11  */
12
13 package org.onap.sdc.workflowdesigner;
14
15 import org.glassfish.jersey.media.multipart.MultiPartFeature;
16 import org.onap.sdc.workflowdesigner.config.AppConfig;
17 import org.onap.sdc.workflowdesigner.resources.ExtendActivityResource;
18 import org.onap.sdc.workflowdesigner.resources.WorkflowModelerResource;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 import com.fasterxml.jackson.annotation.JsonInclude;
23
24 import io.dropwizard.Application;
25 import io.dropwizard.assets.AssetsBundle;
26 import io.dropwizard.server.SimpleServerFactory;
27 import io.dropwizard.setup.Bootstrap;
28 import io.dropwizard.setup.Environment;
29 import io.swagger.jaxrs.config.BeanConfig;
30 import io.swagger.jaxrs.listing.ApiListingResource;
31
32 public class WorkflowDesignerApp extends Application<WorkflowDesignerConfiguration> {
33   private static final Logger LOGGER = LoggerFactory.getLogger(WorkflowDesignerApp.class);
34
35   public static void main(String[] args) throws Exception {
36     new WorkflowDesignerApp().run(args);
37   }
38
39   @Override
40   public String getName() {
41     return "Workflow Designer";
42   }
43
44   @Override
45   public void initialize(Bootstrap<WorkflowDesignerConfiguration> bootstrap) {
46     bootstrap.addBundle(new AssetsBundle("/api-doc", "/api-doc", "index.html", "api-doc"));
47     bootstrap.addBundle(new AssetsBundle("/workflow-modeler", "/workflow-modeler", "index.html",
48         "workflow-modeler"));
49     bootstrap.addBundle(new AssetsBundle("/workflow-modeler", "/", "index.html", "ng"));
50   }
51
52   @Override
53   public void run(WorkflowDesignerConfiguration configuration, Environment environment) {
54     LOGGER.info("Start to initialize Workflow Designer.");
55
56     AppConfig.setSdcServiceProxy(configuration.getSdcServiceProxy());
57
58     environment.jersey().register(new WorkflowModelerResource());
59     environment.jersey().register(new ExtendActivityResource());
60
61     // register rest interface
62     environment.jersey().packages("org.onap.sdc.workflowdesigner.resources");
63     // upload file by inputstream need to register MultiPartFeature
64     environment.jersey().register(MultiPartFeature.class);
65
66     initSwaggerConfig(environment, configuration);
67
68     LOGGER.info("Initialize catalogue finished.");
69   }
70
71   /**
72    * initialize swagger configuration.
73    * 
74    * @param environment environment information
75    * @param configuration catalogue configuration
76    */
77   private void initSwaggerConfig(Environment environment,
78       WorkflowDesignerConfiguration configuration) {
79     environment.jersey().register(new ApiListingResource());
80     environment.getObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL);
81
82     BeanConfig config = new BeanConfig();
83     config.setTitle("Workflow Designer rest API");
84     config.setVersion("1.0.0");
85     config.setResourcePackage("org.onap.sdc.workflowdesigner.resources");
86
87     // set rest api basepath in swagger
88     SimpleServerFactory simpleServerFactory =
89         (SimpleServerFactory) configuration.getServerFactory();
90     String basePath = simpleServerFactory.getApplicationContextPath();
91     String rootPath = simpleServerFactory.getJerseyRootPath().get();
92     rootPath = rootPath.substring(0, rootPath.indexOf("/*"));
93     basePath = basePath.equals("/") ? rootPath
94         : (new StringBuilder()).append(basePath).append(rootPath).toString();
95     config.setBasePath(basePath);
96     config.setScan(true);
97   }
98
99 }