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

org.zodiac.sentinel.base.client.callback.servlet.DefaultServletBlockFallback Maven / Gradle / Ivy

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

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.zodiac.commons.util.ArrayUtil;
import org.zodiac.commons.util.serialize.JsonUtil;
import org.zodiac.commons.web.model.RestResult;
import org.zodiac.commons.web.model.RestResult.RestResultBuilder;
import org.zodiac.sentinel.base.client.callback.BlockFallback;

class DefaultServletBlockFallback implements BlockFallback {

    private HttpServletRequest request;
    private HttpServletResponse response;

    public DefaultServletBlockFallback(HttpServletRequest request, HttpServletResponse response) {
        this.request = request;
        this.response = response;
    }

    @Override
    public Object fallback(HttpStatus status, MediaType contentType, byte[] body, Throwable throwable) throws Exception {
        response.setStatus(status.value());
        response.setContentType(contentType.toString());
        RestResultBuilder builder = RestResult.builder();
        builder.withSuccess(false).withStatusCode(status.value());
        if (null != throwable) {
            builder.withMsg(throwable.getMessage());
        }
        if (ArrayUtil.notEmptyArray(body)) {
            builder.withData(body);
        }
        RestResult restResult = builder.build();
        response.getWriter().print(JsonUtil.toJsonString(restResult));
        return null;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy