Add support for descriptive WebContext name 65/103965/3
authorRobert Varga <robert.varga@pantheon.tech>
Sun, 8 Jan 2023 00:14:30 +0000 (01:14 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Sun, 8 Jan 2023 15:03:53 +0000 (16:03 +0100)
OSGi allows for web context having a descriptive name, which does not
have a servlet equivalent. Allow users to provide it, but fall back to
'.id' suffix when they do not.

JIRA: AAA-243
Change-Id: I2ccef515df4d6aa3e44651be7edf6e4cc7373515
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
web/api/src/main/java/org/opendaylight/aaa/web/WebContext.java
web/impl-osgi/src/main/java/org/opendaylight/aaa/web/osgi/WhiteboardWebServer.java

index 76928a02599e68502e794bbc0128b05f7980577b..d6d87566997760d1ddc11ea1208a02e9e9bc8d93 100644 (file)
@@ -7,6 +7,8 @@
  */
 package org.opendaylight.aaa.web;
 
+import static java.util.Objects.requireNonNull;
+
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
 import java.util.List;
@@ -65,6 +67,13 @@ import org.eclipse.jdt.annotation.NonNull;
  * @author Michael Vorburger.ch
  */
 public interface WebContext {
+    /**
+     * Get the descriptive name of this context.
+     *
+     * @return A descriptive name.
+     */
+    @NonNull String name();
+
     /**
      * Get path which will be used as URL prefix to all registered servlets and filters. Guaranteed to be non-empty
      *
@@ -138,7 +147,7 @@ public interface WebContext {
      * collection, but instead used immediately to create instances.</em>
      */
     final class Builder {
-        private record ImmutableWebContext(String contextPath, ImmutableList<ServletDetails> servlets,
+        private record ImmutableWebContext(String name, String contextPath, ImmutableList<ServletDetails> servlets,
             ImmutableList<FilterDetails> filters, ImmutableList<ServletContextListener> listeners,
             ImmutableList<ResourceDetails> resources, ImmutableMap<String, String> contextParams,
             boolean supportsSessions) implements WebContext {
@@ -150,6 +159,7 @@ public interface WebContext {
         private final ImmutableList.Builder<FilterDetails> filters = ImmutableList.builder();
         private final ImmutableList.Builder<ServletContextListener> listeners = ImmutableList.builder();
         private final ImmutableList.Builder<ResourceDetails> resources = ImmutableList.builder();
+        private String name;
         private String contextPath;
         private boolean supportsSessions = true;
 
@@ -157,8 +167,23 @@ public interface WebContext {
             // Hidden on purpose
         }
 
+        /**
+         * Initializes the value for the {@link WebContext#name() name} attribute.
+         *
+         * @param name A descriptive name
+         * @return {@code this} builder for use in a chained invocation
+         * @throws IllegalArgumentException if {@code contextPath} does not meet specification criteria
+         * @throws NullPointerException if {code contextPath} is {@code null}
+         */
+        @SuppressWarnings("checkstyle:hiddenField")
+        public @NonNull Builder name(final String name) {
+            this.name = requireNonNull(name);
+            return this;
+        }
+
         /**
          * Initializes the value for the {@link WebContext#contextPath() contextPath} attribute. As per Servlet
+         * specification.
          *
          * @param contextPath The value for contextPath
          * @return {@code this} builder for use in a chained invocation
@@ -256,8 +281,9 @@ public interface WebContext {
             if (contextPath == null) {
                 throw new IllegalStateException("No contextPath specified");
             }
-            return new ImmutableWebContext(contextPath, servlets.build(), filters.build(), listeners.build(),
-                resources.build(), contextParams.build(), supportsSessions);
+
+            return new ImmutableWebContext(name != null ? name : contextPath + ".id", contextPath, servlets.build(),
+                filters.build(), listeners.build(), resources.build(), contextParams.build(), supportsSessions);
         }
     }
 }
index 6a2b16b6ad9bd2ddf7386eabba46990168643e10..66bfed751a33c74eec5a0f14e4941a72ec7c5e27 100644 (file)
@@ -106,9 +106,8 @@ public final class WhiteboardWebServer implements WebServer {
         // The order in which we set things up here matters...
 
         // 1. ServletContextHelper, to which all others are bound to
+        final var contextName = webContext.name();
         final var contextPath = webContext.contextPath();
-        // TODO: can we create a better name?
-        final var contextName = contextPath + ".id";
 
         final var contextProps = contextProperties(contextName, contextPath, webContext.contextParams());
         LOG.debug("Registering context {} with properties {}", contextName, contextProps);