Search in sources :

Example 6 with HttpData

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

the class StorageClient method sendMutationRequest.

private CompletableFuture<Void> sendMutationRequest(HttpMethod method, Object request, String url, EventLoop eventLoop, ByteBufAllocator alloc) {
    HttpData data = serializeRequest(request, alloc);
    RequestHeaders headers = RequestHeaders.builder(HttpMethod.POST, url).contentType(MediaType.JSON_UTF_8).build();
    HttpResponse res = httpClient.execute(headers, data);
    return res.aggregateWithPooledObjects(eventLoop, alloc).handle((msg, t) -> {
        if (t != null) {
            throw new RuntimeException("Unexpected error composing file.", t);
        }
        try {
            if (msg.status().equals(HttpStatus.OK)) {
                return null;
            } else {
                throw new IllegalStateException("Could not compose file at " + url + ": " + msg.content().toStringUtf8());
            }
        } finally {
            ReferenceCountUtil.safeRelease(msg.content());
        }
    });
}
Also used : HttpData(com.linecorp.armeria.common.HttpData) HttpResponse(com.linecorp.armeria.common.HttpResponse) RequestHeaders(com.linecorp.armeria.common.RequestHeaders)

Example 7 with HttpData

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

the class StorageClient method readFile.

/**
 * Reads the contents of a file from cloud storage. Ownership of the returned {@link ByteBuf} is
 * transferred to the caller, which must release it. The future will complete with {@code null} if
 * the file is not found.
 */
public CompletableFuture<ByteBuf> readFile(String filename, EventLoop eventLoop, ByteBufAllocator alloc) {
    String url = objectUrlPrefix + urlPathSegmentEscaper().escape(filename) + "?alt=media";
    return httpClient.get(url).aggregateWithPooledObjects(eventLoop, alloc).thenApply(msg -> {
        if (msg.status().equals(HttpStatus.NOT_FOUND)) {
            ReferenceCountUtil.safeRelease(msg.content());
            return null;
        }
        if (!msg.status().equals(HttpStatus.OK)) {
            String response = msg.contentUtf8();
            ReferenceCountUtil.safeRelease(msg.content());
            throw new InvalidResponseException("Could not fetch file at " + filename + ": " + response);
        }
        HttpData data = msg.content();
        if (data instanceof ByteBufHolder) {
            return ((ByteBufHolder) msg.content()).content();
        } else {
            ByteBuf buf = alloc.buffer(data.length());
            buf.writeBytes(data.array());
            return buf;
        }
    });
}
Also used : HttpData(com.linecorp.armeria.common.HttpData) ByteBufHolder(io.netty.buffer.ByteBufHolder) ByteBuf(io.netty.buffer.ByteBuf) InvalidResponseException(com.linecorp.armeria.client.InvalidResponseException)

Example 8 with HttpData

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

the class StorageClient method createFile.

/**
 * Create a new file for uploading data to cloud storage.
 */
public CompletableFuture<FileWriter> createFile(FileRequest request, EventLoop eventLoop, ByteBufAllocator alloc) {
    HttpData data = serializeRequest(request, alloc);
    RequestHeaders headers = RequestHeaders.builder(HttpMethod.POST, uploadUrl).contentType(MediaType.JSON_UTF_8).build();
    HttpResponse res = httpClient.execute(headers, data);
    return res.aggregate(eventLoop).handle((msg, t) -> {
        if (t != null) {
            throw new RuntimeException("Unexpected error creating new file.", t);
        }
        ResponseHeaders responseHeaders = msg.headers();
        if (!responseHeaders.status().equals(HttpStatus.OK)) {
            throw new RuntimeException("Non-successful response when creating new file at " + uploadUrl + ": " + responseHeaders + "\n" + msg.content().toStringUtf8());
        }
        String location = responseHeaders.get(HttpHeaderNames.LOCATION);
        String pathAndQuery = location.substring("https://www.googleapis.com".length());
        return new FileWriter(pathAndQuery, alloc, eventLoop, httpClient);
    });
}
Also used : HttpData(com.linecorp.armeria.common.HttpData) HttpResponse(com.linecorp.armeria.common.HttpResponse) RequestHeaders(com.linecorp.armeria.common.RequestHeaders) ResponseHeaders(com.linecorp.armeria.common.ResponseHeaders)

Example 9 with HttpData

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

the class UnzippingBytesRequestConverter method validateAndStoreSpans.

/**
 * This synchronously decodes the message so that users can see data errors.
 */
