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:
Luca Frosini 2015-06-05 10:54:26 +00:00
parent 942e1451b7
commit d2a01e964d
9 changed files with 104 additions and 20 deletions

View File

@ -6,6 +6,8 @@ package org.gcube.accounting.datamodel.validators;
import java.io.Serializable;
import java.lang.annotation.Annotation;
import org.gcube.accounting.exception.InvalidValueException;
/**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
*
@ -14,7 +16,9 @@ public interface FieldValidator<T extends Annotation> {
Class<T> annotation();
boolean isValid(Serializable toValidate);
public boolean isValid(Serializable toValidate);
public Serializable validate(Serializable toValidate) throws InvalidValueException;
String getErrorSuffix();
}

View File

@ -4,6 +4,7 @@ import java.io.Serializable;
import java.util.Map;
import org.gcube.accounting.datamodel.annotations.NotEmptyIfNotNull;
import org.gcube.accounting.exception.InvalidValueException;
public class NotEmptyIfNotNullValidator implements FieldValidator<NotEmptyIfNotNull>{
@ -23,6 +24,17 @@ public class NotEmptyIfNotNullValidator implements FieldValidator<NotEmptyIfNotN
return !((String)toValidate).isEmpty();
} 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() {
return "is empty";

View File

@ -4,6 +4,7 @@ import java.io.Serializable;
import java.util.Map;
import org.gcube.accounting.datamodel.annotations.NotEmpty;
import org.gcube.accounting.exception.InvalidValueException;
public class NotEmptyValidator implements FieldValidator<NotEmpty>{
@ -23,6 +24,17 @@ public class NotEmptyValidator implements FieldValidator<NotEmpty>{
return !((String)toValidate).isEmpty();
} 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() {
return "is empty";

View File

@ -3,6 +3,7 @@ package org.gcube.accounting.datamodel.validators;
import java.io.Serializable;
import org.gcube.accounting.datamodel.annotations.NotNull;
import org.gcube.accounting.exception.InvalidValueException;
public class NotNullValidator implements FieldValidator<NotNull>{
@ -15,6 +16,13 @@ public class NotNullValidator implements FieldValidator<NotNull>{
return toValidate!=null;
}
public Serializable validate(Serializable toValidate) throws InvalidValueException {
if(isValid(toValidate)){
return toValidate;
}
throw new InvalidValueException(getErrorSuffix());
}
public String getErrorSuffix() {
return "is null";
}

View File

@ -5,6 +5,7 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.gcube.accounting.datamodel.annotations.ValidLong;
import org.gcube.accounting.exception.InvalidValueException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -45,11 +46,24 @@ public class ValidIPValidator implements FieldValidator<ValidLong>{
public boolean isValid(Serializable toValidate) {
try {
return isIpAddress((String) toValidate);
}catch(Exception e){}
return false;
validate(toValidate);
} catch (InvalidValueException e) {
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() {
return "not valid IP Address";
}

View File

@ -3,6 +3,7 @@ package org.gcube.accounting.datamodel.validators;
import java.io.Serializable;
import org.gcube.accounting.datamodel.annotations.ValidLong;
import org.gcube.accounting.exception.InvalidValueException;
public class ValidIntegerValidator implements FieldValidator<ValidLong>{
@ -12,14 +13,24 @@ public class ValidIntegerValidator implements FieldValidator<ValidLong>{
}
public boolean isValid(Serializable toValidate) {
try {
validate(toValidate);
} catch (InvalidValueException e) {
return false;
}
return true;
}
public Serializable validate(Serializable toValidate) throws InvalidValueException {
if(toValidate instanceof Integer){
return true;
return toValidate;
}
Integer integerObj = Integer.getInteger((String) toValidate);
if(integerObj!=null){
return true;
return integerObj;
}
return false;
throw new InvalidValueException(getErrorSuffix());
}
public String getErrorSuffix() {

View File

@ -3,6 +3,7 @@ package org.gcube.accounting.datamodel.validators;
import java.io.Serializable;
import org.gcube.accounting.datamodel.annotations.ValidLong;
import org.gcube.accounting.exception.InvalidValueException;
public class ValidLongValidator implements FieldValidator<ValidLong>{
@ -12,16 +13,25 @@ public class ValidLongValidator implements FieldValidator<ValidLong>{
}
public boolean isValid(Serializable toValidate) {
try {
validate(toValidate);
} catch (InvalidValueException e) {
return false;
}
return true;
}
public Serializable validate(Serializable toValidate) throws InvalidValueException {
if(toValidate instanceof Long){
return true;
return toValidate;
}
Long longObj = Long.getLong((String) toValidate);
if(longObj!=null){
return true;
return longObj;
}
return false;
}
public String getErrorSuffix() {
return String.format("not instace of %s", Long.class.getSimpleName());
}

View File

@ -4,6 +4,7 @@ import java.io.Serializable;
import org.gcube.accounting.datamodel.UsageRecord.OperationResult;
import org.gcube.accounting.datamodel.annotations.ValidOperationResult;
import org.gcube.accounting.exception.InvalidValueException;
public class ValidOperationResultValidator implements FieldValidator<ValidOperationResult>{
@ -12,16 +13,24 @@ public class ValidOperationResultValidator implements FieldValidator<ValidOperat
}
public boolean isValid(Serializable toValidate) {
if(toValidate instanceof OperationResult){
return true;
}
try {
OperationResult operationResult = OperationResult.valueOf((String) toValidate);
if(operationResult !=null){
return true;
}
}catch(Exception e){}
return false;
validate(toValidate);
} catch (InvalidValueException e) {
return false;
}
return true;
}
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() {

View File

@ -14,5 +14,9 @@ public class InvalidValueException extends Exception {
public InvalidValueException(String message) {
super(message);
}
public InvalidValueException(String message, Throwable cause) {
super(message, cause);
}
}