Search in sources :

Example 26 with HttpServletResponseWrapper

use of javax.servlet.http.HttpServletResponseWrapper in project spring-boot by spring-projects.

the class ErrorPageFilterTests method responseCommitted.

@Test
public void responseCommitted() throws Exception {
    this.filter.addErrorPages(new ErrorPage("/error"));
    this.response.setCommitted(true);
    this.chain = new MockFilterChain() {

        @Override
        public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
            ((HttpServletResponse) response).sendError(400, "BAD");
            super.doFilter(request, response);
        }
    };
    this.filter.doFilter(this.request, this.response, this.chain);
    assertThat(this.chain.getRequest()).isEqualTo(this.request);
    assertThat(((HttpServletResponseWrapper) this.chain.getResponse()).getResponse()).isEqualTo(this.response);
    assertThat(((HttpServletResponseWrapper) this.chain.getResponse()).getStatus()).isEqualTo(400);
    assertThat(this.response.getForwardedUrl()).isNull();
    assertThat(this.response.isCommitted()).isTrue();
}
Also used : ServletException(javax.servlet.ServletException) NestedServletException(org.springframework.web.util.NestedServletException) ServletRequest(javax.servlet.ServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) ErrorPage(org.springframework.boot.web.server.ErrorPage) HttpServletResponseWrapper(javax.servlet.http.HttpServletResponseWrapper) IOException(java.io.IOException) MockFilterChain(org.springframework.mock.web.MockFilterChain) Test(org.junit.Test)

Example 27 with HttpServletResponseWrapper

use of javax.servlet.http.HttpServletResponseWrapper in project spring-boot by spring-projects.

the class ErrorPageFilterTests method subClassExceptionError.

@Test
public void subClassExceptionError() throws Exception {
    this.filter.addErrorPages(new ErrorPage(RuntimeException.class, "/500"));
    this.chain = new MockFilterChain() {

        @Override
        public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
            super.doFilter(request, response);
            throw new IllegalStateException("BAD");
        }
    };
    this.filter.doFilter(this.request, this.response, this.chain);
    assertThat(((HttpServletResponseWrapper) this.chain.getResponse()).getStatus()).isEqualTo(500);
    assertThat(this.request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE)).isEqualTo(500);
    assertThat(this.request.getAttribute(RequestDispatcher.ERROR_MESSAGE)).isEqualTo("BAD");
    Map<String, Object> requestAttributes = getAttributesForDispatch("/500");
    assertThat(requestAttributes.get(RequestDispatcher.ERROR_EXCEPTION_TYPE)).isEqualTo(IllegalStateException.class);
    assertThat(requestAttributes.get(RequestDispatcher.ERROR_EXCEPTION)).isInstanceOf(IllegalStateException.class);
    assertThat(this.request.getAttribute(RequestDispatcher.ERROR_EXCEPTION_TYPE)).isNull();
    assertThat(this.request.getAttribute(RequestDispatcher.ERROR_EXCEPTION)).isNull();
    assertThat(this.request.getAttribute(RequestDispatcher.ERROR_REQUEST_URI)).isEqualTo("/test/path");
    assertThat(this.response.isCommitted()).isTrue();
}
Also used : ServletRequest(javax.servlet.ServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) ErrorPage(org.springframework.boot.web.server.ErrorPage) HttpServletResponseWrapper(javax.servlet.http.HttpServletResponseWrapper) IOException(java.io.IOException) ServletException(javax.servlet.ServletException) NestedServletException(org.springframework.web.util.NestedServletException) MockFilterChain(org.springframework.mock.web.MockFilterChain) Test(org.junit.Test)

Example 28 with HttpServletResponseWrapper

use of javax.servlet.http.HttpServletResponseWrapper in project spring-boot by spring-projects.

the class ErrorPageFilterTests method responseIsCommittedWhenStatusIs400PlusDuringAsyncDispatch.

@Test
public void responseIsCommittedWhenStatusIs400PlusDuringAsyncDispatch() throws Exception {
    this.filter.addErrorPages(new ErrorPage("/error"));
    setUpAsyncDispatch();
    this.chain = new MockFilterChain() {

        @Override
        public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
            super.doFilter(request, response);
            ((HttpServletResponse) response).sendError(400, "BAD");
        }
    };
    this.filter.doFilter(this.request, this.response, this.chain);
    assertThat(this.chain.getRequest()).isEqualTo(this.request);
    assertThat(((HttpServletResponseWrapper) this.chain.getResponse()).getResponse()).isEqualTo(this.response);
    assertThat(this.response.isCommitted()).isTrue();
}
Also used : ServletException(javax.servlet.ServletException) NestedServletException(org.springframework.web.util.NestedServletException) ServletRequest(javax.servlet.ServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) ErrorPage(org.springframework.boot.web.server.ErrorPage) HttpServletResponseWrapper(javax.servlet.http.HttpServletResponseWrapper) IOException(java.io.IOException) MockFilterChain(org.springframework.mock.web.MockFilterChain) Test(org.junit.Test)

