From 32ab41fa6a6998ff88cd9dbef31f7c65f39ee0e1 Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Mon, 23 Aug 2021 12:27:41 -0400 Subject: Support multiple filters in RestServer Added an API to RestServer so that multiple Filters may be provided. Issue-ID: POLICY-3531 Change-Id: I21f474c01d58237c744d1d70f5b39c2c90fdb401 Signed-off-by: Jim Hahn --- .../common/endpoints/http/server/RestServer.java | 34 ++++++++++++++++++---- .../endpoints/http/server/test/RestServerTest.java | 32 ++++++++++++++++++-- 2 files changed, 58 insertions(+), 8 deletions(-) (limited to 'policy-endpoints') diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/RestServer.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/RestServer.java index cb50bb33..133a1e7d 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/RestServer.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/RestServer.java @@ -27,6 +27,7 @@ import java.util.List; import java.util.Optional; import java.util.Properties; import java.util.stream.Collectors; +import javax.servlet.Filter; import lombok.ToString; import org.onap.policy.common.endpoints.http.server.aaf.AafAuthFilter; import org.onap.policy.common.endpoints.parameters.RestServerParameters; @@ -59,7 +60,28 @@ public class RestServer extends ServiceManagerContainer { public RestServer(final RestServerParameters restServerParameters, Class aafFilter, Class... jaxrsProviders) { - if (jaxrsProviders.length == 0) { + this(restServerParameters, makeFilterList(aafFilter), Arrays.asList(jaxrsProviders)); + } + + private static List> makeFilterList(Class aafFilter) { + if (aafFilter == null) { + return List.of(); + } else { + return List.of(aafFilter); + } + } + + /** + * Constructs the object. + * + * @param restServerParameters the rest server parameters + * @param filters class of object to use to filter requests, or {@code null} + * @param jaxrsProviders classes providing the services + */ + public RestServer(final RestServerParameters restServerParameters, List> filters, + List> jaxrsProviders) { + + if (jaxrsProviders.isEmpty()) { throw new IllegalArgumentException("no providers specified"); } @@ -67,8 +89,10 @@ public class RestServer extends ServiceManagerContainer { .build(getServerProperties(restServerParameters, getProviderClassNames(jaxrsProviders))); for (HttpServletServer server : this.servers) { - if (aafFilter != null && server.isAaf()) { - server.addFilterClass(null, aafFilter.getName()); + for (Class filter : filters) { + if (!AafAuthFilter.class.isAssignableFrom(filter) || server.isAaf()) { + server.addFilterClass(null, filter.getName()); + } } addAction("REST " + server.getName(), server::start, server::stop); @@ -123,8 +147,8 @@ public class RestServer extends ServiceManagerContainer { * @param jaxrsProviders classes providing the services * @return the provider class names */ - private String getProviderClassNames(Class[] jaxrsProviders) { - return String.join(",", Arrays.stream(jaxrsProviders).map(Class::getName).collect(Collectors.toList())); + private String getProviderClassNames(List> jaxrsProviders) { + return String.join(",", jaxrsProviders.stream().map(Class::getName).collect(Collectors.toList())); } private String getValue(final String value) { diff --git a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/RestServerTest.java b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/RestServerTest.java index 65af469b..1acbe5f5 100644 --- a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/RestServerTest.java +++ b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/RestServerTest.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved. * Modifications Copyright (C) 2021 Bell Canada. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -40,7 +40,12 @@ import java.net.URL; import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.Base64; +import java.util.List; import java.util.Properties; +import javax.servlet.FilterChain; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.ws.rs.Consumes; import javax.ws.rs.POST; @@ -105,7 +110,7 @@ public class RestServerTest { initRealParams(); - realRest = new RestServer(params, null, RealProvider.class) { + realRest = new RestServer(params, (Class) null, RealProvider.class) { @Override protected Properties getServerProperties(RestServerParameters restServerParameters, String names) { Properties props = super.getServerProperties(restServerParameters, names); @@ -166,6 +171,19 @@ public class RestServerTest { verify(server2).stop(); } + @Test + public void testRestServerListList() { + rest = new RestServer(params, List.of(Filter.class, Filter2.class), List.of(Provider1.class, Provider2.class)); + + rest.start(); + verify(server1).start(); + verify(server2).start(); + + rest.stop(); + verify(server1).stop(); + verify(server2).stop(); + } + @Test public void testRestServer_NoAaf() { rest = new RestServer(params, Filter.class, Provider1.class, Provider2.class); @@ -199,7 +217,7 @@ public class RestServerTest { when(server1.isAaf()).thenReturn(true); when(server2.isAaf()).thenReturn(true); - rest = new RestServer(params, null, Provider1.class, Provider2.class); + rest = new RestServer(params, (Class) null, Provider1.class, Provider2.class); verify(server1, never()).addFilterClass(any(), any()); verify(server2, never()).addFilterClass(any(), any()); @@ -365,6 +383,14 @@ public class RestServerTest { } } + private static class Filter2 implements javax.servlet.Filter { + @Override + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) + throws IOException, ServletException { + // do nothing + } + } + private static class Provider1 { private Provider1() { // do nothing -- cgit 1.2.3-korg