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.JacksonHandler;
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 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 servers = HttpServletServer.factory.build(getServerProperties());
63 for (final HttpServletServer server : servers) {
65 server.addFilterClass(null, XacmlPdpAafFilter.class.getCanonicalName());
69 } catch (final Exception exp) {
70 LOGGER.error("Failed to start xacml pdp http server", exp);
77 * Creates the server properties object using restServerParameters.
79 * @return the properties object
81 private Properties getServerProperties() {
82 final Properties props = new Properties();
83 props.setProperty(HTTP_SERVER_SERVICES, restServerParameters.getName());
84 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".host",
85 restServerParameters.getHost());
86 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".port",
87 Integer.toString(restServerParameters.getPort()));
88 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".restClasses",
89 XacmlPdpRestController.class.getCanonicalName());
90 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".managed", "false");
91 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".swagger", "true");
92 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".userName",
93 restServerParameters.getUserName());
94 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".password",
95 restServerParameters.getPassword());
96 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".https",
97 String.valueOf(restServerParameters.isHttps()));
98 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".aaf",
99 String.valueOf(restServerParameters.isAaf()));
107 public boolean stop() {
108 for (final HttpServletServer server : servers) {
111 } catch (final Exception exp) {
112 LOGGER.error("Failed to stop xacml pdp http server", exp);
122 public void shutdown() {
130 public boolean isAlive() {
131 return !servers.isEmpty();
138 public String toString() {
139 final StringBuilder builder = new StringBuilder();
140 builder.append("XacmlPdpRestServer [servers=");
141 builder.append(servers);
143 return builder.toString();