Search in sources :

Example 1 with HttpResponse

use of com.linecorp.armeria.common.HttpResponse in project curiostack by curioswitch.

the class StorageClient method createFile.

/**
 * Create a new file for uploading data to cloud storage.
 */
public ListenableFuture<FileWriter> createFile(String filename, Map<String, String> metadata, RequestContext ctx) {
    FileRequest request = ImmutableFileRequest.builder().name(filename).metadata(metadata).build();
    ByteBuf buf = ctx.alloc().buffer();
    try (ByteBufOutputStream os = new ByteBufOutputStream(buf)) {
        OBJECT_MAPPER.writeValue((DataOutput) os, request);
    } catch (IOException e) {
        buf.release();
        throw new UncheckedIOException("Could not serialize resource JSON to buffer.", e);
    }
    HttpData data = new ByteBufHttpData(buf, true);
    HttpHeaders headers = HttpHeaders.of(HttpMethod.POST, uploadUrl).contentType(MediaType.JSON_UTF_8);
    HttpResponse res = httpClient.execute(headers, data);
    return CompletableFuturesExtra.toListenableFuture(res.aggregate(ctx.contextAwareEventLoop()).handle((msg, t) -> {
        if (t != null) {
            throw new RuntimeException("Unexpected error creating new file.", t);
        }
        HttpHeaders responseHeaders = msg.headers();
        if (!responseHeaders.status().equals(HttpStatus.OK)) {
            throw new RuntimeException("Non-successful response when creating new file: " + responseHeaders + "\n" + msg.content().toStringUtf8());
        }
        String location = responseHeaders.get(HttpHeaderNames.LOCATION);
        String pathAndQuery = location.substring("https://www.googleapis.com/upload/storage/v1".length());
        return new FileWriter(pathAndQuery, ctx, httpClient);
    }));
}
Also used : ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ForStorage(org.curioswitch.curiostack.gcloud.storage.StorageModule.ForStorage) Singleton(javax.inject.Singleton) HttpHeaderNames(com.linecorp.armeria.common.HttpHeaderNames) MediaType(com.linecorp.armeria.common.MediaType) Inject(javax.inject.Inject) ByteBuf(io.netty.buffer.ByteBuf) HttpStatus(com.linecorp.armeria.common.HttpStatus) JsonSerialize(com.fasterxml.jackson.databind.annotation.JsonSerialize) Map(java.util.Map) HttpData(com.linecorp.armeria.common.HttpData) HttpResponse(com.linecorp.armeria.common.HttpResponse) DataOutput(java.io.DataOutput) CompletableFuturesExtra(com.spotify.futures.CompletableFuturesExtra) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) HttpMethod(com.linecorp.armeria.common.HttpMethod) Immutable(org.immutables.value.Value.Immutable) ByteBufHttpData(com.linecorp.armeria.unsafe.ByteBufHttpData) ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) RequestContext(com.linecorp.armeria.common.RequestContext) UncheckedIOException(java.io.UncheckedIOException) HttpClient(com.linecorp.armeria.client.HttpClient) HttpHeaders(com.linecorp.armeria.common.HttpHeaders) JsonDeserialize(com.fasterxml.jackson.databind.annotation.JsonDeserialize) HttpHeaders(com.linecorp.armeria.common.HttpHeaders) ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) HttpResponse(com.linecorp.armeria.common.HttpResponse) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) ByteBuf(io.netty.buffer.ByteBuf) HttpData(com.linecorp.armeria.common.HttpData) ByteBufHttpData(com.linecorp.armeria.unsafe.ByteBufHttpData) ByteBufHttpData(com.linecorp.armeria.unsafe.ByteBufHttpData)

Example 2 with HttpResponse

use of com.linecorp.armeria.common.HttpResponse in project zipkin by openzipkin.

the class ScribeInboundHandler method channelRead.

