Example usage for com.fasterxml.jackson.core JsonProcessingException initCause

List of usage examples for com.fasterxml.jackson.core JsonProcessingException initCause

Introduction

In this page you can find the example usage for com.fasterxml.jackson.core JsonProcessingException initCause.

Prototype

public synchronized Throwable initCause(Throwable cause) 

Source Link

Document

Initializes the cause of this throwable to the specified value.

Usage

From source file:org.opencommercesearch.client.ProductApi.java

/**
 * Receives an API request, translates it into a valid HTTP request which is sent to the product API server and then processes the result by unmarshalling any
 * data into a valid ApiResponse subclass.
 *
 * @param request The API request to be handled.
 * @param clazz   The API response class that should be returned.
 * @param <T>     The API response type that will be returned.
 * @return A API response with data returned from the product API server.
 * @throws ProductApiException If there are underlying HTTP communication issues, if the API responded with errors or if there response parsing problems.
 *///w  ww .  ja v  a 2 s .  c o m
protected <T extends Response> Response handle(Request request, Class<T> clazz) throws ProductApiException {
    int retries = 0;
    Exception lastException = null;

    do {
        org.restlet.Request restletRequest = null;
        org.restlet.Response response = null;

        try {
            String url = getRequestUrl(request);
            if (url == null) {
                throw new IllegalArgumentException("Request " + request.getClass() + " has a null endpoint");
            }

            restletRequest = convertToRestlet(url, request);

            if (!getCacheEnabled()) {
                setHttpHeaderParameter(restletRequest, "X-Cache-Refresh", "true");
            }

            if (request.getHeaderParams() != null) {
                for (Map.Entry<String, String> entry : request.getHeaderParams().entrySet()) {
                    setHttpHeaderParameter(restletRequest, entry.getKey(), entry.getValue());
                }
            }

            if (logger.isDebugEnabled()) {
                logger.debug("Sending API request with base URL: " + restletRequest.getResourceRef());
            }

            response = client.handle(restletRequest);

            if (logger.isDebugEnabled()) {
                logger.debug("API response is '" + response.getStatus() + "'");
            }

            Response apiResponse = unmarshall(response, clazz);
            if (OK_STATUS_SET.contains(response.getStatus())) {
                return apiResponse;
            }

            //Handle special errors
            int statusCode = response.getStatus().getCode();
            String requestUrl = url + "&" + request.getQueryString();
            String message = getResponseMessage(apiResponse, response);

            if (statusCode >= 400 && statusCode < 500) {
                throw new ProductApiException(statusCode, url, message);
            } else {
                lastException = new ProductApiException(statusCode, url, message, lastException);
            }
        } catch (JsonProcessingException e) {
            throw new ProductApiException("Found invalid JSON format in the response.", e);
        } catch (IOException e) {
            if (logger.isDebugEnabled()) {
                logger.debug("Failed to handle API request, attempt " + retries);
            }

            e.initCause(lastException);
            lastException = e;
        } finally {
            if (response != null) {
                response.release();
            }
            if (restletRequest != null) {
                restletRequest.release();
            }
        }
    } while (retries++ < maxRetries);

    throw new ProductApiException("Failed to handle API request.", lastException);
}