use of jakarta.servlet.ServletRegistration in project spring-security by spring-projects.
the class InterceptUrlConfigTests method mockServletContext.
private MockServletContext mockServletContext(String servletPath) {
MockServletContext servletContext = spy(new MockServletContext());
final ServletRegistration registration = mock(ServletRegistration.class);
given(registration.getMappings()).willReturn(Collections.singleton(servletPath));
Answer<Map<String, ? extends ServletRegistration>> answer = (invocation) -> Collections.singletonMap("spring", registration);
given(servletContext.getServletRegistrations()).willAnswer(answer);
return servletContext;
}
use of jakarta.servlet.ServletRegistration in project spring-framework by spring-projects.
the class ServletHttpHandlerAdapter method getServletPath.
private String getServletPath(ServletConfig config) {
String name = config.getServletName();
ServletRegistration registration = config.getServletContext().getServletRegistration(name);
if (registration == null) {
throw new IllegalStateException("ServletRegistration not found for Servlet '" + name + "'");
}
Collection<String> mappings = registration.getMappings();
if (mappings.size() == 1) {
String mapping = mappings.iterator().next();
if (mapping.equals("/")) {
return "";
}
if (mapping.endsWith("/*")) {
String path = mapping.substring(0, mapping.length() - 2);
if (!path.isEmpty() && logger.isDebugEnabled()) {
logger.debug("Found servlet mapping prefix '" + path + "' for '" + name + "'");
}
return path;
}
}
throw new IllegalArgumentException("Expected a single Servlet mapping: " + "either the default Servlet mapping (i.e. '/'), " + "or a path based mapping (e.g. '/*', '/foo/*'). " + "Actual mappings: " + mappings + " for Servlet '" + name + "'");
}
Aggregations