@Override
public void channelRead(ChannelHandlerContext ctx, Object payload) {
    assert payload instanceof ByteBuf;
    HttpRequest request = HttpRequest.of(THRIFT_HEADERS, HttpData.wrap((ByteBuf) payload));
    ServiceRequestContextBuilder requestContextBuilder = ServiceRequestContext.builder(request).service(scribeService).alloc(ctx.alloc());
    if (ctx.executor() instanceof EventLoop) {
        requestContextBuilder.eventLoop((EventLoop) ctx.executor());
    }
    ServiceRequestContext requestContext = requestContextBuilder.build();
    final HttpResponse response;
    try (SafeCloseable unused = requestContext.push()) {
        response = HttpResponse.of(scribeService.serve(requestContext, request));
    } catch (Throwable t) {
        propagateIfFatal(t);
        exceptionCaught(ctx, t);
        return;
    }
    int responseIndex = nextResponseIndex++;
    response.aggregateWithPooledObjects(ctx.executor(), ctx.alloc()).handle((msg, t) -> {
        if (t != null) {
            exceptionCaught(ctx, t);
            return null;
        }
        try (HttpData content = msg.content()) {
            ByteBuf returned = ctx.alloc().buffer(content.length() + 4);
            returned.writeInt(content.length());
            returned.writeBytes(content.byteBuf());
            if (responseIndex == previouslySentResponseIndex + 1) {
                ctx.writeAndFlush(returned);
                previouslySentResponseIndex++;
                flushResponses(ctx);
            } else {
                pendingResponses.put(responseIndex, returned);
            }
        }
        return null;
    });
}
Also used : HttpRequest(com.linecorp.armeria.common.HttpRequest) ServiceRequestContextBuilder(com.linecorp.armeria.server.ServiceRequestContextBuilder) EventLoop(io.netty.channel.EventLoop) ServiceRequestContext(com.linecorp.armeria.server.ServiceRequestContext) HttpData(com.linecorp.armeria.common.HttpData) HttpResponse(com.linecorp.armeria.common.HttpResponse) ByteBuf(io.netty.buffer.ByteBuf) SafeCloseable(com.linecorp.armeria.common.util.SafeCloseable)

Example 3 with HttpResponse

use of com.linecorp.armeria.common.HttpResponse in project zipkin by openzipkin.

the class HttpCall method sendRequest.

CompletableFuture<AggregatedHttpResponse> sendRequest() {
    final HttpResponse response;
    try (SafeCloseable ignored = Clients.withContextCustomizer(ctx -> ctx.logBuilder().name(name))) {
        HttpRequestWriter httpRequest = HttpRequest.streaming(request.headers());
        response = httpClient.execute(httpRequest);
        request.writeBody(httpRequest::tryWrite);
        httpRequest.close();
    }
    CompletableFuture<AggregatedHttpResponse> responseFuture = RequestContext.mapCurrent(ctx -> response.aggregateWithPooledObjects(ctx.eventLoop(), ctx.alloc()), // This should never be used in practice since the module runs in an Armeria server.
    response::aggregate);
    responseFuture = responseFuture.exceptionally(t -> {
        if (t instanceof UnprocessedRequestException) {
            Throwable cause = t.getCause();
            // Go ahead and reduce the output in logs since this is usually a configuration or
            // infrastructure issue and the Armeria stack trace won't help debugging that.
            Exceptions.clearTrace(cause);
            String message = cause.getMessage();
            if (message == null)
                message = cause.getClass().getSimpleName();
            throw new RejectedExecutionException(message, cause);
        } else {
            Exceptions.throwUnsafely(t);
        }
        return null;
    });
    this.responseFuture = responseFuture;
    return responseFuture;
}
Also used : EventExecutor(io.netty.util.concurrent.EventExecutor) AggregatedHttpRequest(com.linecorp.armeria.common.AggregatedHttpRequest) OBJECT_MAPPER(zipkin2.elasticsearch.internal.JsonSerializers.OBJECT_MAPPER) RequestHeaders(com.linecorp.armeria.common.RequestHeaders) CompletableFuture(java.util.concurrent.CompletableFuture) Supplier(java.util.function.Supplier) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) HttpStatus(com.linecorp.armeria.common.HttpStatus) WebClient(com.linecorp.armeria.client.WebClient) Clients(com.linecorp.armeria.client.Clients) AggregatedHttpResponse(com.linecorp.armeria.common.AggregatedHttpResponse) JsonNode(com.fasterxml.jackson.databind.JsonNode) Callback(zipkin2.Callback) HttpRequestWriter(com.linecorp.armeria.common.HttpRequestWriter) Exceptions(com.linecorp.armeria.common.util.Exceptions) UnprocessedRequestException(com.linecorp.armeria.client.UnprocessedRequestException) SafeCloseable(com.linecorp.armeria.common.util.SafeCloseable) HttpData(com.linecorp.armeria.common.HttpData) HttpResponse(com.linecorp.armeria.common.HttpResponse) JsonParser(com.fasterxml.jackson.core.JsonParser) IOException(java.io.IOException) CompletionException(java.util.concurrent.CompletionException) FileNotFoundException(java.io.FileNotFoundException) HttpStatusClass(com.linecorp.armeria.common.HttpStatusClass) RequestContext(com.linecorp.armeria.common.RequestContext) HttpRequest(com.linecorp.armeria.common.HttpRequest) HttpHeaders(com.linecorp.armeria.common.HttpHeaders) JSON_FACTORY(zipkin2.elasticsearch.internal.JsonSerializers.JSON_FACTORY) Call(zipkin2.Call) InputStream(java.io.InputStream) UnprocessedRequestException(com.linecorp.armeria.client.UnprocessedRequestException) HttpRequestWriter(com.linecorp.armeria.common.HttpRequestWriter) AggregatedHttpResponse(com.linecorp.armeria.common.AggregatedHttpResponse) HttpResponse(com.linecorp.armeria.common.HttpResponse) SafeCloseable(com.linecorp.armeria.common.util.SafeCloseable) AggregatedHttpResponse(com.linecorp.armeria.common.AggregatedHttpResponse) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Example 4 with HttpResponse

