Search in sources :

Example 31 with ServletException

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);
    }
}
Also used : ServletException(jakarta.servlet.ServletException) RequestDispatcher(jakarta.servlet.RequestDispatcher)

Example 32 with ServletException

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);
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) FilterChain(jakarta.servlet.FilterChain) ClassPathResource(org.springframework.core.io.ClassPathResource) IOException(java.io.IOException) ServletException(jakarta.servlet.ServletException) ServletRequestBindingException(org.springframework.web.bind.ServletRequestBindingException) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) List(java.util.List) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) Collections(java.util.Collections) FilterChain(jakarta.servlet.FilterChain) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse)

Example 33 with ServletException

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]");
}
Also used : RequestDispatcher(jakarta.servlet.RequestDispatcher) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockFilterChain(org.springframework.mock.web.MockFilterChain) Enumeration(java.util.Enumeration) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) DeferredResult(org.springframework.web.context.request.async.DeferredResult) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) ClientAbortException(org.apache.catalina.connector.ClientAbortException) ServletException(jakarta.servlet.ServletException) WebAsyncManager(org.springframework.web.context.request.async.WebAsyncManager) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) OutputCaptureExtension(org.springframework.boot.testsupport.system.OutputCaptureExtension) BDDMockito.given(org.mockito.BDDMockito.given) NestedServletException(org.springframework.web.util.NestedServletException) Map(java.util.Map) StandardServletAsyncWebRequest(org.springframework.web.context.request.async.StandardServletAsyncWebRequest) ServletRequest(jakarta.servlet.ServletRequest) MissingServletRequestParameterException(org.springframework.web.bind.MissingServletRequestParameterException) WebAsyncUtils(org.springframework.web.context.request.async.WebAsyncUtils) ErrorPage(org.springframework.boot.web.server.ErrorPage) MockRequestDispatcher(org.springframework.mock.web.MockRequestDispatcher) BDDMockito.then(org.mockito.BDDMockito.then) IOException(java.io.IOException) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Test(org.junit.jupiter.api.Test) HttpServletResponseWrapper(jakarta.servlet.http.HttpServletResponseWrapper) HttpStatus(org.springframework.http.HttpStatus) Mockito.never(org.mockito.Mockito.never) ServletResponse(jakarta.servlet.ServletResponse) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) MockFilterConfig(org.springframework.mock.web.MockFilterConfig) CapturedOutput(org.springframework.boot.testsupport.system.CapturedOutput) Mockito.mock(org.mockito.Mockito.mock) ErrorPage(org.springframework.boot.web.server.ErrorPage) Test(org.junit.jupiter.api.Test)

Example 34 with ServletException

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();
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) ServletException(jakarta.servlet.ServletException) Principal(java.security.Principal)

Example 35 with ServletException

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);
    }
}
Also used : HashMap(java.util.HashMap) MockSessionCookieConfig(org.springframework.mock.web.MockSessionCookieConfig) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ServletException(jakarta.servlet.ServletException) Filter(jakarta.servlet.Filter) ServletContext(jakarta.servlet.ServletContext) Servlet(jakarta.servlet.Servlet) SessionCookieConfig(jakarta.servlet.SessionCookieConfig) MockSessionCookieConfig(org.springframework.mock.web.MockSessionCookieConfig)

Aggregations

ServletException (jakarta.servlet.ServletException)127 IOException (java.io.IOException)78 Test (org.junit.jupiter.api.Test)31 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)23 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)23 ServletContext (jakarta.servlet.ServletContext)19 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)17 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)15 FilterChain (jakarta.servlet.FilterChain)13 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)13 Enumeration (java.util.Enumeration)12 BeforeEach (org.junit.jupiter.api.BeforeEach)12 HttpHeaders (org.springframework.http.HttpHeaders)11 BeforeMethod (org.testng.annotations.BeforeMethod)11 ServletConfig (jakarta.servlet.ServletConfig)10 ServletRequest (jakarta.servlet.ServletRequest)10 ServletResponse (jakarta.servlet.ServletResponse)10 Arrays (java.util.Arrays)10 UnavailableException (jakarta.servlet.UnavailableException)9 HttpMethod (org.springframework.http.HttpMethod)9