refs #200: Create accouting-lib library
https://support.d4science.org/issues/200 Fixing validators git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/accounting/accounting-lib@115237 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
942e1451b7
commit
d2a01e964d
|
@ -6,6 +6,8 @@ package org.gcube.accounting.datamodel.validators;
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.lang.annotation.Annotation;
|
import java.lang.annotation.Annotation;
|
||||||
|
|
||||||
|
import org.gcube.accounting.exception.InvalidValueException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
|
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
|
||||||
*
|
*
|
||||||
|
@ -14,7 +16,9 @@ public interface FieldValidator<T extends Annotation> {
|
||||||
|
|
||||||
Class<T> annotation();
|
Class<T> annotation();
|
||||||
|
|
||||||
boolean isValid(Serializable toValidate);
|
public boolean isValid(Serializable toValidate);
|
||||||
|
|
||||||
|
public Serializable validate(Serializable toValidate) throws InvalidValueException;
|
||||||
|
|
||||||
String getErrorSuffix();
|
String getErrorSuffix();
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import java.io.Serializable;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.gcube.accounting.datamodel.annotations.NotEmptyIfNotNull;
|
import org.gcube.accounting.datamodel.annotations.NotEmptyIfNotNull;
|
||||||
|
import org.gcube.accounting.exception.InvalidValueException;
|
||||||
|
|
||||||
public class NotEmptyIfNotNullValidator implements FieldValidator<NotEmptyIfNotNull>{
|
public class NotEmptyIfNotNullValidator implements FieldValidator<NotEmptyIfNotNull>{
|
||||||
|
|
||||||
|
@ -24,6 +25,17 @@ public class NotEmptyIfNotNullValidator implements FieldValidator<NotEmptyIfNotN
|
||||||
} else return true;
|
} else return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Serializable validate(Serializable toValidate) throws InvalidValueException {
|
||||||
|
try{
|
||||||
|
if(isValid(toValidate)){
|
||||||
|
return toValidate;
|
||||||
|
}
|
||||||
|
}catch(Exception e){
|
||||||
|
throw new InvalidValueException(getErrorSuffix(), e.getCause());
|
||||||
|
}
|
||||||
|
throw new InvalidValueException(getErrorSuffix());
|
||||||
|
}
|
||||||
|
|
||||||
public String getErrorSuffix() {
|
public String getErrorSuffix() {
|
||||||
return "is empty";
|
return "is empty";
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import java.io.Serializable;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.gcube.accounting.datamodel.annotations.NotEmpty;
|
import org.gcube.accounting.datamodel.annotations.NotEmpty;
|
||||||
|
import org.gcube.accounting.exception.InvalidValueException;
|
||||||
|
|
||||||
public class NotEmptyValidator implements FieldValidator<NotEmpty>{
|
public class NotEmptyValidator implements FieldValidator<NotEmpty>{
|
||||||
|
|
||||||
|
@ -24,6 +25,17 @@ public class NotEmptyValidator implements FieldValidator<NotEmpty>{
|
||||||
} else return true;
|
} else return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Serializable validate(Serializable toValidate) throws InvalidValueException {
|
||||||
|
try{
|
||||||
|
if(isValid(toValidate)){
|
||||||
|
return toValidate;
|
||||||
|
}
|
||||||
|
}catch(Exception e){
|
||||||
|
throw new InvalidValueException(getErrorSuffix(), e.getCause());
|
||||||
|
}
|
||||||
|
throw new InvalidValueException(getErrorSuffix());
|
||||||
|
}
|
||||||
|
|
||||||
public String getErrorSuffix() {
|
public String getErrorSuffix() {
|
||||||
return "is empty";
|
return "is empty";
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ package org.gcube.accounting.datamodel.validators;
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
|
||||||
import org.gcube.accounting.datamodel.annotations.NotNull;
|
import org.gcube.accounting.datamodel.annotations.NotNull;
|
||||||
|
import org.gcube.accounting.exception.InvalidValueException;
|
||||||
|
|
||||||
|
|
||||||
public class NotNullValidator implements FieldValidator<NotNull>{
|
public class NotNullValidator implements FieldValidator<NotNull>{
|
||||||
|
@ -15,6 +16,13 @@ public class NotNullValidator implements FieldValidator<NotNull>{
|
||||||
return toValidate!=null;
|
return toValidate!=null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Serializable validate(Serializable toValidate) throws InvalidValueException {
|
||||||
|
if(isValid(toValidate)){
|
||||||
|
return toValidate;
|
||||||
|
}
|
||||||
|
throw new InvalidValueException(getErrorSuffix());
|
||||||
|
}
|
||||||
|
|
||||||
public String getErrorSuffix() {
|
public String getErrorSuffix() {
|
||||||
return "is null";
|
return "is null";
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import org.gcube.accounting.datamodel.annotations.ValidLong;
|
import org.gcube.accounting.datamodel.annotations.ValidLong;
|
||||||
|
import org.gcube.accounting.exception.InvalidValueException;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
@ -45,10 +46,23 @@ public class ValidIPValidator implements FieldValidator<ValidLong>{
|
||||||
|
|
||||||
public boolean isValid(Serializable toValidate) {
|
public boolean isValid(Serializable toValidate) {
|
||||||
try {
|
try {
|
||||||
return isIpAddress((String) toValidate);
|
validate(toValidate);
|
||||||
}catch(Exception e){}
|
} catch (InvalidValueException e) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Serializable validate(Serializable toValidate) throws InvalidValueException {
|
||||||
|
try {
|
||||||
|
if(isIpAddress((String) toValidate)){
|
||||||
|
return (String) toValidate;
|
||||||
|
}
|
||||||
|
}catch (Exception e) {
|
||||||
|
throw new InvalidValueException(getErrorSuffix(), e.getCause());
|
||||||
|
}
|
||||||
|
throw new InvalidValueException(getErrorSuffix());
|
||||||
|
}
|
||||||
|
|
||||||
public String getErrorSuffix() {
|
public String getErrorSuffix() {
|
||||||
return "not valid IP Address";
|
return "not valid IP Address";
|
||||||
|
|
|
@ -3,6 +3,7 @@ package org.gcube.accounting.datamodel.validators;
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
|
||||||
import org.gcube.accounting.datamodel.annotations.ValidLong;
|
import org.gcube.accounting.datamodel.annotations.ValidLong;
|
||||||
|
import org.gcube.accounting.exception.InvalidValueException;
|
||||||
|
|
||||||
|
|
||||||
public class ValidIntegerValidator implements FieldValidator<ValidLong>{
|
public class ValidIntegerValidator implements FieldValidator<ValidLong>{
|
||||||
|
@ -12,14 +13,24 @@ public class ValidIntegerValidator implements FieldValidator<ValidLong>{
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isValid(Serializable toValidate) {
|
public boolean isValid(Serializable toValidate) {
|
||||||
if(toValidate instanceof Integer){
|
try {
|
||||||
|
validate(toValidate);
|
||||||
|
} catch (InvalidValueException e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Serializable validate(Serializable toValidate) throws InvalidValueException {
|
||||||
|
if(toValidate instanceof Integer){
|
||||||
|
return toValidate;
|
||||||
|
}
|
||||||
Integer integerObj = Integer.getInteger((String) toValidate);
|
Integer integerObj = Integer.getInteger((String) toValidate);
|
||||||
if(integerObj!=null){
|
if(integerObj!=null){
|
||||||
return true;
|
return integerObj;
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
|
throw new InvalidValueException(getErrorSuffix());
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getErrorSuffix() {
|
public String getErrorSuffix() {
|
||||||
|
|
|
@ -3,6 +3,7 @@ package org.gcube.accounting.datamodel.validators;
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
|
||||||
import org.gcube.accounting.datamodel.annotations.ValidLong;
|
import org.gcube.accounting.datamodel.annotations.ValidLong;
|
||||||
|
import org.gcube.accounting.exception.InvalidValueException;
|
||||||
|
|
||||||
|
|
||||||
public class ValidLongValidator implements FieldValidator<ValidLong>{
|
public class ValidLongValidator implements FieldValidator<ValidLong>{
|
||||||
|
@ -12,12 +13,21 @@ public class ValidLongValidator implements FieldValidator<ValidLong>{
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isValid(Serializable toValidate) {
|
public boolean isValid(Serializable toValidate) {
|
||||||
if(toValidate instanceof Long){
|
try {
|
||||||
|
validate(toValidate);
|
||||||
|
} catch (InvalidValueException e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Serializable validate(Serializable toValidate) throws InvalidValueException {
|
||||||
|
if(toValidate instanceof Long){
|
||||||
|
return toValidate;
|
||||||
|
}
|
||||||
Long longObj = Long.getLong((String) toValidate);
|
Long longObj = Long.getLong((String) toValidate);
|
||||||
if(longObj!=null){
|
if(longObj!=null){
|
||||||
return true;
|
return longObj;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import java.io.Serializable;
|
||||||
|
|
||||||
import org.gcube.accounting.datamodel.UsageRecord.OperationResult;
|
import org.gcube.accounting.datamodel.UsageRecord.OperationResult;
|
||||||
import org.gcube.accounting.datamodel.annotations.ValidOperationResult;
|
import org.gcube.accounting.datamodel.annotations.ValidOperationResult;
|
||||||
|
import org.gcube.accounting.exception.InvalidValueException;
|
||||||
|
|
||||||
public class ValidOperationResultValidator implements FieldValidator<ValidOperationResult>{
|
public class ValidOperationResultValidator implements FieldValidator<ValidOperationResult>{
|
||||||
|
|
||||||
|
@ -12,16 +13,24 @@ public class ValidOperationResultValidator implements FieldValidator<ValidOperat
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isValid(Serializable toValidate) {
|
public boolean isValid(Serializable toValidate) {
|
||||||
if(toValidate instanceof OperationResult){
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
try {
|
try {
|
||||||
OperationResult operationResult = OperationResult.valueOf((String) toValidate);
|
validate(toValidate);
|
||||||
if(operationResult !=null){
|
} catch (InvalidValueException e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}catch(Exception e){}
|
|
||||||
return false;
|
public Serializable validate(Serializable toValidate) throws InvalidValueException {
|
||||||
|
if(toValidate instanceof OperationResult){
|
||||||
|
return toValidate;
|
||||||
|
}
|
||||||
|
Integer integerObj = Integer.getInteger((String) toValidate);
|
||||||
|
if(integerObj!=null){
|
||||||
|
return integerObj;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new InvalidValueException(getErrorSuffix());
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getErrorSuffix() {
|
public String getErrorSuffix() {
|
||||||
|
|
|
@ -15,4 +15,8 @@ public class InvalidValueException extends Exception {
|
||||||
super(message);
|
super(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public InvalidValueException(String message, Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue