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();
}
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();
}
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();
}
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();
}
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);
}
}
Aggregations