Guard against multiple web registration attempts 88/73488/2
authorTom Pantelis <tompantelis@gmail.com>
Wed, 27 Jun 2018 16:35:57 +0000 (12:35 -0400)
committerTom Pantelis <tompantelis@gmail.com>
Fri, 29 Jun 2018 02:26:54 +0000 (02:26 +0000)
If both restconf-auth and noath features are installed, both
try to register the web context and PAX web fails and renders
restconf unusable.

JIRA: CONTROLLER-1844

Change-Id: Ie488c792d849616efa806e82121883fe73fb9c6e
Signed-off-by: Tom Pantelis <tompantelis@gmail.com>
restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/restconf/web/Bierman02WebRegistrarImpl.java

index 9e41c62e06290c1cc2ddb07a0e43be93e57c9d66..fb6f5bc821cb3a62dbf49bb4153095e8d4bdc481 100644 (file)
@@ -7,6 +7,7 @@
  */
 package org.opendaylight.netconf.sal.restconf.web;
 
+import java.util.concurrent.atomic.AtomicBoolean;
 import javax.servlet.ServletException;
 import javax.ws.rs.core.Application;
 import org.opendaylight.aaa.filterchain.configuration.CustomFilterAdapterConfiguration;
@@ -19,6 +20,8 @@ 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.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * Implementation of Bierman02WebRegistrar.
@@ -26,13 +29,15 @@ import org.opendaylight.aaa.web.servlet.ServletSupport;
  * @author Thomas Pantelis
  */
 public class Bierman02WebRegistrarImpl implements Bierman02WebRegistrar {
+    private static final Logger LOG = LoggerFactory.getLogger(Bierman02WebRegistrarImpl.class);
 
-    private WebContextRegistration registraton;
     private final WebServer webServer;
     private final WebContextSecurer webContextSecurer;
     private final ServletSupport servletSupport;
     private final Application webApp;
     private final CustomFilterAdapterConfiguration customFilterAdapterConfig;
+    private volatile WebContextRegistration registraton;
+    private final AtomicBoolean registered = new AtomicBoolean(false);
 
     public Bierman02WebRegistrarImpl(WebServer webServer,  WebContextSecurer webContextSecurer,
             ServletSupport servletSupport, Application webApp,
@@ -45,8 +50,10 @@ public class Bierman02WebRegistrarImpl implements Bierman02WebRegistrar {
     }
 
     public void close() {
-        if (registraton != null) {
-            registraton.close();
+        if (registered.compareAndSet(true, false)) {
+            if (registraton != null) {
+                registraton.close();
+            }
         }
     }
 
@@ -61,6 +68,11 @@ public class Bierman02WebRegistrarImpl implements Bierman02WebRegistrar {
     }
 
     private void register(boolean authenticate) {
+        if (!registered.compareAndSet(false, true)) {
+            LOG.warn("Web context has already been registered", new Exception("call site"));
+            return;
+        }
+
         WebContextBuilder webContextBuilder = WebContext.builder().contextPath("restconf").supportsSessions(true)
                 .addServlet(ServletDetails.builder().servlet(servletSupport.createHttpServletBuilder(webApp).build())
                     .addUrlPattern("/*").build())