Search in sources :

Example 1 with RequestHeaders

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

the class ZipkinUiConfigurationTest method serveIndex.

AggregatedHttpResponse serveIndex(Cookie... cookies) {
    RequestHeaders headers = RequestHeaders.of(HttpMethod.GET, "/");
    String encodedCookies = ClientCookieEncoder.LAX.encode(cookies);
    if (encodedCookies != null) {
        headers = headers.toBuilder().set(HttpHeaderNames.COOKIE, encodedCookies).build();
    }
    HttpRequest req = HttpRequest.of(headers);
    try {
        return context.getBean(HttpService.class).serve(ServiceRequestContext.of(req), req).aggregate().get();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : HttpRequest(com.linecorp.armeria.common.HttpRequest) HttpService(com.linecorp.armeria.server.HttpService) RequestHeaders(com.linecorp.armeria.common.RequestHeaders) BeanCreationException(org.springframework.beans.factory.BeanCreationException) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException)

Example 2 with RequestHeaders

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

the class FileWriter method doUploadChunk.

private CompletableFuture<Void> doUploadChunk(ByteBuf chunk, boolean endOfFile) {
    int length = chunk.readableBytes();
    long limit = filePosition + length;
    StringBuilder range = new StringBuilder("bytes ");
    if (length == 0) {
        range.append('*');
    } else {
        range.append(filePosition).append('-').append(limit - 1);
    }
    range.append('/');
    if (endOfFile) {
        range.append(limit);
    } else {
        range.append('*');
    }
    RequestHeaders headers = RequestHeaders.of(HttpMethod.PUT, uploadUrl, HttpHeaderNames.CONTENT_RANGE, range.toString());
    HttpData data = HttpData.wrap(chunk).withEndOfStream();
    chunk.retain();
    return httpClient.execute(headers, data).aggregate(eventLoop).thenComposeAsync(msg -> {
        ResponseHeaders responseHeaders = msg.headers();
        if (!responseHeaders.status().codeClass().equals(HttpStatusClass.SUCCESS) && responseHeaders.status().code() != 308) {
            chunk.release();
            throw new RuntimeException("Unsuccessful response uploading chunk: endOfFile: " + endOfFile + " Request headers: " + headers + "\n" + " Response headers: " + responseHeaders + "\n" + msg.content().toStringUtf8());
        }
        String responseRange = responseHeaders.get(HttpHeaderNames.RANGE);
        if (responseRange == null) {
            chunk.release();
            return completedFuture(null);
        }
        long responseLimit = rangeHeaderLimit(responseHeaders.get(HttpHeaderNames.RANGE));
        filePosition = responseLimit + 1;
        int notUploaded = (int) (limit - 1 - responseLimit);
        if (notUploaded > 0) {
            chunk.readerIndex(chunk.writerIndex() - notUploaded);
            if (endOfFile) {
                return doUploadChunk(chunk, true);
            }
            if (unfinishedChunk == null) {
                copyUnfinishedBuffer(chunk);
            } else {
                ByteBuf newUnfinished = alloc.buffer(chunk.readableBytes() + unfinishedChunk.readableBytes());
                newUnfinished.writeBytes(chunk).writeBytes(unfinishedChunk);
                unfinishedChunk.release();
                unfinishedChunk = newUnfinished;
            }
        }
        chunk.release();
        return completedFuture(null);
    }, eventLoop);
}
Also used : HttpData(com.linecorp.armeria.common.HttpData) ByteString(com.google.protobuf.ByteString) RequestHeaders(com.linecorp.armeria.common.RequestHeaders) ResponseHeaders(com.linecorp.armeria.common.ResponseHeaders) ByteBuf(io.netty.buffer.ByteBuf)

Example 3 with RequestHeaders

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

the class StorageClient method delete.

public CompletableFuture<Void> delete(String filename) {
    String url = objectUrlPrefix + urlPathSegmentEscaper().escape(filename);
    RequestHeaders headers = RequestHeaders.builder(HttpMethod.DELETE, url).contentType(MediaType.JSON_UTF_8).build();
    return httpClient.execute(headers).aggregate().handle((msg, t) -> {
        if (t != null) {
            throw new RuntimeException("Unexpected error deleting file.", t);
        }
        if (msg.status().codeClass().equals(HttpStatusClass.SUCCESS)) {
            return null;
        } else {
            throw new IllegalStateException("Could not delete file at " + url + ": " + msg.content().toStringUtf8());
        }
    });
}
Also used : RequestHeaders(com.linecorp.armeria.common.RequestHeaders)

Example 4 with RequestHeaders

use of com.linecorp.armeria.common.RequestHeaders 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 5 with RequestHeaders

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

Aggregations

RequestHeaders (com.linecorp.armeria.common.RequestHeaders)5 HttpData (com.linecorp.armeria.common.HttpData)3 HttpResponse (com.linecorp.armeria.common.HttpResponse)2 ResponseHeaders (com.linecorp.armeria.common.ResponseHeaders)2 ByteString (com.google.protobuf.ByteString)1 HttpRequest (com.linecorp.armeria.common.HttpRequest)1 HttpService (com.linecorp.armeria.server.HttpService)1 ByteBuf (io.netty.buffer.ByteBuf)1 BeanCreationException (org.springframework.beans.factory.BeanCreationException)1 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)1