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());
}
});
}
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;
}
});
}
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);
});
}
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);
}
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;
}
Aggregations