Search in sources :

Example 1 with CONTENT_TYPE

use of io.servicetalk.http.api.HttpHeaderNames.CONTENT_TYPE 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 CONTENT_TYPE

use of io.servicetalk.http.api.HttpHeaderNames.CONTENT_TYPE in project servicetalk by apple.

the class TestServiceStreaming method newEchoResponse.

private static StreamingHttpResponse newEchoResponse(final StreamingHttpRequest req, final StreamingHttpResponseFactory factory) {
    final StreamingHttpResponse response = factory.ok().version(req.version()).transformMessageBody(pub -> pub.ignoreElements().merge(req.messageBody())).transform(new StatelessTrailersTransformer<>());
    final CharSequence contentLength = req.headers().get(CONTENT_LENGTH);
    if (contentLength != null) {
        response.addHeader(CONTENT_LENGTH, contentLength);
    }
    final CharSequence contentType = req.headers().get(CONTENT_TYPE);
    if (contentType != null) {
        response.addHeader(CONTENT_TYPE, contentType);
    }
    final CharSequence transferEncoding = req.headers().get(TRANSFER_ENCODING);
    if (transferEncoding != null) {
        response.addHeader(TRANSFER_ENCODING, transferEncoding);
    }
    return response;
}
Also used : StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) Publisher(io.servicetalk.concurrent.api.Publisher) LoggerFactory(org.slf4j.LoggerFactory) CONTENT_TYPE(io.servicetalk.http.api.HttpHeaderNames.CONTENT_TYPE) Publisher.empty(io.servicetalk.concurrent.api.Publisher.empty) Function(java.util.function.Function) HttpServiceContext(io.servicetalk.http.api.HttpServiceContext) Single.succeeded(io.servicetalk.concurrent.api.Single.succeeded) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) Publisher.from(io.servicetalk.concurrent.api.Publisher.from) DELIBERATE_EXCEPTION(io.servicetalk.concurrent.internal.DeliberateException.DELIBERATE_EXCEPTION) ConnectionContext(io.servicetalk.transport.api.ConnectionContext) Logger(org.slf4j.Logger) Single(io.servicetalk.concurrent.api.Single) Completable(io.servicetalk.concurrent.api.Completable) NO_CONTENT(io.servicetalk.http.api.HttpResponseStatus.NO_CONTENT) TRANSFER_ENCODING(io.servicetalk.http.api.HttpHeaderNames.TRANSFER_ENCODING) StatelessTrailersTransformer(io.servicetalk.http.api.StatelessTrailersTransformer) CONTENT_LENGTH(io.servicetalk.http.api.HttpHeaderNames.CONTENT_LENGTH) Single.never(io.servicetalk.concurrent.api.Single.never) Buffer(io.servicetalk.buffer.api.Buffer) String.valueOf(java.lang.String.valueOf) StreamingHttpService(io.servicetalk.http.api.StreamingHttpService) NOT_FOUND(io.servicetalk.http.api.HttpResponseStatus.NOT_FOUND) StreamingHttpResponseFactory(io.servicetalk.http.api.StreamingHttpResponseFactory) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse)

Example 3 with CONTENT_TYPE

use of io.servicetalk.http.api.HttpHeaderNames.CONTENT_TYPE in project servicetalk by apple.

the class SslProvidersTest method setUp.

private void setUp(SslProvider serverSslProvider, SslProvider clientSslProvider, int payloadLength) throws Exception {
    payloadBody = randomString(payloadLength);
    serverContext = HttpServers.forAddress(localAddress(0)).sslConfig(new ServerSslConfigBuilder(DefaultTestCerts::loadServerPem, DefaultTestCerts::loadServerKey).provider(serverSslProvider).build()).listenBlockingAndAwait((ctx, request, responseFactory) -> {
        assertThat(ctx.sslSession(), is(notNullValue()));
        assertThat(request.path(), is("/path"));
        assertThat(request.headers().get(CONTENT_TYPE), is(TEXT_PLAIN_UTF_8));
        assertThat(request.payloadBody(textSerializerUtf8()), is("request-payload-body-" + payloadBody));
        return responseFactory.ok().payloadBody("response-payload-body-" + payloadBody, textSerializerUtf8());
    });
    client = HttpClients.forSingleAddress(serverHostAndPort(serverContext)).ioExecutor(NettyIoExecutors.createIoExecutor("client-io")).sslConfig(new ClientSslConfigBuilder(DefaultTestCerts::loadServerCAPem).peerHost(serverPemHostname()).provider(clientSslProvider).build()).buildBlocking();
}
Also used : ServerSslConfigBuilder(io.servicetalk.transport.api.ServerSslConfigBuilder) CONTENT_TYPE(io.servicetalk.http.api.HttpHeaderNames.CONTENT_TYPE) OPENSSL(io.servicetalk.transport.api.SslProvider.OPENSSL) SslProvider(io.servicetalk.transport.api.SslProvider) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) HttpSerializers.textSerializerUtf8(io.servicetalk.http.api.HttpSerializers.textSerializerUtf8) AddressUtils.serverHostAndPort(io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort) DefaultTestCerts(io.servicetalk.test.resources.DefaultTestCerts) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) JDK(io.servicetalk.transport.api.SslProvider.JDK) MethodSource(org.junit.jupiter.params.provider.MethodSource) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) NettyIoExecutors(io.servicetalk.transport.netty.NettyIoExecutors) AddressUtils.localAddress(io.servicetalk.transport.netty.internal.AddressUtils.localAddress) ServerContext(io.servicetalk.transport.api.ServerContext) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) HttpResponse(io.servicetalk.http.api.HttpResponse) Arguments(org.junit.jupiter.params.provider.Arguments) OK(io.servicetalk.http.api.HttpResponseStatus.OK) DefaultTestCerts.serverPemHostname(io.servicetalk.test.resources.DefaultTestCerts.serverPemHostname) AfterEach(org.junit.jupiter.api.AfterEach) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Stream(java.util.stream.Stream) TEXT_PLAIN_UTF_8(io.servicetalk.http.api.HttpHeaderValues.TEXT_PLAIN_UTF_8) ClientSslConfigBuilder(io.servicetalk.transport.api.ClientSslConfigBuilder) Matchers.is(org.hamcrest.Matchers.is) DefaultTestCerts(io.servicetalk.test.resources.DefaultTestCerts) ClientSslConfigBuilder(io.servicetalk.transport.api.ClientSslConfigBuilder) ServerSslConfigBuilder(io.servicetalk.transport.api.ServerSslConfigBuilder)

Example 4 with CONTENT_TYPE

use of io.servicetalk.http.api.HttpHeaderNames.CONTENT_TYPE 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)4 Buffer (io.servicetalk.buffer.api.Buffer)2 Completable (io.servicetalk.concurrent.api.Completable)2 Publisher (io.servicetalk.concurrent.api.Publisher)2 CONTENT_LENGTH (io.servicetalk.http.api.HttpHeaderNames.CONTENT_LENGTH)2 TRANSFER_ENCODING (io.servicetalk.http.api.HttpHeaderNames.TRANSFER_ENCODING)2 APPLICATION_JSON (io.servicetalk.http.api.HttpHeaderValues.APPLICATION_JSON)2 TEXT_PLAIN_UTF_8 (io.servicetalk.http.api.HttpHeaderValues.TEXT_PLAIN_UTF_8)2 Function (java.util.function.Function)2 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 AsciiString.of (io.netty.util.AsciiString.of)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