}
@Override
- public void requireAuthentication(WebContextBuilder webContextBuilder, String... urlPatterns) {
- webContextBuilder
- .addListener(shiroEnvironmentLoaderListener)
-
- // AAA filter in front of these REST web services as well as for moon endpoints
- .addFilter(FilterDetails.builder().filter(new AAAShiroFilter()).addUrlPatterns(urlPatterns).build())
-
- // CORS filter
- .addFilter(FilterDetails.builder().filter(new CrossOriginFilter()).addUrlPatterns(urlPatterns)
- .putInitParam("allowedOrigins", "*")
- .putInitParam("allowedMethods", "GET,POST,OPTIONS,DELETE,PUT,HEAD")
- .putInitParam("allowedHeaders", "origin, content-type, accept, authorization")
- .build());
+ public void requireAuthentication(WebContextBuilder webContextBuilder, boolean asyncSupported,
+ String... urlPatterns) {
+ webContextBuilder.addListener(shiroEnvironmentLoaderListener)
+
+ // AAA filter in front of these REST web services as well as for moon endpoints
+ .addFilter(FilterDetails.builder()
+ .filter(new AAAShiroFilter())
+ .addUrlPatterns(urlPatterns)
+ .asyncSupported(asyncSupported)
+ .build())
+
+ // CORS filter
+ .addFilter(FilterDetails.builder()
+ .filter(new CrossOriginFilter())
+ .addUrlPatterns(urlPatterns)
+ .asyncSupported(asyncSupported)
+ .putInitParam("allowedOrigins", "*")
+ .putInitParam("allowedMethods", "GET,POST,OPTIONS,DELETE,PUT,HEAD")
+ .putInitParam("allowedHeaders", "origin, content-type, accept, authorization")
+ .build());
+
}
}
Map<String, String> initParams();
+ @Default default Boolean getAsyncSupported() {
+ return false;
+ }
+
}
Map<String, String> initParams();
+ @Default default Boolean getAsyncSupported() {
+ return false;
+ }
}
* @author Michael Vorburger.ch
*/
public interface WebContextSecurer {
+ /**
+ * Configures the WebContext in an implementation specific manner so that it requires authentication to access the
+ * given URL Patterns. Typically, this will be done by adding a {@code javax.servlet.Filter} (or several, and
+ * whatever else they need).
+ *
+ * @param webContextBuilder builder to secure
+ * @param asyncSupported true if asynchronous communication should also be supported
+ * @param urlPatterns URL patterns that require authentication
+ */
+ void requireAuthentication(WebContextBuilder webContextBuilder, boolean asyncSupported, String... urlPatterns);
/**
- * Configures the WebContext in an implementation specific manner so that it requires
- * authentication to access the given URL Patterns. Typically, this will be done by
- * adding a <code>javax.servlet.Filter</code> (or several, and whatever else they need).
+ * Configures the WebContext in an implementation specific manner so that it requires authentication to access the
+ * given URL Patterns. Typically, this will be done by adding a {@code javax.servlet.Filter} (or several, and
+ * whatever else they need).
+ *
+ * <p>
+ * This method is equivalent to {@code requireAuthentication(webContextBuilder, false, urlPatterns}.
*/
- void requireAuthentication(WebContextBuilder webContextBuilder, String... urlPatterns);
+ default void requireAuthentication(final WebContextBuilder webContextBuilder, final String... urlPatterns) {
+ requireAuthentication(webContextBuilder, false, urlPatterns);
+ }
/**
- * Configures the WebContext so that all its URL patterns (<code>/**</code>) require authentication.
+ * Configures the WebContext so that all its URL patterns ({@code/**}) require authentication.
* @see #requireAuthentication(WebContextBuilder, String...)
*/
- default void requireAuthentication(WebContextBuilder webContextBuilder) {
+ default void requireAuthentication(final WebContextBuilder webContextBuilder) {
requireAuthentication(webContextBuilder, "/*");
}
}
--- /dev/null
+/*
+ * Copyright (c) 2020 Lumina Networks, Inc. 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.tests;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThrows;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+
+import javax.servlet.Filter;
+import org.junit.Test;
+import org.opendaylight.aaa.web.FilterDetails;
+
+public class FilterDetailsTest {
+
+ @Test
+ public void testDefaultValue() {
+ FilterDetails filterDetails = FilterDetails.builder()
+ .filter(mock(Filter.class))
+ .addUrlPattern("test")
+ .addUrlPattern("another")
+ .name("custom")
+ .putInitParam("key", "value")
+ .build();
+
+ assertFalse(filterDetails.getAsyncSupported());
+ }
+
+ @Test
+ public void testAsyncFalse() {
+ FilterDetails filterDetails = FilterDetails.builder()
+ .filter(mock(Filter.class))
+ .addUrlPattern("test")
+ .addUrlPattern("another")
+ .name("custom")
+ .putInitParam("key", "value")
+ .asyncSupported(false)
+ .build();
+
+ assertFalse(filterDetails.getAsyncSupported());
+ }
+
+ @Test
+ public void testAsyncTrue() {
+ FilterDetails filterDetails = FilterDetails.builder()
+ .filter(mock(Filter.class))
+ .addUrlPattern("test")
+ .addUrlPattern("another")
+ .name("custom")
+ .putInitParam("key", "value")
+ .asyncSupported(true)
+ .build();
+
+ assertTrue(filterDetails.getAsyncSupported());
+ }
+
+ @Test
+ public void testException() {
+ assertThrows(IllegalStateException.class, () -> {
+ FilterDetails.builder().build();
+ });
+ }
+
+}
--- /dev/null
+/*
+ * Copyright (c) 2020 Lumina Networks, Inc. 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.tests;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThrows;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+
+import javax.servlet.Servlet;
+import org.junit.Test;
+import org.opendaylight.aaa.web.ServletDetails;
+
+public class ServletDetailsTest {
+
+ @Test
+ public void testDefaultValue() {
+ ServletDetails servletDetails = ServletDetails.builder()
+ .servlet(mock(Servlet.class))
+ .addUrlPattern("test")
+ .addUrlPattern("another")
+ .name("custom")
+ .putInitParam("key", "value")
+ .build();
+
+ assertFalse(servletDetails.getAsyncSupported());
+ }
+
+ @Test
+ public void testAsyncFalse() {
+ ServletDetails servletDetails = ServletDetails.builder()
+ .servlet(mock(Servlet.class))
+ .addUrlPattern("test")
+ .addUrlPattern("another")
+ .name("custom")
+ .putInitParam("key", "value")
+ .asyncSupported(false)
+ .build();
+
+ assertFalse(servletDetails.getAsyncSupported());
+ }
+
+ @Test
+ public void testAsyncTrue() {
+ ServletDetails servletDetails = ServletDetails.builder()
+ .servlet(mock(Servlet.class))
+ .addUrlPattern("test")
+ .addUrlPattern("another")
+ .name("custom")
+ .putInitParam("key", "value")
+ .asyncSupported(true)
+ .build();
+
+ assertTrue(servletDetails.getAsyncSupported());
+ }
+
+ @Test
+ public void testException() {
+ assertThrows(IllegalStateException.class, () -> {
+ ServletDetails.builder().build();
+ });
+ }
+}
// 3. Filters - because subsequent servlets should already be covered by the filters
for (FilterDetails filter : webContext.filters()) {
registerFilter(osgiHttpContext, filter.urlPatterns(), filter.name(), filter.filter(),
- filter.initParams());
+ filter.initParams(), filter.getAsyncSupported());
}
// 4. servlets - 'bout time for 'em by now, don't you think? ;)
for (ServletDetails servlet : webContext.servlets()) {
registerServlet(osgiHttpContext, servlet.urlPatterns(), servlet.name(), servlet.servlet(),
- servlet.initParams());
+ servlet.initParams(), servlet.getAsyncSupported());
}
try {
}
private void registerFilter(final HttpContext osgiHttpContext, final List<String> urlPatterns,
- final String name, final Filter filter, final Map<String, String> params) {
- boolean asyncSupported = false;
+ final String name, final Filter filter, final Map<String, String> params,
+ Boolean asyncSupported) {
String[] absUrlPatterns = absolute(urlPatterns);
- LOG.info("Registering Filter for aliases {}: {}", absUrlPatterns, filter);
+ LOG.info("Registering Filter for aliases {}: {} with async: {}", absUrlPatterns,
+ filter, asyncSupported);
paxWeb.registerFilter(filter, absUrlPatterns, new String[] { name }, new MapDictionary<>(params),
asyncSupported, osgiHttpContext);
registeredFilters.add(filter);
}
private void registerServlet(final HttpContext osgiHttpContext, final List<String> urlPatterns,
- final String name, final Servlet servlet, final Map<String, String> params) throws ServletException {
+ final String name, final Servlet servlet, final Map<String, String> params,
+ Boolean asyncSupported) throws ServletException {
int loadOnStartup = 1;
- boolean asyncSupported = false;
String[] absUrlPatterns = absolute(urlPatterns);
- LOG.info("Registering Servlet for aliases {}: {}", absUrlPatterns, servlet);
+ LOG.info("Registering Servlet for aliases {}: {} with async: {}", absUrlPatterns,
+ servlet, asyncSupported);
paxWeb.registerServlet(servlet, name, absUrlPatterns, new MapDictionary<>(params), loadOnStartup,
asyncSupported, osgiHttpContext);
registeredServlets.add(servlet);
// NB: This is a NOOP WebContextSecurer
// TODO: LATER offer one with a fixed uid/pwd for HTTP BASIC, using Jetty's Filter
- bind(WebContextSecurer.class).toInstance((webContextBuilder, urlPatterns) -> { });
+ bind(WebContextSecurer.class).toInstance((webContextBuilder, asyncSupported, urlPatterns) -> { });
}
}