Example usage for org.springframework.http HttpStatus valueOf

List of usage examples for org.springframework.http HttpStatus valueOf

Introduction

In this page you can find the example usage for org.springframework.http HttpStatus valueOf.

Prototype

public static HttpStatus valueOf(int statusCode) 

Source Link

Document

Return the enum constant of this type with the specified numeric value.

Usage

From source file:org.talend.dataprep.command.GenericCommand.java

/**
 * Runs a data prep command with the following steps:
 * <ul>//from www. j a va  2 s. c o m
 * <li>Gets the HTTP command to execute (see {@link #execute(Supplier)}.</li>
 * <li>Gets the behavior to adopt based on returned HTTP code (see {@link #on(HttpStatus...)}).</li>
 * <li>If no behavior was defined for returned code, returns an error as defined in {@link #onError(Function)}</li>
 * <li>If a behavior was defined, invokes defined behavior.</li>
 * </ul>
 *
 * @return A instance of <code>T</code>.
 * @throws Exception If command execution fails.
 */
@Override
protected T run() throws Exception {
    final HttpRequestBase request = httpCall.get();

    // update request header with security token
    if (StringUtils.isNotBlank(authenticationToken)) {
        request.addHeader(AUTHORIZATION, authenticationToken);
    }

    final HttpResponse response;
    try {
        LOGGER.trace("Requesting {} {}", request.getMethod(), request.getURI());
        response = client.execute(request);
    } catch (Exception e) {
        throw onError.apply(e);
    }
    commandResponseHeaders = response.getAllHeaders();

    status = HttpStatus.valueOf(response.getStatusLine().getStatusCode());

    // do we have a behavior for this status code (even an error) ?
    // if yes use it
    BiFunction<HttpRequestBase, HttpResponse, T> function = behavior.get(status);
    if (function != null) {
        try {
            return function.apply(request, response);
        } catch (Exception e) {
            throw onError.apply(e);
        }
    }

    // handle response's HTTP status
    if (status.is4xxClientError() || status.is5xxServerError()) {
        // Http status >= 400 so apply onError behavior
        return callOnError(onError).apply(request, response);
    } else {
        // Http status is not error so apply onError behavior
        return behavior.getOrDefault(status, missingBehavior()).apply(request, response);
    }
}