2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.pdpx.main.rest;
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Properties;
26 import org.onap.policy.common.capabilities.Startable;
27 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
28 import org.onap.policy.pdpx.main.parameters.RestServerParameters;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * Class to manage life cycle of xacml pdp rest server.
36 public class XacmlPdpRestServer implements Startable {
38 private static final String SEPARATOR = ".";
39 private static final String HTTP_SERVER_SERVICES = "http.server.services";
40 private static final Logger LOGGER = LoggerFactory.getLogger(XacmlPdpRestServer.class);
42 private List<HttpServletServer> servers = new ArrayList<>();
44 private RestServerParameters restServerParameters;
47 * Constructor for instantiating XacmlPdpRestServer.
49 * @param restServerParameters the rest server parameters
51 public XacmlPdpRestServer(final RestServerParameters restServerParameters) {
52 this.restServerParameters = restServerParameters;
59 public boolean start() {
61 servers = HttpServletServer.factory.build(getServerProperties());
62 for (final HttpServletServer server : servers) {
65 } catch (final Exception exp) {
66 LOGGER.error("Failed to start xacml pdp http server", exp);
73 * Creates the server properties object using restServerParameters.
75 * @return the properties object
77 private Properties getServerProperties() {
78 final Properties props = new Properties();
79 props.setProperty(HTTP_SERVER_SERVICES, restServerParameters.getName());
80 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".host",
81 restServerParameters.getHost());
82 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".port",
83 Integer.toString(restServerParameters.getPort()));
84 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".restClasses",
85 XacmlPdpRestController.class.getCanonicalName());
86 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".managed", "false");
87 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".swagger", "true");
88 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".userName",
89 restServerParameters.getUserName());
90 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".password",
91 restServerParameters.getPassword());
99 public boolean stop() {
100 for (final HttpServletServer server : servers) {
103 } catch (final Exception exp) {
104 LOGGER.error("Failed to stop xacml pdp http server", exp);
114 public void shutdown() {
122 public boolean isAlive() {
123 return !servers.isEmpty();
130 public String toString() {
131 final StringBuilder builder = new StringBuilder();
132 builder.append("XacmlPdpRestServer [servers=");
133 builder.append(servers);
135 return builder.toString();