Search in sources :

Example 11 with HttpResponse

use of com.canoo.platform.core.http.HttpResponse in project dolphin-platform by canoo.

the class HttpClientTests method testSimpleGetWithJsonContentType.

@Test
public void testSimpleGetWithJsonContentType() throws Exception {
    // given:
    final HttpClient client = PlatformClient.getService(HttpClient.class);
    final AtomicBoolean actionCalled = new AtomicBoolean(false);
    final AtomicBoolean doneCalled = new AtomicBoolean(false);
    final AtomicBoolean errorCalled = new AtomicBoolean(false);
    // when:
    final CompletableFuture<HttpResponse<String>> future = client.get("http://localhost:" + freePort).withoutContent().readString(HttpHeaderConstants.JSON_MIME_TYPE).onDone(response -> {
        actionCalled.set(true);
        doneCalled.set(true);
    }).onError(e -> {
        actionCalled.set(true);
        errorCalled.set(true);
    }).execute();
    // then:
    final HttpResponse<String> response = future.get(1_000, TimeUnit.MILLISECONDS);
    assertThat("response not defined", response, notNullValue());
    assertThat("Wrong response code", response.getStatusCode(), is(200));
    assertThat("Content should not be null", response.getRawContent(), notNullValue());
    final String json = response.getContent();
    final Gson gson = new Gson();
    final DummyJson dummy = gson.fromJson(json, DummyJson.class);
    assertThat("No JSON object created", dummy, notNullValue());
    assertThat("Wrong name", dummy.getName(), is("Joe"));
    assertThat("Wrong age", dummy.getAge(), is(33));
    assertThat("Wrong isJavaChampion", dummy.isJavaChampion(), is(true));
    assertThatDoneCalledAndErrorNotCalled(actionCalled, doneCalled, errorCalled);
}
Also used : AfterClass(org.testng.annotations.AfterClass) SC_HTTP_RESOURCE_NOTFOUND(com.canoo.dp.impl.platform.core.http.HttpStatus.SC_HTTP_RESOURCE_NOTFOUND) ByteArrayProvider(com.canoo.platform.core.http.ByteArrayProvider) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) ConnectionException(com.canoo.platform.core.http.ConnectionException) HttpResponse(com.canoo.platform.core.http.HttpResponse) BeforeClass(org.testng.annotations.BeforeClass) HttpClient(com.canoo.platform.core.http.HttpClient) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.testng.annotations.Test) CompletableFuture(java.util.concurrent.CompletableFuture) HttpHeaderConstants(com.canoo.dp.impl.platform.core.http.HttpHeaderConstants) BadResponseException(com.canoo.platform.core.http.BadResponseException) SC_HTTP_UNAUTHORIZED(com.canoo.dp.impl.platform.core.http.HttpStatus.SC_HTTP_UNAUTHORIZED) ServerSocket(java.net.ServerSocket) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) PlatformClient(com.canoo.platform.client.PlatformClient) Assert(org.testng.Assert) Gson(com.google.gson.Gson) Matchers.is(org.hamcrest.Matchers.is) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Spark(spark.Spark) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HttpClient(com.canoo.platform.core.http.HttpClient) HttpResponse(com.canoo.platform.core.http.HttpResponse) Gson(com.google.gson.Gson) Test(org.testng.annotations.Test)

Example 12 with HttpResponse

use of com.canoo.platform.core.http.HttpResponse in project dolphin-platform by canoo.

the class HttpCallExecutorImpl method execute.

@Override
public CompletableFuture<HttpResponse<R>> execute() {
    final CompletableFuture<HttpResponse<R>> completableFuture = new CompletableFuture<>();
    executor.submit(() -> {
        try {
            final HttpResponse<R> result = provider.get();
            final int statusCode = result.getStatusCode();
            if (statusCode >= 300) {
                final HttpException e = new BadResponseException(result, "Bad Response: " + statusCode);
                if (errorHandler != null) {
                    uiExecutor.execute(() -> errorHandler.accept(e));
                }
                completableFuture.completeExceptionally(e);
            } else {
                if (onDone != null) {
                    uiExecutor.execute(() -> onDone.accept(result));
                }
                completableFuture.complete(result);
            }
        } catch (final HttpException e) {
            if (errorHandler != null) {
                uiExecutor.execute(() -> errorHandler.accept(e));
            }
            completableFuture.completeExceptionally(e);
        }
    });
    return completableFuture;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) BadResponseException(com.canoo.platform.core.http.BadResponseException) HttpResponse(com.canoo.platform.core.http.HttpResponse) HttpException(com.canoo.platform.core.http.HttpException)

