Search in sources :

Example 6 with WebClient

use of com.linecorp.armeria.client.WebClient in project curiostack by curioswitch.

the class ComputeEngineAccessTokenProvider method fetchToken.

@Override
protected CompletableFuture<AggregatedHttpResponse> fetchToken(Type type) {
    URI uri = URI.create(ComputeEngineCredentials.getTokenServerEncodedUrl());
    // In practice, this URL shouldn't change at runtime but it's not infeasible, and since this
    // shouldn't be executed often, just create a client every time.
    WebClient client = WebClient.builder("h1c://" + uri.getAuthority() + "/").decorator(LoggingClient.builder().newDecorator()).build();
    return client.execute(RequestHeaders.of(HttpMethod.GET, uri.getPath(), METADATA_FLAVOR_HEADER, "Google")).aggregate();
}
Also used : URI(java.net.URI) WebClient(com.linecorp.armeria.client.WebClient)

Example 7 with WebClient

use of com.linecorp.armeria.client.WebClient in project scribejava by scribejava.

the class ArmeriaHttpClient method doExecuteAsync.

private <T> CompletableFuture<T> doExecuteAsync(String userAgent, Map<String, String> headers, Verb httpVerb, String completeUrl, Supplier<HttpData> contentSupplier, OAuthAsyncRequestCallback<T> callback, OAuthRequest.ResponseConverter<T> converter) {
    // Get the URI and Path
    final URI uri = URI.create(completeUrl);
    final String path = getServicePath(uri);
    // Fetch/Create WebClient instance for a given Endpoint
    final WebClient client = getClient(uri);
    // Build HTTP request
    final RequestHeadersBuilder headersBuilder = RequestHeaders.of(getHttpMethod(httpVerb), path).toBuilder();
    headersBuilder.add(headers.entrySet());
    if (userAgent != null) {
        headersBuilder.add(OAuthConstants.USER_AGENT_HEADER_NAME, userAgent);
    }
    // Build the request body and execute HTTP request
    final HttpResponse response;
    if (httpVerb.isPermitBody()) {
        // POST, PUT, PATCH and DELETE methods
        final HttpData contents = contentSupplier.get();
        if (httpVerb.isRequiresBody() && contents == null) {
            // POST or PUT methods
            throw new IllegalArgumentException("Contents missing for request method " + httpVerb.name());
        }
        if (headersBuilder.contentType() == null) {
            headersBuilder.contentType(MediaType.FORM_DATA);
        }
        if (contents != null) {
            response = client.execute(headersBuilder.build(), contents);
        } else {
            response = client.execute(headersBuilder.build());
        }
    } else {
        response = client.execute(headersBuilder.build());
    }
    // Aggregate HTTP response (asynchronously) and return the result Future
    return response.aggregate().thenApply(aggregatedResponse -> whenResponseComplete(callback, converter, aggregatedResponse)).exceptionally(throwable -> completeExceptionally(callback, throwable));
}
Also used : RequestHeaders(com.linecorp.armeria.common.RequestHeaders) MultipartPayload(com.github.scribejava.core.httpclient.multipart.MultipartPayload) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) Supplier(java.util.function.Supplier) MediaType(com.linecorp.armeria.common.MediaType) AbstractAsyncOnlyHttpClient(com.github.scribejava.core.httpclient.AbstractAsyncOnlyHttpClient) RequestHeadersBuilder(com.linecorp.armeria.common.RequestHeadersBuilder) Future(java.util.concurrent.Future) HttpStatus(com.linecorp.armeria.common.HttpStatus) WebClient(com.linecorp.armeria.client.WebClient) Objects.requireNonNull(java.util.Objects.requireNonNull) Map(java.util.Map) AggregatedHttpResponse(com.linecorp.armeria.common.AggregatedHttpResponse) URI(java.net.URI) HttpData(com.linecorp.armeria.common.HttpData) OAuthAsyncRequestCallback(com.github.scribejava.core.model.OAuthAsyncRequestCallback) HttpResponse(com.linecorp.armeria.common.HttpResponse) OAuthConstants(com.github.scribejava.core.model.OAuthConstants) Files(java.nio.file.Files) Verb(com.github.scribejava.core.model.Verb) IOException(java.io.IOException) HttpMethod(com.linecorp.armeria.common.HttpMethod) File(java.io.File) Lock(java.util.concurrent.locks.Lock) OAuthRequest(com.github.scribejava.core.model.OAuthRequest) Response(com.github.scribejava.core.model.Response) MultipartUtils(com.github.scribejava.core.httpclient.multipart.MultipartUtils) InputStream(java.io.InputStream) HttpData(com.linecorp.armeria.common.HttpData) AggregatedHttpResponse(com.linecorp.armeria.common.AggregatedHttpResponse) HttpResponse(com.linecorp.armeria.common.HttpResponse) RequestHeadersBuilder(com.linecorp.armeria.common.RequestHeadersBuilder) URI(java.net.URI) WebClient(com.linecorp.armeria.client.WebClient)

