Search in sources :

Example 1 with WebException

use of org.talend.sdk.component.form.api.WebException in project component-runtime by Talend.

the class SpringRestClient method toCompletionStage.

// poor man way but we'll enhance it on demand
private <T> CompletableFuture<T> toCompletionStage(final ListenableFuture<ResponseEntity<T>> future) {
    final CompletableFuture<T> result = new CompletableFuture<>();
    future.addCallback(new ListenableFutureCallback<ResponseEntity<T>>() {

        @Override
        public void onFailure(final Throwable throwable) {
            if (HttpServerErrorException.class.isInstance(throwable)) {
                result.completeExceptionally(toException(HttpServerErrorException.class.cast(throwable)));
            } else if (RestClientException.class.isInstance(throwable)) {
                result.completeExceptionally(toException(RestClientException.class.cast(throwable)));
            } else {
                result.completeExceptionally(new WebException(throwable, -1, singletonMap("error", throwable.getMessage())));
            }
        }

        @Override
        public void onSuccess(final ResponseEntity<T> t) {
            result.complete(t.getBody());
        }
    });
    return result;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) ResponseEntity(org.springframework.http.ResponseEntity) WebException(org.talend.sdk.component.form.api.WebException) HttpServerErrorException(org.springframework.web.client.HttpServerErrorException)

Example 2 with WebException

use of org.talend.sdk.component.form.api.WebException in project component-runtime by Talend.

the class SpringRestClient method toException.

private WebException toException(final HttpServerErrorException hsee) {
    try {
        return new WebException(hsee, hsee.getRawStatusCode(), new HttpMessageConverterExtractor<Map<String, Object>>(mapType.getType(), delegate.getMessageConverters()).extractData(new ClientHttpResponse() {

            @Override
            public HttpStatus getStatusCode() {
                return HttpStatus.OK;
            }

            @Override
            public int getRawStatusCode() {
                return 200;
            }

            @Override
            public String getStatusText() {
                return "";
            }

            @Override
            public void close() {
            // no-op
            }

            @Override
            public InputStream getBody() {
                return new ByteArrayInputStream(hsee.getResponseBodyAsByteArray());
            }

            @Override
            public HttpHeaders getHeaders() {
                final HttpHeaders json = json();
                json.add(HttpHeaders.CONTENT_TYPE, "application/json");
                return json;
            }
        }));
    } catch (final IOException e) {
        throw new IllegalStateException(e);
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) WebException(org.talend.sdk.component.form.api.WebException) HttpMessageConverterExtractor(org.springframework.web.client.HttpMessageConverterExtractor) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse)

Example 3 with WebException

use of org.talend.sdk.component.form.api.WebException in project component-runtime by Talend.

the class WebAppComponentProxy method onException.

private void onException(final AsyncResponse response, final Throwable e) {
    final UiActionResult payload;
    final int status;
    if (WebException.class.isInstance(e)) {
        final WebException we = WebException.class.cast(e);
        status = we.getStatus();
        payload = actionService.map(we);
    } else if (CompletionException.class.isInstance(e)) {
        final CompletionException actualException = CompletionException.class.cast(e);
        log.error(actualException.getMessage(), actualException);
        status = Response.Status.BAD_GATEWAY.getStatusCode();
        payload = actionService.map(new WebException(actualException, -1, emptyMap()));
    } else {
        log.error(e.getMessage(), e);
        status = Response.Status.BAD_GATEWAY.getStatusCode();
        payload = actionService.map(new WebException(e, -1, emptyMap()));
    }
    response.resume(new WebApplicationException(Response.status(status).entity(payload).build()));
}
Also used : WebException(org.talend.sdk.component.form.api.WebException) WebApplicationException(javax.ws.rs.WebApplicationException) CompletionException(java.util.concurrent.CompletionException) UiActionResult(org.talend.sdk.component.form.model.UiActionResult)

Aggregations

WebException (org.talend.sdk.component.form.api.WebException)3 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CompletionException (java.util.concurrent.CompletionException)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 HttpHeaders (org.springframework.http.HttpHeaders)1 ResponseEntity (org.springframework.http.ResponseEntity)1 ClientHttpResponse (org.springframework.http.client.ClientHttpResponse)1 HttpMessageConverterExtractor (org.springframework.web.client.HttpMessageConverterExtractor)1 HttpServerErrorException (org.springframework.web.client.HttpServerErrorException)1 UiActionResult (org.talend.sdk.component.form.model.UiActionResult)1