Example 13 with HttpResponse

use of com.canoo.platform.core.http.HttpResponse in project dolphin-platform by canoo.

the class HttpCallResponseBuilderImpl method readObject.

@Override
public <R> Promise<HttpResponse<R>, HttpException> readObject(final Class<R> responseType) {
    Assert.requireNonNull(responseType, "responseType");
    connection.addRequestHeader(new HttpHeaderImpl(ACCEPT_CHARSET_HEADER, CHARSET));
    connection.addRequestHeader(new HttpHeaderImpl(ACCEPT_HEADER, JSON_MIME_TYPE));
    final ResponseContentConverter<R> converter = b -> gson.fromJson(new String(b, CHARSET), responseType);
    return createExecutor(converter);
}
Also used : HttpHeaderImpl(com.canoo.dp.impl.platform.core.http.HttpHeaderImpl) ByteArrayProvider(com.canoo.platform.core.http.ByteArrayProvider) HttpException(com.canoo.platform.core.http.HttpException) Assert(com.canoo.dp.impl.platform.core.Assert) ConnectionException(com.canoo.platform.core.http.ConnectionException) HttpResponse(com.canoo.platform.core.http.HttpResponse) HttpClientConnection(com.canoo.dp.impl.platform.core.http.HttpClientConnection) HttpHeader(com.canoo.platform.core.http.HttpHeader) HttpCallResponseBuilder(com.canoo.platform.core.http.HttpCallResponseBuilder) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IOException(java.io.IOException) CHARSET(com.canoo.dp.impl.platform.core.http.HttpHeaderConstants.CHARSET) API(org.apiguardian.api.API) INTERNAL(org.apiguardian.api.API.Status.INTERNAL) DolphinRuntimeException(com.canoo.platform.core.DolphinRuntimeException) Promise(com.canoo.platform.core.functional.Promise) List(java.util.List) ACCEPT_CHARSET_HEADER(com.canoo.dp.impl.platform.core.http.HttpHeaderConstants.ACCEPT_CHARSET_HEADER) HttpURLConnectionHandler(com.canoo.platform.core.http.HttpURLConnectionHandler) Gson(com.google.gson.Gson) JSON_MIME_TYPE(com.canoo.dp.impl.platform.core.http.HttpHeaderConstants.JSON_MIME_TYPE) Collections(java.util.Collections) ClientConfiguration(com.canoo.platform.client.ClientConfiguration) ACCEPT_HEADER(com.canoo.dp.impl.platform.core.http.HttpHeaderConstants.ACCEPT_HEADER) ACCEPT_CHARSET_HEADER(com.canoo.dp.impl.platform.core.http.HttpHeaderConstants.ACCEPT_CHARSET_HEADER) ACCEPT_HEADER(com.canoo.dp.impl.platform.core.http.HttpHeaderConstants.ACCEPT_HEADER) HttpHeaderImpl(com.canoo.dp.impl.platform.core.http.HttpHeaderImpl)

Aggregations

HttpResponse (com.canoo.platform.core.http.HttpResponse)13 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)12 ByteArrayProvider (com.canoo.platform.core.http.ByteArrayProvider)10 ConnectionException (com.canoo.platform.core.http.ConnectionException)10 HttpClient (com.canoo.platform.core.http.HttpClient)10 Gson (com.google.gson.Gson)10 Test (org.testng.annotations.Test)10 BadResponseException (com.canoo.platform.core.http.BadResponseException)9 CompletableFuture (java.util.concurrent.CompletableFuture)9 HttpHeaderConstants (com.canoo.dp.impl.platform.core.http.HttpHeaderConstants)8 SC_HTTP_RESOURCE_NOTFOUND (com.canoo.dp.impl.platform.core.http.HttpStatus.SC_HTTP_RESOURCE_NOTFOUND)8 SC_HTTP_UNAUTHORIZED (com.canoo.dp.impl.platform.core.http.HttpStatus.SC_HTTP_UNAUTHORIZED)8 PlatformClient (com.canoo.platform.client.PlatformClient)8 ServerSocket (java.net.ServerSocket)8 ExecutionException (java.util.concurrent.ExecutionException)8 TimeUnit (java.util.concurrent.TimeUnit)8 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)8 Matchers.is (org.hamcrest.Matchers.is)8 Matchers.notNullValue (org.hamcrest.Matchers.notNullValue)8 Assert (org.testng.Assert)8