use of com.linecorp.armeria.common.HttpResponse in project curiostack by curioswitch.

the class ArmeriaSdkHttpClient method execute.

@Override
public CompletableFuture<Void> execute(AsyncExecuteRequest executeRequest) {
    SdkHttpRequest httpRequest = executeRequest.request();
    SdkAsyncHttpResponseHandler handler = executeRequest.responseHandler();
    RequestHeadersBuilder headersBuilder = RequestHeaders.builder(convert(httpRequest.method()), httpRequest.getUri().toString());
    executeRequest.requestContentPublisher().contentLength().ifPresent(contentLength -> headersBuilder.add(HttpHeaderNames.CONTENT_LENGTH, contentLength.toString()));
    for (Map.Entry<String, List<String>> header : httpRequest.headers().entrySet()) {
        headersBuilder.add(header.getKey(), header.getValue());
    }
    Publisher<HttpData> requestStream = delegate -> executeRequest.requestContentPublisher().subscribe(new SdkToHttpDataSubscriber(delegate));
    HttpRequest request = HttpRequest.of(headersBuilder.build(), requestStream);
    HttpResponse response = client.execute(request);
    response.subscribe(new ResponseSubscriber(handler));
    CompletableFuture<Void> completionFuture = response.whenComplete();
    completionFuture.whenComplete((unused, t) -> {
        if (t != null) {
            // Subscriber.onError, SdkAsyncHttpResponseHandler.onError, the returned future, and any
            // thrown exception are all ways of communicating errors to the SDK. This seems like two
            // too many but cover all the bases just in case.
            handler.onError(t);
        }
    });
    return completionFuture;
}
Also used : RequestHeaders(com.linecorp.armeria.common.RequestHeaders) DelegatingSubscriber(software.amazon.awssdk.utils.async.DelegatingSubscriber) CompletableFuture(java.util.concurrent.CompletableFuture) AttributeMap(software.amazon.awssdk.utils.AttributeMap) HttpHeaderNames(com.linecorp.armeria.common.HttpHeaderNames) ByteBuffer(java.nio.ByteBuffer) Unpooled(io.netty.buffer.Unpooled) RequestHeadersBuilder(com.linecorp.armeria.common.RequestHeadersBuilder) MonotonicNonNull(org.checkerframework.checker.nullness.qual.MonotonicNonNull) HttpObject(com.linecorp.armeria.common.HttpObject) WebClient(com.linecorp.armeria.client.WebClient) Map(java.util.Map) SdkHttpMethod(software.amazon.awssdk.http.SdkHttpMethod) SdkAsyncHttpClient(software.amazon.awssdk.http.async.SdkAsyncHttpClient) SdkAsyncHttpResponseHandler(software.amazon.awssdk.http.async.SdkAsyncHttpResponseHandler) Subscriber(org.reactivestreams.Subscriber) Nullable(javax.annotation.Nullable) HttpData(com.linecorp.armeria.common.HttpData) SdkHttpResponse(software.amazon.awssdk.http.SdkHttpResponse) HttpResponse(com.linecorp.armeria.common.HttpResponse) SdkHttpRequest(software.amazon.awssdk.http.SdkHttpRequest) Publisher(org.reactivestreams.Publisher) ClientOptions(com.linecorp.armeria.client.ClientOptions) HttpMethod(com.linecorp.armeria.common.HttpMethod) AsyncExecuteRequest(software.amazon.awssdk.http.async.AsyncExecuteRequest) HttpRequest(com.linecorp.armeria.common.HttpRequest) List(java.util.List) HttpHeaders(com.linecorp.armeria.common.HttpHeaders) ResponseHeaders(com.linecorp.armeria.common.ResponseHeaders) Subscription(org.reactivestreams.Subscription) SdkHttpRequest(software.amazon.awssdk.http.SdkHttpRequest) HttpRequest(com.linecorp.armeria.common.HttpRequest) SdkHttpResponse(software.amazon.awssdk.http.SdkHttpResponse) HttpResponse(com.linecorp.armeria.common.HttpResponse) SdkHttpRequest(software.amazon.awssdk.http.SdkHttpRequest) SdkAsyncHttpResponseHandler(software.amazon.awssdk.http.async.SdkAsyncHttpResponseHandler) HttpData(com.linecorp.armeria.common.HttpData) List(java.util.List) RequestHeadersBuilder(com.linecorp.armeria.common.RequestHeadersBuilder) AttributeMap(software.amazon.awssdk.utils.AttributeMap) Map(java.util.Map)

