1 /*******************************************************************************
2 * Copyright 2016-2017 ZTE, Inc. and others.
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software distributed under the License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
13 ******************************************************************************/
14 package org.onap.msb.apiroute;
16 import org.onap.msb.apiroute.health.ApiRouteHealthCheck;
17 import org.onap.msb.apiroute.resources.ApiRouteResource;
18 import org.onap.msb.apiroute.resources.CustomRouteResource;
19 import org.onap.msb.apiroute.resources.IuiRouteResource;
20 import org.onap.msb.apiroute.resources.MicroServiceResource;
21 import org.onap.msb.apiroute.wrapper.InitRouteServiceWrapper;
22 import org.onap.msb.apiroute.wrapper.util.ConfigUtil;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 import com.fasterxml.jackson.annotation.JsonInclude;
28 import io.dropwizard.Application;
29 import io.dropwizard.assets.AssetsBundle;
30 import io.dropwizard.server.SimpleServerFactory;
31 import io.dropwizard.setup.Bootstrap;
32 import io.dropwizard.setup.Environment;
33 import io.swagger.jaxrs.config.BeanConfig;
34 import io.swagger.jaxrs.listing.ApiListingResource;
35 import java.util.Optional;
37 public class ApiRouteApp extends Application<ApiRouteAppConfig> {
39 private static final Logger LOGGER = LoggerFactory.getLogger(ApiRouteApp.class);
41 public static void main(String[] args) throws Exception {
42 new ApiRouteApp().run(args);
44 InitRouteServiceWrapper.getInstance().initFilterConfig();
46 InitRouteServiceWrapper.getInstance().initDataSynchro();
48 InitRouteServiceWrapper.getInstance().initHealthCheck();
53 public String getName() {
54 return " MicroService Bus ";
58 public void initialize(Bootstrap<ApiRouteAppConfig> bootstrap) {
59 super.initialize(bootstrap);
64 public void run(ApiRouteAppConfig configuration, Environment environment) throws Exception {
67 ConfigUtil.getInstance().initRootPath();
70 String iuiRootPath = ConfigUtil.getInstance().getIUI_ROOT_PATH();
72 // new AssetsBundle("/iui-metrics", "/"+iuiRootPath+"/microservices/metrics","index.html",
73 // "iui-metrics").run(environment);
75 new AssetsBundle("/iui-route", "/" + iuiRootPath + "/microservices", "index.html", "iui-microservices")
78 new AssetsBundle("/api-doc", "/" + iuiRootPath + "/microservices/api-doc", "index.html", "api-doc")
81 new AssetsBundle("/ext", "/" + iuiRootPath + "/microservices/ext", "index.html", "ext").run(environment);
85 final ApiRouteHealthCheck healthCheck = new ApiRouteHealthCheck();
86 environment.healthChecks().register("consulCheck", healthCheck);
88 environment.jersey().register(new ApiRouteResource());
89 environment.jersey().register(new IuiRouteResource());
90 environment.jersey().register(new CustomRouteResource());
91 environment.jersey().register(new MicroServiceResource());
93 // initSwaggerConfig(environment, configuration);
95 ConfigUtil.getInstance().initConsulIp();
96 ConfigUtil.getInstance().initDiscoverInfo(configuration);
97 // InitRouteServiceWrapper.getInstance().initMetricsConfig(configuration);
104 private void initSwaggerConfig(Environment environment, ApiRouteAppConfig configuration) {
106 environment.jersey().register(new ApiListingResource());
107 environment.getObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL);
109 BeanConfig config = new BeanConfig();
110 config.setTitle("ApiRoute RESTful API");
111 config.setVersion("1.0.0");
112 config.setResourcePackage("org.onap.msb.apiroute.resources");
113 SimpleServerFactory simpleServerFactory = (SimpleServerFactory) configuration.getServerFactory();
114 String basePath = simpleServerFactory.getApplicationContextPath();
115 Optional<String> optRootPath = simpleServerFactory.getJerseyRootPath();
117 String rootPath = optRootPath.get();
119 rootPath = rootPath.substring(0, rootPath.indexOf("/*"));
121 basePath = basePath.equals("/") ? rootPath : (new StringBuilder()).append(basePath).append(rootPath).toString();
123 LOGGER.info("getApplicationContextPath: " + basePath);
124 config.setBasePath(basePath);
125 config.setScan(true);