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);
}
}
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);
}
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;
}
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);
}
Aggregations