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.common.gson.GsonMessageBodyHandler;
29 import org.onap.policy.pdpx.main.parameters.RestServerParameters;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 * Class to manage life cycle of xacml pdp rest server.
37 public class XacmlPdpRestServer implements Startable {
39 private static final String SEPARATOR = ".";
40 private static final String HTTP_SERVER_SERVICES = "http.server.services";
41 private static final Logger LOGGER = LoggerFactory.getLogger(XacmlPdpRestServer.class);
43 private List<HttpServletServer> servers = new ArrayList<>();
45 private final RestServerParameters restServerParameters;
48 * Constructor for instantiating XacmlPdpRestServer.
50 * @param restServerParameters the rest server parameters
52 public XacmlPdpRestServer(final RestServerParameters restServerParameters) {
53 this.restServerParameters = restServerParameters;
60 public boolean start() {
62 LOGGER.info("Starting XacmlPdpRestServer...");
65 // Get the server properties
67 servers = HttpServletServer.factory.build(getServerProperties());
69 // Start all the servers
71 for (final HttpServletServer server : servers) {
73 server.addFilterClass(null, XacmlPdpAafFilter.class.getName());
77 LOGGER.info("servers are started");
78 } catch (final Exception exp) {
79 LOGGER.error("Failed to start xacml pdp http server", exp);
86 * Creates the server properties object using restServerParameters.
88 * @return the properties object
90 private Properties getServerProperties() {
91 final Properties props = new Properties();
92 props.setProperty(HTTP_SERVER_SERVICES, restServerParameters.getName());
93 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".host",
94 restServerParameters.getHost());
95 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".port",
96 Integer.toString(restServerParameters.getPort()));
97 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".restClasses",
98 XacmlPdpRestController.class.getName());
99 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".managed", "false");
100 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".swagger", "true");
101 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".userName",
102 restServerParameters.getUserName());
103 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".password",
104 restServerParameters.getPassword());
105 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".https",
106 String.valueOf(restServerParameters.isHttps()));
107 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".aaf",
108 String.valueOf(restServerParameters.isAaf()));
109 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".serialization.provider",
110 GsonMessageBodyHandler.class.getName());
118 public boolean stop() {
119 for (final HttpServletServer server : servers) {
122 } catch (final Exception exp) {
123 LOGGER.error("Failed to stop xacml pdp 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("XacmlPdpRestServer [servers=");
152 builder.append(servers);
154 return builder.toString();