Java Code Examples for com.linecorp.armeria.common.ResponseHeaders#status()

The following examples show how to use com.linecorp.armeria.common.ResponseHeaders#status() . 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: ServerSentEvents.java    From armeria with Apache License 2.0 6 votes vote down vote up
static ResponseHeaders ensureHttpStatus(ResponseHeaders headers) {
    final HttpStatus status = headers.status();
    if (status.equals(HttpStatus.OK)) {
        return headers;
    }

    if (!warnedStatusCode) {
        logger.warn(
                "Overwriting the HTTP status code from '{}' to '{}' for Server-Sent Events. " +
                "Do not set an HTTP status code on the HttpHeaders when calling factory methods in '{}', " +
                "or set '{}' if you want to specify its status code. " +
                "Please refer to https://www.w3.org/TR/eventsource/ for more information.",
                status, HttpStatus.OK, ServerSentEvents.class.getSimpleName(), HttpStatus.OK);
        warnedStatusCode = true;
    }
    return headers.toBuilder().status(HttpStatus.OK).build();
}
 
Example 2
Source File: JsonTextSequences.java    From armeria with Apache License 2.0 6 votes vote down vote up
static ResponseHeaders ensureHttpStatus(ResponseHeaders headers) {
    final HttpStatus status = headers.status();
    if (status.equals(HttpStatus.OK)) {
        return headers;
    }

    if (!warnedStatusCode) {
        logger.warn(
                "Overwriting the HTTP status code from '{}' to '{}' for JSON Text Sequences. " +
                "Do not set an HTTP status code on the HttpHeaders when calling factory methods in '{}', " +
                "or set '{}' if you want to specify its status code. " +
                "Please refer to https://tools.ietf.org/html/rfc7464 for more information.",
                status, HttpStatus.OK, JsonTextSequences.class.getSimpleName(), HttpStatus.OK);
        warnedStatusCode = true;
    }
    return headers.toBuilder().status(HttpStatus.OK).build();
}
 
Example 3
Source File: ArmeriaEurekaClientTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
private static <T> EurekaHttpResponse<T> convertResponse(HttpResponse response, Class<T> type) {
    final AggregatedHttpResponse aggregatedRes = response.aggregate().join();
    T t = null;
    final ResponseHeaders headers = aggregatedRes.headers();
    if (headers.status() == HttpStatus.OK) {
        final EntityBodyConverter converter = new EntityBodyConverter();
        try {
            // noinspection unchecked
            t = (T) converter.read(
                    aggregatedRes.content().toInputStream(), type,
                    MediaType.valueOf(headers.contentType().toString()));
        } catch (IOException e) {
            throw new RuntimeException("Unexpected exception while converting response body:", e);
        }
    }

    return anEurekaHttpResponse(aggregatedRes.status().code(), t)
            .headers(headersOf(headers))
            .build();
}
 
Example 4
Source File: ArmeriaClientHttpResponse.java    From armeria with Apache License 2.0 5 votes vote down vote up
ArmeriaClientHttpResponse(ResponseHeaders headers,
                          ResponseBodyPublisher publisher,
                          DataBufferFactoryWrapper<?> factoryWrapper) {
    this.headers = requireNonNull(headers, "headers");
    status = headers.status();

    body = Flux.from(publisher).cast(HttpData.class).map(factoryWrapper::toDataBuffer);
}
 
Example 5
Source File: HttpHealthChecker.java    From armeria with Apache License 2.0 4 votes vote down vote up
@Override
public void onNext(HttpObject obj) {
    if (closeable.isClosing()) {
        subscription.cancel();
        return;
    }

    try {
        if (!(obj instanceof ResponseHeaders)) {
            ReferenceCountUtil.release(obj);
            return;
        }

        final ResponseHeaders headers = (ResponseHeaders) obj;
        updateLongPollingSettings(headers);

        final HttpStatus status = headers.status();
        final HttpStatusClass statusClass = status.codeClass();
        switch (statusClass) {
            case INFORMATIONAL:
                maybeSchedulePingCheck();
                break;
            case SERVER_ERROR:
                receivedExpectedResponse = true;
                break;
            case SUCCESS:
                isHealthy = true;
                receivedExpectedResponse = true;
                break;
            default:
                if (status == HttpStatus.NOT_MODIFIED) {
                    isHealthy = wasHealthy;
                    receivedExpectedResponse = true;
                } else {
                    // Do not use long polling on an unexpected status for safety.
                    maxLongPollingSeconds = 0;

                    if (statusClass == HttpStatusClass.CLIENT_ERROR) {
                        logger.warn("{} Unexpected 4xx health check response: {} A 4xx response " +
                                    "generally indicates a misconfiguration of the client. " +
                                    "Did you happen to forget to configure the {}'s client options?",
                                    reqCtx, headers, HealthCheckedEndpointGroup.class.getSimpleName());
                    } else {
                        logger.warn("{} Unexpected health check response: {}", reqCtx, headers);
                    }
                }
        }
    } finally {
        subscription.request(1);
    }
}
 
Example 6
Source File: HealthCheckedEndpointGroupLongPollingTest.java    From armeria with Apache License 2.0 4 votes vote down vote up
@Test
void longPollingDisabledOnStop() throws Exception {
    final BlockingQueue<RequestLog> healthCheckRequestLogs = new LinkedTransferQueue<>();
    this.healthCheckRequestLogs = healthCheckRequestLogs;
    final Endpoint endpoint = Endpoint.of("127.0.0.1", server.httpPort());
    try (HealthCheckedEndpointGroup endpointGroup = build(
            HealthCheckedEndpointGroup.builder(endpoint, HEALTH_CHECK_PATH))) {

        // Check the initial state (healthy).
        assertThat(endpointGroup.endpoints()).containsExactly(endpoint);

        // Drop the first request.
        healthCheckRequestLogs.take();

        // Stop the server.
        server.stop();
        waitForGroup(endpointGroup, null);

        // Must receive the '503 Service Unavailable' response with long polling disabled,
        // so that the next health check respects the backoff.
        for (;;) {
            final ResponseHeaders stoppingResponseHeaders = healthCheckRequestLogs.take().responseHeaders();
            if (stoppingResponseHeaders.status() == HttpStatus.OK) {
                // It is possible to get '200 OK' if the server sent a response before the shutdown.
                // Just try again so that another health check request is sent.
                continue;
            }

            assertThat(stoppingResponseHeaders.status()).isEqualTo(HttpStatus.SERVICE_UNAVAILABLE);
            assertThat(stoppingResponseHeaders.getLong("armeria-lphc")).isNull();
            break;
        }

        // Check the next check respected backoff, because there's no point of
        // sending a request immediately only to get a 'connection refused' error.
        final Stopwatch stopwatch = Stopwatch.createStarted();
        healthCheckRequestLogs.take();
        assertThat(stopwatch.elapsed(TimeUnit.MILLISECONDS))
                .isGreaterThan(RETRY_INTERVAL.toMillis() * 4 / 5);
    }
}