DefaultWebApplicationRequestMapper implements WebApplicationRequestMapper { @Override public Set<String> addServletMapping(String servletName, String... urlPatterns) { if (isEmpty(urlPatterns)) { throw new IllegalArgumentException("Mappings for " + servletName + " cannot be empty"); } Set<String> mappedToOtherServlet = stream(urlPatterns) .filter(servletMappings::containsKey) .filter(urlPattern -> !servletMappings.get(urlPattern).equals(servletName)) .collect(toSet()); if (!mappedToOtherServlet.isEmpty()) { return mappedToOtherServlet; } for (String urlPattern : urlPatterns) { servletMappings.put(urlPattern, servletName); } return emptySet(); }  @Override Set<String> addFilterMapping(Set<DispatcherType> dispatcherTypes, String filterName, String... urlPatterns); @Override Set<String> addFilterMappingBeforeExisting(Set<DispatcherType> dispatcherTypes, String filterName, String... urlPatterns); @Override Set<String> addServletMapping(String servletName, String... urlPatterns); @Override Collection<String> findFilterMappings(DispatcherType dispatcherType, String path); @Override DefaultWebApplicationRequestMapping findServletMapping(String path); @Override Collection<String> getServletMappings(String servletName); @Override String getServletName(String mapping);  }
@Test void testFindExactServletMatch() throws Exception { DefaultWebApplicationRequestMapper webAppRequestMapper = new DefaultWebApplicationRequestMapper(); DefaultWebApplication webApp = new DefaultWebApplication(); webApp.setWebApplicationRequestMapper(webAppRequestMapper); webApp.addServlet("echo", new TestEcho1Servlet()); webApp.addServletMapping("echo", "/echo"); webApp.initialize(); webApp.start(); TestWebApplicationRequest request = new TestWebApplicationRequest(); request.setServletPath("/echo"); TestWebApplicationResponse response = new TestWebApplicationResponse(); response.setBodyOnly(true); webApp.service(request, response); assertEquals("ECHO", new String(response.getResponseBytes())); }
@Test void testFindExtensionServletMatch() throws Exception { DefaultWebApplicationRequestMapper webAppRequestMapper = new DefaultWebApplicationRequestMapper(); DefaultWebApplication webApp = new DefaultWebApplication(); webApp.setWebApplicationRequestMapper(webAppRequestMapper); webApp.addServlet("echo", new TestEcho1Servlet()); webApp.addServletMapping("echo", "*.echo"); webApp.initialize(); webApp.start(); TestWebApplicationRequest request = new TestWebApplicationRequest(); request.setServletPath("/echo.echo"); TestWebApplicationResponse response = new TestWebApplicationResponse(); response.setBodyOnly(true); webApp.service(request, response); assertEquals("ECHO", new String(response.getResponseBytes())); }
@Test void testFindPrefixServletMatch3() throws Exception { DefaultWebApplicationRequestMapper webAppRequestMapper = new DefaultWebApplicationRequestMapper(); DefaultWebApplication webApp = new DefaultWebApplication(); webApp.setWebApplicationRequestMapper(webAppRequestMapper); webApp.addServlet("echo", new TestEcho1Servlet()); webApp.addServletMapping("echo", "/echo/*"); webApp.initialize(); webApp.start(); TestWebApplicationRequest request = new TestWebApplicationRequest(); request.setServletPath("/echo2/test.echo"); TestWebApplicationResponse response = new TestWebApplicationResponse(); response.setBodyOnly(true); webApp.service(request, response); assertEquals(404, response.getStatus()); }
@Test void testFindPrefixServletMatch4() throws Exception { DefaultWebApplicationRequestMapper webAppRequestMapper = new DefaultWebApplicationRequestMapper(); DefaultWebApplication webApp = new DefaultWebApplication(); webApp.setWebApplicationRequestMapper(webAppRequestMapper); webApp.addServlet("echo", new TestEcho1Servlet()); webApp.addServletMapping("echo", "/echo/*"); webApp.initialize(); webApp.start(); TestWebApplicationRequest request = new TestWebApplicationRequest(); request.setServletPath("/echo"); TestWebApplicationResponse response = new TestWebApplicationResponse(); response.setBodyOnly(true); webApp.service(request, response); assertEquals("ECHO", new String(response.getResponseBytes())); }
@Test void testFindPrefixServletMatch5() throws Exception { DefaultWebApplicationRequestMapper webAppRequestMapper = new DefaultWebApplicationRequestMapper(); DefaultWebApplication webApp = new DefaultWebApplication(); webApp.setWebApplicationRequestMapper(webAppRequestMapper); webApp.addServlet("echo", new TestEcho1Servlet()); webApp.addServletMapping("echo", "/echo/*"); webApp.initialize(); webApp.start(); TestWebApplicationRequest request = new TestWebApplicationRequest(); request.setServletPath("/echo2"); TestWebApplicationResponse response = new TestWebApplicationResponse(); response.setBodyOnly(true); webApp.service(request, response); assertEquals(404, response.getStatus()); }
@Test void testFindExtensionServletMatch2() throws Exception { DefaultWebApplicationRequestMapper webAppRequestMapper = new DefaultWebApplicationRequestMapper(); DefaultWebApplication webApp = new DefaultWebApplication(); webApp.setWebApplicationRequestMapper(webAppRequestMapper); webApp.addServlet("echo2", new TestEcho1Servlet()); webApp.addServletMapping("echo2", "*.echo2"); webApp.initialize(); webApp.start(); TestWebApplicationRequest request = new TestWebApplicationRequest(); request.setServletPath("/echo.echo"); request.setWebApplication(new DefaultWebApplication()); TestWebApplicationResponse response = new TestWebApplicationResponse(); webApp.service(request, response); assertTrue(response.getStatus() != 200); }
@Test void testFindPrefixServletMatch() throws Exception { DefaultWebApplicationRequestMapper webAppRequestMapper = new DefaultWebApplicationRequestMapper(); DefaultWebApplication webApp = new DefaultWebApplication(); webApp.setWebApplicationRequestMapper(webAppRequestMapper); webApp.addServlet("echo", new TestEcho1Servlet()); webApp.addServletMapping("echo", "/echo/*"); webApp.initialize(); webApp.start(); TestWebApplicationRequest request = new TestWebApplicationRequest(); request.setServletPath("/echo/test.echo"); TestWebApplicationResponse response = new TestWebApplicationResponse(); response.setBodyOnly(true); webApp.service(request, response); assertEquals("ECHO", new String(response.getResponseBytes())); }
@Test void testFindPrefixServletMatch2() throws Exception { DefaultWebApplicationRequestMapper webAppRequestMapper = new DefaultWebApplicationRequestMapper(); DefaultWebApplication webApp = new DefaultWebApplication(); webApp.setWebApplicationRequestMapper(webAppRequestMapper); webApp.addServlet("echo", new TestEcho1Servlet()); webApp.addServletMapping("echo", "/*"); webApp.initialize(); webApp.start(); TestWebApplicationRequest request = new TestWebApplicationRequest(); request.setServletPath("/echo/test.echo"); TestWebApplicationResponse response = new TestWebApplicationResponse(); response.setBodyOnly(true); webApp.service(request, response); assertEquals("ECHO", new String(response.getResponseBytes())); }
@Test void testAddServletMapping() { DefaultWebApplicationRequestMapper requestMapper = new DefaultWebApplicationRequestMapper(); Set<String> result = requestMapper.addServletMapping("servlet", "*.html"); assertTrue(result.isEmpty()); result = requestMapper.addServletMapping("servlet", "*.html"); assertTrue(result.isEmpty()); DefaultWebApplicationRequestMapping mapping = requestMapper.findServletMapping("/index.html?q=keyword"); assertNotNull(mapping); }