Search in sources :

Example 1 with APPLICATION_JSON

use of io.servicetalk.http.api.HttpHeaderValues.APPLICATION_JSON in project servicetalk by apple.

the class HttpReporter method encodedSpansReporter.

private static Function<Buffer, Completable> encodedSpansReporter(final HttpClient client, final Codec codec) {
    final String path;
    final CharSequence contentType;
    switch(codec) {
        case JSON_V1:
            path = V1_PATH;
            contentType = APPLICATION_JSON;
            break;
        case JSON_V2:
            path = V2_PATH;
            contentType = APPLICATION_JSON;
            break;
        case THRIFT:
            path = V2_PATH;
            contentType = THRIFT_CONTENT_TYPE;
            break;
        case PROTO3:
            path = V2_PATH;
            contentType = PROTO_CONTENT_TYPE;
            break;
        default:
            throw new IllegalArgumentException("Unknown codec: " + codec);
    }
    return encodedSpans -> client.request(client.post(path).setHeader(CONTENT_TYPE, contentType).payloadBody(encodedSpans)).beforeOnSuccess(response -> {
        HttpResponseStatus status = response.status();
        if (status.statusClass() != SUCCESSFUL_2XX) {
            LOGGER.info("Unexpected response from the collector. Response headers: {}", response.toString((__, headerValue) -> headerValue));
        }
    }).ignoreElement().onErrorComplete(cause -> {
        LOGGER.error("Failed to send a span, ignoring.", cause);
        return true;
    });
}
Also used : APPLICATION_JSON(io.servicetalk.http.api.HttpHeaderValues.APPLICATION_JSON) Publisher(io.servicetalk.concurrent.api.Publisher) LoggerFactory(org.slf4j.LoggerFactory) Span(zipkin2.Span) CONTENT_TYPE(io.servicetalk.http.api.HttpHeaderNames.CONTENT_TYPE) CharSequences.newAsciiString(io.servicetalk.buffer.api.CharSequences.newAsciiString) Duration.ofSeconds(java.time.Duration.ofSeconds) Function(java.util.function.Function) ArrayList(java.util.ArrayList) SourceAdapters.fromSource(io.servicetalk.concurrent.api.SourceAdapters.fromSource) Duration(java.time.Duration) Objects.requireNonNull(java.util.Objects.requireNonNull) Component(zipkin2.Component) SpanBytesEncoder(zipkin2.codec.SpanBytesEncoder) HttpClient(io.servicetalk.http.api.HttpClient) AsyncCloseable(io.servicetalk.concurrent.api.AsyncCloseable) Reporter(zipkin2.reporter.Reporter) Nonnull(javax.annotation.Nonnull) Processors.newCompletableProcessor(io.servicetalk.concurrent.api.Processors.newCompletableProcessor) Accumulator(io.servicetalk.concurrent.api.BufferStrategy.Accumulator) Processors.newPublisherProcessorDropHeadOnOverflow(io.servicetalk.concurrent.api.Processors.newPublisherProcessorDropHeadOnOverflow) Logger(org.slf4j.Logger) FutureUtils.awaitTermination(io.servicetalk.concurrent.internal.FutureUtils.awaitTermination) PublisherSource(io.servicetalk.concurrent.PublisherSource) OK(zipkin2.CheckResult.OK) Completable(io.servicetalk.concurrent.api.Completable) CompositeCloseable(io.servicetalk.concurrent.api.CompositeCloseable) CheckResult.failed(zipkin2.CheckResult.failed) BufferStrategies.forCountOrTime(io.servicetalk.concurrent.api.BufferStrategies.forCountOrTime) AsyncCloseables.newCompositeCloseable(io.servicetalk.concurrent.api.AsyncCloseables.newCompositeCloseable) CheckResult(zipkin2.CheckResult) AsyncCloseables.toAsyncCloseable(io.servicetalk.concurrent.api.AsyncCloseables.toAsyncCloseable) SourceAdapters.toSource(io.servicetalk.concurrent.api.SourceAdapters.toSource) CompletableSource(io.servicetalk.concurrent.CompletableSource) SUCCESSFUL_2XX(io.servicetalk.http.api.HttpResponseStatus.StatusClass.SUCCESSFUL_2XX) SingleAddressHttpClientBuilder(io.servicetalk.http.api.SingleAddressHttpClientBuilder) List(java.util.List) Buffer(io.servicetalk.buffer.api.Buffer) BufferAllocator(io.servicetalk.buffer.api.BufferAllocator) Completable.completed(io.servicetalk.concurrent.api.Completable.completed) Collections(java.util.Collections) HttpResponseStatus(io.servicetalk.http.api.HttpResponseStatus) HttpResponseStatus(io.servicetalk.http.api.HttpResponseStatus) CharSequences.newAsciiString(io.servicetalk.buffer.api.CharSequences.newAsciiString)

Example 2 with APPLICATION_JSON

use of io.servicetalk.http.api.HttpHeaderValues.APPLICATION_JSON in project servicetalk by apple.

the class HeaderUtilsTest method checkContentTypeCases.

@Test
void checkContentTypeCases() {
    final String invalidContentType = "invalid";
    final Predicate<HttpHeaders> jsonContentTypeValidator = headers -> headers.contains(CONTENT_TYPE, APPLICATION_JSON);
    checkContentType(headersWithContentType(APPLICATION_JSON), jsonContentTypeValidator);
    SerializationException e = assertThrows(SerializationException.class, () -> checkContentType(headersWithContentType(of(invalidContentType)), jsonContentTypeValidator));
    assertThat(e.getMessage(), containsString(invalidContentType));
}
Also used : APPLICATION_JSON(io.servicetalk.http.api.HttpHeaderValues.APPLICATION_JSON) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) SerializationException(io.servicetalk.serialization.api.SerializationException) CONTENT_TYPE(io.servicetalk.http.api.HttpHeaderNames.CONTENT_TYPE) HeaderUtils.checkContentType(io.servicetalk.http.api.HeaderUtils.checkContentType) HeaderUtils.isTransferEncodingChunked(io.servicetalk.http.api.HeaderUtils.isTransferEncodingChunked) HeaderUtils.pathMatches(io.servicetalk.http.api.HeaderUtils.pathMatches) Charset(java.nio.charset.Charset) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) ORIGIN(io.servicetalk.http.api.HttpHeaderNames.ORIGIN) APPLICATION_X_WWW_FORM_URLENCODED(io.servicetalk.http.api.HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) HeaderUtils.isTchar(io.servicetalk.http.api.HeaderUtils.isTchar) CHUNKED(io.servicetalk.http.api.HttpHeaderValues.CHUNKED) Predicate(java.util.function.Predicate) UTF_8(java.nio.charset.StandardCharsets.UTF_8) UTF_16(java.nio.charset.StandardCharsets.UTF_16) TRANSFER_ENCODING(io.servicetalk.http.api.HttpHeaderNames.TRANSFER_ENCODING) AsciiString.of(io.netty.util.AsciiString.of) CharsetDecoder(java.nio.charset.CharsetDecoder) CONTENT_LENGTH(io.servicetalk.http.api.HttpHeaderNames.CONTENT_LENGTH) Test(org.junit.jupiter.api.Test) US_ASCII(java.nio.charset.StandardCharsets.US_ASCII) CharsetEncoder(java.nio.charset.CharsetEncoder) TEXT_PLAIN_UTF_8(io.servicetalk.http.api.HttpHeaderValues.TEXT_PLAIN_UTF_8) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) INSTANCE(io.servicetalk.http.api.EmptyHttpHeaders.INSTANCE) APPLICATION_X_WWW_FORM_URLENCODED_UTF_8(io.servicetalk.http.api.HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED_UTF_8) TEXT_PLAIN(io.servicetalk.http.api.HttpHeaderValues.TEXT_PLAIN) Matchers.containsString(org.hamcrest.Matchers.containsString) DEFAULT_DEBUG_HEADER_FILTER(io.servicetalk.http.api.HeaderUtils.DEFAULT_DEBUG_HEADER_FILTER) SerializationException(io.servicetalk.serialization.api.SerializationException) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.jupiter.api.Test)

Aggregations

CONTENT_TYPE (io.servicetalk.http.api.HttpHeaderNames.CONTENT_TYPE)2 APPLICATION_JSON (io.servicetalk.http.api.HttpHeaderValues.APPLICATION_JSON)2 AsciiString.of (io.netty.util.AsciiString.of)1 Buffer (io.servicetalk.buffer.api.Buffer)1 BufferAllocator (io.servicetalk.buffer.api.BufferAllocator)1 CharSequences.newAsciiString (io.servicetalk.buffer.api.CharSequences.newAsciiString)1 CompletableSource (io.servicetalk.concurrent.CompletableSource)1 PublisherSource (io.servicetalk.concurrent.PublisherSource)1 AsyncCloseable (io.servicetalk.concurrent.api.AsyncCloseable)1 AsyncCloseables.newCompositeCloseable (io.servicetalk.concurrent.api.AsyncCloseables.newCompositeCloseable)1 AsyncCloseables.toAsyncCloseable (io.servicetalk.concurrent.api.AsyncCloseables.toAsyncCloseable)1 BufferStrategies.forCountOrTime (io.servicetalk.concurrent.api.BufferStrategies.forCountOrTime)1 Accumulator (io.servicetalk.concurrent.api.BufferStrategy.Accumulator)1 Completable (io.servicetalk.concurrent.api.Completable)1 Completable.completed (io.servicetalk.concurrent.api.Completable.completed)1 CompositeCloseable (io.servicetalk.concurrent.api.CompositeCloseable)1 Processors.newCompletableProcessor (io.servicetalk.concurrent.api.Processors.newCompletableProcessor)1 Processors.newPublisherProcessorDropHeadOnOverflow (io.servicetalk.concurrent.api.Processors.newPublisherProcessorDropHeadOnOverflow)1 Publisher (io.servicetalk.concurrent.api.Publisher)1 SourceAdapters.fromSource (io.servicetalk.concurrent.api.SourceAdapters.fromSource)1