use of jakarta.servlet.ServletException in project spring-framework by spring-projects.
the class InternalResourceView method renderMergedOutputModel.
/**
* Render the internal resource given the specified model.
* This includes setting the model as request attributes.
*/
@Override
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
// Expose the model object as request attributes.
exposeModelAsRequestAttributes(model, request);
// Expose helpers as request attributes, if any.
exposeHelpers(request);
// Determine the path for the request dispatcher.
String dispatcherPath = prepareForRendering(request, response);
// Obtain a RequestDispatcher for the target resource (typically a JSP).
RequestDispatcher rd = getRequestDispatcher(request, dispatcherPath);
if (rd == null) {
throw new ServletException("Could not get RequestDispatcher for [" + getUrl() + "]: Check that the corresponding file exists within your web application archive!");
}
// If already included or response already committed, perform include, else forward.
if (useInclude(request, response)) {
response.setContentType(getContentType());
if (logger.isDebugEnabled()) {
logger.debug("Including [" + getUrl() + "]");
}
rd.include(request, response);
} else {
// Note: The forwarded resource is supposed to determine the content type itself.
if (logger.isDebugEnabled()) {
logger.debug("Forwarding to [" + getUrl() + "]");
}
rd.forward(request, response);
}
}
use of jakarta.servlet.ServletException in project spring-framework by spring-projects.
the class ResourceUrlEncodingFilterTests method testEncodeUrl.
private void testEncodeUrl(MockHttpServletRequest request, String url, String expected) throws ServletException, IOException {
FilterChain chain = (req, res) -> {
req.setAttribute(ResourceUrlProviderExposingInterceptor.RESOURCE_URL_PROVIDER_ATTR, this.urlProvider);
String result = ((HttpServletResponse) res).encodeURL(url);
assertThat(result).isEqualTo(expected);
};
this.filter.doFilter(request, new MockHttpServletResponse(), chain);
}
use of jakarta.servlet.ServletException in project spring-boot by spring-projects.
the class ErrorPageFilterTests method errorMessageForRequestWithPathInfo.
@Test
void errorMessageForRequestWithPathInfo(CapturedOutput output) throws IOException, ServletException {
this.request.setServletPath("/test");
this.request.setPathInfo("/alpha");
this.filter.addErrorPages(new ErrorPage("/error"));
this.chain = new TestFilterChain((request, response, chain) -> {
chain.call();
throw new RuntimeException();
});
this.filter.doFilter(this.request, this.response, this.chain);
assertThat(output).contains("request [/test/alpha]");
}
use of jakarta.servlet.ServletException in project spring-boot by spring-projects.
the class AfterSecurityFilter method doFilter.
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
Principal principal = ((HttpServletRequest) request).getUserPrincipal();
if (principal == null) {
throw new ServletException("No user principal");
}
response.getWriter().write(principal.getName());
response.getWriter().flush();
}
use of jakarta.servlet.ServletException in project spring-boot by spring-projects.
the class MockServletWebServer method initialize.
private void initialize() {
try {
this.servletContext = mock(ServletContext.class);
lenient().doAnswer((invocation) -> {
RegisteredServlet registeredServlet = new RegisteredServlet(invocation.getArgument(1));
MockServletWebServer.this.registeredServlets.add(registeredServlet);
return registeredServlet.getRegistration();
}).when(this.servletContext).addServlet(anyString(), any(Servlet.class));
lenient().doAnswer((invocation) -> {
RegisteredFilter registeredFilter = new RegisteredFilter(invocation.getArgument(1));
MockServletWebServer.this.registeredFilters.add(registeredFilter);
return registeredFilter.getRegistration();
}).when(this.servletContext).addFilter(anyString(), any(Filter.class));
final SessionCookieConfig sessionCookieConfig = new MockSessionCookieConfig();
given(this.servletContext.getSessionCookieConfig()).willReturn(sessionCookieConfig);
final Map<String, String> initParameters = new HashMap<>();
lenient().doAnswer((invocation) -> {
initParameters.put(invocation.getArgument(0), invocation.getArgument(1));
return null;
}).when(this.servletContext).setInitParameter(anyString(), anyString());
given(this.servletContext.getInitParameterNames()).willReturn(Collections.enumeration(initParameters.keySet()));
lenient().doAnswer((invocation) -> initParameters.get(invocation.getArgument(0))).when(this.servletContext).getInitParameter(anyString());
given(this.servletContext.getAttributeNames()).willReturn(Collections.emptyEnumeration());
for (Initializer initializer : this.initializers) {
initializer.onStartup(this.servletContext);
}
} catch (ServletException ex) {
throw new RuntimeException(ex);
}
}
Aggregations