Java Code Examples for com.linecorp.armeria.common.HttpResponse#abort()

The following examples show how to use com.linecorp.armeria.common.HttpResponse#abort() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DecodedHttpRequest.java    From armeria with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the specified {@link HttpResponse} which responds to this request. This is always called
 * by the {@link HttpServerHandler} after the handler gets the {@link HttpResponse} from an
 * {@link HttpService}.
 */
void setResponse(HttpResponse response) {
    if (isResponseAborted) {
        // This means that we already tried to close the request, so abort the response immediately.
        if (!response.isComplete()) {
            response.abort();
        }
    } else {
        this.response = response;
    }
}
 
Example 2
Source File: HttpClientIntegrationTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Test
void responseAbortWithException() throws InterruptedException {
    final WebClient client = WebClient.of(server.httpUri());
    final HttpRequest request = HttpRequest.streaming(HttpMethod.GET, "/client-aborted");
    final HttpResponse response = client.execute(request);

    await().until(() -> completed);
    final IllegalStateException badState = new IllegalStateException("bad state");
    response.abort(badState);
    assertThatThrownBy(() -> response.aggregate().join())
            .isInstanceOf(CompletionException.class)
            .hasCause(badState);
}
 
Example 3
Source File: RetryingClientTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Test
void doNotRetryWhenResponseIsAborted() throws Exception {
    final List<Throwable> abortCauses =
            Arrays.asList(null, new IllegalStateException("abort stream with a specified cause"));
    for (Throwable abortCause : abortCauses) {
        final AtomicReference<ClientRequestContext> context = new AtomicReference<>();
        final WebClient client =
                WebClient.builder(server.httpUri())
                         .decorator(RetryingClient.newDecorator(retryAlways))
                         .decorator((delegate, ctx, req) -> {
                             context.set(ctx);
                             return delegate.execute(ctx, req);
                         })
                         .build();
        final HttpResponse httpResponse = client.get("/response-abort");
        if (abortCause == null) {
            httpResponse.abort();
        } else {
            httpResponse.abort(abortCause);
        }

        final RequestLog log = context.get().log().whenComplete().join();
        assertThat(responseAbortServiceCallCounter.get()).isOne();
        assertThat(log.requestCause()).isNull();
        if (abortCause == null) {
            assertThat(log.responseCause()).isExactlyInstanceOf(AbortedStreamException.class);
        } else {
            assertThat(log.responseCause()).isSameAs(abortCause);
        }

        // Sleep 3 more seconds to check if there was another retry.
        TimeUnit.SECONDS.sleep(3);
        assertThat(responseAbortServiceCallCounter.get()).isOne();
        responseAbortServiceCallCounter.decrementAndGet();
    }
}