001/**
002 * Copyright (c) 2015-2022, Michael Yang 杨福海 (fuhai999@gmail.com).
003 * <p>
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * <p>
008 * http://www.apache.org/licenses/LICENSE-2.0
009 * <p>
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package io.jboot.web.validate.interceptor;
017
018import com.jfinal.aop.Invocation;
019import com.jfinal.core.Controller;
020import com.jfinal.kit.Ret;
021import io.jboot.utils.RequestUtil;
022import io.jboot.utils.StrUtil;
023import io.jboot.web.validate.ValidateRenderType;
024
025/**
026 * @author michael yang (fuhai999@gmail.com)
027 */
028public class ValidateInterceptorUtil {
029
030
031    /**
032     * 通过自定义的 ValidExceptionRetBuilder,可以自定义验证错误输出的 json 内容
033     */
034    private static ValidExceptionRetBuilder validExceptionRetBuilder = ValidExceptionRetBuilder.DEFAULT_BUILDER;
035
036    public static ValidExceptionRetBuilder getValidExceptionRetBuilder() {
037        return validExceptionRetBuilder;
038    }
039
040    public static void setValidExceptionRetBuilder(ValidExceptionRetBuilder validExceptionRetBuilder) {
041        ValidateInterceptorUtil.validExceptionRetBuilder = validExceptionRetBuilder;
042    }
043
044
045    static String buildErrorMessage(Invocation inv, String annotation) {
046        StringBuilder sb = new StringBuilder();
047        sb.append("method \"").append(inv.getController().getClass().getName())
048                .append(".")
049                .append(inv.getMethodName())
050                .append("()\"")
051                .append(" has intercepted by annotation ")
052                .append(annotation);
053        return sb.toString();
054    }
055
056
057    static void renderValidException(Controller controller, String renderType, String formName, String message, String redirectUrl, String htmlPath, int errorCode) {
058        String reason = StrUtil.isNotBlank(message) ? (formName + " validate failed: " + message) : (formName + " validate failed.");
059        switch (renderType) {
060            case ValidateRenderType.DEFAULT:
061                if (RequestUtil.isAjaxRequest(controller.getRequest())
062                        || RequestUtil.isJsonContentType(controller.getRequest())) {
063
064                    Ret baseRet = Ret.fail("message", message)
065                            .set("reason", reason)
066                            .set("errorCode", errorCode)
067                            .setIfNotNull("formName", formName);
068
069                    Ret ret = validExceptionRetBuilder.build(baseRet);
070                    controller.renderJson(ret);
071                } else {
072                    controller.renderText(reason);
073                }
074                break;
075            case ValidateRenderType.JSON:
076                Ret baseRet = Ret.fail("message", message)
077                        .set("reason", reason)
078                        .set("errorCode", errorCode)
079                        .setIfNotNull("formName", formName);
080                Ret ret = validExceptionRetBuilder.build(baseRet);
081                controller.renderJson(ret);
082                break;
083            case ValidateRenderType.REDIRECT:
084                controller.redirect(redirectUrl);
085                break;
086            case ValidateRenderType.HTML:
087                controller.render(htmlPath);
088                break;
089            case ValidateRenderType.TEXT:
090                controller.renderText(message);
091                break;
092            default:
093                throw new IllegalArgumentException("Can not process render : " + renderType);
094        }
095    }
096
097
098    public static interface ValidExceptionRetBuilder {
099
100        ValidExceptionRetBuilder DEFAULT_BUILDER = ret -> ret;
101
102        Ret build(Ret baseRet);
103    }
104
105
106}