use of org.eweb4j.mvc.config.bean.FieldConfigBean in project eweb4j-framework by laiweiwei.
the class IntegerSizeValidator method validate.
public Validation validate(ValidatorConfigBean val, Context context) {
Map<String, String> valError = new HashMap<String, String>();
for (FieldConfigBean f : val.getField()) {
String[] value = context.getQueryParamMap().get(f.getName());
if (value == null || value.length == 0)
continue;
String mess = f.getMessage();
int min = 0;
int max = 0;
boolean minFlag = false;
boolean maxFlag = false;
for (ParamConfigBean p : f.getParam()) {
if (Validators.MIN_SIZE_PARAM.equals(p.getName())) {
min = Integer.parseInt(p.getValue());
minFlag = true;
} else if (Validators.MAX_SIZE_PARAM.equals(p.getName())) {
max = Integer.parseInt(p.getValue());
maxFlag = true;
}
if (minFlag && maxFlag)
break;
}
for (String v : value) {
if (!v.matches(RegexList.integer_regexp))
continue;
int iv = Integer.parseInt(v);
mess = mess.replace("{min}", min + "").replace("{max}", max + "");
if (iv < min) {
valError.put(f.getName(), mess);
break;
} else if (iv > max) {
valError.put(f.getName(), mess);
break;
}
}
context.getRequest().setAttribute(f.getName(), value);
}
Validation validation = new Validation();
if (!valError.isEmpty())
validation.getErrors().put(val.getName(), valError);
return validation;
}
use of org.eweb4j.mvc.config.bean.FieldConfigBean in project eweb4j-framework by laiweiwei.
the class StringLengthValidator method validate.
public Validation validate(ValidatorConfigBean val, Context context) {
Map<String, String> valError = new HashMap<String, String>();
for (FieldConfigBean f : val.getField()) {
String[] value = context.getQueryParamMap().get(f.getName());
if (value == null || value.length == 0)
continue;
String mess = f.getMessage();
int min = 0;
int max = 0;
boolean minFlag = false;
boolean maxFlag = false;
for (ParamConfigBean p : f.getParam()) {
if (Validators.MIN_LENGTH_PARAM.equals(p.getName())) {
min = Integer.parseInt(p.getValue());
minFlag = true;
} else if (Validators.MAX_LENGTH_PARAM.equals(p.getName())) {
max = Integer.parseInt(p.getValue());
maxFlag = true;
}
if (minFlag && maxFlag)
break;
}
mess = mess.replace("{min}", min + "").replace("{max}", max + "");
for (String v : value) {
if (v.length() < min) {
valError.put(f.getName(), mess);
break;
} else if (v.length() > max) {
valError.put(f.getName(), mess);
break;
}
}
context.getRequest().setAttribute(f.getName(), value);
}
Validation validation = new Validation();
if (!valError.isEmpty())
validation.getErrors().put(val.getName(), valError);
return validation;
}
use of org.eweb4j.mvc.config.bean.FieldConfigBean in project eweb4j-framework by laiweiwei.
the class UploadValidator method validate.
public Validation validate(ValidatorConfigBean val, Context context) {
Map<String, String> valError = new HashMap<String, String>();
long countAllSize = 0;
for (FieldConfigBean f : val.getField()) {
String fieldName = f.getName();
String msg = f.getMessage();
List<UploadFile> files = context.getUploadMap().get(fieldName);
if (files == null || files.isEmpty())
continue;
for (UploadFile file : files) {
String fileName = file.getFileName();
String fileContentType = file.getContentType();
String tmpDir = null;
long memoryMax = 0;
String memoryMaxStr = null;
long allSizeMax = 0;
String allSizeMaxStr = null;
long oneSizeMax = 0;
String oneSizeMaxStr = null;
String[] suffixArray = null;
for (ParamConfigBean p : f.getParam()) {
if (Validators.UPLOAD_TMP_DIR.equalsIgnoreCase(p.getName())) {
tmpDir = p.getValue();
continue;
}
if (Validators.UPLOAD_MAX_MEMORY_SIZE.equalsIgnoreCase(p.getName())) {
memoryMax = CommonUtil.strToInt(CommonUtil.parseFileSize(p.getValue()) + "");
memoryMaxStr = p.getValue();
continue;
}
if (Validators.UPLOAD_MAX_REQ_SIZE.equalsIgnoreCase(p.getName())) {
allSizeMax = CommonUtil.parseFileSize(p.getValue());
allSizeMaxStr = p.getValue();
continue;
}
if (Validators.UPLOAD_MAX_FILE_SIZE.equalsIgnoreCase(p.getName())) {
oneSizeMax = CommonUtil.parseFileSize(p.getValue());
oneSizeMaxStr = p.getValue();
continue;
}
if (Validators.UPLOAD_SUFFIX.equalsIgnoreCase(p.getName())) {
suffixArray = p.getValue().split(",");
continue;
}
}
if (suffixArray != null && suffixArray.length > 0) {
boolean isOk = false;
for (String suffix : suffixArray) {
if (fileName.toLowerCase().endsWith("." + suffix.toLowerCase())) {
isOk = true;
break;
}
}
if (!isOk) {
valError.put(fieldName, getParamValue(Validators.UPLOAD_SUFFIX_MSG, f.getParam()).replace("{suffix}", Arrays.asList(suffixArray).toString()));
break;
}
}
long fileSize = file.getSize();
if (fileSize > oneSizeMax) {
valError.put(fieldName, getParamValue(Validators.UPLOAD_MAX_FILE_SIZE_MSG, f.getParam()).replace("{maxFileSize}", oneSizeMaxStr));
break;
}
countAllSize += fileSize;
if (countAllSize > allSizeMax) {
valError.put(fieldName, getParamValue(Validators.UPLOAD_MAX_REQ_SIZE_MSG, f.getParam()).replace("{maxRequestSize}", allSizeMaxStr));
break;
}
}
}
Validation validation = new Validation();
if (!valError.isEmpty())
validation.getErrors().put(val.getName(), valError);
return validation;
}
use of org.eweb4j.mvc.config.bean.FieldConfigBean in project eweb4j-framework by laiweiwei.
the class IntImpl method create.
public ValidatorConfigBean create(String fieldName, ValidatorConfigBean val) {
if (this.ann == null)
return null;
if (val == null || !Validators.INT.equals(val.getName())) {
val = new ValidatorConfigBean();
val.setName(Validators.INT);
}
FieldConfigBean fcb = new FieldConfigBean();
fcb.setName(fieldName);
fcb.setMessage(CommonUtil.parsePropValue(ann.mess()));
val.getField().add(fcb);
return val;
}
use of org.eweb4j.mvc.config.bean.FieldConfigBean in project eweb4j-framework by laiweiwei.
the class QQImpl method create.
public ValidatorConfigBean create(String fieldName, ValidatorConfigBean val) {
if (this.ann == null)
return null;
if (val == null || !Validators.QQ.equals(val.getName())) {
val = new ValidatorConfigBean();
val.setName(Validators.QQ);
}
FieldConfigBean fcb = new FieldConfigBean();
fcb.setName(fieldName);
fcb.setMessage(CommonUtil.parsePropValue(ann.mess()));
val.getField().add(fcb);
return val;
}
Aggregations