From 0a50b602405f56d7916dbed24f04a8a80466d093 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Sun, 8 Jan 2023 01:14:30 +0100 Subject: [PATCH] Add support for descriptive WebContext name 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 --- .../org/opendaylight/aaa/web/WebContext.java | 32 +++++++++++++++++-- .../aaa/web/osgi/WhiteboardWebServer.java | 3 +- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/web/api/src/main/java/org/opendaylight/aaa/web/WebContext.java b/web/api/src/main/java/org/opendaylight/aaa/web/WebContext.java index 76928a025..d6d875669 100644 --- a/web/api/src/main/java/org/opendaylight/aaa/web/WebContext.java +++ b/web/api/src/main/java/org/opendaylight/aaa/web/WebContext.java @@ -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. */ final class Builder { - private record ImmutableWebContext(String contextPath, ImmutableList servlets, + private record ImmutableWebContext(String name, String contextPath, ImmutableList servlets, ImmutableList filters, ImmutableList listeners, ImmutableList resources, ImmutableMap contextParams, boolean supportsSessions) implements WebContext { @@ -150,6 +159,7 @@ public interface WebContext { private final ImmutableList.Builder filters = ImmutableList.builder(); private final ImmutableList.Builder listeners = ImmutableList.builder(); private final ImmutableList.Builder 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); } } } diff --git a/web/impl-osgi/src/main/java/org/opendaylight/aaa/web/osgi/WhiteboardWebServer.java b/web/impl-osgi/src/main/java/org/opendaylight/aaa/web/osgi/WhiteboardWebServer.java index 6a2b16b6a..66bfed751 100644 --- a/web/impl-osgi/src/main/java/org/opendaylight/aaa/web/osgi/WhiteboardWebServer.java +++ b/web/impl-osgi/src/main/java/org/opendaylight/aaa/web/osgi/WhiteboardWebServer.java @@ -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); -- 2.36.6