下面列出了怎么用org.apache.http.ConnectionClosedException的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
void testWaitForJsonFieldAppearsHandledException() throws IOException, IllegalAccessException, NoSuchFieldException
{
when(httpClient.execute(argThat(base -> base instanceof HttpRequestBase
&& base.getMethod().equals(GET)
&& base.getURI().equals(URI.create(URL))),
argThat(context -> context instanceof HttpClientContext)))
.thenThrow(new ConnectionClosedException());
HttpRequestExecutor httpRequestExecutor = new HttpRequestExecutor(httpClient, httpTestContext, softAssert);
Field executorField = jsonResponseValidationSteps.getClass().getDeclaredField(HTTP_REQUEST_EXECUTOR_FIELD);
executorField.setAccessible(true);
executorField.set(jsonResponseValidationSteps, httpRequestExecutor);
jsonResponseValidationSteps.waitForJsonFieldAppearance(STRING_PATH, URL, Duration.ofSeconds(1),
DURATION_DIVIDER);
verify(softAssert).recordFailedAssertion(
(Exception) argThat(arg -> arg instanceof ConnectionClosedException
&& "Connection is closed".equals(((Exception) arg).getMessage())));
}
@Override
public Void call() throws IOException, DockerException {
try (final LogStream logStream =
dockerClient.logs(containerId, stdout(), stderr(), follow())) {
log.info("attaching stdout/stderr for job={}, container={}", jobId, containerId);
logStreamFollower.followLog(jobId, containerId, logStream);
} catch (InterruptedException e) {
// Ignore
} catch (final Throwable t) {
if (!(Throwables.getRootCause(t) instanceof ConnectionClosedException)) {
log.warn("error streaming log for job={}, container={}", jobId, containerId, t);
}
throw t;
}
return null;
}
private HttpResponse executeHttpCallSafely(HttpMethod httpMethod, String endpoint, String relativeURL)
throws IOException
{
try
{
return execute(httpMethod, endpoint, relativeURL);
}
catch (HttpRequestBuildException | ConnectionClosedException e)
{
softAssert.recordFailedAssertion(e);
return null;
}
}
@Test
void testExecuteHttpRequestConnectionClosedException() throws IOException
{
when(httpClient.execute(argThat(e -> e instanceof HttpRequestBase && URL.equals(e.getURI().toString())),
nullable(HttpContext.class))).thenThrow(new ConnectionClosedException());
httpRequestExecutor.executeHttpRequest(HttpMethod.GET, URL, Optional.empty());
verify(softAssert).recordFailedAssertion(
(Exception) argThat(arg -> arg instanceof ConnectionClosedException
&& "Connection is closed".equals(((Exception) arg).getMessage())));
verify(httpTestContext).releaseRequestData();
}
@Override
public void log(final Exception ex) {
if (ex instanceof SocketTimeoutException || ex instanceof ConnectionClosedException) {
// Do nothing
} else {
ex.printStackTrace();
}
}
@Override
public BackgroundException map(final IOException failure) {
if(failure instanceof ConnectionClosedException) {
final StringBuilder buffer = new StringBuilder();
this.append(buffer, failure.getMessage());
return new ConnectionRefusedException(buffer.toString(), failure);
}
return super.map(failure);
}
protected BackgroundException wrap(final T failure, final String title, final StringBuilder buffer) {
if(buffer.toString().isEmpty()) {
log.warn(String.format("No message for failure %s", failure));
this.append(buffer, LocaleFactory.localizedString("Unknown"));
}
for(Throwable cause : ExceptionUtils.getThrowableList(failure)) {
if(cause instanceof InterruptedIOException) {
// Handling socket timeouts
return new ConnectionTimeoutException(buffer.toString(), failure);
}
if(cause instanceof TimeoutException) {
//
return new ConnectionTimeoutException(buffer.toString(), failure);
}
if(cause instanceof SocketException) {
return new DefaultSocketExceptionMappingService().map((SocketException) cause);
}
if(cause instanceof EOFException) {
return new ConnectionRefusedException(buffer.toString(), failure);
}
if(cause instanceof UnknownHostException) {
return new ResolveFailedException(buffer.toString(), failure);
}
if(cause instanceof NoHttpResponseException) {
return new ConnectionRefusedException(buffer.toString(), failure);
}
if(cause instanceof ConnectionClosedException) {
return new ConnectionRefusedException(buffer.toString(), failure);
}
if(cause instanceof InterruptedException) {
return new ConnectionCanceledException(buffer.toString(), failure);
}
}
if(failure instanceof RuntimeException) {
return new ConnectionCanceledException(title, buffer.toString(), failure);
}
return new BackgroundException(title, buffer.toString(), failure);
}
private static boolean isPrematureEndException(Throwable exception) {
return exception instanceof ConnectionClosedException && exception.getMessage() != null &&
exception.getMessage().startsWith("Premature end of Content-Length");
}