Remove WebContextRegistration 01/101401/2
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 1 Jun 2022 01:02:09 +0000 (03:02 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Wed, 1 Jun 2022 01:32:44 +0000 (03:32 +0200)
We have yangtools as a dependecy and WebContextRegistration is not
bringing anything to the table when compared to
yangtools.concepts.Registration. Remove it and use a simple Regisration
instead.

Change-Id: Iec504fad5c56c5c8ee2cc5744700ec544fc0f7f5
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
aaa-shiro/impl/src/main/java/org/opendaylight/aaa/shiro/web/env/WebInitializer.java
web/api/pom.xml
web/api/src/main/java/org/opendaylight/aaa/web/WebContextRegistration.java [deleted file]
web/api/src/main/java/org/opendaylight/aaa/web/WebServer.java
web/impl-jetty/src/main/java/org/opendaylight/aaa/web/jetty/JettyWebServer.java
web/impl-jetty/src/test/java/org/opendaylight/aaa/web/test/AbstractWebServerTest.java
web/impl-osgi/src/main/java/org/opendaylight/aaa/web/osgi/PaxWebServer.java
web/testutils/src/test/java/org/opendaylight/aaa/web/testutils/test/WebTestModuleTest.java

index 04bae5f0876a4016e91097a393bad1b041117115..de82f61694bd0893778f3ec904277c6dfc53c14a 100644 (file)
@@ -20,10 +20,10 @@ import org.opendaylight.aaa.web.FilterDetails;
 import org.opendaylight.aaa.web.ServletDetails;
 import org.opendaylight.aaa.web.WebContext;
 import org.opendaylight.aaa.web.WebContextBuilder;
-import org.opendaylight.aaa.web.WebContextRegistration;
 import org.opendaylight.aaa.web.WebContextSecurer;
 import org.opendaylight.aaa.web.WebServer;
 import org.opendaylight.aaa.web.servlet.ServletSupport;
+import org.opendaylight.yangtools.concepts.Registration;
 
 /**
  * Initializer for web components.
@@ -35,13 +35,12 @@ import org.opendaylight.aaa.web.servlet.ServletSupport;
  */
 @Singleton
 public class WebInitializer {
-
-    private final WebContextRegistration registraton;
+    private final Registration registraton;
 
     @Inject
-    public WebInitializer(WebServer webServer, ClaimCache claimCache, IIDMStore iidMStore,
-            WebContextSecurer webContextSecurer, ServletSupport servletSupport,
-            CustomFilterAdapterConfiguration customFilterAdapterConfig) throws ServletException {
+    public WebInitializer(final WebServer webServer, final ClaimCache claimCache, final IIDMStore iidMStore,
+            final WebContextSecurer webContextSecurer, final ServletSupport servletSupport,
+            final CustomFilterAdapterConfiguration customFilterAdapterConfig) throws ServletException {
 
         WebContextBuilder webContextBuilder = WebContext.builder().contextPath("auth").supportsSessions(true)
 
@@ -55,7 +54,7 @@ public class WebInitializer {
 
         webContextSecurer.requireAuthentication(webContextBuilder, "/*", "/moon/*");
 
-        this.registraton = webServer.registerWebContext(webContextBuilder.build());
+        registraton = webServer.registerWebContext(webContextBuilder.build());
     }
 
     @PreDestroy
index a80f362164ff47a49d8ed8439aaf92bfc0cf8813..6128ce6b2b63b8a8db7860fa5880e0af1de046f6 100644 (file)
@@ -32,5 +32,9 @@
       <artifactId>value</artifactId>
       <classifier>annotations</classifier>
     </dependency>
+    <dependency>
+      <groupId>org.opendaylight.yangtools</groupId>
+      <artifactId>concepts</artifactId>
+    </dependency>
   </dependencies>
 </project>
diff --git a/web/api/src/main/java/org/opendaylight/aaa/web/WebContextRegistration.java b/web/api/src/main/java/org/opendaylight/aaa/web/WebContextRegistration.java
deleted file mode 100644 (file)
index 9faab1e..0000000
+++ /dev/null
@@ -1,21 +0,0 @@
-/*
- * Copyright (c) 2018 Red Hat, Inc. and others. All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-package org.opendaylight.aaa.web;
-
-/**
- * {@link WebContext} registration.
- * Allows to {@link #close()} the web context, which unregisters its servlets, filters and listeners.
- *
- * @author Michael Vorburger.ch
- */
-public interface WebContextRegistration extends AutoCloseable {
-
-    @Override
-    void close(); // does not throw Exception
-
-}
index ceb24f3d52298c25cf4b8a4ea1f9ca0b20a7c662..c7964381fcce33a5a2a9755051537cb660023f91 100644 (file)
@@ -8,6 +8,7 @@
 package org.opendaylight.aaa.web;
 
 import javax.servlet.ServletException;
+import org.opendaylight.yangtools.concepts.Registration;
 
 /**
  * Web server (HTTP). This service API allows ODL applications to register web
@@ -21,7 +22,6 @@ import javax.servlet.ServletException;
  * @author Michael Vorburger.ch
  */
 public interface WebServer {
-
     /**
      * Register a new web context.
      *
@@ -29,7 +29,7 @@ public interface WebServer {
      * @return registration which allows to close the context (and remove its servlets etc.)
      * @throws ServletException if registration of any of the components of the web context failed
      */
-    WebContextRegistration registerWebContext(WebContext webContext) throws ServletException;
+    Registration registerWebContext(WebContext webContext) throws ServletException;
 
     /**
      * Base URL of this web server, without any contexts. In production, this would
@@ -40,5 +40,4 @@ public interface WebServer {
      * @return base URL, with http[s] prefix and port, NOT ending in slash
      */
     String getBaseURL();
-
 }
index 77bfde376a389268ca578be23c5fb0757129ca46..35837c56820d7e38a81211873eabc15be51e852c 100644 (file)
@@ -23,8 +23,8 @@ import org.eclipse.jetty.servlet.ServletContextHandler;
 import org.eclipse.jetty.servlet.ServletHolder;
 import org.eclipse.jetty.util.component.AbstractLifeCycle;
 import org.opendaylight.aaa.web.WebContext;
-import org.opendaylight.aaa.web.WebContextRegistration;
 import org.opendaylight.aaa.web.WebServer;
+import org.opendaylight.yangtools.concepts.Registration;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -53,7 +53,7 @@ public class JettyWebServer implements WebServer {
         checkArgument(httpPort >= 0, "httpPort must be positive");
         checkArgument(httpPort < 65536, "httpPort must < 65536");
 
-        this.server = new Server();
+        server = new Server();
         server.setStopAtShutdown(true);
 
         http = new ServerConnector(server);
@@ -62,7 +62,7 @@ public class JettyWebServer implements WebServer {
         http.setIdleTimeout(HTTP_SERVER_IDLE_TIMEOUT);
         server.addConnector(http);
 
-        this.contextHandlerCollection = new ContextHandlerCollection();
+        contextHandlerCollection = new ContextHandlerCollection();
         server.setHandler(contextHandlerCollection);
     }
 
@@ -77,7 +77,7 @@ public class JettyWebServer implements WebServer {
     @PostConstruct
     public void start() throws Exception {
         server.start();
-        this.httpPort = http.getLocalPort();
+        httpPort = http.getLocalPort();
         LOG.info("Started Jetty-based HTTP web server on port {} ({}).", httpPort, hashCode());
     }
 
@@ -90,7 +90,7 @@ public class JettyWebServer implements WebServer {
     }
 
     @Override
-    public synchronized WebContextRegistration registerWebContext(final WebContext webContext) throws ServletException {
+    public synchronized Registration registerWebContext(final WebContext webContext) throws ServletException {
         String contextPathWithSlashPrefix = webContext.contextPath().startsWith("/")
                 ? webContext.contextPath() : "/" + webContext.contextPath();
         ServletContextHandler handler = new ServletContextHandler(contextHandlerCollection, contextPathWithSlashPrefix,
index 2907347966bd137d845cdf6ae25b3e0a7889fb7e..56426de3b8136c47727825c30ef158ec1957faa2 100644 (file)
@@ -13,7 +13,6 @@ import static org.junit.Assert.assertTrue;
 
 import com.google.common.io.CharStreams;
 import java.io.IOException;
-import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.net.URL;
 import java.nio.charset.StandardCharsets;
@@ -22,8 +21,6 @@ import org.junit.Test;
 import org.opendaylight.aaa.web.FilterDetails;
 import org.opendaylight.aaa.web.ServletDetails;
 import org.opendaylight.aaa.web.WebContext;
-import org.opendaylight.aaa.web.WebContextBuilder;
-import org.opendaylight.aaa.web.WebContextRegistration;
 import org.opendaylight.aaa.web.WebServer;
 
 /**
@@ -32,60 +29,64 @@ import org.opendaylight.aaa.web.WebServer;
  * @author Michael Vorburger.ch
  */
 public abstract class AbstractWebServerTest {
-
-    /** Returns the WebServer to test.  This should not return a new one on each call, but a fresh one per test. */
+    // Returns the WebServer to test.  This should not return a new one on each call, but a fresh one per test.
     protected abstract WebServer getWebServer();
 
     @Test
     public void testAddAfterStart() throws ServletException, IOException {
-        WebContextBuilder webContextBuilder = WebContext.builder().contextPath("/test1");
-        webContextBuilder.addServlet(
-                ServletDetails.builder().addUrlPattern("/*").name("Test").servlet(new TestServlet()).build());
-        WebContextRegistration webContextRegistration = getWebServer().registerWebContext(webContextBuilder.build());
-        checkTestServlet(getWebServer().getBaseURL() + "/test1");
-        webContextRegistration.close();
+        var webContext = WebContext.builder()
+            .contextPath("/test1")
+            .addServlet(ServletDetails.builder().addUrlPattern("/*").name("Test").servlet(new TestServlet()).build())
+            .build();
+        try (var webContextRegistration = getWebServer().registerWebContext(webContext)) {
+            checkTestServlet(getWebServer().getBaseURL() + "/test1");
+        }
     }
 
     @Test
     public void testAddAfterStartWithoutSlashOnContext() throws ServletException, IOException {
         // NB subtle difference to previous test: contextPath("test1") instead of /test1 with slash!
-        WebContextBuilder webContextBuilder = WebContext.builder().contextPath("test1");
-        webContextBuilder.addServlet(
-                ServletDetails.builder().addUrlPattern("/*").name("Test").servlet(new TestServlet()).build());
-        WebContextRegistration webContextRegistration = getWebServer().registerWebContext(webContextBuilder.build());
-        checkTestServlet(getWebServer().getBaseURL() + "/test1");
-        webContextRegistration.close();
+        var webContext = WebContext.builder()
+            .contextPath("test1")
+            .addServlet(ServletDetails.builder().addUrlPattern("/*").name("Test").servlet(new TestServlet()).build())
+            .build();
+        try (var webContextRegistration = getWebServer().registerWebContext(webContext)) {
+            checkTestServlet(getWebServer().getBaseURL() + "/test1");
+        }
     }
 
     @Test
     public void testAddFilter() throws Exception {
-        TestFilter testFilter = new TestFilter();
-        WebContextBuilder webContextBuilder = WebContext.builder().contextPath("/testingFilters");
-        webContextBuilder
+        var testFilter = new TestFilter();
+        var webContext = WebContext.builder()
+            .contextPath("/testingFilters")
             .putContextParam("testParam1", "avalue")
-            .addFilter(FilterDetails.builder().addUrlPattern("/*").name("Test").filter(testFilter).build());
-        WebContextRegistration webContextRegistration = getWebServer().registerWebContext(webContextBuilder.build());
-        assertTrue(testFilter.isInitialized);
-        webContextRegistration.close();
+            .addFilter(FilterDetails.builder().addUrlPattern("/*").name("Test").filter(testFilter).build())
+            .build();
+        try (var webContextRegistration = getWebServer().registerWebContext(webContext)) {
+            assertTrue(testFilter.isInitialized);
+        }
     }
 
     @Test
     public void testRegisterListener() throws Exception {
-        WebContextBuilder webContextBuilder = WebContext.builder().contextPath("/testingListener");
-        TestListener testListener = new TestListener();
-        webContextBuilder.addListener(testListener);
+        var testListener = new TestListener();
+        var webContext = WebContext.builder()
+            .contextPath("/testingListener")
+            .addListener(testListener)
+            .build();
         assertFalse(testListener.isInitialized);
-        WebContextRegistration webContextRegistration = getWebServer().registerWebContext(webContextBuilder.build());
-        assertTrue(testListener.isInitialized);
-        webContextRegistration.close();
+        try (var webContextRegistration = getWebServer().registerWebContext(webContext)) {
+            assertTrue(testListener.isInitialized);
+        }
         assertFalse(testListener.isInitialized);
     }
 
-    static void checkTestServlet(String urlPrefix) throws IOException {
-        try (InputStream inputStream = new URL(urlPrefix + "/something").openConnection().getInputStream()) {
+    static void checkTestServlet(final String urlPrefix) throws IOException {
+        try (var inputStream = new URL(urlPrefix + "/something").openConnection().getInputStream()) {
             // The hard-coded ASCII here is strictly speaking wrong of course
             // (should interpret header from reply), but good enough for a test.
-            try (InputStreamReader reader = new InputStreamReader(inputStream, StandardCharsets.US_ASCII)) {
+            try (var reader = new InputStreamReader(inputStream, StandardCharsets.US_ASCII)) {
                 assertEquals("hello, world\r\n", CharStreams.toString(reader));
             }
         }
index 4287efce1315322da1cd461a18adebde7d05649e..1ec9e38c5ab79865a95aa9e748a477e96f45c936 100644 (file)
@@ -22,8 +22,9 @@ import org.opendaylight.aaa.web.FilterDetails;
 import org.opendaylight.aaa.web.ResourceDetails;
 import org.opendaylight.aaa.web.ServletDetails;
 import org.opendaylight.aaa.web.WebContext;
-import org.opendaylight.aaa.web.WebContextRegistration;
 import org.opendaylight.aaa.web.WebServer;
+import org.opendaylight.yangtools.concepts.AbstractRegistration;
+import org.opendaylight.yangtools.concepts.Registration;
 import org.ops4j.pax.web.service.WebContainer;
 import org.ops4j.pax.web.service.WebContainerDTO;
 import org.osgi.framework.Bundle;
@@ -75,7 +76,7 @@ public final class PaxWebServer implements WebServer {
     }
 
     @Override
-    public WebContextRegistration registerWebContext(final WebContext webContext) throws ServletException {
+    public Registration registerWebContext(final WebContext webContext) throws ServletException {
         return new WebContextImpl(local, webContext);
     }
 
@@ -100,7 +101,7 @@ public final class PaxWebServer implements WebServer {
         LOG.info("Deactivated WebServer instance for {}", bundle);
     }
 
-    private static class WebContextImpl implements WebContextRegistration {
+    private static class WebContextImpl extends AbstractRegistration {
         private final String contextPath;
         private final WebContainer paxWeb;
         private final List<Servlet> registeredServlets = new ArrayList<>();
@@ -114,7 +115,7 @@ public final class PaxWebServer implements WebServer {
             // (it assumes always with session); but other implementation support without.
 
             this.paxWeb = paxWeb;
-            this.contextPath = webContext.contextPath();
+            contextPath = webContext.contextPath();
 
             // NB This is NOT the URL prefix of the context, but the context.id which is
             // used while registering the HttpContext in the OSGi service registry.
@@ -147,7 +148,7 @@ public final class PaxWebServer implements WebServer {
 
             try {
                 for (ResourceDetails resource: webContext.resources()) {
-                    String alias = ensurePrependedSlash(this.contextPath + ensurePrependedSlash(resource.alias()));
+                    String alias = ensurePrependedSlash(contextPath + ensurePrependedSlash(resource.alias()));
                     paxWeb.registerResources(alias, ensurePrependedSlash(resource.name()), osgiHttpContext);
                     registeredResources.add(alias);
                 }
@@ -195,7 +196,7 @@ public final class PaxWebServer implements WebServer {
         }
 
         @Override
-        public void close() {
+        protected void removeRegistration() {
             // The order is relevant here.. Servlets first, then Filters, Listeners last; this is the inverse of above
             registeredServlets.forEach(paxWeb::unregisterServlet);
             registeredFilters.forEach(paxWeb::unregisterFilter);
index 113320cf84196afa6708769900a86ca46c6bce4b..2480b19c9fd0b0288fb58ecd0a807ae741be202b 100644 (file)
@@ -20,8 +20,6 @@ import org.junit.Rule;
 import org.junit.Test;
 import org.opendaylight.aaa.web.ServletDetails;
 import org.opendaylight.aaa.web.WebContext;
-import org.opendaylight.aaa.web.WebContextBuilder;
-import org.opendaylight.aaa.web.WebContextRegistration;
 import org.opendaylight.aaa.web.WebServer;
 import org.opendaylight.aaa.web.testutils.TestWebClient;
 import org.opendaylight.aaa.web.testutils.WebTestModule;
@@ -42,10 +40,15 @@ public class WebTestModuleTest {
 
     @Test
     public void testServlet() throws ServletException, IOException, InterruptedException, URISyntaxException {
-        WebContextBuilder webContextBuilder = WebContext.builder().contextPath("/test1");
-        webContextBuilder.addServlet(
-                ServletDetails.builder().addUrlPattern("/hello").name("Test").servlet(new TestServlet()).build());
-        try (WebContextRegistration webContextRegistration = webServer.registerWebContext(webContextBuilder.build())) {
+        var webContext = WebContext.builder()
+            .contextPath("/test1")
+            .addServlet(ServletDetails.builder()
+                .addUrlPattern("/hello")
+                .name("Test")
+                .servlet(new TestServlet())
+                .build())
+            .build();
+        try (var webContextRegistration = webServer.registerWebContext(webContext)) {
             assertEquals("hello, world", webClient.request("GET", "test1/hello").body());
             assertEquals("hello, world", webClient.request("GET", "/test1/hello").body());
         }