Search in sources :

Example 16 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 17 with WebClient

use of com.linecorp.armeria.client.WebClient in project data-prepper by opensearch-project.

the class HTTPSourceTest method testHTTPJsonResponse415.

@Test
public void testHTTPJsonResponse415() {
    // Prepare
    final int testMaxPendingRequests = 1;
    final int testThreadCount = 1;
    final int serverTimeoutInMillis = 500;
    when(sourceConfig.getRequestTimeoutInMillis()).thenReturn(serverTimeoutInMillis);
    when(sourceConfig.getMaxPendingRequests()).thenReturn(testMaxPendingRequests);
    when(sourceConfig.getThreadCount()).thenReturn(testThreadCount);
    HTTPSourceUnderTest = new HTTPSource(sourceConfig, pluginMetrics, pluginFactory);
    // Start the source
    HTTPSourceUnderTest.start(testBuffer);
    refreshMeasurements();
    final RequestHeaders testRequestHeaders = RequestHeaders.builder().scheme(SessionProtocol.HTTP).authority("127.0.0.1:2021").method(HttpMethod.POST).path("/log/ingest").contentType(MediaType.JSON_UTF_8).build();
    final HttpData testHttpData = HttpData.ofUtf8("[{\"log\": \"somelog\"}]");
    // Fill in the buffer
    WebClient.of().execute(testRequestHeaders, testHttpData).aggregate().whenComplete((i, ex) -> assertSecureResponseWithStatusCode(i, HttpStatus.OK)).join();
    // Disable client timeout
    WebClient testWebClient = WebClient.builder().responseTimeoutMillis(0).build();
    // When/Then
    testWebClient.execute(testRequestHeaders, testHttpData).aggregate().whenComplete((i, ex) -> assertSecureResponseWithStatusCode(i, HttpStatus.REQUEST_TIMEOUT)).join();
    // verify metrics
    final Measurement requestReceivedCount = MetricsTestUtil.getMeasurementFromList(requestsReceivedMeasurements, Statistic.COUNT);
    Assertions.assertEquals(2.0, requestReceivedCount.getValue());
    final Measurement successRequestsCount = MetricsTestUtil.getMeasurementFromList(successRequestsMeasurements, Statistic.COUNT);
    Assertions.assertEquals(1.0, successRequestsCount.getValue());
    final Measurement requestTimeoutsCount = MetricsTestUtil.getMeasurementFromList(requestTimeoutsMeasurements, Statistic.COUNT);
    Assertions.assertEquals(1.0, requestTimeoutsCount.getValue());
    final Measurement requestProcessDurationMax = MetricsTestUtil.getMeasurementFromList(requestProcessDurationMeasurements, Statistic.MAX);
    final double maxDurationInMillis = 1000 * requestProcessDurationMax.getValue();
    Assertions.assertTrue(maxDurationInMillis > serverTimeoutInMillis);
}
Also used : Statistic(io.micrometer.core.instrument.Statistic) BeforeEach(org.junit.jupiter.api.BeforeEach) RequestHeaders(com.linecorp.armeria.common.RequestHeaders) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) MetricsTestUtil(com.amazon.dataprepper.metrics.MetricsTestUtil) Matchers.not(org.hamcrest.Matchers.not) ArmeriaHttpAuthenticationProvider(com.amazon.dataprepper.armeria.authentication.ArmeriaHttpAuthenticationProvider) AsciiString(io.netty.util.AsciiString) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Server(com.linecorp.armeria.server.Server) WebClient(com.linecorp.armeria.client.WebClient) Map(java.util.Map) Record(com.amazon.dataprepper.model.record.Record) PluginFactory(com.amazon.dataprepper.model.plugin.PluginFactory) Path(java.nio.file.Path) ClientFactory(com.linecorp.armeria.client.ClientFactory) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) CheckpointState(com.amazon.dataprepper.model.CheckpointState) Collection(java.util.Collection) CompletionException(java.util.concurrent.CompletionException) HttpMethod(com.linecorp.armeria.common.HttpMethod) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) Test(org.junit.jupiter.api.Test) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) IOUtils(org.apache.commons.io.IOUtils) MockedStatic(org.mockito.MockedStatic) List(java.util.List) Matchers.contains(org.hamcrest.Matchers.contains) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Matchers.is(org.hamcrest.Matchers.is) ServerBuilder(com.linecorp.armeria.server.ServerBuilder) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) SessionProtocol(com.linecorp.armeria.common.SessionProtocol) Mock(org.mockito.Mock) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) MetricNames(com.amazon.dataprepper.metrics.MetricNames) ResponseTimeoutException(com.linecorp.armeria.client.ResponseTimeoutException) Mockito.lenient(org.mockito.Mockito.lenient) MediaType(com.linecorp.armeria.common.MediaType) ArrayList(java.util.ArrayList) ArgumentCaptor(org.mockito.ArgumentCaptor) HttpStatus(com.linecorp.armeria.common.HttpStatus) AggregatedHttpResponse(com.linecorp.armeria.common.AggregatedHttpResponse) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) BlockingBuffer(com.amazon.dataprepper.plugins.buffer.blockingbuffer.BlockingBuffer) PluginMetrics(com.amazon.dataprepper.metrics.PluginMetrics) HttpData(com.linecorp.armeria.common.HttpData) PluginSetting(com.amazon.dataprepper.model.configuration.PluginSetting) Log(com.amazon.dataprepper.model.log.Log) Files(java.nio.file.Files) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) Measurement(io.micrometer.core.instrument.Measurement) File(java.io.File) Mockito.verify(org.mockito.Mockito.verify) ExecutionException(java.util.concurrent.ExecutionException) Mockito(org.mockito.Mockito) AfterEach(org.junit.jupiter.api.AfterEach) StringJoiner(java.util.StringJoiner) Assertions(org.junit.jupiter.api.Assertions) Mockito.reset(org.mockito.Mockito.reset) InputStream(java.io.InputStream) Measurement(io.micrometer.core.instrument.Measurement) HttpData(com.linecorp.armeria.common.HttpData) RequestHeaders(com.linecorp.armeria.common.RequestHeaders) WebClient(com.linecorp.armeria.client.WebClient) Test(org.junit.jupiter.api.Test)

Example 18 with WebClient

use of com.linecorp.armeria.client.WebClient in project data-prepper by opensearch-project.

the class UnauthenticatedArmeriaHttpAuthenticationProviderTest method httpRequest_with_BasicAuthentication_responds_OK.

@Test
void httpRequest_with_BasicAuthentication_responds_OK() {
    final WebClient client = WebClient.builder(server.httpUri()).auth(BasicToken.of(UUID.randomUUID().toString(), UUID.randomUUID().toString())).build();
    final AggregatedHttpResponse httpResponse = client.get("/test").aggregate().join();
    assertThat(httpResponse.status(), equalTo(HttpStatus.OK));
}
Also used : WebClient(com.linecorp.armeria.client.WebClient) AggregatedHttpResponse(com.linecorp.armeria.common.AggregatedHttpResponse) Test(org.junit.jupiter.api.Test)

Example 19 with WebClient

use of com.linecorp.armeria.client.WebClient in project data-prepper by opensearch-project.

the class UnauthenticatedArmeriaHttpAuthenticationProviderTest method httpRequest_without_authentication_responds_OK.

@Test
void httpRequest_without_authentication_responds_OK() {
    final WebClient client = WebClient.of(server.httpUri());
    final AggregatedHttpResponse httpResponse = client.get("/test").aggregate().join();
    assertThat(httpResponse.status(), equalTo(HttpStatus.OK));
}
Also used : WebClient(com.linecorp.armeria.client.WebClient) AggregatedHttpResponse(com.linecorp.armeria.common.AggregatedHttpResponse) Test(org.junit.jupiter.api.Test)

Example 20 with WebClient

use of com.linecorp.armeria.client.WebClient in project data-prepper by opensearch-project.

the class UnauthenticatedGrpcAuthenticationProviderTest method httpRequest_with_BasicAuthentication_responds_OK.

@Test
void httpRequest_with_BasicAuthentication_responds_OK() {
    final WebClient client = WebClient.builder(server.httpUri()).auth(BasicToken.of(UUID.randomUUID().toString(), UUID.randomUUID().toString())).build();
    HttpRequest request = HttpRequest.of(RequestHeaders.builder().method(HttpMethod.POST).path("/grpc.health.v1.Health/Check").contentType(MediaType.JSON_UTF_8).build());
    final AggregatedHttpResponse httpResponse = client.execute(request).aggregate().join();
    // TODO: Figure out how to get SampleHealthGrpcService to return a status of 200
    assertThat(httpResponse.status(), equalTo(HttpStatus.SERVICE_UNAVAILABLE));
}
Also used : HttpRequest(com.linecorp.armeria.common.HttpRequest) WebClient(com.linecorp.armeria.client.WebClient) AggregatedHttpResponse(com.linecorp.armeria.common.AggregatedHttpResponse) Test(org.junit.jupiter.api.Test)

Aggregations

WebClient (com.linecorp.armeria.client.WebClient)36 Test (org.junit.jupiter.api.Test)22 AggregatedHttpResponse (com.linecorp.armeria.common.AggregatedHttpResponse)20 Server (com.linecorp.armeria.server.Server)6 IOException (java.io.IOException)6 Map (java.util.Map)6 HttpRequest (com.linecorp.armeria.common.HttpRequest)5 HttpMethod (com.linecorp.armeria.common.HttpMethod)4 RequestHeaders (com.linecorp.armeria.common.RequestHeaders)4 Path (java.nio.file.Path)4 List (java.util.List)4 Order (org.junit.jupiter.api.Order)4 TestMethodOrder (org.junit.jupiter.api.TestMethodOrder)4 HttpData (com.linecorp.armeria.common.HttpData)3 HttpResponse (com.linecorp.armeria.common.HttpResponse)3 HttpStatus (com.linecorp.armeria.common.HttpStatus)3 MediaType (com.linecorp.armeria.common.MediaType)3 File (java.io.File)3 Files (java.nio.file.Files)3 ArmeriaHttpAuthenticationProvider (com.amazon.dataprepper.armeria.authentication.ArmeriaHttpAuthenticationProvider)2