Search in sources :

Example 1 with ConnectionException

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

Aggregations

ACCEPT_CHARSET_HEADER (com.canoo.dp.impl.platform.core.http.HttpHeaderConstants.ACCEPT_CHARSET_HEADER)1 ACCEPT_HEADER (com.canoo.dp.impl.platform.core.http.HttpHeaderConstants.ACCEPT_HEADER)1 DolphinRuntimeException (com.canoo.platform.core.DolphinRuntimeException)1 ConnectionException (com.canoo.platform.core.http.ConnectionException)1 HttpException (com.canoo.platform.core.http.HttpException)1 HttpHeader (com.canoo.platform.core.http.HttpHeader)1 IOException (java.io.IOException)1