Example 29 with HttpServletResponseWrapper

use of javax.servlet.http.HttpServletResponseWrapper in project spring-boot by spring-projects.

the class ErrorPageFilterTests method responseIsCommittedWhenRequestIsAsyncAndExceptionIsThrown.

@Test
public void responseIsCommittedWhenRequestIsAsyncAndExceptionIsThrown() throws Exception {
    this.filter.addErrorPages(new ErrorPage("/error"));
    this.request.setAsyncStarted(true);
    this.chain = new MockFilterChain() {

        @Override
        public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
            super.doFilter(request, response);
            throw new RuntimeException("BAD");
        }
    };
    this.filter.doFilter(this.request, this.response, this.chain);
    assertThat(this.chain.getRequest()).isEqualTo(this.request);
    assertThat(((HttpServletResponseWrapper) this.chain.getResponse()).getResponse()).isEqualTo(this.response);
    assertThat(this.response.isCommitted()).isTrue();
}
Also used : ServletException(javax.servlet.ServletException) NestedServletException(org.springframework.web.util.NestedServletException) ServletRequest(javax.servlet.ServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) ErrorPage(org.springframework.boot.web.server.ErrorPage) HttpServletResponseWrapper(javax.servlet.http.HttpServletResponseWrapper) IOException(java.io.IOException) MockFilterChain(org.springframework.mock.web.MockFilterChain) Test(org.junit.Test)

Example 30 with HttpServletResponseWrapper

use of javax.servlet.http.HttpServletResponseWrapper in project pulsar by yahoo.

the class ApiVersionFilter method doFilter.

@Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
    try {
        String minApiVersion = pulsar.getLocalZkCache().getData(MIN_API_VERSION_PATH, Deserializers.STRING_DESERIALIZER).orElseThrow(() -> new KeeperException.NoNodeException());
        String requestApiVersion = getRequestApiVersion(req);
        if (shouldAllowRequest(req.getRemoteAddr(), minApiVersion, requestApiVersion)) {
            // Allow the request to continue by invoking the next filter in
            // the chain.
            chain.doFilter(req, resp);
        } else {
            // The client's API version is less than the min supported,
            // reject the request.
            HttpServletResponse httpResponse = (HttpServletResponse) resp;
            HttpServletResponseWrapper respWrapper = new HttpServletResponseWrapper(httpResponse);
            respWrapper.sendError(HttpServletResponse.SC_BAD_REQUEST, "Unsuported Client version");
        }
    } catch (Exception ex) {
        LOG.warn("[{}] Unable to safely determine client version eligibility. Allowing request", req.getRemoteAddr());
        chain.doFilter(req, resp);
    }
}
Also used : HttpServletResponseWrapper(javax.servlet.http.HttpServletResponseWrapper) HttpServletResponse(javax.servlet.http.HttpServletResponse) KeeperException(org.apache.zookeeper.KeeperException) KeeperException(org.apache.zookeeper.KeeperException) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Aggregations

HttpServletResponseWrapper (javax.servlet.http.HttpServletResponseWrapper)34 HttpServletResponse (javax.servlet.http.HttpServletResponse)29 ServletResponse (javax.servlet.ServletResponse)23 IOException (java.io.IOException)22 ServletRequest (javax.servlet.ServletRequest)20 Test (org.junit.Test)20 ServletException (javax.servlet.ServletException)19 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)15 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)15 MockFilterChain (org.springframework.mock.web.MockFilterChain)14 NestedServletException (org.springframework.web.util.NestedServletException)14 ErrorPage (org.springframework.boot.web.server.ErrorPage)12 HttpServletRequest (javax.servlet.http.HttpServletRequest)10 HttpServletRequestWrapper (javax.servlet.http.HttpServletRequestWrapper)8 PrintWriter (java.io.PrintWriter)6 ServletOutputStream (javax.servlet.ServletOutputStream)5 Provider (com.google.inject.Provider)4 ServletTestUtils.newFakeHttpServletRequest (com.google.inject.servlet.ServletTestUtils.newFakeHttpServletRequest)4 ServletTestUtils.newFakeHttpServletResponse (com.google.inject.servlet.ServletTestUtils.newFakeHttpServletResponse)4 Filter (javax.servlet.Filter)4