replace web.xml with programmatic registration 39/70339/2
authorMichael Vorburger <vorburger@redhat.com>
Mon, 26 Feb 2018 13:45:53 +0000 (14:45 +0100)
committerMichael Vorburger <vorburger@redhat.com>
Wed, 4 Apr 2018 18:30:48 +0000 (18:30 +0000)
this is just the first step; once this is in,
we can replace much of the static etc. and directly
wire the web components and the OSGi beans and services.

The blueprint.xml is needed because of the weird AAA-170
issue with the pax-cdi-api @OsgiService annotation.

Using change Ia3a16df71384610a75acf3d28205c973c554d477
in AAA, we directly use the WebContextSecurer instead of
relying on aaa-shiro in code because that makes Neutron
code de-coupled from AAA (as it should be) and prevents
the KarafIniWebEnvironment dead-lock, see AAA-171.

Change-Id: I1dc13069b7e0ec456941f523e9dd4d560841f3e6
Signed-off-by: Michael Vorburger <vorburger@redhat.com>
northbound-api/pom.xml
northbound-api/src/main/java/org/opendaylight/neutron/northbound/api/WebInitializer.java [new file with mode: 0644]
northbound-api/src/main/resources/WEB-INF/web.xml [deleted file]
northbound-api/src/main/resources/org/opendaylight/blueprint/blueprint.xml [new file with mode: 0644]
transcriber/pom.xml

index edab405890651031cba6d7f728d78af7b41fec3d..3931ba8975a318d2afef85e317d1cb12274f5d95 100644 (file)
       <groupId>com.sun.jersey</groupId>
       <artifactId>jersey-server</artifactId>
     </dependency>
+    <dependency>
+      <groupId>com.sun.jersey</groupId>
+      <artifactId>jersey-servlet</artifactId>
+    </dependency>
     <dependency>
       <groupId>org.codehaus.enunciate</groupId>
       <artifactId>enunciate-core-annotations</artifactId>
       <artifactId>neutron-spi</artifactId>
       <version>${project.version}</version>
     </dependency>
+    <dependency>
+      <groupId>org.eclipse.jetty</groupId>
+      <artifactId>jetty-servlets</artifactId>
+    </dependency>
     <dependency>
       <groupId>org.osgi</groupId>
       <artifactId>org.osgi.core</artifactId>
     </dependency>
+    <dependency>
+      <groupId>javax.inject</groupId>
+      <artifactId>javax.inject</artifactId>
+      <optional>true</optional>
+    </dependency>
+    <dependency>
+      <groupId>org.opendaylight.aaa.web</groupId>
+      <artifactId>web-api</artifactId>
+      <version>0.8.0-SNAPSHOT</version>
+    </dependency>
   </dependencies>
   <build>
     <plugins>
+      <plugin>
+       <groupId>org.apache.aries.blueprint</groupId>
+       <artifactId>blueprint-maven-plugin</artifactId>
+      </plugin>
       <plugin>
         <groupId>org.apache.felix</groupId>
         <artifactId>maven-bundle-plugin</artifactId>
             <Import-Package>
               com.sun.jersey.spi.container.servlet,
               org.eclipse.jetty.servlets,
-              org.opendaylight.aaa.shiro.filters,
-              org.opendaylight.aaa.shiro.realm,
-              org.opendaylight.aaa.shiro.web.env,
-              org.apache.shiro.web.env,
               !org.codehaus.enunciate.jaxrs,
 
               com.sun.xml.bind;resolution:=optional,
               *
             </Import-Package>
 
-            <Web-ContextPath>/controller/nb/v2/neutron</Web-ContextPath>
-
             <!-- Workaround for javax.ws.rs version mismatch with jackson/jersey/moxy -->
             <Embed-Transitive>true</Embed-Transitive>
             <Embed-Dependency>org.eclipse.persistence.moxy;inline</Embed-Dependency>
