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.alibaba.fastjson.JSON; 019import com.alibaba.fastjson.JSONObject; 020import com.alibaba.fastjson.JSONPath; 021import com.jfinal.aop.Interceptor; 022import com.jfinal.aop.Invocation; 023import com.jfinal.log.Log; 024import io.jboot.Jboot; 025import io.jboot.utils.AnnotationUtil; 026import io.jboot.utils.ArrayUtil; 027import io.jboot.utils.StrUtil; 028import io.jboot.web.validate.EmptyValidate; 029import io.jboot.web.validate.Form; 030import io.jboot.web.validate.FormType; 031 032/** 033 * 验证拦截器 034 */ 035public class EmptyValidateInterceptor implements Interceptor { 036 037 private static final Log LOG = Log.getLog("Validate"); 038 039 @Override 040 public void intercept(Invocation inv) { 041 042 EmptyValidate emptyParaValidate = inv.getMethod().getAnnotation(EmptyValidate.class); 043 if (emptyParaValidate != null && !validateEmpty(inv, emptyParaValidate)) { 044 if (Jboot.isDevMode()){ 045 LOG.error(ValidateInterceptorUtil.buildErrorMessage(inv,"@EmptyValidate")); 046 } 047 return; 048 } 049 050 inv.invoke(); 051 } 052 053 054 /** 055 * 非空判断验证 056 * 057 * @param inv 058 * @param emptyParaValidate 059 * @return 060 */ 061 private boolean validateEmpty(Invocation inv, EmptyValidate emptyParaValidate) { 062 Form[] forms = emptyParaValidate.value(); 063 if (ArrayUtil.isNullOrEmpty(forms)) { 064 return true; 065 } 066 067 068 for (Form formAnnotation : forms) { 069 String formName = AnnotationUtil.get(formAnnotation.name()); 070 String formType = AnnotationUtil.get(formAnnotation.type()); 071 if (StrUtil.isBlank(formName)) { 072 throw new IllegalArgumentException("@Form.value must not be empty in " + inv.getController().getClass().getName() + "." + inv.getMethodName()); 073 } 074 String paraValue = null; 075 if (FormType.FORM_DATA.equalsIgnoreCase(formType)) { 076 paraValue = inv.getController().getPara(formName); 077 } else if (FormType.RAW_DATA.equalsIgnoreCase(formType)) { 078 try { 079 JSONObject json = JSON.parseObject(inv.getController().getRawData()); 080 if (json != null) { 081 Object tmp = JSONPath.eval(json, "$." + formName); 082 if (tmp != null) { 083 paraValue = tmp.toString(); 084 } 085 } 086 } catch (Exception e) { 087 paraValue = null; 088 } 089 } else { 090 throw new IllegalArgumentException("@EmptyValidate not support form type : " + formType + ", " + 091 "see: io.jboot.web.controller.validate.FormType"); 092 } 093 094 if (paraValue == null || paraValue.trim().length() == 0) { 095 ValidateInterceptorUtil.renderValidException(inv.getController() 096 , AnnotationUtil.get(emptyParaValidate.renderType()) 097 , formName 098 , AnnotationUtil.get(formAnnotation.message()) 099 , AnnotationUtil.get(emptyParaValidate.redirectUrl()) 100 , AnnotationUtil.get(emptyParaValidate.htmlPath()) 101 , formAnnotation.errorCode() 102 ); 103 104 return false; 105 } 106 } 107 108 return true; 109 } 110 111 112 113 114 115}