aboutsummaryrefslogtreecommitdiffstats
path: root/policy-endpoints/src/main/java/org/openecomp/policy/drools/http
diff options
context:
space:
mode:
Diffstat (limited to 'policy-endpoints/src/main/java/org/openecomp/policy/drools/http')
-rw-r--r--policy-endpoints/src/main/java/org/openecomp/policy/drools/http/client/HttpClient.java48
-rw-r--r--policy-endpoints/src/main/java/org/openecomp/policy/drools/http/client/HttpClientFactory.java185
-rw-r--r--policy-endpoints/src/main/java/org/openecomp/policy/drools/http/client/internal/JerseyClient.java242
-rw-r--r--policy-endpoints/src/main/java/org/openecomp/policy/drools/http/server/HttpServletServer.java81
-rw-r--r--policy-endpoints/src/main/java/org/openecomp/policy/drools/http/server/HttpServletServerFactory.java206
-rw-r--r--policy-endpoints/src/main/java/org/openecomp/policy/drools/http/server/internal/JettyJerseyServer.java130
-rw-r--r--policy-endpoints/src/main/java/org/openecomp/policy/drools/http/server/internal/JettyServletServer.java353
7 files changed, 1245 insertions, 0 deletions
diff --git a/policy-endpoints/src/main/java/org/openecomp/policy/drools/http/client/HttpClient.java b/policy-endpoints/src/main/java/org/openecomp/policy/drools/http/client/HttpClient.java
new file mode 100644
index 00000000..2e81b2c8
--- /dev/null
+++ b/policy-endpoints/src/main/java/org/openecomp/policy/drools/http/client/HttpClient.java
@@ -0,0 +1,48 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * policy-endpoints
+ * ================================================================================
+ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+package org.openecomp.policy.drools.http.client;
+
+import javax.ws.rs.core.Response;
+
+import org.openecomp.policy.drools.properties.Startable;
+
+public interface HttpClient extends Startable {
+
+ public Response get(String path);
+
+ public Response get();
+
+ public static <T> T getBody(Response response, Class<T> entityType) {
+ return response.readEntity(entityType);
+ }
+
+ public String getName();
+ public boolean isHttps();
+ public boolean isSelfSignedCerts();
+ public String getHostname();
+ public int getPort();
+ public String getBasePath();
+ public String getUserName();
+ public String getPassword();
+ public String getBaseUrl();
+
+
+ public static final HttpClientFactory factory = new IndexedHttpClientFactory();
+}
diff --git a/policy-endpoints/src/main/java/org/openecomp/policy/drools/http/client/HttpClientFactory.java b/policy-endpoints/src/main/java/org/openecomp/policy/drools/http/client/HttpClientFactory.java
new file mode 100644
index 00000000..53a8c2b2
--- /dev/null
+++ b/policy-endpoints/src/main/java/org/openecomp/policy/drools/http/client/HttpClientFactory.java
@@ -0,0 +1,185 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * policy-endpoints
+ * ================================================================================
+ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+package org.openecomp.policy.drools.http.client;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Properties;
+
+import org.openecomp.policy.drools.http.client.internal.JerseyClient;
+import org.openecomp.policy.drools.properties.PolicyProperties;
+
+public interface HttpClientFactory {
+
+ public HttpClient build(String name, boolean https,
+ boolean selfSignedCerts,
+ String hostname, int port,
+ String baseUrl, String userName,
+ String password, boolean managed)
+ throws Exception;
+
+ public ArrayList<HttpClient> build(Properties properties) throws Exception;
+
+ public HttpClient get(String name);
+
+ public List<HttpClient> inventory();
+
+ public void destroy(String name);
+
+ public void destroy();
+}
+
+class IndexedHttpClientFactory implements HttpClientFactory {
+
+ protected HashMap<String, HttpClient> clients = new HashMap<String, HttpClient>();
+
+ @Override
+ public synchronized HttpClient build(String name, boolean https, boolean selfSignedCerts,
+ String hostname, int port,
+ String baseUrl, String userName, String password,
+ boolean managed)
+ throws Exception {
+ if (clients.containsKey(name))
+ return clients.get(name);
+
+ JerseyClient client =
+ new JerseyClient(name, https, selfSignedCerts, hostname, port, baseUrl, userName, password);
+
+ if (managed)
+ clients.put(name, client);
+
+ return client;
+ }
+
+ @Override
+ public synchronized ArrayList<HttpClient> build(Properties properties) throws Exception {
+ ArrayList<HttpClient> clientList = new ArrayList<HttpClient>();
+
+ String clientNames = properties.getProperty(PolicyProperties.PROPERTY_HTTP_CLIENT_SERVICES);
+ if (clientNames == null || clientNames.isEmpty()) {
+ return clientList;
+ }
+
+ List<String> clientNameList =
+ new ArrayList<String>(Arrays.asList(clientNames.split("\\s*,\\s*")));
+
+ for (String clientName : clientNameList) {
+ String httpsString = properties.getProperty(PolicyProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." +
+ clientName +
+ PolicyProperties.PROPERTY_HTTP_HTTPS_SUFFIX);
+ boolean https = false;
+ if (httpsString != null && !httpsString.isEmpty()) {
+ https = Boolean.parseBoolean(httpsString);
+ }
+
+ String hostName = properties.getProperty(PolicyProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." +
+ clientName +
+ PolicyProperties.PROPERTY_HTTP_HOST_SUFFIX);
+
+ String servicePortString = properties.getProperty(PolicyProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." +
+ clientName +
+ PolicyProperties.PROPERTY_HTTP_PORT_SUFFIX);
+ int port;
+ try {
+ if (servicePortString == null || servicePortString.isEmpty()) {
+ continue;
+ }
+ port = Integer.parseInt(servicePortString);
+ } catch (NumberFormatException nfe) {
+ nfe.printStackTrace();
+ continue;
+ }
+
+ String baseUrl = properties.getProperty(PolicyProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." +
+ clientName +
+ PolicyProperties.PROPERTY_HTTP_URL_SUFFIX);
+
+ String userName = properties.getProperty(PolicyProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." +
+ clientName +
+ PolicyProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX);
+
+ String password = properties.getProperty(PolicyProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." +
+ clientName +
+ PolicyProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX);
+
+ String managedString = properties.getProperty(PolicyProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." +
+ clientName +
+ PolicyProperties.PROPERTY_MANAGED_SUFFIX);
+ boolean managed = true;
+ if (managedString != null && !managedString.isEmpty()) {
+ managed = Boolean.parseBoolean(managedString);
+ }
+
+ try {
+ HttpClient client =
+ this.build(clientName, https, https, hostName, port, baseUrl,
+ userName, password, managed);
+ clientList.add(client);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ return clientList;
+ }
+
+ @Override
+ public synchronized HttpClient get(String name) {
+ if (clients.containsKey(name)) {
+ return clients.get(name);
+ }
+
+ throw new IllegalArgumentException("Http Client " + name + " not found");
+ }
+
+ @Override
+ public synchronized List<HttpClient> inventory() {
+ return new ArrayList<HttpClient>(this.clients.values());
+ }
+
+ @Override
+ public synchronized void destroy(String name) {
+ if (!clients.containsKey(name)) {
+ return;
+ }
+
+ HttpClient client = clients.remove(name);
+ try {
+ client.shutdown();
+ } catch (IllegalStateException e) {
+ e.printStackTrace();
+ }
+ }
+
+ @Override
+ public void destroy() {
+ List<HttpClient> clientsInventory = this.inventory();
+ for (HttpClient client: clientsInventory) {
+ client.shutdown();
+ }
+
+ synchronized(this) {
+ this.clients.clear();
+ }
+ }
+
+}
diff --git a/policy-endpoints/src/main/java/org/openecomp/policy/drools/http/client/internal/JerseyClient.java b/policy-endpoints/src/main/java/org/openecomp/policy/drools/http/client/internal/JerseyClient.java
new file mode 100644
index 00000000..4fa59dc8
--- /dev/null
+++ b/policy-endpoints/src/main/java/org/openecomp/policy/drools/http/client/internal/JerseyClient.java
@@ -0,0 +1,242 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * policy-healthcheck
+ * ================================================================================
+ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.openecomp.policy.drools.http.client.internal;
+
+import java.security.SecureRandom;
+import java.security.cert.CertificateException;
+import java.security.cert.X509Certificate;
+
+import javax.net.ssl.HostnameVerifier;
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.SSLSession;
+import javax.net.ssl.TrustManager;
+import javax.net.ssl.X509TrustManager;
+import javax.ws.rs.client.Client;
+import javax.ws.rs.client.ClientBuilder;
+import javax.ws.rs.core.Response;
+
+import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
+import org.openecomp.policy.drools.http.client.HttpClient;
+
+import com.fasterxml.jackson.annotation.JsonIgnore;
+
+public class JerseyClient implements HttpClient {
+
+ protected final String name;
+ protected final boolean https;
+ protected final boolean selfSignedCerts;
+ protected final String hostname;
+ protected final int port;
+ protected final String basePath;
+ protected final String userName;
+ protected final String password;
+
+ protected final Client client;
+ protected final String baseUrl;
+
+ protected boolean alive = true;
+
+
+ public JerseyClient(String name, boolean https,
+ boolean selfSignedCerts,
+ String hostname, int port,
+ String basePath, String userName,
+ String password)
+ throws Exception {
+
+ super();
+
+ if (name == null || name.isEmpty())
+ throw new IllegalArgumentException("Name must be provided");
+
+ if (hostname == null || hostname.isEmpty())
+ throw new IllegalArgumentException("Hostname must be provided");
+
+ if (port <= 0 && port >= 65535)
+ throw new IllegalArgumentException("Invalid Port provided: " + port);
+
+ this.name = name;
+ this.https = https;
+ this.hostname = hostname;
+ this.port = port;
+ this.basePath = basePath;
+ this.userName = userName;
+ this.password = password;
+ this.selfSignedCerts = selfSignedCerts;
+
+ StringBuffer tmpBaseUrl = new StringBuffer();
+ if (this.https) {
+ tmpBaseUrl.append("https://");
+ ClientBuilder clientBuilder;
+ SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
+ if (this.selfSignedCerts) {
+ sslContext.init(null, new TrustManager[]{new X509TrustManager() {
+ @Override
+ public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {}
+ @Override
+ public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {}
+ @Override
+ public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; }
+
+ }}, new SecureRandom());
+ clientBuilder = ClientBuilder.newBuilder().sslContext(sslContext).hostnameVerifier(new HostnameVerifier() {
+ @Override
+ public boolean verify(String hostname, SSLSession session) {return true;}
+ });
+ } else {
+ sslContext.init(null, null, null);
+ clientBuilder = ClientBuilder.newBuilder().sslContext(sslContext);
+ }
+ this.client = clientBuilder.build();
+ } else {
+ tmpBaseUrl.append("http://");
+ this.client = ClientBuilder.newClient();
+ }
+
+ if (this.userName != null && !this.userName.isEmpty() &&
+ this.password != null && !this.password.isEmpty()) {
+ HttpAuthenticationFeature authFeature = HttpAuthenticationFeature.basic(userName, password);
+ this.client.register(authFeature);
+ }
+
+ this.baseUrl = tmpBaseUrl.append(this.hostname).append(":").
+ append(this.port).append("/").
+ append((this.basePath == null) ? "" : this.basePath).
+ toString();
+ }
+
+ @Override
+ public Response get(String path) {
+ if (path != null && !path.isEmpty())
+ return this.client.target(this.baseUrl).path(path).request().get();
+ else
+ return this.client.target(this.baseUrl).request().get();
+ }
+
+ @Override
+ public Response get() {
+ return this.client.target(this.baseUrl).request().get();
+ }
+
+
+ @Override
+ public boolean start() throws IllegalStateException {
+ return alive;
+ }
+
+ @Override
+ public boolean stop() throws IllegalStateException {
+ return !alive;
+ }
+
+ @Override
+ public void shutdown() throws IllegalStateException {
+ synchronized(this) {
+ alive = false;
+ }
+
+ try {
+ this.client.close();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ @Override
+ public synchronized boolean isAlive() {
+ return this.alive;
+ }
+
+ @Override
+ public String getName() {
+ return name;
+ }
+
+ @Override
+ public boolean isHttps() {
+ return https;
+ }
+
+ @Override
+ public boolean isSelfSignedCerts() {
+ return selfSignedCerts;
+ }
+
+ @Override
+ public String getHostname() {
+ return hostname;
+ }
+
+ public int getPort() {
+ return port;
+ }
+
+ @Override
+ public String getBasePath() {
+ return basePath;
+ }
+
+ @Override
+ public String getUserName() {
+ return userName;
+ }
+
+ @JsonIgnore
+ @Override
+ public String getPassword() {
+ return password;
+ }
+
+ @Override
+ public String getBaseUrl() {
+ return baseUrl;
+ }
+
+ @Override
+ public String toString() {
+ StringBuilder builder = new StringBuilder();
+ builder.append("JerseyClient [name=");
+ builder.append(name);
+ builder.append(", https=");
+ builder.append(https);
+ builder.append(", selfSignedCerts=");
+ builder.append(selfSignedCerts);
+ builder.append(", hostname=");
+ builder.append(hostname);
+ builder.append(", port=");
+ builder.append(port);
+ builder.append(", basePath=");
+ builder.append(basePath);
+ builder.append(", userName=");
+ builder.append(userName);
+ builder.append(", password=");
+ builder.append(password);
+ builder.append(", client=");
+ builder.append(client);
+ builder.append(", baseUrl=");
+ builder.append(baseUrl);
+ builder.append(", alive=");
+ builder.append(alive);
+ builder.append("]");
+ return builder.toString();
+ }
+
+}
diff --git a/policy-endpoints/src/main/java/org/openecomp/policy/drools/http/server/HttpServletServer.java b/policy-endpoints/src/main/java/org/openecomp/policy/drools/http/server/HttpServletServer.java
new file mode 100644
index 00000000..5f5dd787
--- /dev/null
+++ b/policy-endpoints/src/main/java/org/openecomp/policy/drools/http/server/HttpServletServer.java
@@ -0,0 +1,81 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * policy-endpoints
+ * ================================================================================
+ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+package org.openecomp.policy.drools.http.server;
+
+import org.openecomp.policy.drools.properties.Startable;
+
+/**
+ * A Jetty Server to server REST Requests
+ */
+public interface HttpServletServer extends Startable {
+
+ /**
+ *
+ * @return port
+ */
+ public int getPort();
+
+ /**
+ * enables basic authentication with user and password on the the relative path relativeUriPath
+ *
+ * @param user
+ * @param password
+ * @param relativeUriPath
+ */
+ public void setBasicAuthentication(String user, String password, String relativeUriPath);
+
+ /**
+ * adds a JAX-RS servlet class to serve REST requests
+ *
+ * @param servletPath
+ * @param restClass
+ * @throws IllegalArgumentException
+ * @throws IllegalStateException
+ */
+ public void addServletClass(String servletPath, String restClass)
+ throws IllegalArgumentException, IllegalStateException;
+
+ /**
+ * adds a package containing JAX-RS classes to serve REST requests
+ *
+ * @param servletPath
+ * @param restPackage
+ * @throws IllegalArgumentException
+ * @throws IllegalStateException
+ */
+ public void addServletPackage(String servletPath, String restPackage)
+ throws IllegalArgumentException, IllegalStateException;
+
+ /**
+ * blocking start of the http server
+ *
+ * @param maxWaitTime max time to wait for the start to take place
+ * @return true if start was successful
+ *
+ * @throws IllegalArgumentException if arguments are invalid
+ */
+ public boolean waitedStart(long maxWaitTime) throws IllegalArgumentException;
+
+
+ /**
+ * factory for managing and tracking DMAAP sources
+ */
+ public static HttpServletServerFactory factory = new IndexedHttpServletServerFactory();
+}
diff --git a/policy-endpoints/src/main/java/org/openecomp/policy/drools/http/server/HttpServletServerFactory.java b/policy-endpoints/src/main/java/org/openecomp/policy/drools/http/server/HttpServletServerFactory.java
new file mode 100644
index 00000000..bd5ae242
--- /dev/null
+++ b/policy-endpoints/src/main/java/org/openecomp/policy/drools/http/server/HttpServletServerFactory.java
@@ -0,0 +1,206 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * policy-endpoints
+ * ================================================================================
+ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+package org.openecomp.policy.drools.http.server;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Properties;
+
+import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
+import org.openecomp.policy.common.logging.flexlogger.Logger;
+import org.openecomp.policy.drools.http.server.internal.JettyJerseyServer;
+import org.openecomp.policy.drools.properties.PolicyProperties;
+
+/**
+ * Jetty Server Factory
+ */
+public interface HttpServletServerFactory {
+
+ public HttpServletServer build(String name, String host, int port, String contextPath, boolean managed)
+ throws IllegalArgumentException;
+
+ public ArrayList<HttpServletServer> build(Properties properties) throws IllegalArgumentException;
+
+ public HttpServletServer get(int port);
+ public List<HttpServletServer> inventory();
+ public void destroy(int port);
+ public void destroy();
+}
+
+class IndexedHttpServletServerFactory implements HttpServletServerFactory {
+
+ protected static Logger logger = FlexLogger.getLogger(IndexedHttpServletServerFactory.class);
+
+ protected HashMap<Integer, JettyJerseyServer> servers = new HashMap<Integer, JettyJerseyServer>();
+
+ @Override
+ public synchronized HttpServletServer build(String name, String host, int port,
+ String contextPath, boolean managed)
+ throws IllegalArgumentException {
+
+ if (servers.containsKey(port))
+ return servers.get(port);
+
+ JettyJerseyServer server = new JettyJerseyServer(name, host, port, contextPath);
+ if (managed)
+ servers.put(port, server);
+
+ return server;
+ }
+
+ @Override
+ public synchronized ArrayList<HttpServletServer> build(Properties properties)
+ throws IllegalArgumentException {
+
+ ArrayList<HttpServletServer> serviceList = new ArrayList<HttpServletServer>();
+
+ String serviceNames = properties.getProperty(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES);
+ if (serviceNames == null || serviceNames.isEmpty()) {
+ logger.warn("No topic for HTTP Service " + properties);
+ return serviceList;
+ }
+
+ List<String> serviceNameList =
+ new ArrayList<String>(Arrays.asList(serviceNames.split("\\s*,\\s*")));
+
+ for (String serviceName : serviceNameList) {
+ String servicePortString = properties.getProperty(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." +
+ serviceName +
+ PolicyProperties.PROPERTY_HTTP_PORT_SUFFIX);
+
+ int servicePort;
+ try {
+ if (servicePortString == null || servicePortString.isEmpty()) {
+ if (logger.isWarnEnabled())
+ logger.warn("No HTTP port for service in " + serviceName);
+ continue;
+ }
+ servicePort = Integer.parseInt(servicePortString);
+ } catch (NumberFormatException nfe) {
+ if (logger.isWarnEnabled())
+ logger.warn("No HTTP port for service in " + serviceName);
+ continue;
+ }
+
+ String hostName = properties.getProperty(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." +
+ serviceName +
+ PolicyProperties.PROPERTY_HTTP_HOST_SUFFIX);
+
+ String contextUriPath = properties.getProperty(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." +
+ serviceName +
+ PolicyProperties.PROPERTY_HTTP_CONTEXT_URIPATH_SUFFIX);
+
+ String userName = properties.getProperty(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." +
+ serviceName +
+ PolicyProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX);
+
+ String password = properties.getProperty(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." +
+ serviceName +
+ PolicyProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX);
+
+ String authUriPath = properties.getProperty(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." +
+ serviceName +
+ PolicyProperties.PROPERTY_HTTP_AUTH_URIPATH_SUFFIX);
+
+ String restClasses = properties.getProperty(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." +
+ serviceName +
+ PolicyProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX);
+
+ String restPackages = properties.getProperty(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." +
+ serviceName +
+ PolicyProperties.PROPERTY_HTTP_REST_PACKAGES_SUFFIX);
+ String restUriPath = properties.getProperty(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." +
+ serviceName +
+ PolicyProperties.PROPERTY_HTTP_REST_URIPATH_SUFFIX);
+
+ String managedString = properties.getProperty(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." +
+ serviceName +
+ PolicyProperties.PROPERTY_MANAGED_SUFFIX);
+ boolean managed = true;
+ if (managedString != null && !managedString.isEmpty()) {
+ managed = Boolean.parseBoolean(managedString);
+ }
+
+ HttpServletServer service = build(serviceName, hostName, servicePort, contextUriPath, managed);
+ if (userName != null && !userName.isEmpty() && password != null && !password.isEmpty()) {
+ service.setBasicAuthentication(userName, password, authUriPath);
+ }
+
+ if (restClasses != null && !restClasses.isEmpty()) {
+ List<String> restClassesList =
+ new ArrayList<String>(Arrays.asList(restClasses.split("\\s*,\\s*")));
+ for (String restClass : restClassesList)
+ service.addServletClass(restUriPath, restClass);
+ }
+
+ if (restPackages != null && !restPackages.isEmpty()) {
+ List<String> restPackageList =
+ new ArrayList<String>(Arrays.asList(restPackages.split("\\s*,\\s*")));
+ for (String restPackage : restPackageList)
+ service.addServletPackage(restUriPath, restPackage);
+ }
+
+ serviceList.add(service);
+ }
+
+ return serviceList;
+ }
+
+ @Override
+ public synchronized HttpServletServer get(int port) throws IllegalArgumentException {
+
+ if (servers.containsKey(port)) {
+ return servers.get(port);
+ }
+
+ throw new IllegalArgumentException("Http Server for " + port + " not found");
+ }
+
+ @Override
+ public synchronized List<HttpServletServer> inventory() {
+ return new ArrayList<HttpServletServer>(this.servers.values());
+ }
+
+ @Override
+ public synchronized void destroy(int port) throws IllegalArgumentException, IllegalStateException {
+
+ if (!servers.containsKey(port)) {
+ return;
+ }
+
+ HttpServletServer server = servers.remove(port);
+ server.shutdown();
+ }
+
+ @Override
+ public synchronized void destroy() throws IllegalArgumentException, IllegalStateException {
+ List<HttpServletServer> servers = this.inventory();
+ for (HttpServletServer server: servers) {
+ server.shutdown();
+ }
+
+ synchronized(this) {
+ this.servers.clear();
+ }
+ }
+
+}
diff --git a/policy-endpoints/src/main/java/org/openecomp/policy/drools/http/server/internal/JettyJerseyServer.java b/policy-endpoints/src/main/java/org/openecomp/policy/drools/http/server/internal/JettyJerseyServer.java
new file mode 100644
index 00000000..4914a4cb
--- /dev/null
+++ b/policy-endpoints/src/main/java/org/openecomp/policy/drools/http/server/internal/JettyJerseyServer.java
@@ -0,0 +1,130 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * policy-endpoints
+ * ================================================================================
+ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+package org.openecomp.policy.drools.http.server.internal;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+
+import org.eclipse.jetty.servlet.ServletHolder;
+
+import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
+import org.openecomp.policy.common.logging.flexlogger.Logger;
+
+/**
+ * REST Jetty Server using Jersey
+ */
+public class JettyJerseyServer extends JettyServletServer {
+
+ protected static final String JERSEY_PACKAGES_PARAM = "jersey.config.server.provider.packages";
+ protected static final String JERSEY_CLASSNAMES_PARAM = "jersey.config.server.provider.classnames";
+
+ protected static Logger logger = FlexLogger.getLogger(JettyJerseyServer.class);
+
+ protected ArrayList<String> packages = new ArrayList<String>();
+ protected HashMap<String, ServletHolder> servlets =
+ new HashMap<String, ServletHolder>();
+
+ public JettyJerseyServer(String name, String host, int port, String contextPath)
+ throws IllegalArgumentException {
+ super(name, host, port, contextPath);
+ }
+
+ protected synchronized ServletHolder getServlet(String servletPath)
+ throws IllegalArgumentException {
+
+ if (servletPath == null || servletPath.isEmpty())
+ servletPath = "/*";
+
+ ServletHolder jerseyServlet = servlets.get(servletPath);
+ if (jerseyServlet == null) {
+ jerseyServlet = context.addServlet
+ (org.glassfish.jersey.servlet.ServletContainer.class, servletPath);
+ jerseyServlet.setInitOrder(0);
+ String initPackages =
+ jerseyServlet.getInitParameter(JERSEY_PACKAGES_PARAM);
+ if (initPackages == null) {
+ jerseyServlet.setInitParameter(
+ JERSEY_PACKAGES_PARAM,
+ "com.jersey.jaxb,com.fasterxml.jackson.jaxrs.json");
+ }
+ this.servlets.put(servletPath, jerseyServlet);
+ }
+
+ return jerseyServlet;
+ }
+
+ @Override
+ public synchronized void addServletPackage(String servletPath, String restPackage)
+ throws IllegalArgumentException, IllegalStateException {
+
+ if (restPackage == null || restPackage.isEmpty())
+ throw new IllegalArgumentException("No discoverable REST package provided");
+
+ ServletHolder jerseyServlet = this.getServlet(servletPath);
+ if (jerseyServlet == null)
+ throw new IllegalStateException("Unexpected, no Jersey Servlet class");
+
+ String initPackages =
+ jerseyServlet.getInitParameter(JERSEY_PACKAGES_PARAM);
+ if (initPackages == null)
+ throw new IllegalStateException("Unexpected, no Init Parameters loaded");
+
+ jerseyServlet.setInitParameter(
+ JERSEY_PACKAGES_PARAM,
+ initPackages + "," + restPackage);
+
+ if (logger.isDebugEnabled())
+ logger.debug(this + "Added REST Package: " + jerseyServlet.dump());
+ }
+
+ @Override
+ public synchronized void addServletClass(String servletPath, String restClass)
+ throws IllegalArgumentException, IllegalStateException {
+
+ if (restClass == null || restClass.isEmpty())
+ throw new IllegalArgumentException("No discoverable REST class provided");
+
+ ServletHolder jerseyServlet = this.getServlet(servletPath);
+ if (jerseyServlet == null)
+ throw new IllegalStateException("Unexpected, no Jersey Servlet class");
+
+ String initClasses =
+ jerseyServlet.getInitParameter(JERSEY_CLASSNAMES_PARAM);
+ if (initClasses == null)
+ initClasses = restClass;
+ else
+ initClasses = initClasses + "," + restClass;
+
+ jerseyServlet.setInitParameter(
+ JERSEY_CLASSNAMES_PARAM,
+ initClasses);
+
+ if (logger.isDebugEnabled())
+ logger.debug(this + "Added REST Class: " + jerseyServlet.dump());
+ }
+
+ @Override
+ public String toString() {
+ StringBuilder builder = new StringBuilder();
+ builder.append("JerseyJettyServer [packages=").append(packages).append(", servlets=").append(servlets)
+ .append(", toString()=").append(super.toString()).append("]");
+ return builder.toString();
+ }
+}
diff --git a/policy-endpoints/src/main/java/org/openecomp/policy/drools/http/server/internal/JettyServletServer.java b/policy-endpoints/src/main/java/org/openecomp/policy/drools/http/server/internal/JettyServletServer.java
new file mode 100644
index 00000000..74360e80
--- /dev/null
+++ b/policy-endpoints/src/main/java/org/openecomp/policy/drools/http/server/internal/JettyServletServer.java
@@ -0,0 +1,353 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * policy-endpoints
+ * ================================================================================
+ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+package org.openecomp.policy.drools.http.server.internal;
+
+import org.eclipse.jetty.security.ConstraintMapping;
+import org.eclipse.jetty.security.ConstraintSecurityHandler;
+import org.eclipse.jetty.security.HashLoginService;
+import org.eclipse.jetty.security.authentication.BasicAuthenticator;
+import org.eclipse.jetty.server.Server;
+import org.eclipse.jetty.server.ServerConnector;
+import org.eclipse.jetty.servlet.ServletContextHandler;
+import org.eclipse.jetty.util.security.Constraint;
+import org.eclipse.jetty.util.security.Credential;
+
+import org.openecomp.policy.common.logging.eelf.MessageCodes;
+import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
+import org.openecomp.policy.common.logging.flexlogger.Logger;
+import org.openecomp.policy.drools.http.server.HttpServletServer;
+
+import com.fasterxml.jackson.annotation.JsonIgnore;
+
+/**
+ * Http Server implementation using Embedded Jetty
+ */
+public abstract class JettyServletServer implements HttpServletServer, Runnable {
+
+ private static Logger logger = FlexLogger.getLogger(JettyServletServer.class);
+
+ protected final String name;
+
+ protected final String host;
+ protected final int port;
+
+ protected String user;
+ protected String password;
+
+ protected final String contextPath;
+
+ protected final Server jettyServer;
+ protected final ServletContextHandler context;
+ protected final ServerConnector connector;
+
+ protected volatile Thread jettyThread;
+
+ protected Object startCondition = new Object();
+
+ public JettyServletServer(String name, String host, int port, String contextPath)
+ throws IllegalArgumentException {
+
+ if (name == null || name.isEmpty())
+ name = "http-" + port;
+
+ if (port <= 0 && port >= 65535)
+ throw new IllegalArgumentException("Invalid Port provided: " + port);
+
+ if (host == null || host.isEmpty())
+ host = "localhost";
+
+ if (contextPath == null || contextPath.isEmpty())
+ contextPath = "/";
+
+ this.name = name;
+
+ this.host = host;
+ this.port = port;
+
+ this.contextPath = contextPath;
+
+ this.context = new ServletContextHandler(ServletContextHandler.SESSIONS);
+ this.context.setContextPath(contextPath);
+
+ this.jettyServer = new Server();
+
+ this.connector = new ServerConnector(this.jettyServer);
+ this.connector.setName(name);
+ this.connector.setReuseAddress(true);
+ this.connector.setPort(port);
+ this.connector.setHost(host);
+
+ this.jettyServer.addConnector(this.connector);
+ this.jettyServer.setHandler(context);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void setBasicAuthentication(String user, String password, String servletPath) {
+ if (user == null || user.isEmpty() || password == null || password.isEmpty())
+ throw new IllegalArgumentException("Missing user and/or password");
+
+ if (servletPath == null || servletPath.isEmpty())
+ servletPath = "/*";
+
+ HashLoginService hashLoginService = new HashLoginService();
+ hashLoginService.putUser(user,
+ Credential.getCredential(password),
+ new String[] {"user"});
+ hashLoginService.setName(this.connector.getName() + "-login-service");
+
+ Constraint constraint = new Constraint();
+ constraint.setName(Constraint.__BASIC_AUTH);
+ constraint.setRoles(new String[]{"user"});
+ constraint.setAuthenticate(true);
+
+ ConstraintMapping constraintMapping = new ConstraintMapping();
+ constraintMapping.setConstraint(constraint);
+ constraintMapping.setPathSpec(servletPath);
+
+ ConstraintSecurityHandler securityHandler = new ConstraintSecurityHandler();
+ securityHandler.setAuthenticator(new BasicAuthenticator());
+ securityHandler.setRealmName(this.connector.getName() + "-realm");
+ securityHandler.addConstraintMapping(constraintMapping);
+ securityHandler.setLoginService(hashLoginService);
+
+ this.context.setSecurityHandler(securityHandler);
+
+ this.user = user;
+ this.password = password;
+ }
+
+ /**
+ * Jetty Server Execution
+ */
+ @Override
+ public void run() {
+ try {
+ if (logger.isInfoEnabled())
+ logger.info(this + " STARTING " + this.jettyServer.dump());
+
+ this.jettyServer.start();
+
+ synchronized(this.startCondition) {
+ this.startCondition.notifyAll();
+ }
+
+ this.jettyServer.join();
+ } catch (Exception e) {
+ logger.warn(MessageCodes.EXCEPTION_ERROR, e,
+ "Error found while running management server", this.toString());
+ }
+ }
+
+ @Override
+ public boolean waitedStart(long maxWaitTime) throws IllegalArgumentException {
+
+ if (maxWaitTime < 0)
+ throw new IllegalArgumentException("max-wait-time cannot be negative");
+
+ long pendingWaitTime = maxWaitTime;
+
+ if (!this.start())
+ return false;
+
+ synchronized (this.startCondition) {
+
+ while (!this.jettyServer.isRunning()) {
+ try {
+ long startTs = System.currentTimeMillis();
+
+ this.startCondition.wait(pendingWaitTime);
+
+ if (maxWaitTime == 0)
+ /* spurious notification */
+ continue;
+
+ long endTs = System.currentTimeMillis();
+ pendingWaitTime = pendingWaitTime - (endTs - startTs);
+
+ if (logger.isInfoEnabled())
+ logger.info(this + "Pending time is " + pendingWaitTime +
+ " ms.");
+
+ if (pendingWaitTime <= 0)
+ return false;
+
+ } catch (InterruptedException e) {
+ logger.warn("waited-start has been interrupted");
+ return false;
+ }
+ }
+
+ return (this.jettyServer.isRunning());
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public boolean start() throws IllegalStateException {
+ if (logger.isDebugEnabled())
+ logger.debug(this + "START");
+
+ synchronized(this) {
+ if (jettyThread == null ||
+ !this.jettyThread.isAlive()) {
+
+ this.jettyThread = new Thread(this);
+ this.jettyThread.setName(this.name + "-" + this.port);
+ this.jettyThread.start();
+ }
+ }
+
+ return true;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public boolean stop() throws IllegalStateException {
+ logger.info(this + "STOP");
+
+ synchronized(this) {
+ if (jettyThread == null) {
+ return true;
+ }
+
+ if (!jettyThread.isAlive()) {
+ this.jettyThread = null;
+ }
+
+ try {
+ this.connector.stop();
+ } catch (Exception e) {
+ logger.error(MessageCodes.EXCEPTION_ERROR, e,
+ "Error while stopping management server", this.toString());
+ e.printStackTrace();
+ }
+
+ try {
+ this.jettyServer.stop();
+ } catch (Exception e) {
+ logger.error(MessageCodes.EXCEPTION_ERROR, e,
+ "Error while stopping management server", this.toString());
+ return false;
+ }
+
+ Thread.yield();
+ }
+
+ return true;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void shutdown() throws IllegalStateException {
+ logger.info(this + "SHUTDOWN");
+
+ this.stop();
+
+ if (this.jettyThread == null)
+ return;
+
+ Thread jettyThreadCopy = this.jettyThread;
+
+ if (jettyThreadCopy.isAlive()) {
+ try {
+ jettyThreadCopy.join(1000L);
+ } catch (InterruptedException e) {
+ logger.warn(MessageCodes.EXCEPTION_ERROR, e,
+ "Error while shutting down management server", this.toString());
+ }
+ if (!jettyThreadCopy.isInterrupted()) {
+ try {
+ jettyThreadCopy.interrupt();
+ } catch(Exception e) {
+ // do nothing
+ logger.warn("exception while shutting down (OK)");
+ }
+ }
+ }
+
+ this.jettyServer.destroy();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public boolean isAlive() {
+ if (this.jettyThread != null)
+ return this.jettyThread.isAlive();
+
+ return false;
+ }
+
+ @Override
+ public int getPort() {
+ return this.port;
+ }
+
+ /**
+ * @return the name
+ */
+ public String getName() {
+ return name;
+ }
+
+ /**
+ * @return the host
+ */
+ public String getHost() {
+ return host;
+ }
+
+ /**
+ * @return the user
+ */
+ public String getUser() {
+ return user;
+ }
+
+ /**
+ * @return the password
+ */
+ @JsonIgnore
+ public String getPassword() {
+ return password;
+ }
+
+ @Override
+ public String toString() {
+ StringBuilder builder = new StringBuilder();
+ builder.append("JettyServer [name=").append(name).append(", host=").append(host).append(", port=").append(port)
+ .append(", user=").append(user).append(", password=").append((password != null)).append(", contextPath=")
+ .append(contextPath).append(", jettyServer=").append(jettyServer).append(", context=").append(this.context)
+ .append(", connector=").append(connector).append(", jettyThread=").append(jettyThread)
+ .append("]");
+ return builder.toString();
+ }
+
+}