2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
6 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
20 * SPDX-License-Identifier: Apache-2.0
21 * ============LICENSE_END=========================================================
24 package org.onap.policy.api.main.rest;
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.Properties;
29 import org.onap.policy.api.main.parameters.RestServerParameters;
30 import org.onap.policy.api.main.rest.aaf.AafApiFilter;
31 import org.onap.policy.common.capabilities.Startable;
32 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
33 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
34 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
35 import org.onap.policy.common.gson.GsonMessageBodyHandler;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
41 * Class to manage life cycle of api rest server.
44 public class ApiRestServer implements Startable {
46 private static final Logger LOGGER = LoggerFactory.getLogger(ApiRestServer.class);
48 private List<HttpServletServer> servers = new ArrayList<>();
50 private RestServerParameters restServerParameters;
53 * Constructor for instantiating ApiRestServer.
55 * @param restServerParameters the rest server parameters
57 public ApiRestServer(final RestServerParameters restServerParameters) {
58 this.restServerParameters = restServerParameters;
65 public boolean start() {
67 servers = HttpServletServerFactoryInstance.getServerFactory().build(getServerProperties());
68 for (HttpServletServer server : servers) {
70 server.addFilterClass(null, AafApiFilter.class.getName());
74 } catch (final Exception exp) {
75 LOGGER.error("Failed to start api http server", exp);
82 * Creates the server properties object using restServerParameters.
84 * @return the properties object
86 private Properties getServerProperties() {
87 final Properties props = new Properties();
89 PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + restServerParameters.getName();
91 props.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES, restServerParameters.getName());
92 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, restServerParameters.getHost());
93 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX,
94 Integer.toString(restServerParameters.getPort()));
95 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX,
96 String.join(",", LegacyApiRestController.class.getName(),
97 ApiRestController.class.getName()));
98 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "false");
99 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SWAGGER_SUFFIX, "true");
100 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX,
101 restServerParameters.getUserName());
102 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX,
103 restServerParameters.getPassword());
104 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX,
105 String.valueOf(restServerParameters.isHttps()));
106 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_AAF_SUFFIX,
107 String.valueOf(restServerParameters.isAaf()));
108 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SERIALIZATION_PROVIDER,
109 GsonMessageBodyHandler.class.getName());
118 public boolean stop() {
119 for (final HttpServletServer server : servers) {
122 } catch (final Exception exp) {
123 LOGGER.error("Failed to stop api http server", exp);
133 public void shutdown() {
141 public boolean isAlive() {
142 return !servers.isEmpty();
149 public String toString() {
150 final StringBuilder builder = new StringBuilder();
151 builder.append("ApiRestServer [servers=");
152 builder.append(servers);
154 return builder.toString();