Add web/servlet-api and jersey2 implementation 19/70919/9
authorRobert Varga <robert.varga@pantheon.tech>
Fri, 13 Apr 2018 15:08:40 +0000 (17:08 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Wed, 18 Apr 2018 10:00:07 +0000 (12:00 +0200)
web/servlet-api provides implementation-agnostic entrypoints for
creating Clients and HttpServlets.

web/servlet-jersey2 provides an implementation based on jersey-2.25.1.

This split allows us to have applications independent of the
implementation.

Change-Id: I77d92fb8764aa28817d5dcac9f8450dc42017429
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
artifacts/pom.xml
features/odl-aaa-web/pom.xml
web/pom.xml
web/servlet-api/pom.xml [new file with mode: 0644]
web/servlet-api/src/main/java/org/opendaylight/aaa/web/servlet/HttpServletBuilder.java [new file with mode: 0644]
web/servlet-api/src/main/java/org/opendaylight/aaa/web/servlet/ServletSupport.java [new file with mode: 0644]
web/servlet-jersey2/pom.xml [new file with mode: 0644]
web/servlet-jersey2/src/main/java/org/opendaylight/aaa/web/servlet/jersey2/JerseyHttpServletBuilder.java [new file with mode: 0644]
web/servlet-jersey2/src/main/java/org/opendaylight/aaa/web/servlet/jersey2/JerseyServletSupport.java [new file with mode: 0644]
web/servlet-jersey2/src/main/java/org/opendaylight/aaa/web/servlet/jersey2/osgi/Activator.java [new file with mode: 0644]

index 15f429313e78727ce3847133f15aac5edc63f8a1..77573fd781345c9883f41b7dcea7d205ee3ad901 100644 (file)
                 <version>${project.version}</version>
             </dependency>
 
+            <dependency>
+                <groupId>${project.groupId}.web</groupId>
+                <artifactId>web-api</artifactId>
+                <version>${project.version}</version>
+            </dependency>
+            <dependency>
+                <groupId>${project.groupId}.web</groupId>
+                <artifactId>web-osgi-impl</artifactId>
+                <version>${project.version}</version>
+            </dependency>
+            <dependency>
+                <groupId>${project.groupId}.web</groupId>
+                <artifactId>servlet-api</artifactId>
+                <version>${project.version}</version>
+            </dependency>
+            <dependency>
+                <groupId>${project.groupId}.web</groupId>
+                <artifactId>servlet-jersey2</artifactId>
+                <version>${project.version}</version>
+            </dependency>
+
             <dependency>
                 <groupId>${project.groupId}</groupId>
                 <artifactId>features-aaa</artifactId>
index 76e044dc071abbf0352e2c6f19a5a62827466e40..6fd090bec9b75ca5b9b9f432ec397033baab730c 100644 (file)
 
     <name>ODL :: aaa :: ${project.artifactId}</name>
 
+    <dependencyManagement>
+        <dependencies>
+            <dependency>
+                <groupId>${project.groupId}</groupId>
+                <artifactId>aaa-artifacts</artifactId>
+                <version>${project.version}</version>
+                <type>pom</type>
+                <scope>import</scope>
+            </dependency>
+            <dependency>
+                <groupId>org.glassfish.jersey</groupId>
+                <artifactId>jersey-bom</artifactId>
+                <version>2.25.1</version>
+                <type>pom</type>
+                <scope>import</scope>
+            </dependency>
+        </dependencies>
+    </dependencyManagement>
+
     <dependencies>
         <dependency>
             <groupId>org.opendaylight.odlparent</groupId>
         <dependency>
             <groupId>${project.groupId}.web</groupId>
             <artifactId>web-osgi-impl</artifactId>
-            <version>${project.version}</version>
         </dependency>
+
+        <dependency>
+            <groupId>${project.groupId}.web</groupId>
+            <artifactId>servlet-jersey2</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.glassfish.jersey.bundles.repackaged</groupId>
+            <artifactId>jersey-guava</artifactId>
+            <version>2.25.1</version>
+        </dependency>
+
         <dependency>
             <groupId>org.opendaylight.odlparent</groupId>
             <artifactId>odl-karaf-feat-jetty</artifactId>
index a578ae3bc6d2061902567991da1680f2626a8546..6926961ae8e242ae812429bd216bfb0c86a5da51 100644 (file)
@@ -27,5 +27,7 @@
     <module>api</module>
     <module>impl-osgi</module>
     <module>impl-jetty</module>
+    <module>servlet-api</module>
+    <module>servlet-jersey2</module>
   </modules>
 </project>
diff --git a/web/servlet-api/pom.xml b/web/servlet-api/pom.xml
new file mode 100644 (file)
index 0000000..51dc303
--- /dev/null
@@ -0,0 +1,45 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Copyright © 2018 Pantheon Technologies, s.r.o. 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
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+
+    <parent>
+        <groupId>org.opendaylight.aaa</groupId>
+        <artifactId>aaa-parent</artifactId>
+        <version>0.8.0-SNAPSHOT</version>
+        <relativePath>../../parent</relativePath>
+    </parent>
+
+    <groupId>org.opendaylight.aaa.web</groupId>
+    <artifactId>servlet-api</artifactId>
+    <name>ODL :: aaa :: ${project.artifactId}</name>
+    <packaging>bundle</packaging>
+
+    <dependencies>
+        <dependency>
+            <groupId>com.google.guava</groupId>
+            <artifactId>guava</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>javax.servlet</groupId>
+            <artifactId>javax.servlet-api</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>javax.ws.rs</groupId>
+            <artifactId>javax.ws.rs-api</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.opendaylight.yangtools</groupId>
+            <artifactId>concepts</artifactId>
+        </dependency>
+    </dependencies>
+
+</project>
diff --git a/web/servlet-api/src/main/java/org/opendaylight/aaa/web/servlet/HttpServletBuilder.java b/web/servlet-api/src/main/java/org/opendaylight/aaa/web/servlet/HttpServletBuilder.java
new file mode 100644 (file)
index 0000000..636553d
--- /dev/null
@@ -0,0 +1,27 @@
+/*
+ * Copyright (c) 2018 Pantheon Technologies s.r.o. 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.servlet;
+
+import com.google.common.annotations.Beta;
+import javax.annotation.concurrent.NotThreadSafe;
+import javax.servlet.http.HttpServlet;
+import javax.ws.rs.core.Application;
+import javax.ws.rs.core.Configurable;
+import org.opendaylight.yangtools.concepts.Builder;
+
+/**
+ * Utility methods for instantiating {@link HttpServlet}s from {@link Application}s, abstracting the servlet
+ * implementation from application developers.
+ *
+ * @author Robert Varga
+ */
+@Beta
+@NotThreadSafe
+public interface HttpServletBuilder extends Builder<HttpServlet>, Configurable<HttpServletBuilder> {
+
+}
diff --git a/web/servlet-api/src/main/java/org/opendaylight/aaa/web/servlet/ServletSupport.java b/web/servlet-api/src/main/java/org/opendaylight/aaa/web/servlet/ServletSupport.java
new file mode 100644 (file)
index 0000000..946f31a
--- /dev/null
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2018 Pantheon Technologies s.r.o. 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.servlet;
+
+import com.google.common.annotations.Beta;
+import javax.ws.rs.client.ClientBuilder;
+import javax.ws.rs.core.Application;
+import org.eclipse.jdt.annotation.NonNullByDefault;
+
+/**
+ * Main API entry point. An implementation of this interface is injected in the application.
+ *
+ * @author Robert Varga
+ */
+@Beta
+@NonNullByDefault
+public interface ServletSupport {
+    /**
+     * Create a new {@link ClientBuilder}.
+     *
+     * @return A new ClientBuilder.
+     */
+    ClientBuilder newClientBuilder();
+
+    /**
+     * Create a new {@link HttpServletBuilder} for an {@link Application}.
+     *
+     * @return A new HttpServletBuilder.
+     */
+    HttpServletBuilder createHttpServletBuilder(Application application);
+}
diff --git a/web/servlet-jersey2/pom.xml b/web/servlet-jersey2/pom.xml
new file mode 100644 (file)
index 0000000..9a258c5
--- /dev/null
@@ -0,0 +1,77 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Copyright © 2018 Pantheon Technologies, s.r.o. 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
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+
+    <parent>
+        <groupId>org.opendaylight.aaa</groupId>
+        <artifactId>aaa-parent</artifactId>
+        <version>0.8.0-SNAPSHOT</version>
+        <relativePath>../../parent</relativePath>
+    </parent>
+
+    <groupId>org.opendaylight.aaa.web</groupId>
+    <artifactId>servlet-jersey2</artifactId>
+    <name>ODL :: aaa :: ${project.artifactId}</name>
+    <packaging>bundle</packaging>
+
+
+    <dependencyManagement>
+        <dependencies>
+            <dependency>
+                <groupId>org.glassfish.jersey</groupId>
+                <artifactId>jersey-bom</artifactId>
+                <version>2.25.1</version>
+                <type>pom</type>
+                <scope>import</scope>
+            </dependency>
+        </dependencies>
+    </dependencyManagement>
+
+
+    <dependencies>
+        <dependency>
+            <groupId>org.opendaylight.aaa.web</groupId>
+            <artifactId>servlet-api</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.glassfish.jersey.containers</groupId>
+            <artifactId>jersey-container-servlet</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.glassfish.jersey.core</groupId>
+            <artifactId>jersey-client</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.core</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.kohsuke.metainf-services</groupId>
+            <artifactId>metainf-services</artifactId>
+        </dependency>
+    </dependencies>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.felix</groupId>
+                <artifactId>maven-bundle-plugin</artifactId>
+                <extensions>true</extensions>
+                <configuration>
+                    <instructions>
+                        <Include-Resource>{META-INF/services=${project.build.directory}/classes/META-INF/services}</Include-Resource>
+                        <Bundle-Activator>org.opendaylight.aaa.web.servlet.jersey2.osgi.Activator</Bundle-Activator>
+                    </instructions>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+</project>
diff --git a/web/servlet-jersey2/src/main/java/org/opendaylight/aaa/web/servlet/jersey2/JerseyHttpServletBuilder.java b/web/servlet-jersey2/src/main/java/org/opendaylight/aaa/web/servlet/jersey2/JerseyHttpServletBuilder.java
new file mode 100644 (file)
index 0000000..1159bca
--- /dev/null
@@ -0,0 +1,93 @@
+/*
+ * Copyright (c) 2018 Pantheon Technologies s.r.o. 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.servlet.jersey2;
+
+import java.util.Map;
+import javax.servlet.http.HttpServlet;
+import javax.ws.rs.core.Application;
+import javax.ws.rs.core.Configuration;
+import org.glassfish.jersey.server.ResourceConfig;
+import org.glassfish.jersey.servlet.ServletContainer;
+import org.opendaylight.aaa.web.servlet.HttpServletBuilder;
+
+/**
+ * Jersey-based implementation of {@link HttpServletBuilder}.
+ *
+ * @author Robert Varga
+ */
+final class JerseyHttpServletBuilder implements HttpServletBuilder {
+    private ResourceConfig config;
+
+    JerseyHttpServletBuilder(final Application application) {
+        config = ResourceConfig.forApplication(application);
+    }
+
+    @Override
+    public HttpServlet build() {
+        return new ServletContainer(config);
+    }
+
+    @Override
+    public Configuration getConfiguration() {
+        return config;
+    }
+
+    @Override
+    public HttpServletBuilder property(final String name, final Object value) {
+        config = config.property(name, value);
+        return this;
+    }
+
+    @Override
+    public HttpServletBuilder register(final Class<?> componentClass) {
+        config = config.register(componentClass);
+        return this;
+    }
+
+    @Override
+    public HttpServletBuilder register(final Class<?> componentClass, final int priority) {
+        config = config.register(componentClass, priority);
+        return this;
+    }
+
+    @Override
+    public HttpServletBuilder register(final Class<?> componentClass, final Class<?>... contracts) {
+        config = config.register(componentClass, contracts);
+        return this;
+    }
+
+    @Override
+    public HttpServletBuilder register(final Class<?> componentClass, final Map<Class<?>, Integer> contracts) {
+        config = config.register(componentClass, contracts);
+        return this;
+    }
+
+    @Override
+    public HttpServletBuilder register(final Object component) {
+        config = config.register(component);
+        return this;
+    }
+
+    @Override
+    public HttpServletBuilder register(final Object component, final int priority) {
+        config = config.register(component, priority);
+        return this;
+    }
+
+    @Override
+    public HttpServletBuilder register(final Object component, final Class<?>... contracts) {
+        config = config.register(component, contracts);
+        return this;
+    }
+
+    @Override
+    public HttpServletBuilder register(final Object component, final Map<Class<?>, Integer> contracts) {
+        config = config.register(component, contracts);
+        return this;
+    }
+}
diff --git a/web/servlet-jersey2/src/main/java/org/opendaylight/aaa/web/servlet/jersey2/JerseyServletSupport.java b/web/servlet-jersey2/src/main/java/org/opendaylight/aaa/web/servlet/jersey2/JerseyServletSupport.java
new file mode 100644 (file)
index 0000000..3bfe50e
--- /dev/null
@@ -0,0 +1,34 @@
+/*
+ * Copyright (c) 2018 Pantheon Technologies s.r.o. 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.servlet.jersey2;
+
+import com.google.common.annotations.Beta;
+import javax.annotation.concurrent.ThreadSafe;
+import javax.ws.rs.client.ClientBuilder;
+import javax.ws.rs.core.Application;
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.glassfish.jersey.client.JerseyClientBuilder;
+import org.kohsuke.MetaInfServices;
+import org.opendaylight.aaa.web.servlet.HttpServletBuilder;
+import org.opendaylight.aaa.web.servlet.ServletSupport;
+
+@Beta
+@MetaInfServices
+@NonNullByDefault
+@ThreadSafe
+public final class JerseyServletSupport implements ServletSupport {
+    @Override
+    public HttpServletBuilder createHttpServletBuilder(final Application application) {
+        return new JerseyHttpServletBuilder(application);
+    }
+
+    @Override
+    public ClientBuilder newClientBuilder() {
+        return new JerseyClientBuilder();
+    }
+}
diff --git a/web/servlet-jersey2/src/main/java/org/opendaylight/aaa/web/servlet/jersey2/osgi/Activator.java b/web/servlet-jersey2/src/main/java/org/opendaylight/aaa/web/servlet/jersey2/osgi/Activator.java
new file mode 100644 (file)
index 0000000..fad0a7d
--- /dev/null
@@ -0,0 +1,31 @@
+/*
+ * Copyright (c) 2018 Pantheon Technologies s.r.o. 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.servlet.jersey2.osgi;
+
+import org.opendaylight.aaa.web.servlet.ServletSupport;
+import org.opendaylight.aaa.web.servlet.jersey2.JerseyServletSupport;
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceRegistration;
+
+public class Activator implements BundleActivator {
+    private ServiceRegistration<ServletSupport> reg;
+
+    @Override
+    public void start(final BundleContext context) {
+        reg = context.registerService(ServletSupport.class, new JerseyServletSupport(), null);
+    }
+
+    @Override
+    public void stop(final BundleContext context) {
+        if (reg != null) {
+            reg.unregister();
+            reg = null;
+        }
+    }
+}