@SuppressWarnings("FutureReturnValueIgnored")
// check? Say it is somehow canceled, would we take action? Would callback.onError() be redundant?
HttpResponse validateAndStoreSpans(SpanBytesDecoder decoder, ServiceRequestContext ctx, HttpRequest req) {
    CompletableCallback result = new CompletableCallback();
    req.aggregateWithPooledObjects(ctx.eventLoop(), ctx.alloc()).handle((msg, t) -> {
        if (t != null) {
            result.onError(t);
            return null;
        }
        final HttpData requestContent;
        try {
            requestContent = UnzippingBytesRequestConverter.convertRequest(ctx, msg);
        } catch (Throwable t1) {
            propagateIfFatal(t1);
            result.onError(t1);
            return null;
        }
        try (HttpData content = requestContent) {
            // logging already handled upstream in UnzippingBytesRequestConverter where request context exists
            if (content.isEmpty()) {
                result.onSuccess(null);
                return null;
            }
            final ByteBuffer nioBuffer = content.byteBuf().nioBuffer();
            try {
                SpanBytesDecoderDetector.decoderForListMessage(nioBuffer);
            } catch (IllegalArgumentException e) {
                result.onError(new IllegalArgumentException("Expected a " + decoder + " encoded list\n"));
                return null;
            } catch (Throwable t1) {
                result.onError(t1);
                return null;
            }
            SpanBytesDecoder unexpectedDecoder = testForUnexpectedFormat(decoder, nioBuffer);
            if (unexpectedDecoder != null) {
                result.onError(new IllegalArgumentException("Expected a " + decoder + " encoded list, but received: " + unexpectedDecoder + "\n"));
                return null;
            }
            // collector.accept might block so need to move off the event loop. We make sure the
            // callback is context aware to continue the trace.
            Executor executor = ctx.makeContextAware(ctx.blockingTaskExecutor());
            try {
                collector.acceptSpans(nioBuffer, decoder, result, executor);
            } catch (Throwable t1) {
                result.onError(t1);
                return null;
            }
        }
        return null;
    });
    return HttpResponse.from(result);
}
Also used : Executor(java.util.concurrent.Executor) HttpData(com.linecorp.armeria.common.HttpData) SpanBytesDecoder(zipkin2.codec.SpanBytesDecoder) ByteBuffer(java.nio.ByteBuffer)

Example 10 with HttpData

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

the class UnzippingBytesRequestConverter method convertRequest.

static HttpData convertRequest(ServiceRequestContext ctx, AggregatedHttpRequest request) {
    ZipkinHttpCollector.metrics.incrementMessages();
    String encoding = request.headers().get(HttpHeaderNames.CONTENT_ENCODING);
    HttpData content = request.content();
    if (!content.isEmpty() && encoding != null && encoding.contains("gzip")) {
        content = StreamDecoderFactory.gzip().newDecoder(ctx.alloc()).decode(content);
        // The implementation of the armeria decoder is to return an empty body on failure
        if (content.isEmpty()) {
            ZipkinHttpCollector.maybeLog("Malformed gzip body", ctx, request);
            content.close();
            throw new IllegalArgumentException("Cannot gunzip spans");
        }
    }
    if (content.isEmpty())
        ZipkinHttpCollector.maybeLog("Empty POST body", ctx, request);
    if (content.length() == 2 && "[]".equals(content.toStringAscii())) {
        ZipkinHttpCollector.maybeLog("Empty JSON list POST body", ctx, request);
        content.close();
        content = HttpData.empty();
    }
    ZipkinHttpCollector.metrics.incrementBytes(content.length());
    return content;
}
Also used : HttpData(com.linecorp.armeria.common.HttpData)

Aggregations

HttpData (com.linecorp.armeria.common.HttpData)11 HttpResponse (com.linecorp.armeria.common.HttpResponse)5 RequestHeaders (com.linecorp.armeria.common.RequestHeaders)4 ByteBuf (io.netty.buffer.ByteBuf)4 ResponseHeaders (com.linecorp.armeria.common.ResponseHeaders)3 HttpHeaderNames (com.linecorp.armeria.common.HttpHeaderNames)2 HttpHeaders (com.linecorp.armeria.common.HttpHeaders)2 HttpMethod (com.linecorp.armeria.common.HttpMethod)2 HttpRequest (com.linecorp.armeria.common.HttpRequest)2 HttpStatus (com.linecorp.armeria.common.HttpStatus)2 IOException (java.io.IOException)2 ByteBuffer (java.nio.ByteBuffer)2 Map (java.util.Map)2 JsonParser (com.fasterxml.jackson.core.JsonParser)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 JsonDeserialize (com.fasterxml.jackson.databind.annotation.JsonDeserialize)1 JsonSerialize (com.fasterxml.jackson.databind.annotation.JsonSerialize)1 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)1 ByteString (com.google.protobuf.ByteString)1