Example 5 with HttpResponse

use of com.linecorp.armeria.common.HttpResponse in project curiostack by curioswitch.

the class ArmeriaSdkHttpClientTest method converts_success.

@Test
void converts_success() {
    StreamMessage<ByteBuffer> requestStream = StreamMessage.of(ByteBuffer.wrap("meow".getBytes(StandardCharsets.UTF_8)), ByteBuffer.wrap("woof".getBytes(StandardCharsets.UTF_8)));
    AtomicReference<SdkHttpResponse> responseHeaders = new AtomicReference<>();
    AtomicReference<Throwable> responseError = new AtomicReference<>();
    AtomicReference<Throwable> responseSubscriberError = new AtomicReference<>();
    AtomicBoolean responseComplete = new AtomicBoolean();
    List<ByteBuffer> responsePayloads = new ArrayList<>();
    AsyncExecuteRequest request = AsyncExecuteRequest.builder().request(SdkHttpRequest.builder().protocol("https").method(SdkHttpMethod.POST).host("github.com").appendHeader("X-Animals", "cat").appendHeader("X-Animals", "dog").appendHeader("Content-Type", "application/json").encodedPath("/foo").appendRawQueryParameter("bar", "baz").build()).responseHandler(new SdkAsyncHttpResponseHandler() {

        @Override
        public void onHeaders(SdkHttpResponse headers) {
            responseHeaders.set(headers);
        }

        @Override
        public void onStream(Publisher<ByteBuffer> stream) {
            stream.subscribe(new Subscriber<ByteBuffer>() {

                @Override
                public void onSubscribe(Subscription s) {
                    s.request(Long.MAX_VALUE);
                }

                @Override
                public void onNext(ByteBuffer byteBuffer) {
                    responsePayloads.add(byteBuffer);
                }

                @Override
                public void onError(Throwable t) {
                    responseSubscriberError.set(t);
                }

                @Override
                public void onComplete() {
                    responseComplete.set(true);
                }
            });
        }

        @Override
        public void onError(Throwable error) {
            responseError.set(error);
        }
    }).requestContentPublisher(new SdkHttpContentPublisher() {

        @Override
        public Optional<Long> contentLength() {
            return Optional.of((long) ("body1" + "body2").length());
        }

        @Override
        public void subscribe(Subscriber<? super ByteBuffer> s) {
            requestStream.subscribe(s);
        }
    }).fullDuplex(false).build();
    AtomicReference<AggregatedHttpRequest> aggregatedRequest = new AtomicReference<>();
    when(webClient.execute(any(HttpRequest.class))).thenAnswer((Answer<HttpResponse>) invocation -> {
        HttpRequest httpRequest = invocation.getArgument(0);
        aggregatedRequest.set(httpRequest.aggregate().join());
        return HttpResponse.of(ResponseHeaders.of(HttpStatus.OK, HttpHeaderNames.CONTENT_TYPE, "application/json", HttpHeaderNames.of("X-Countries"), "Japan", HttpHeaderNames.of("X-Countries"), "US"), HttpData.ofUtf8("purr"), HttpData.ofUtf8("growl"));
    });
    CompletableFuture<Void> responseFuture = client.execute(request);
    assertThat(responseFuture.join()).isNull();
    assertThat(responseHeaders.get()).satisfies(headers -> {
        assertThat(headers.statusCode()).isEqualTo(HttpStatus.OK.code());
        assertThat(headers.statusText()).hasValue(HttpStatus.OK.reasonPhrase());
        assertThat(headers.headers()).containsEntry("content-type", ImmutableList.of("application/json")).containsEntry("x-countries", ImmutableList.of("Japan", "US"));
    });
    assertThat(responsePayloads).extracting(buf -> Unpooled.wrappedBuffer(buf).toString(StandardCharsets.UTF_8)).containsExactly("purr", "growl");
    assertThat(responseError.get()).isNull();
    assertThat(responseSubscriberError.get()).isNull();
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) AggregatedHttpRequest(com.linecorp.armeria.common.AggregatedHttpRequest) Mock(org.mockito.Mock) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) HttpHeaderNames(com.linecorp.armeria.common.HttpHeaderNames) ByteBuffer(java.nio.ByteBuffer) Unpooled(io.netty.buffer.Unpooled) ArrayList(java.util.ArrayList) Answer(org.mockito.stubbing.Answer) ImmutableList(com.google.common.collect.ImmutableList) CurioAssertions.assertThat(org.curioswitch.common.testing.assertj.CurioAssertions.assertThat) HttpStatus(com.linecorp.armeria.common.HttpStatus) WebClient(com.linecorp.armeria.client.WebClient) SdkHttpMethod(software.amazon.awssdk.http.SdkHttpMethod) SdkAsyncHttpResponseHandler(software.amazon.awssdk.http.async.SdkAsyncHttpResponseHandler) Subscriber(org.reactivestreams.Subscriber) HttpData(com.linecorp.armeria.common.HttpData) SdkHttpResponse(software.amazon.awssdk.http.SdkHttpResponse) HttpResponse(com.linecorp.armeria.common.HttpResponse) SdkHttpRequest(software.amazon.awssdk.http.SdkHttpRequest) Publisher(org.reactivestreams.Publisher) SdkHttpContentPublisher(software.amazon.awssdk.http.async.SdkHttpContentPublisher) Mockito.when(org.mockito.Mockito.when) StandardCharsets(java.nio.charset.StandardCharsets) AsyncExecuteRequest(software.amazon.awssdk.http.async.AsyncExecuteRequest) Test(org.junit.jupiter.api.Test) HttpRequest(com.linecorp.armeria.common.HttpRequest) List(java.util.List) StreamMessage(com.linecorp.armeria.common.stream.StreamMessage) ResponseHeaders(com.linecorp.armeria.common.ResponseHeaders) Subscription(org.reactivestreams.Subscription) Optional(java.util.Optional) ArrayList(java.util.ArrayList) SdkHttpResponse(software.amazon.awssdk.http.SdkHttpResponse) AggregatedHttpRequest(com.linecorp.armeria.common.AggregatedHttpRequest) AsyncExecuteRequest(software.amazon.awssdk.http.async.AsyncExecuteRequest) Subscription(org.reactivestreams.Subscription) SdkHttpContentPublisher(software.amazon.awssdk.http.async.SdkHttpContentPublisher) AggregatedHttpRequest(com.linecorp.armeria.common.AggregatedHttpRequest) SdkHttpRequest(software.amazon.awssdk.http.SdkHttpRequest) HttpRequest(com.linecorp.armeria.common.HttpRequest) Optional(java.util.Optional) SdkHttpResponse(software.amazon.awssdk.http.SdkHttpResponse) HttpResponse(com.linecorp.armeria.common.HttpResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) Publisher(org.reactivestreams.Publisher) SdkHttpContentPublisher(software.amazon.awssdk.http.async.SdkHttpContentPublisher) ByteBuffer(java.nio.ByteBuffer) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SdkAsyncHttpResponseHandler(software.amazon.awssdk.http.async.SdkAsyncHttpResponseHandler) Test(org.junit.jupiter.api.Test)

