Skip to content

Commit

Permalink
Require name in WebContext.Builder
Browse files Browse the repository at this point in the history
Using a dot may produce an incorrect session value in PaxWebSessionHandler.
Furthermore a context path is not a good replacement for context name,
as the latter is used ServletContextHelper, where things like slashes
are a big no-no.

Require a name to be explicitly specified.

JIRA: AAA-279
Change-Id: I0849e5ae2598bef624f31dc440985b7b5be0aa60
Signed-off-by: Peter Suna <[email protected]>
Signed-off-by: Robert Varga <[email protected]>
(cherry picked from commit 07dd470)
  • Loading branch information
PeterSuna committed Sep 2, 2024
1 parent aba4c88 commit 1568b4e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -278,12 +278,15 @@ public Builder supportsSessions(final boolean supportsSessions) {
* @throws IllegalStateException if any required attributes are missing
*/
public @NonNull WebContext build() {
if (name == null) {
throw new IllegalStateException("No name specified");
}
if (contextPath == null) {
throw new IllegalStateException("No contextPath specified");
}

return new ImmutableWebContext(name != null ? name : contextPath + ".id", contextPath, servlets.build(),
filters.build(), listeners.build(), resources.build(), contextParams.build(), supportsSessions);
return new ImmutableWebContext(name, contextPath, servlets.build(), filters.build(), listeners.build(),
resources.build(), contextParams.build(), supportsSessions);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,14 @@ public void testEmptyBuilder() {

@Test
public void testMinimalBuilder() {
assertTrue(WebContext.builder().contextPath("/test").build().supportsSessions());
assertEquals("/test", WebContext.builder().contextPath("/test").supportsSessions(false).build().contextPath());
assertTrue(WebContext.builder().name("test").contextPath("/test").build().supportsSessions());
assertEquals("/test",
WebContext.builder().name("test").contextPath("/test").supportsSessions(false).build().contextPath());
}

@Test
public void testAddSimpleServlet() {
WebContext webContext = WebContext.builder().contextPath("/test")
WebContext webContext = WebContext.builder().name("test").contextPath("/test")
.addServlet(ServletDetails.builder().servlet(mock(Servlet.class)).addUrlPattern("/test").build())
.build();
assertThat(webContext.servlets(), hasSize(1));
Expand All @@ -54,27 +55,27 @@ public void testAddSimpleServlet() {

@Test
public void testAddFullServlet() {
WebContext.builder().contextPath("/test").addServlet(ServletDetails.builder().servlet(mock(Servlet.class))
.addUrlPattern("/test").addUrlPattern("/another").name("custom").putInitParam("key", "value").build())
.build();
WebContext.builder().name("test").contextPath("/test").addServlet(ServletDetails.builder()
.servlet(mock(Servlet.class)).addUrlPattern("/test").addUrlPattern("/another").name("custom")
.putInitParam("key", "value").build()).build();
}

@Test
public void testAddFilter() {
WebContext.builder().contextPath("/test")
WebContext.builder().name("test").contextPath("/test")
.addFilter(FilterDetails.builder().filter(mock(Filter.class)).addUrlPattern("/test").build()).build();
}

@Test
public void testAddListener() {
assertThat(WebContext.builder().contextPath("/test").addListener(mock(ServletContextListener.class)).build()
.listeners(), hasSize(1));
assertThat(WebContext.builder().name("test").contextPath("/test")
.addListener(mock(ServletContextListener.class)).build().listeners(), hasSize(1));
}

@Test
public void testContextParam() {
assertEquals(Map.of("key", "value"),
WebContext.builder().contextPath("/test").putContextParam("key", "value").build().contextParams());
assertEquals(Map.of("key", "value"), WebContext.builder().name("test").contextPath("/test")
.putContextParam("key", "value").build().contextParams());
}

@Test
Expand All @@ -86,6 +87,13 @@ public void testBadContextPath() {
assertBadContextPath("Context path '/test/' ends with '/'", "/test/");
}

@Test
public void testNoName() {
final var ex = assertThrows(IllegalStateException.class,
() -> WebContext.builder().contextPath("/test").build());
assertEquals("No name specified", ex.getMessage());
}

private static void assertBadContextPath(final String expectedMessage, final String contextPath) {
final var builder = WebContext.builder();
final var ex = assertThrows(IllegalArgumentException.class, () -> builder.contextPath(contextPath));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public abstract class AbstractWebServerTest {
@Test
public void testAddAfterStart() throws ServletException, IOException {
var webContext = WebContext.builder()
.name("test")
.contextPath("/test1")
.addServlet(ServletDetails.builder().addUrlPattern("/*").name("Test").servlet(new TestServlet()).build())
.build();
Expand All @@ -57,6 +58,7 @@ public void testAddAfterStartWithoutSlashOnServlet() throws ServletException, IO
public void testAddFilter() throws Exception {
var testFilter = new TestFilter();
var webContext = WebContext.builder()
.name("testFilter")
.contextPath("/testingFilters")
.putContextParam("testParam1", "avalue")
.addFilter(FilterDetails.builder().addUrlPattern("/*").name("Test").filter(testFilter).build())
Expand All @@ -81,6 +83,7 @@ public void testAddFilterWithoutSlash() throws Exception {
public void testRegisterListener() throws Exception {
var testListener = new TestListener();
var webContext = WebContext.builder()
.name("testListen")
.contextPath("/testingListener")
.addListener(testListener)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
import static org.junit.Assert.assertEquals;

import java.io.IOException;
import java.net.URISyntaxException;
import javax.inject.Inject;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
Expand All @@ -39,8 +37,9 @@ public class WebTestModuleTest {
@Inject TestWebClient webClient;

@Test
public void testServlet() throws ServletException, IOException, InterruptedException, URISyntaxException {
public void testServlet() throws Exception {
var webContext = WebContext.builder()
.name("test")
.contextPath("/test1")
.addServlet(ServletDetails.builder()
.addUrlPattern("/hello")
Expand Down

0 comments on commit 1568b4e

Please sign in to comment.