Search in sources :

Example 26 with ServletException

use of jakarta.servlet.ServletException in project spring-framework by spring-projects.

the class HiddenHttpMethodFilterTests method filterWithParameterForMethod.

private void filterWithParameterForMethod(String methodParam, String expectedMethod) throws IOException, ServletException {
    MockHttpServletRequest request = new MockHttpServletRequest("POST", "/hotels");
    if (methodParam != null) {
        request.addParameter("_method", methodParam);
    }
    MockHttpServletResponse response = new MockHttpServletResponse();
    FilterChain filterChain = (filterRequest, filterResponse) -> assertThat(((HttpServletRequest) filterRequest).getMethod()).as("Invalid method").isEqualTo(expectedMethod);
    this.filter.doFilter(request, response, filterChain);
}
Also used : Test(org.junit.jupiter.api.Test) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) FilterChain(jakarta.servlet.FilterChain) IOException(java.io.IOException) ServletException(jakarta.servlet.ServletException) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) FilterChain(jakarta.servlet.FilterChain) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse)

Example 27 with ServletException

use of jakarta.servlet.ServletException in project spring-framework by spring-projects.

the class ServletForwardingController method handleRequestInternal.

@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
    ServletContext servletContext = getServletContext();
    Assert.state(servletContext != null, "No ServletContext");
    RequestDispatcher rd = servletContext.getNamedDispatcher(this.servletName);
    if (rd == null) {
        throw new ServletException("No servlet with name '" + this.servletName + "' defined in web.xml");
    }
    // If already included, include again, else forward.
    if (useInclude(request, response)) {
        rd.include(request, response);
        if (logger.isTraceEnabled()) {
            logger.trace("Included servlet [" + this.servletName + "] in ServletForwardingController '" + this.beanName + "'");
        }
    } else {
        rd.forward(request, response);
        if (logger.isTraceEnabled()) {
            logger.trace("Forwarded to servlet [" + this.servletName + "] in ServletForwardingController '" + this.beanName + "'");
        }
    }
    return null;
}
Also used : ServletException(jakarta.servlet.ServletException) ServletContext(jakarta.servlet.ServletContext) RequestDispatcher(jakarta.servlet.RequestDispatcher)

Example 28 with ServletException

use of jakarta.servlet.ServletException in project spring-framework by spring-projects.

the class ResponseEntityExceptionHandlerTests method controllerAdviceWithNestedExceptionWithinDispatcherServlet.

@Test
public void controllerAdviceWithNestedExceptionWithinDispatcherServlet() throws Exception {
    StaticWebApplicationContext ctx = new StaticWebApplicationContext();
    ctx.registerSingleton("controller", NestedExceptionThrowingController.class);
    ctx.registerSingleton("exceptionHandler", ApplicationExceptionHandler.class);
    ctx.refresh();
    DispatcherServlet servlet = new DispatcherServlet(ctx);
    servlet.init(new MockServletConfig());
    try {
        servlet.service(this.servletRequest, this.servletResponse);
    } catch (ServletException ex) {
        boolean condition1 = ex.getCause() instanceof IllegalStateException;
        assertThat(condition1).isTrue();
        boolean condition = ex.getCause().getCause() instanceof ServletRequestBindingException;
        assertThat(condition).isTrue();
    }
}
Also used : ServletException(jakarta.servlet.ServletException) ServletRequestBindingException(org.springframework.web.bind.ServletRequestBindingException) DispatcherServlet(org.springframework.web.servlet.DispatcherServlet) MockServletConfig(org.springframework.web.testfixture.servlet.MockServletConfig) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) Test(org.junit.jupiter.api.Test)

Example 29 with ServletException

use of jakarta.servlet.ServletException in project spring-framework by spring-projects.

the class UndertowRequestUpgradeStrategy method upgradeInternal.

@Override
protected void upgradeInternal(ServerHttpRequest request, ServerHttpResponse response, @Nullable String selectedProtocol, List<Extension> selectedExtensions, Endpoint endpoint) throws HandshakeFailureException {
    HttpServletRequest servletRequest = getHttpServletRequest(request);
    HttpServletResponse servletResponse = getHttpServletResponse(response);
    StringBuffer requestUrl = servletRequest.getRequestURL();
    // shouldn't matter
    String path = servletRequest.getRequestURI();
    Map<String, String> pathParams = Collections.emptyMap();
    ServerEndpointRegistration endpointConfig = new ServerEndpointRegistration(path, endpoint);
    endpointConfig.setSubprotocols(Collections.singletonList(selectedProtocol));
    endpointConfig.setExtensions(selectedExtensions);
    try {
        getContainer(servletRequest).doUpgrade(servletRequest, servletResponse, endpointConfig, pathParams);
    } catch (ServletException ex) {
        throw new HandshakeFailureException("Servlet request failed to upgrade to WebSocket: " + requestUrl, ex);
    } catch (IOException ex) {
        throw new HandshakeFailureException("Response update failed during upgrade to WebSocket: " + requestUrl, ex);
    }
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) ServletException(jakarta.servlet.ServletException) HandshakeFailureException(org.springframework.web.socket.server.HandshakeFailureException) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) IOException(java.io.IOException)

Example 30 with ServletException

use of jakarta.servlet.ServletException in project spring-framework by spring-projects.

the class HttpRequestHandlerTests method testHttpRequestHandlerServletPassThrough.

@Test
public void testHttpRequestHandlerServletPassThrough() throws Exception {
    MockServletContext servletContext = new MockServletContext();
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.getBeanFactory().registerSingleton("myHandler", (HttpRequestHandler) (req, res) -> {
        assertThat(req).isSameAs(request);
        assertThat(res).isSameAs(response);
        String exception = request.getParameter("exception");
        if ("ServletException".equals(exception)) {
            throw new ServletException("test");
        }
        if ("IOException".equals(exception)) {
            throw new IOException("test");
        }
        res.getWriter().write("myResponse");
    });
    wac.setServletContext(servletContext);
    wac.refresh();
    servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    Servlet servlet = new HttpRequestHandlerServlet();
    servlet.init(new MockServletConfig(servletContext, "myHandler"));
    servlet.service(request, response);
    assertThat(response.getContentAsString()).isEqualTo("myResponse");
    request.setParameter("exception", "ServletException");
    assertThatExceptionOfType(ServletException.class).isThrownBy(() -> servlet.service(request, response)).withMessage("test");
    request.setParameter("exception", "IOException");
    assertThatIOException().isThrownBy(() -> servlet.service(request, response)).withMessage("test");
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) WebApplicationContext(org.springframework.web.context.WebApplicationContext) MockServletConfig(org.springframework.web.testfixture.servlet.MockServletConfig) IOException(java.io.IOException) ServletException(jakarta.servlet.ServletException) Assertions.assertThatIOException(org.assertj.core.api.Assertions.assertThatIOException) Test(org.junit.jupiter.api.Test) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Servlet(jakarta.servlet.Servlet) HttpRequestHandler(org.springframework.web.HttpRequestHandler) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) ServletException(jakarta.servlet.ServletException) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Servlet(jakarta.servlet.Servlet) MockServletConfig(org.springframework.web.testfixture.servlet.MockServletConfig) IOException(java.io.IOException) Assertions.assertThatIOException(org.assertj.core.api.Assertions.assertThatIOException) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

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