Aggregations

HttpResponse (com.linecorp.armeria.common.HttpResponse)9 HttpData (com.linecorp.armeria.common.HttpData)7 HttpRequest (com.linecorp.armeria.common.HttpRequest)5 WebClient (com.linecorp.armeria.client.WebClient)4 HttpHeaderNames (com.linecorp.armeria.common.HttpHeaderNames)4 RequestHeaders (com.linecorp.armeria.common.RequestHeaders)4 HttpHeaders (com.linecorp.armeria.common.HttpHeaders)3 HttpStatus (com.linecorp.armeria.common.HttpStatus)3 ResponseHeaders (com.linecorp.armeria.common.ResponseHeaders)3 List (java.util.List)3 Map (java.util.Map)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 ClientOptions (com.linecorp.armeria.client.ClientOptions)2 AggregatedHttpRequest (com.linecorp.armeria.common.AggregatedHttpRequest)2 HttpMethod (com.linecorp.armeria.common.HttpMethod)2 RequestContext (com.linecorp.armeria.common.RequestContext)2 SafeCloseable (com.linecorp.armeria.common.util.SafeCloseable)2 ByteBuf (io.netty.buffer.ByteBuf)2 Unpooled (io.netty.buffer.Unpooled)2 IOException (java.io.IOException)2