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;
import org.opendaylight.aaa.web.servlet.ServletSupport;
+import org.opendaylight.yangtools.concepts.Registration;
/**
* Initializer for web components.
*/
@Singleton
public class WebInitializer {
-
- private final WebContextRegistration registraton;
+ private final Registration registraton;
@Inject
- public WebInitializer(WebServer webServer, ClaimCache claimCache, IIDMStore iidMStore,
- WebContextSecurer webContextSecurer, ServletSupport servletSupport,
- CustomFilterAdapterConfiguration customFilterAdapterConfig) throws ServletException {
+ public WebInitializer(final WebServer webServer, final ClaimCache claimCache, final IIDMStore iidMStore,
+ final WebContextSecurer webContextSecurer, final ServletSupport servletSupport,
+ final CustomFilterAdapterConfiguration customFilterAdapterConfig) throws ServletException {
WebContextBuilder webContextBuilder = WebContext.builder().contextPath("auth").supportsSessions(true)
webContextSecurer.requireAuthentication(webContextBuilder, "/*", "/moon/*");
- this.registraton = webServer.registerWebContext(webContextBuilder.build());
+ registraton = webServer.registerWebContext(webContextBuilder.build());
}
@PreDestroy
<artifactId>value</artifactId>
<classifier>annotations</classifier>
</dependency>
+ <dependency>
+ <groupId>org.opendaylight.yangtools</groupId>
+ <artifactId>concepts</artifactId>
+ </dependency>
</dependencies>
</project>
+++ /dev/null
-/*
- * 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.aaa.web;
-
-/**
- * {@link WebContext} registration.
- * Allows to {@link #close()} the web context, which unregisters its servlets, filters and listeners.
- *
- * @author Michael Vorburger.ch
- */
-public interface WebContextRegistration extends AutoCloseable {
-
- @Override
- void close(); // does not throw Exception
-
-}
package org.opendaylight.aaa.web;
import javax.servlet.ServletException;
+import org.opendaylight.yangtools.concepts.Registration;
/**
* Web server (HTTP). This service API allows ODL applications to register web
* @author Michael Vorburger.ch
*/
public interface WebServer {
-
/**
* Register a new web context.
*
* @return registration which allows to close the context (and remove its servlets etc.)
* @throws ServletException if registration of any of the components of the web context failed
*/
- WebContextRegistration registerWebContext(WebContext webContext) throws ServletException;
+ Registration registerWebContext(WebContext webContext) throws ServletException;
/**
* Base URL of this web server, without any contexts. In production, this would
* @return base URL, with http[s] prefix and port, NOT ending in slash
*/
String getBaseURL();
-
}
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.util.component.AbstractLifeCycle;
import org.opendaylight.aaa.web.WebContext;
-import org.opendaylight.aaa.web.WebContextRegistration;
import org.opendaylight.aaa.web.WebServer;
+import org.opendaylight.yangtools.concepts.Registration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
checkArgument(httpPort >= 0, "httpPort must be positive");
checkArgument(httpPort < 65536, "httpPort must < 65536");
- this.server = new Server();
+ server = new Server();
server.setStopAtShutdown(true);
http = new ServerConnector(server);
http.setIdleTimeout(HTTP_SERVER_IDLE_TIMEOUT);
server.addConnector(http);
- this.contextHandlerCollection = new ContextHandlerCollection();
+ contextHandlerCollection = new ContextHandlerCollection();
server.setHandler(contextHandlerCollection);
}
@PostConstruct
public void start() throws Exception {
server.start();
- this.httpPort = http.getLocalPort();
+ httpPort = http.getLocalPort();
LOG.info("Started Jetty-based HTTP web server on port {} ({}).", httpPort, hashCode());
}
}
@Override
- public synchronized WebContextRegistration registerWebContext(final WebContext webContext) throws ServletException {
+ public synchronized Registration registerWebContext(final WebContext webContext) throws ServletException {
String contextPathWithSlashPrefix = webContext.contextPath().startsWith("/")
? webContext.contextPath() : "/" + webContext.contextPath();
ServletContextHandler handler = new ServletContextHandler(contextHandlerCollection, contextPathWithSlashPrefix,
import com.google.common.io.CharStreams;
import java.io.IOException;
-import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import org.opendaylight.aaa.web.FilterDetails;
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.WebServer;
/**
* @author Michael Vorburger.ch
*/
public abstract class AbstractWebServerTest {
-
- /** Returns the WebServer to test. This should not return a new one on each call, but a fresh one per test. */
+ // Returns the WebServer to test. This should not return a new one on each call, but a fresh one per test.
protected abstract WebServer getWebServer();
@Test
public void testAddAfterStart() throws ServletException, IOException {
- WebContextBuilder webContextBuilder = WebContext.builder().contextPath("/test1");
- webContextBuilder.addServlet(
- ServletDetails.builder().addUrlPattern("/*").name("Test").servlet(new TestServlet()).build());
- WebContextRegistration webContextRegistration = getWebServer().registerWebContext(webContextBuilder.build());
- checkTestServlet(getWebServer().getBaseURL() + "/test1");
- webContextRegistration.close();
+ var webContext = WebContext.builder()
+ .contextPath("/test1")
+ .addServlet(ServletDetails.builder().addUrlPattern("/*").name("Test").servlet(new TestServlet()).build())
+ .build();
+ try (var webContextRegistration = getWebServer().registerWebContext(webContext)) {
+ checkTestServlet(getWebServer().getBaseURL() + "/test1");
+ }
}
@Test
public void testAddAfterStartWithoutSlashOnContext() throws ServletException, IOException {
// NB subtle difference to previous test: contextPath("test1") instead of /test1 with slash!
- WebContextBuilder webContextBuilder = WebContext.builder().contextPath("test1");
- webContextBuilder.addServlet(
- ServletDetails.builder().addUrlPattern("/*").name("Test").servlet(new TestServlet()).build());
- WebContextRegistration webContextRegistration = getWebServer().registerWebContext(webContextBuilder.build());
- checkTestServlet(getWebServer().getBaseURL() + "/test1");
- webContextRegistration.close();
+ var webContext = WebContext.builder()
+ .contextPath("test1")
+ .addServlet(ServletDetails.builder().addUrlPattern("/*").name("Test").servlet(new TestServlet()).build())
+ .build();
+ try (var webContextRegistration = getWebServer().registerWebContext(webContext)) {
+ checkTestServlet(getWebServer().getBaseURL() + "/test1");
+ }
}
@Test
public void testAddFilter() throws Exception {
- TestFilter testFilter = new TestFilter();
- WebContextBuilder webContextBuilder = WebContext.builder().contextPath("/testingFilters");
- webContextBuilder
+ var testFilter = new TestFilter();
+ var webContext = WebContext.builder()
+ .contextPath("/testingFilters")
.putContextParam("testParam1", "avalue")
- .addFilter(FilterDetails.builder().addUrlPattern("/*").name("Test").filter(testFilter).build());
- WebContextRegistration webContextRegistration = getWebServer().registerWebContext(webContextBuilder.build());
- assertTrue(testFilter.isInitialized);
- webContextRegistration.close();
+ .addFilter(FilterDetails.builder().addUrlPattern("/*").name("Test").filter(testFilter).build())
+ .build();
+ try (var webContextRegistration = getWebServer().registerWebContext(webContext)) {
+ assertTrue(testFilter.isInitialized);
+ }
}
@Test
public void testRegisterListener() throws Exception {
- WebContextBuilder webContextBuilder = WebContext.builder().contextPath("/testingListener");
- TestListener testListener = new TestListener();
- webContextBuilder.addListener(testListener);
+ var testListener = new TestListener();
+ var webContext = WebContext.builder()
+ .contextPath("/testingListener")
+ .addListener(testListener)
+ .build();
assertFalse(testListener.isInitialized);
- WebContextRegistration webContextRegistration = getWebServer().registerWebContext(webContextBuilder.build());
- assertTrue(testListener.isInitialized);
- webContextRegistration.close();
+ try (var webContextRegistration = getWebServer().registerWebContext(webContext)) {
+ assertTrue(testListener.isInitialized);
+ }
assertFalse(testListener.isInitialized);
}
- static void checkTestServlet(String urlPrefix) throws IOException {
- try (InputStream inputStream = new URL(urlPrefix + "/something").openConnection().getInputStream()) {
+ static void checkTestServlet(final String urlPrefix) throws IOException {
+ try (var inputStream = new URL(urlPrefix + "/something").openConnection().getInputStream()) {
// The hard-coded ASCII here is strictly speaking wrong of course
// (should interpret header from reply), but good enough for a test.
- try (InputStreamReader reader = new InputStreamReader(inputStream, StandardCharsets.US_ASCII)) {
+ try (var reader = new InputStreamReader(inputStream, StandardCharsets.US_ASCII)) {
assertEquals("hello, world\r\n", CharStreams.toString(reader));
}
}
import org.opendaylight.aaa.web.ResourceDetails;
import org.opendaylight.aaa.web.ServletDetails;
import org.opendaylight.aaa.web.WebContext;
-import org.opendaylight.aaa.web.WebContextRegistration;
import org.opendaylight.aaa.web.WebServer;
+import org.opendaylight.yangtools.concepts.AbstractRegistration;
+import org.opendaylight.yangtools.concepts.Registration;
import org.ops4j.pax.web.service.WebContainer;
import org.ops4j.pax.web.service.WebContainerDTO;
import org.osgi.framework.Bundle;
}
@Override
- public WebContextRegistration registerWebContext(final WebContext webContext) throws ServletException {
+ public Registration registerWebContext(final WebContext webContext) throws ServletException {
return new WebContextImpl(local, webContext);
}
LOG.info("Deactivated WebServer instance for {}", bundle);
}
- private static class WebContextImpl implements WebContextRegistration {
+ private static class WebContextImpl extends AbstractRegistration {
private final String contextPath;
private final WebContainer paxWeb;
private final List<Servlet> registeredServlets = new ArrayList<>();
// (it assumes always with session); but other implementation support without.
this.paxWeb = paxWeb;
- this.contextPath = webContext.contextPath();
+ contextPath = webContext.contextPath();
// NB This is NOT the URL prefix of the context, but the context.id which is
// used while registering the HttpContext in the OSGi service registry.
try {
for (ResourceDetails resource: webContext.resources()) {
- String alias = ensurePrependedSlash(this.contextPath + ensurePrependedSlash(resource.alias()));
+ String alias = ensurePrependedSlash(contextPath + ensurePrependedSlash(resource.alias()));
paxWeb.registerResources(alias, ensurePrependedSlash(resource.name()), osgiHttpContext);
registeredResources.add(alias);
}
}
@Override
- public void close() {
+ protected void removeRegistration() {
// The order is relevant here.. Servlets first, then Filters, Listeners last; this is the inverse of above
registeredServlets.forEach(paxWeb::unregisterServlet);
registeredFilters.forEach(paxWeb::unregisterFilter);
import org.junit.Test;
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.WebServer;
import org.opendaylight.aaa.web.testutils.TestWebClient;
import org.opendaylight.aaa.web.testutils.WebTestModule;
@Test
public void testServlet() throws ServletException, IOException, InterruptedException, URISyntaxException {
- WebContextBuilder webContextBuilder = WebContext.builder().contextPath("/test1");
- webContextBuilder.addServlet(
- ServletDetails.builder().addUrlPattern("/hello").name("Test").servlet(new TestServlet()).build());
- try (WebContextRegistration webContextRegistration = webServer.registerWebContext(webContextBuilder.build())) {
+ var webContext = WebContext.builder()
+ .contextPath("/test1")
+ .addServlet(ServletDetails.builder()
+ .addUrlPattern("/hello")
+ .name("Test")
+ .servlet(new TestServlet())
+ .build())
+ .build();
+ try (var webContextRegistration = webServer.registerWebContext(webContext)) {
assertEquals("hello, world", webClient.request("GET", "test1/hello").body());
assertEquals("hello, world", webClient.request("GET", "/test1/hello").body());
}