use of com.linecorp.armeria.common.HttpResponse 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.HttpResponse 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.HttpResponse in project curiostack by curioswitch.
the class GoogleCredentialsDecoratingClient method execute.
@Override
@SuppressWarnings("FutureReturnValueIgnored")
public HttpResponse execute(ClientRequestContext ctx, HttpRequest req) {
CompletableFuture<HttpResponse> resFuture = new CompletableFuture<>();
credentials.getRequestMetadata(URI.create(req.path()), authExecutor, new RequestMetadataCallback() {
@Override
public void onSuccess(Map<String, List<String>> metadata) {
metadata.forEach((key, values) -> {
for (String value : values) {
req.headers().add(HttpHeaderNames.of(key), value);
}
});
try {
ctx.contextAwareEventLoop().submit(() -> resFuture.complete(delegate().execute(ctx, req)));
} catch (Exception e) {
resFuture.completeExceptionally(e);
}
}
@Override
public void onFailure(Throwable t) {
resFuture.completeExceptionally(t);
}
});
return HttpResponse.from(resFuture);
}
use of com.linecorp.armeria.common.HttpResponse in project zipkin by openzipkin.
the class ElasticsearchExtension method computeStorageBuilder.
Builder computeStorageBuilder() {
WebClientBuilder builder = WebClient.builder(baseUrl()).factory(ClientFactory.builder().useHttp2Preface(false).build());
builder.decorator((delegate, ctx, req) -> {
final HttpResponse response = delegate.execute(ctx, req);
return HttpResponse.from(response.aggregate().thenApply(r -> {
// ES will return a 'warning' response header when using deprecated api, detect this and
// fail early so we can do something about it.
// Example usage: https://github.com/elastic/elasticsearch/blob/3049e55f093487bb582a7e49ad624961415ba31c/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/IndexPrivilegeIntegTests.java#L559
final String warningHeader = r.headers().get("warning");
if (warningHeader != null) {
if (IGNORE_THESE_WARNINGS.stream().noneMatch(p -> p.matcher(warningHeader).find())) {
throw new IllegalArgumentException("Detected usage of deprecated API for request " + req.toString() + ":\n" + warningHeader);
}
}
// Convert AggregatedHttpResponse back to HttpResponse.
return r.toHttpResponse();
}));
});
// com.linecorp.armeria.client.logging
if (Boolean.parseBoolean(System.getenv("ES_DEBUG"))) {
ClientOptionsBuilder options = ClientOptions.builder();
LoggingClientBuilder loggingBuilder = LoggingClient.builder().requestLogLevel(LogLevel.INFO).successfulResponseLogLevel(LogLevel.INFO);
options.decorator(loggingBuilder.newDecorator());
options.decorator(ContentPreviewingClient.newDecorator(Integer.MAX_VALUE));
builder.options(options.build());
}
WebClient client = builder.build();
return ElasticsearchStorage.newBuilder(new ElasticsearchStorage.LazyHttpClient() {
@Override
public WebClient get() {
return client;
}
@Override
public void close() {
client.endpointGroup().close();
}
@Override
public String toString() {
return client.uri().toString();
}
}).index("zipkin-test").flushOnWrites(true);
}
Aggregations