import java.util.Locale;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
-import org.apache.catalina.connector.ClientAbortException;
import org.picocontainer.Startable;
import org.sonar.api.i18n.I18n;
import org.sonar.api.server.ServerSide;
} catch (ServerException e) {
sendErrors(response, e.httpCode(), new Errors().add(Message.of(e.getMessage())));
} catch (Exception e) {
- Throwable cause = e.getCause();
- if (cause != null && cause instanceof ClientAbortException) {
+ Response.Stream stream = response.stream();
+ if (stream instanceof ServletResponse.ServletStream && ((ServletResponse.ServletStream) stream).response().isCommitted()) {
// Request has been aborted by the client, nothing can been done as Tomcat has committed the response
LOGGER.debug("Request {} has been aborted by client, error is '{}'", request, e.getMessage());
return;
import java.io.IOException;
import java.util.Locale;
+import javax.servlet.http.HttpServletResponse;
import org.apache.catalina.connector.ClientAbortException;
import org.apache.commons.io.IOUtils;
import org.junit.After;
}
@Test
- public void does_not_fail_when_request_is_aborted() throws Exception {
+ public void does_not_fail_when_request_is_aborted_and_response_is_committed() throws Exception {
ValidatingRequest request = new TestRequest().setMethod("GET").setPath("/api/system/fail_with_client_abort_exception");
- DumbResponse response = new DumbResponse();
+ Response response = mock(Response.class);
+ ServletResponse.ServletStream servletStream = mock(ServletResponse.ServletStream.class);
+ when(response.stream()).thenReturn(servletStream);
+ HttpServletResponse httpServletResponse = mock(HttpServletResponse.class);
+ when(httpServletResponse.isCommitted()).thenReturn(true);
+ when(servletStream.response()).thenReturn(httpServletResponse);
underTest.execute(request, response);
- assertThat(response.stream().outputAsString()).isEmpty();
assertThat(logTester.logs(LoggerLevel.DEBUG)).isNotEmpty();
}