From 0d62e1d1e8d0a7744a62f437605a4197ec6e3285 Mon Sep 17 00:00:00 2001 From: Jorge Hernandez Date: Wed, 8 Aug 2018 10:28:36 -0500 Subject: generic jetty filter and cadi support Change-Id: I363e44e85e1d89c6254218629010d5c3e1507e0a Issue-ID: POLICY-1043 Signed-off-by: Jorge Hernandez --- .../endpoints/http/server/HttpServletServer.java | 32 ++++++++++++++-------- .../http/server/HttpServletServerFactory.java | 12 ++++++++ .../http/server/internal/JettyServletServer.java | 17 ++++++++++++ 3 files changed, 49 insertions(+), 12 deletions(-) (limited to 'policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http') diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/HttpServletServer.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/HttpServletServer.java index c1d1a353..a2dd948a 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/HttpServletServer.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/HttpServletServer.java @@ -1,6 +1,6 @@ /* * ============LICENSE_START======================================================= - * policy-endpoints + * ONAP * ================================================================================ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ @@ -23,30 +23,38 @@ package org.onap.policy.common.endpoints.http.server; import org.onap.policy.common.capabilities.Startable; /** - * A Jetty Server to server REST Requests + * Http Servlet Server interface */ public interface HttpServletServer extends Startable { /** - * factory for managing and tracking DMAAP sources + * Factory of Http Servlet Servers */ - public static HttpServletServerFactory factory = new IndexedHttpServletServerFactory(); + HttpServletServerFactory factory = new IndexedHttpServletServerFactory(); /** * * @return port */ - public int getPort(); + int getPort(); /** * enables basic authentication with user and password on the the relative path relativeUriPath * - * @param user - * @param password - * @param relativeUriPath + * @param user user + * @param password password + * @param relativeUriPath relative path */ - public void setBasicAuthentication(String user, String password, String relativeUriPath); + void setBasicAuthentication(String user, String password, String relativeUriPath); + + /** + * adds a filter at the specified path + * + * @param filterPath filter path + * @param filterClass filter class + */ + void addFilterClass(String filterPath, String filterClass); /** * adds a JAX-RS servlet class to serve REST requests @@ -57,7 +65,7 @@ public interface HttpServletServer extends Startable { * @throws IllegalArgumentException unable to process because of invalid input * @throws IllegalStateException unable to process because of invalid state */ - public void addServletClass(String servletPath, String restClass); + void addServletClass(String servletPath, String restClass); /** * adds a package containing JAX-RS classes to serve REST requests @@ -68,7 +76,7 @@ public interface HttpServletServer extends Startable { * @throws IllegalArgumentException unable to process because of invalid input * @throws IllegalStateException unable to process because of invalid state */ - public void addServletPackage(String servletPath, String restPackage); + void addServletPackage(String servletPath, String restPackage); /** * blocking start of the http server @@ -79,5 +87,5 @@ public interface HttpServletServer extends Startable { * @throws IllegalArgumentException if arguments are invalid * @throws InterruptedException if the blocking operation is interrupted */ - public boolean waitedStart(long maxWaitTime) throws InterruptedException; + boolean waitedStart(long maxWaitTime) throws InterruptedException; } diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/HttpServletServerFactory.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/HttpServletServerFactory.java index c7d2b1bf..4a430b20 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/HttpServletServerFactory.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/HttpServletServerFactory.java @@ -26,6 +26,7 @@ import java.util.HashMap; import java.util.List; import java.util.Properties; +import org.onap.aaf.cadi.filter.CadiFilter; import org.onap.policy.common.endpoints.http.server.internal.JettyJerseyServer; import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties; import org.slf4j.Logger; @@ -222,6 +223,13 @@ class IndexedHttpServletServerFactory implements HttpServletServerFactory { https = Boolean.parseBoolean(httpsString); } + String aafString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + + serviceName + PolicyEndPointProperties.PROPERTY_AAF_SUFFIX); + boolean aaf = false; + if (aafString != null && !aafString.isEmpty()) { + aaf = Boolean.parseBoolean(httpsString); + } + HttpServletServer service = build(serviceName, https, hostName, servicePort, contextUriPath, swagger, managed); if (userName != null && !userName.isEmpty() && password != null && !password.isEmpty()) { service.setBasicAuthentication(userName, password, authUriPath); @@ -241,6 +249,10 @@ class IndexedHttpServletServerFactory implements HttpServletServerFactory { } } + if (aaf) { + service.addFilterClass(contextUriPath, CadiFilter.class.getCanonicalName()); + } + serviceList.add(service); } diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/internal/JettyServletServer.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/internal/JettyServletServer.java index a4cc9b5f..b22a9401 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/internal/JettyServletServer.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/internal/JettyServletServer.java @@ -22,6 +22,8 @@ package org.onap.policy.common.endpoints.http.server.internal; import com.fasterxml.jackson.annotation.JsonIgnore; +import java.util.EnumSet; +import javax.servlet.DispatcherType; import org.eclipse.jetty.security.ConstraintMapping; import org.eclipse.jetty.security.ConstraintSecurityHandler; import org.eclipse.jetty.security.HashLoginService; @@ -175,6 +177,21 @@ public abstract class JettyServletServer implements HttpServletServer, Runnable this(name, false, host, port, contextPath); } + @Override + public void addFilterClass(String aFilterPath, String aFilterClass) { + if (aFilterClass == null || aFilterClass.isEmpty()) { + throw new IllegalArgumentException("No filter class provided"); + } + + String filterPath = aFilterPath; + if (aFilterPath == null || aFilterPath.isEmpty()) { + filterPath = "/*"; + } + + context.addFilter(aFilterClass, filterPath, + EnumSet.of(DispatcherType.INCLUDE, DispatcherType.REQUEST)); + } + public ServerConnector httpsConnector() { SslContextFactory sslContextFactory = new SslContextFactory(); -- cgit 1.2.3-korg