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

cc.shacocloud.mirage.web.AbstractHandlerExceptionResolver Maven / Gradle / Ivy

package cc.shacocloud.mirage.web;

import io.vertx.core.Future;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpHeaders;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.Ordered;
import org.jetbrains.annotations.Nullable;

/**
 * {@link HandlerExceptionResolver}实现的抽象基类并实现{@link Ordered}接口。默认为{@link #getOrder()}
 */
public abstract class AbstractHandlerExceptionResolver implements HandlerExceptionResolver, Ordered {

    private boolean preventResponseCaching = false;

    protected final Log logger = LogFactory.getLog(getClass());

    @Override
    public Future resolveException(HttpRequest request, HttpResponse response,
                                           VertxInvokeHandler handler, Throwable cause) {
        if (shouldApplyTo(request, handler)) {
            prepareResponse(cause, response);

            return doResolveException(request, response, handler, cause);
        }
        return Future.succeededFuture();
    }

    /**
     * 检查这个解析器是否应该应用于给定的处理程序。
     * 

* 这里永远返回 {@code ture},子类可以自行实现 */ protected boolean shouldApplyTo(HttpRequest request, @Nullable VertxInvokeHandler handler) { return true; } /** * 为处理给定请求期间发生的给定异常构建日志消息 * * @return 要使用的日志消息 */ protected String buildLogMessage(Throwable ex, HttpRequest request) { return "解析异常: [" + ex + "]"; } /** * 为异常情况准备响应。 *

* 默认实现阻止响应被缓存,如果{@link #setPreventResponseCaching "preventResponseCaching"}属性被设置为"true"。 * * @param cause 在处理程序执行期间抛出的异常 * @param response 当前的HTTP响应 * @see #preventCaching */ protected void prepareResponse(Throwable cause, HttpResponse response) { if (this.preventResponseCaching) { preventCaching(response); } } /** * 通过设置相应的HTTP {@code Cache-Control: no-store}头阻止响应被缓存。 * * @param response 当前的HTTP响应 */ protected void preventCaching(HttpResponse response) { response.headers().add(HttpHeaders.CACHE_CONTROL, "no-store"); } /** * 指定是否阻止此异常解析器解析的任何视图的HTTP响应缓存。 *

* 默认值是{@code false}。将其切换为{@code true},以自动生成HTTP响应头以抑制响应缓存 */ public void setPreventResponseCaching(boolean preventResponseCaching) { this.preventResponseCaching = preventResponseCaching; } /** * 实际解决在处理程序执行期间抛出的给定异常 */ protected abstract Future doResolveException(HttpRequest request, HttpResponse response, VertxInvokeHandler handler, Throwable cause); @Override public int getOrder() { return Ordered.LOWEST_PRECEDENCE; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy