Search in sources :

Example 1 with HttpException

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

the class HttpCallResponseBuilderImpl method handleRequest.

private <R> HttpResponse<R> handleRequest(final ResponseContentConverter<R> converter) throws HttpException {
    Assert.requireNonNull(converter, "converter");
    if (handled.get()) {
        throw new DolphinRuntimeException("Http call already handled");
    }
    handled.set(true);
    requestHandlers.forEach(h -> h.handle(connection.getConnection()));
    final byte[] rawBytes = dataProvider.get();
    try {
        connection.writeRequestContent(rawBytes);
    } catch (final IOException e) {
        throw new ConnectionException("Can not connect to server", e);
    }
    try {
        int responseCode = connection.readResponseCode();
        final byte[] rawContent = connection.readResponseContent();
        responseHandlers.forEach(h -> h.handle(connection.getConnection()));
        final R content = converter.convert(rawContent);
        final List<HttpHeader> headers = connection.getResponseHeaders();
        return new HttpResponseImpl<R>(headers, responseCode, rawContent, content);
    } catch (IOException e) {
        throw new ConnectionException("No response from server", e);
    } catch (Exception e) {
        throw new HttpException("Can not handle response", e);
    }
}
Also used : 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) HttpHeader(com.canoo.platform.core.http.HttpHeader) DolphinRuntimeException(com.canoo.platform.core.DolphinRuntimeException) HttpException(com.canoo.platform.core.http.HttpException) IOException(java.io.IOException) ConnectionException(com.canoo.platform.core.http.ConnectionException) HttpException(com.canoo.platform.core.http.HttpException) ConnectionException(com.canoo.platform.core.http.ConnectionException) IOException(java.io.IOException) DolphinRuntimeException(com.canoo.platform.core.DolphinRuntimeException)

Example 2 with HttpException

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

the class HttpCallResponseBuilderImpl method readString.

@Override
public Promise<HttpResponse<String>, HttpException> readString() {
    connection.addRequestHeader(new HttpHeaderImpl(ACCEPT_CHARSET_HEADER, CHARSET));
    final ResponseContentConverter<String> converter = b -> new String(b, CHARSET);
    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) HttpHeaderImpl(com.canoo.dp.impl.platform.core.http.HttpHeaderImpl)

Example 3 with HttpException

use of com.canoo.platform.core.http.HttpException 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 4 with HttpException

use of com.canoo.platform.core.http.HttpException 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

HttpException (com.canoo.platform.core.http.HttpException)4 ACCEPT_CHARSET_HEADER (com.canoo.dp.impl.platform.core.http.HttpHeaderConstants.ACCEPT_CHARSET_HEADER)3 ACCEPT_HEADER (com.canoo.dp.impl.platform.core.http.HttpHeaderConstants.ACCEPT_HEADER)3 DolphinRuntimeException (com.canoo.platform.core.DolphinRuntimeException)3 ConnectionException (com.canoo.platform.core.http.ConnectionException)3 HttpHeader (com.canoo.platform.core.http.HttpHeader)3 HttpResponse (com.canoo.platform.core.http.HttpResponse)3 IOException (java.io.IOException)3 Assert (com.canoo.dp.impl.platform.core.Assert)2 HttpClientConnection (com.canoo.dp.impl.platform.core.http.HttpClientConnection)2 CHARSET (com.canoo.dp.impl.platform.core.http.HttpHeaderConstants.CHARSET)2 JSON_MIME_TYPE (com.canoo.dp.impl.platform.core.http.HttpHeaderConstants.JSON_MIME_TYPE)2 HttpHeaderImpl (com.canoo.dp.impl.platform.core.http.HttpHeaderImpl)2 ClientConfiguration (com.canoo.platform.client.ClientConfiguration)2 Promise (com.canoo.platform.core.functional.Promise)2 ByteArrayProvider (com.canoo.platform.core.http.ByteArrayProvider)2 HttpCallResponseBuilder (com.canoo.platform.core.http.HttpCallResponseBuilder)2 HttpURLConnectionHandler (com.canoo.platform.core.http.HttpURLConnectionHandler)2 Gson (com.google.gson.Gson)2 Collections (java.util.Collections)2