Search in sources :

Example 1 with RequestContext

use of com.linecorp.armeria.common.RequestContext 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 RequestContext

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

the class ServiceAccountAccessTokenProvider method refreshRequestContent.

@Override
ByteBuf refreshRequestContent(Type type) {
    long currentTimeMillis = clock().millis();
    String assertion = createAssertion(type, currentTimeMillis);
    QueryStringEncoder formEncoder = new QueryStringEncoder("");
    formEncoder.addParam("grant_type", GRANT_TYPE);
    formEncoder.addParam("assertion", assertion);
    String contentWithQuestionMark = formEncoder.toString();
    ByteBufAllocator alloc = RequestContext.mapCurrent(RequestContext::alloc, () -> PooledByteBufAllocator.DEFAULT);
    assert alloc != null;
    ByteBuf content = alloc.buffer(contentWithQuestionMark.length() - 1);
    ByteBufUtil.writeAscii(content, contentWithQuestionMark.substring(1));
    return content;
}
Also used : ByteBufAllocator(io.netty.buffer.ByteBufAllocator) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) RequestContext(com.linecorp.armeria.common.RequestContext) ByteBuf(io.netty.buffer.ByteBuf) QueryStringEncoder(io.netty.handler.codec.http.QueryStringEncoder)

Example 3 with RequestContext

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

the class BulkCallBuilder method build.

/**
 * Creates a bulk request when there is more than one object to store
 */
public HttpCall<Void> build() {
    QueryStringEncoder urlBuilder = new QueryStringEncoder("/_bulk");
    if (pipeline != null)
        urlBuilder.addParam("pipeline", pipeline);
    if (waitForRefresh)
        urlBuilder.addParam("refresh", "wait_for");
    ByteBufAllocator alloc = RequestContext.mapCurrent(RequestContext::alloc, () -> PooledByteBufAllocator.DEFAULT);
    HttpCall.RequestSupplier request = new BulkRequestSupplier(entries, shouldAddType, RequestHeaders.of(HttpMethod.POST, urlBuilder.toString(), HttpHeaderNames.CONTENT_TYPE, MediaType.JSON_UTF_8), alloc);
    return http.newCall(request, CHECK_FOR_ERRORS, tag);
}
Also used : ByteBufAllocator(io.netty.buffer.ByteBufAllocator) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) HttpCall(zipkin2.elasticsearch.internal.client.HttpCall) RequestContext(com.linecorp.armeria.common.RequestContext) QueryStringEncoder(io.netty.handler.codec.http.QueryStringEncoder)

Aggregations

RequestContext (com.linecorp.armeria.common.RequestContext)3 ByteBuf (io.netty.buffer.ByteBuf)2 ByteBufAllocator (io.netty.buffer.ByteBufAllocator)2 PooledByteBufAllocator (io.netty.buffer.PooledByteBufAllocator)2 QueryStringEncoder (io.netty.handler.codec.http.QueryStringEncoder)2 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 HttpClient (com.linecorp.armeria.client.HttpClient)1 HttpData (com.linecorp.armeria.common.HttpData)1 HttpHeaderNames (com.linecorp.armeria.common.HttpHeaderNames)1 HttpHeaders (com.linecorp.armeria.common.HttpHeaders)1 HttpMethod (com.linecorp.armeria.common.HttpMethod)1 HttpResponse (com.linecorp.armeria.common.HttpResponse)1 HttpStatus (com.linecorp.armeria.common.HttpStatus)1 MediaType (com.linecorp.armeria.common.MediaType)1 ByteBufHttpData (com.linecorp.armeria.unsafe.ByteBufHttpData)1 CompletableFuturesExtra (com.spotify.futures.CompletableFuturesExtra)1 ByteBufOutputStream (io.netty.buffer.ByteBufOutputStream)1