Example 8 with WebClient

use of com.linecorp.armeria.client.WebClient in project curiostack by curioswitch.

the class RoutingService method serve.

@Override
public HttpResponse serve(ServiceRequestContext ctx, HttpRequest req) {
    RoutingContext mappingContext = ctx.routingContext();
    final WebClient client;
    if (pathClients != null && mappingContext.query() == null) {
        client = pathClients.get(mappingContext);
    } else {
        client = find(mappingContext);
    }
    if (client == null) {
        return HttpResponse.of(HttpStatus.NOT_FOUND);
    }
    // We don't want to pass the external domain name through to the backend server since this
    // causes problems with the TLS handshake between this server and the backend (the external
    // hostname does not match the names we use in our certs for server to server communication).
    req = req.withHeaders(req.headers().toBuilder().authority("").build());
    return client.execute(req);
}
Also used : RoutingContext(com.linecorp.armeria.server.RoutingContext) WebClient(com.linecorp.armeria.client.WebClient)

Example 9 with WebClient

use of com.linecorp.armeria.client.WebClient in project curiostack by curioswitch.

the class RoutingConfigLoader method load.

Map<Route, WebClient> load(Path configPath) {
    final RoutingConfig config;
    try {
        config = OBJECT_MAPPER.readValue(configPath.toFile(), RoutingConfig.class);
    } catch (IOException e) {
        throw new UncheckedIOException("Could not read routing config.", e);
    }
    Map<String, WebClient> clients = config.getTargets().stream().collect(toImmutableMap(Target::getName, t -> clientBuilderFactory.create(t.getName(), addSerializationFormat(t.getUrl())).build(WebClient.class)));
    return config.getRules().stream().collect(toImmutableMap(r -> Route.builder().path(r.getPathPattern()).build(), r -> clients.get(r.getTarget())));
}
Also used : ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Route(com.linecorp.armeria.server.Route) IOException(java.io.IOException) Singleton(javax.inject.Singleton) UncheckedIOException(java.io.UncheckedIOException) Inject(javax.inject.Inject) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) WebClient(com.linecorp.armeria.client.WebClient) Map(java.util.Map) YAMLFactory(com.fasterxml.jackson.dataformat.yaml.YAMLFactory) ClientBuilderFactory(org.curioswitch.common.server.framework.armeria.ClientBuilderFactory) Target(org.curioswitch.curiostack.gateway.RoutingConfig.Target) Path(java.nio.file.Path) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) WebClient(com.linecorp.armeria.client.WebClient)

Example 10 with WebClient

use of com.linecorp.armeria.client.WebClient in project zipkin by openzipkin.

the class ZipkinElasticsearchStorageConfigurationTest method providesBasicAuthInterceptor_whenDynamicCredentialsConfigured.

@Test
public void providesBasicAuthInterceptor_whenDynamicCredentialsConfigured() {
    String credentialsFile = pathOfResource("es-credentials");
    TestPropertyValues.of("zipkin.storage.type:elasticsearch", "zipkin.storage.elasticsearch.hosts:127.0.0.1:1234", "zipkin.storage.elasticsearch.credentials-file:" + credentialsFile, "zipkin.storage.elasticsearch.credentials-refresh-interval:2").applyTo(context);
    Access.registerElasticsearch(context);
    context.refresh();
    HttpClientFactory factory = context.getBean(HttpClientFactory.class);
    WebClient client = WebClient.builder("http://127.0.0.1:1234").option(ClientOptions.DECORATION, factory.options.decoration()).build();
    assertThat(client.as(BasicAuthInterceptor.class)).isNotNull();
    BasicCredentials basicCredentials = Objects.requireNonNull(client.as(BasicAuthInterceptor.class)).basicCredentials;
    String credentials = basicCredentials.getCredentials();
    assertThat(credentials).isEqualTo("Basic Zm9vOmJhcg==");
}
Also used : WebClient(com.linecorp.armeria.client.WebClient) Test(org.junit.Test)

Aggregations

WebClient (com.linecorp.armeria.client.WebClient)16 AggregatedHttpResponse (com.linecorp.armeria.common.AggregatedHttpResponse)5 Test (org.junit.jupiter.api.Test)5 IOException (java.io.IOException)4 HttpResponse (com.linecorp.armeria.common.HttpResponse)3 Map (java.util.Map)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 HttpMethod (com.linecorp.armeria.common.HttpMethod)2 RequestHeadersBuilder (com.linecorp.armeria.common.RequestHeadersBuilder)2 File (java.io.File)2 URI (java.net.URI)2 Files (java.nio.file.Files)2 Path (java.nio.file.Path)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 Test (org.junit.Test)2 LoggerFactory (org.slf4j.LoggerFactory)2 TreeNode (com.fasterxml.jackson.core.TreeNode)1 YAMLFactory (com.fasterxml.jackson.dataformat.yaml.YAMLFactory)1 JSON (com.fasterxml.jackson.jr.ob.JSON)1