diff --git a/northbound-api/src/main/java/org/opendaylight/neutron/northbound/api/WebInitializer.java b/northbound-api/src/main/java/org/opendaylight/neutron/northbound/api/WebInitializer.java
new file mode 100644 (file)
index 0000000..0d9003e
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * 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.neutron.northbound.api;
+
+import javax.annotation.PreDestroy;
+import javax.inject.Inject;
+import javax.inject.Singleton;
+import javax.servlet.ServletException;
+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;
+
+/**
+ * Initializer for web components.
+ *
+ * @author Michael Vorburger.ch
+ */
+@Singleton
+public class WebInitializer {
+
+    private final WebContextRegistration registraton;
+
+    @Inject
+    public WebInitializer(WebServer webServer, WebContextSecurer webContextSecurer) throws ServletException {
+        WebContextBuilder webContextBuilder = WebContext.builder()
+            .contextPath("/controller/nb/v2/neutron").supportsSessions(true)
+            // TODO confirm through testing that Jersey & Neutron are fine without sessions, and false instead true
+
+            .addServlet(ServletDetails.builder()
+                    .servlet(new com.sun.jersey.spi.container.servlet.ServletContainer(
+                            new NeutronNorthboundRSApplication()))
+                    .addUrlPattern("/*").build());
+
+        webContextSecurer.requireAuthentication(webContextBuilder, "/*");
+
+        this.registraton = webServer.registerWebContext(webContextBuilder.build());
+    }
+
+    @PreDestroy
+    public void close() {
+        registraton.close();
+    }
+
+}
diff --git a/northbound-api/src/main/resources/WEB-INF/web.xml b/northbound-api/src/main/resources/WEB-INF/web.xml
deleted file mode 100644 (file)
index 71376ed..0000000
+++ /dev/null
@@ -1,66 +0,0 @@
-<?xml version="1.0" encoding="ISO-8859-1"?>
-<!--
- Copyright (c) 2018 Intel Corporation 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
--->
-<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
-        version="3.0">
-  <servlet>
-    <servlet-name>JAXRSNeutron</servlet-name>
-    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
-    <init-param>
-      <param-name>javax.ws.rs.Application</param-name>
-      <param-value>org.opendaylight.neutron.northbound.api.NeutronNorthboundRSApplication</param-value>
-    </init-param>
-    <load-on-startup>1</load-on-startup>
-  </servlet>
-
-  <servlet-mapping>
-    <servlet-name>JAXRSNeutron</servlet-name>
-    <url-pattern>/*</url-pattern>
-  </servlet-mapping>
-
-  <context-param>
-    <param-name>shiroEnvironmentClass</param-name>
-    <param-value>org.opendaylight.aaa.shiro.web.env.KarafIniWebEnvironment</param-value>
-  </context-param>
-
-  <listener>
-    <listener-class>org.apache.shiro.web.env.EnvironmentLoaderListener</listener-class>
-  </listener>
-
-  <filter>
-    <filter-name>ShiroFilter</filter-name>
-    <filter-class>org.opendaylight.aaa.shiro.filters.AAAShiroFilter</filter-class>
-  </filter>
-
-  <filter-mapping>
-    <filter-name>ShiroFilter</filter-name>
-    <url-pattern>/*</url-pattern>
-  </filter-mapping>
-
-  <filter>
-    <filter-name>cross-origin-restconf</filter-name>
-    <filter-class>org.eclipse.jetty.servlets.CrossOriginFilter</filter-class>
-    <init-param>
-      <param-name>allowedOrigins</param-name>
-      <param-value>*</param-value>
-    </init-param>
-    <init-param>
-      <param-name>allowedMethods</param-name>
-      <param-value>GET,POST,OPTIONS,DELETE,PUT,HEAD</param-value>
-    </init-param>
-    <init-param>
-      <param-name>allowedHeaders</param-name>
-      <param-value>origin, content-type, accept, authorization</param-value>
-    </init-param>
-  </filter>
-  <filter-mapping>
-    <filter-name>cross-origin-restconf</filter-name>
-    <url-pattern>/*</url-pattern>
-  </filter-mapping>
-</web-app>
diff --git a/northbound-api/src/main/resources/org/opendaylight/blueprint/blueprint.xml b/northbound-api/src/main/resources/org/opendaylight/blueprint/blueprint.xml
new file mode 100644 (file)
index 0000000..53bc0ad
--- /dev/null
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
+  xmlns:odl="http://opendaylight.org/xmlns/blueprint/v1.0.0">
+
+  <reference id="webServer" interface="org.opendaylight.aaa.web.WebServer" />
+
+  <reference id="webContextSecurer" interface="org.opendaylight.aaa.web.WebContextSecurer" />
+
+</blueprint>
index d707acf842f9e2db84e9857d34bd31814a8032b4..004b994fb1a4bb2f185b67e9e4f2bf1a79c037d3 100644 (file)
       <plugin>
         <groupId>org.apache.aries.blueprint</groupId>
         <artifactId>blueprint-maven-plugin</artifactId>
+        <configuration>
+          <scanPaths>
+            <!-- This is required so that the @Singleton @Inject annotations used in northbound-api,
+                 which is a dependency of this project JUST for its BadRequestException, do not cause
+                 the autowire.xml generated in this project to include the <bean> etc. of northbound-api
+                 to be repeated (which would be wrong and causes e.g. "Unresolved ref/idref to component: webContextSecurer");
+                 see also https://wiki.opendaylight.org/view/BestPractices/DI_Guidelines#Nota_Bene for more background about this.
+                 A better solution may be to move the BadRequestException to a new artifact ("common" ?), or perhaps, seeing that it
+                 extends javax.ws.rs.WebApplicationException, make transcriber have its own Exception to be independent of JAX WS.  -->
+            <scanPath>org.opendaylight.neutron.transcriber</scanPath>
+          </scanPaths>
+        </configuration>
       </plugin>
     </plugins>
   </build>