2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
4 * Modifications Copyright (C) 2019 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.pdpx.main.rest;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.Properties;
27 import org.onap.policy.common.capabilities.Startable;
28 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
29 import org.onap.policy.common.endpoints.parameters.RestServerParameters;
30 import org.onap.policy.common.gson.GsonMessageBodyHandler;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * Class to manage life cycle of xacml pdp rest server.
38 public class XacmlPdpRestServer implements Startable {
40 private static final String SEPARATOR = ".";
41 private static final String HTTP_SERVER_SERVICES = "http.server.services";
42 private static final Logger LOGGER = LoggerFactory.getLogger(XacmlPdpRestServer.class);
44 private List<HttpServletServer> servers = new ArrayList<>();
46 private final RestServerParameters restServerParameters;
49 * Constructor for instantiating XacmlPdpRestServer.
51 * @param restServerParameters the rest server parameters
53 public XacmlPdpRestServer(final RestServerParameters restServerParameters) {
54 this.restServerParameters = restServerParameters;
61 public boolean start() {
63 LOGGER.info("Starting XacmlPdpRestServer...");
66 // Get the server properties
68 servers = HttpServletServer.factory.build(getServerProperties());
70 // Start all the servers
72 for (final HttpServletServer server : servers) {
74 server.addFilterClass(null, XacmlPdpAafFilter.class.getName());
78 LOGGER.info("servers are started");
79 } catch (final Exception exp) {
80 LOGGER.error("Failed to start xacml pdp http server", exp);
87 * Creates the server properties object using restServerParameters.
89 * @return the properties object
91 private Properties getServerProperties() {
92 final Properties props = new Properties();
93 props.setProperty(HTTP_SERVER_SERVICES, restServerParameters.getName());
94 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".host",
95 restServerParameters.getHost());
96 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".port",
97 Integer.toString(restServerParameters.getPort()));
98 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".restClasses",
99 XacmlPdpRestController.class.getName());
100 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".managed", "false");
101 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".swagger", "true");
102 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".userName",
103 restServerParameters.getUserName());
104 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".password",
105 restServerParameters.getPassword());
106 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".https",
107 String.valueOf(restServerParameters.isHttps()));
108 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".aaf",
109 String.valueOf(restServerParameters.isAaf()));
110 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".serialization.provider",
111 GsonMessageBodyHandler.class.getName());
119 public boolean stop() {
120 for (final HttpServletServer server : servers) {
123 } catch (final Exception exp) {
124 LOGGER.error("Failed to stop xacml pdp http server", exp);
134 public void shutdown() {
142 public boolean isAlive() {
143 return !servers.isEmpty();
150 public String toString() {
151 final StringBuilder builder = new StringBuilder();
152 builder.append("XacmlPdpRestServer [servers=");
153 builder.append(servers);
155 return builder.toString();