Search in sources :

Example 1 with InvalidResponseException

use of com.linecorp.armeria.client.InvalidResponseException 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)

Aggregations

InvalidResponseException (com.linecorp.armeria.client.InvalidResponseException)1 HttpData (com.linecorp.armeria.common.HttpData)1 ByteBuf (io.netty.buffer.ByteBuf)1 ByteBufHolder (io.netty.buffer.ByteBufHolder)1