All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.zodiac.sentinel.base.client.callback.BlockFallback Maven / Gradle / Ivy

package org.zodiac.sentinel.base.client.callback;

import java.nio.charset.Charset;
import java.util.Objects;
import java.util.function.Supplier;

import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.zodiac.commons.util.ArrayUtil;
import org.zodiac.commons.util.lang.Strings;
import org.zodiac.sdk.toolkit.constants.CharsetConstants;

public interface BlockFallback {

    default  T fallback(Object body, Throwable throwable) throws Exception {
        return fallback(MediaType.APPLICATION_JSON, body, throwable);
    }

    default  T fallback(MediaType contentType, Object body, Throwable throwable) throws Exception {
        return fallback(HttpStatus.TOO_MANY_REQUESTS, contentType, body, throwable);
    }

    default  T fallback(HttpStatus status, Object body, Throwable throwable) throws Exception {
        return fallback(status, MediaType.APPLICATION_JSON, body, throwable);
    }

    default  T fallback(HttpStatus status, MediaType contentType, Object body, Throwable throwable) throws Exception {
        return fallback(status, contentType, Objects.requireNonNull(body, "body").toString(), CharsetConstants.UTF_8, throwable);
    }

    /*===============================================================================================*/

    default  T fallback(CharSequence body, Throwable throwable) throws Exception {
        return fallback(body, CharsetConstants.UTF_8_NAME, throwable);
    }

    default  T fallback(CharSequence body, String charset, Throwable throwable) throws Exception {
        return fallback(HttpStatus.TOO_MANY_REQUESTS, body, charset, throwable);
    }

    default  T fallback(HttpStatus status, CharSequence body, String charset, Throwable throwable) throws Exception {
        return fallback(status, MediaType.APPLICATION_JSON, body, charset, throwable);
    }

    default  T fallback(MediaType contentType, CharSequence body, String charset, Throwable throwable) throws Exception {
        return fallback(HttpStatus.TOO_MANY_REQUESTS, contentType, body, charset, throwable);
    }

    default  T fallback(HttpStatus status, MediaType contentType, CharSequence body, String charset, Throwable throwable) throws Exception {
        return fallback(status, contentType, body,
            Strings.notBlank(charset) ? Charset.forName(charset) : CharsetConstants.UTF_8, throwable);
    }

    default  T fallback(HttpStatus status, MediaType contentType, CharSequence body, Throwable throwable) throws Exception {
        return fallback(status, contentType, body, CharsetConstants.UTF_8, throwable);
    }

    default  T fallback(HttpStatus status, MediaType contentType, CharSequence body, Charset charset, Throwable throwable) throws Exception {
        return fallback(status, contentType, String.valueOf(Objects.requireNonNull(body, "body"))
            .getBytes(null != charset ? charset : CharsetConstants.UTF_8), throwable);
    }

    /*===============================================================================================*/

    default  T fallback(Supplier bodySupplier, Throwable throwable) throws Exception {
        return fallback(HttpStatus.TOO_MANY_REQUESTS, bodySupplier, throwable);
    }

    default  T fallback(MediaType contentType, Supplier bodySupplier, Throwable throwable) throws Exception {
        return fallback(HttpStatus.TOO_MANY_REQUESTS, contentType, bodySupplier, throwable);
    }

    default  T fallback(HttpStatus status, Supplier bodySupplier, Throwable throwable) throws Exception {
        return fallback(status, MediaType.APPLICATION_JSON, bodySupplier, throwable);
    }

    default  T fallback(HttpStatus status, MediaType contentType, Supplier bodySupplier, Throwable throwable) throws Exception {
        return fallback(status, contentType, Objects.requireNonNull(Objects.requireNonNull(bodySupplier, "bodySupplier").get(), "body"), throwable);
    }

    /*===============================================================================================*/

    default  T fallback(byte[] body, Throwable throwable) throws Exception {
        return fallback(HttpStatus.TOO_MANY_REQUESTS, body, throwable);
    }

    default  T fallback(HttpStatus status, byte[] body, Throwable throwable) throws Exception {
        return fallback(status, MediaType.APPLICATION_JSON, body, throwable);
    }

    default  T fallback(MediaType contentType, byte[] body, Throwable throwable) throws Exception {
        return fallback(HttpStatus.TOO_MANY_REQUESTS, contentType, body, throwable);
    }

    default  T fallback(HttpStatus status, MediaType contentType, Throwable throwable) throws Exception {
        return fallback(status, contentType, ArrayUtil.EMPTY_BYTE_ARRAY, throwable);
    }

     T fallback(HttpStatus status, MediaType contentType, byte[] body, Throwable throwable) throws Exception;

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy