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@115249 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
69911fd78d
commit
9ceb6ccbca
|
@ -72,7 +72,7 @@ public abstract class RawUsageRecord implements UsageRecord, Serializable {
|
|||
*/
|
||||
protected Map<String, Serializable> resourceSpecificProperties;
|
||||
|
||||
protected final Map<String, List<FieldValidator<? extends Annotation>>> validation;
|
||||
protected final Map<String, List<FieldValidator>> validation;
|
||||
|
||||
protected void initializeValidation() {
|
||||
logger.debug("Initializing Field Validators");
|
||||
|
@ -84,12 +84,12 @@ public abstract class RawUsageRecord implements UsageRecord, Serializable {
|
|||
} catch (Exception e) {
|
||||
continue;
|
||||
}
|
||||
List<FieldValidator<? extends Annotation>> fieldValidators = new ArrayList<FieldValidator<? extends Annotation>>();
|
||||
List<FieldValidator> fieldValidators = new ArrayList<FieldValidator>();
|
||||
validation.put(keyString, fieldValidators);
|
||||
for (Annotation annotation : field.getAnnotations()){
|
||||
if (annotation.annotationType().isAnnotationPresent(ValidityChecker.class)){
|
||||
Class<? extends FieldValidator<?>> managedClass = ((ValidityChecker)annotation.annotationType().getAnnotation(ValidityChecker.class)).managed();
|
||||
FieldValidator<?> validator;
|
||||
Class<? extends FieldValidator> managedClass = ((ValidityChecker)annotation.annotationType().getAnnotation(ValidityChecker.class)).managed();
|
||||
FieldValidator validator;
|
||||
try {
|
||||
validator = managedClass.newInstance();
|
||||
} catch (InstantiationException | IllegalAccessException e) {
|
||||
|
@ -104,7 +104,7 @@ public abstract class RawUsageRecord implements UsageRecord, Serializable {
|
|||
|
||||
protected RawUsageRecord(){
|
||||
this.resourceSpecificProperties = new HashMap<String, Serializable>();
|
||||
this.validation = new HashMap<String, List<FieldValidator<? extends Annotation>>>();
|
||||
this.validation = new HashMap<String, List<FieldValidator>>();
|
||||
initializeValidation();
|
||||
this.resourceSpecificProperties.put(ID, UUID.randomUUID().toString());
|
||||
this.resourceSpecificProperties.put(RESOURCE_TYPE, this.getClass().getSimpleName());
|
||||
|
@ -113,7 +113,7 @@ public abstract class RawUsageRecord implements UsageRecord, Serializable {
|
|||
}
|
||||
|
||||
protected RawUsageRecord(Map<String, Serializable> properties) throws InvalidValueException {
|
||||
this.validation = new HashMap<String, List<FieldValidator<? extends Annotation>>>();
|
||||
this.validation = new HashMap<String, List<FieldValidator>>();
|
||||
initializeValidation();
|
||||
setResourceSpecificProperties(properties);
|
||||
}
|
||||
|
@ -391,9 +391,9 @@ public abstract class RawUsageRecord implements UsageRecord, Serializable {
|
|||
|
||||
protected Serializable validateField(String key, Serializable serializable) throws InvalidValueException {
|
||||
Serializable checkedValue = serializable;
|
||||
List<FieldValidator<? extends Annotation>> fieldValidators = validation.get(key);
|
||||
List<FieldValidator> fieldValidators = validation.get(key);
|
||||
if(fieldValidators!=null){
|
||||
for(FieldValidator<? extends Annotation> fieldValidator : fieldValidators){
|
||||
for(FieldValidator fieldValidator : fieldValidators){
|
||||
checkedValue = fieldValidator.validate(checkedValue);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
package org.gcube.accounting.datamodel.annotations;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Inherited;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Target(ElementType.FIELD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Inherited
|
||||
public @interface DeprecatedField {
|
||||
|
||||
Class<? extends DeprecationAction> action();
|
||||
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
package org.gcube.accounting.datamodel.annotations;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.gcube.accounting.datamodel.UsageRecord;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
|
||||
*/
|
||||
public interface DeprecationAction {
|
||||
|
||||
public Serializable manage(String key, Serializable manage, UsageRecord usageRecord);
|
||||
|
||||
}
|
|
@ -20,6 +20,6 @@ import org.gcube.accounting.datamodel.validators.FieldValidator;
|
|||
@Inherited
|
||||
public @interface ValidityChecker {
|
||||
|
||||
Class<? extends FieldValidator<?>> managed();
|
||||
Class<? extends FieldValidator> managed();
|
||||
}
|
||||
|
||||
|
|
|
@ -7,10 +7,12 @@ import java.util.Calendar;
|
|||
|
||||
import org.gcube.accounting.datamodel.RawUsageRecord;
|
||||
import org.gcube.accounting.datamodel.SingleUsageRecord;
|
||||
import org.gcube.accounting.datamodel.annotations.DeprecatedField;
|
||||
import org.gcube.accounting.datamodel.annotations.NotEmpty;
|
||||
import org.gcube.accounting.datamodel.annotations.Required;
|
||||
import org.gcube.accounting.datamodel.annotations.ValidInteger;
|
||||
import org.gcube.accounting.datamodel.annotations.ValidLong;
|
||||
import org.gcube.accounting.datamodel.validators.ConvertToOperationResult;
|
||||
import org.gcube.accounting.exception.InvalidValueException;
|
||||
|
||||
/**
|
||||
|
@ -38,8 +40,10 @@ public class JobUsageRecord extends RawUsageRecord implements SingleUsageRecord
|
|||
public static final String JOB_START_TIME = "jobStartTime";
|
||||
@Required @ValidLong
|
||||
public static final String JOB_END_TIME = "jobEndTime";
|
||||
@NotEmpty
|
||||
|
||||
@DeprecatedField(action=ConvertToOperationResult.class) @NotEmpty //TODO
|
||||
protected static final String JOB_STATUS = "jobStatus";
|
||||
|
||||
@ValidInteger
|
||||
public static final String VMS_USED = "vmsUsed";
|
||||
@ValidLong
|
||||
|
|
|
@ -5,7 +5,11 @@ package org.gcube.accounting.datamodel.implementations;
|
|||
|
||||
import org.gcube.accounting.datamodel.RawUsageRecord;
|
||||
import org.gcube.accounting.datamodel.SingleUsageRecord;
|
||||
import org.gcube.accounting.datamodel.annotations.DeprecatedField;
|
||||
import org.gcube.accounting.datamodel.annotations.NotEmpty;
|
||||
import org.gcube.accounting.datamodel.annotations.NotEmptyIfNotNull;
|
||||
import org.gcube.accounting.datamodel.annotations.Required;
|
||||
import org.gcube.accounting.datamodel.validators.Ignore;
|
||||
import org.gcube.accounting.exception.InvalidValueException;
|
||||
|
||||
/**
|
||||
|
@ -19,24 +23,37 @@ public class PortletUsageRecord extends RawUsageRecord implements SingleUsageRec
|
|||
*/
|
||||
private static final long serialVersionUID = 8339324883678974869L;
|
||||
|
||||
@NotEmpty
|
||||
public static final String USER_ID = "userId";
|
||||
@NotEmpty
|
||||
@DeprecatedField(action=Ignore.class) @NotEmptyIfNotNull
|
||||
protected static final String USER_ID = "userId";
|
||||
|
||||
@Required @NotEmpty
|
||||
public static final String PORTLET_ID = "portletId";
|
||||
@Required @NotEmpty
|
||||
public static final String OPERATION_ID = "operationId";
|
||||
// TODO More
|
||||
|
||||
public PortletUsageRecord(){
|
||||
super();
|
||||
}
|
||||
|
||||
|
||||
@Deprecated
|
||||
public String getUserId() {
|
||||
return (String) this.resourceSpecificProperties.get(USER_ID);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public void setUserId(String userId) throws InvalidValueException {
|
||||
setResourceSpecificProperty(USER_ID, userId);
|
||||
}
|
||||
|
||||
public String getPortletId() {
|
||||
return (String) this.resourceSpecificProperties.get(PORTLET_ID);
|
||||
}
|
||||
|
||||
public void setPortletId(String portletId) throws InvalidValueException {
|
||||
setResourceSpecificProperty(PORTLET_ID, portletId);
|
||||
}
|
||||
|
||||
public String getOperationId() {
|
||||
return (String) this.resourceSpecificProperties.get(OPERATION_ID);
|
||||
}
|
||||
|
|
|
@ -5,9 +5,12 @@ package org.gcube.accounting.datamodel.implementations;
|
|||
|
||||
import org.gcube.accounting.datamodel.RawUsageRecord;
|
||||
import org.gcube.accounting.datamodel.SingleUsageRecord;
|
||||
import org.gcube.accounting.datamodel.annotations.DeprecatedField;
|
||||
import org.gcube.accounting.datamodel.annotations.NotEmpty;
|
||||
import org.gcube.accounting.datamodel.annotations.Required;
|
||||
import org.gcube.accounting.datamodel.annotations.ValidIP;
|
||||
import org.gcube.accounting.datamodel.annotations.ValidInteger;
|
||||
import org.gcube.accounting.datamodel.validators.Ignore;
|
||||
import org.gcube.accounting.exception.InvalidValueException;
|
||||
|
||||
/**
|
||||
|
@ -23,20 +26,18 @@ public class ServiceUsageRecord extends RawUsageRecord implements SingleUsageRec
|
|||
|
||||
@ValidIP
|
||||
public static final String CALLER_IP = "callerIP";
|
||||
@NotEmpty
|
||||
@Required @NotEmpty
|
||||
public static final String CALLER_SCOPE = "callerScope";
|
||||
@NotEmpty
|
||||
@Required @NotEmpty
|
||||
public static final String REF_HOST = "refHost";
|
||||
@NotEmpty
|
||||
@Required @NotEmpty
|
||||
public static final String REF_VM = "refVM";
|
||||
@NotEmpty
|
||||
@DeprecatedField(action=Ignore.class) @NotEmpty
|
||||
public static final String DOMAIN = "domain";
|
||||
//@Aggregated
|
||||
@ValidInteger
|
||||
public static final String INVOCATION_COUNT = "invocationCount";
|
||||
//@Aggregated
|
||||
protected static final String INVOCATION_COUNT = "invocationCount";
|
||||
@ValidInteger
|
||||
public static final String AVERAGE_INVOCATION_COUNT = "averageInvocationTime";
|
||||
protected static final String AVERAGE_INVOCATION_COUNT = "averageInvocationTime";
|
||||
@NotEmpty
|
||||
public static final String SERVICE_CLASS = "serviceClass";
|
||||
@NotEmpty
|
||||
|
@ -78,11 +79,11 @@ public class ServiceUsageRecord extends RawUsageRecord implements SingleUsageRec
|
|||
setResourceSpecificProperty(REF_VM, refVM);
|
||||
}
|
||||
|
||||
public String getDomain() {
|
||||
protected String getDomain() {
|
||||
return (String) this.resourceSpecificProperties.get(DOMAIN);
|
||||
}
|
||||
|
||||
public void setDomain(String domain) throws InvalidValueException {
|
||||
protected void setDomain(String domain) throws InvalidValueException {
|
||||
setResourceSpecificProperty(DOMAIN, domain);
|
||||
}
|
||||
|
||||
|
|
|
@ -3,20 +3,21 @@
|
|||
*/
|
||||
package org.gcube.accounting.datamodel.implementations;
|
||||
|
||||
import org.gcube.accounting.datamodel.RawUsageRecord;
|
||||
import org.gcube.accounting.datamodel.SingleUsageRecord;
|
||||
import org.gcube.accounting.datamodel.annotations.NotEmpty;
|
||||
import org.gcube.accounting.datamodel.annotations.NotEmptyIfNotNull;
|
||||
import org.gcube.accounting.datamodel.annotations.Required;
|
||||
import org.gcube.accounting.datamodel.annotations.ValidIP;
|
||||
import org.gcube.accounting.datamodel.annotations.ValidInteger;
|
||||
import org.gcube.accounting.datamodel.annotations.ValidLong;
|
||||
import org.gcube.accounting.datamodel.implementations.aggregated.StorageStatusUsageRecord;
|
||||
import org.gcube.accounting.exception.InvalidValueException;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
|
||||
*
|
||||
*/
|
||||
public class StorageUsageUsageRecord extends StorageStatusUsageRecord implements SingleUsageRecord {
|
||||
public class StorageUsageUsageRecord extends RawUsageRecord implements SingleUsageRecord {
|
||||
|
||||
/**
|
||||
* Generated Serial Version UID
|
||||
|
@ -24,13 +25,11 @@ public class StorageUsageUsageRecord extends StorageStatusUsageRecord implements
|
|||
private static final long serialVersionUID = 1381025822586583326L;
|
||||
|
||||
@NotEmptyIfNotNull
|
||||
public static final String OBJECT_URI = "objectURI";
|
||||
@NotEmpty
|
||||
public static final String OPERATION_TYPE = "operationType";
|
||||
@ValidIP
|
||||
public static final String CALLER_IP = "callerIP";
|
||||
@NotEmpty
|
||||
public static final String PROVIDER_ID = "providerId";
|
||||
@NotEmptyIfNotNull
|
||||
public static final String OBJECT_URI = "objectURI";
|
||||
@Required @NotEmpty
|
||||
public static final String OPERATION_TYPE = "operationType";
|
||||
@NotEmpty
|
||||
public static final String QUALIFIER = "qualifier";
|
||||
@NotEmpty
|
||||
|
@ -39,7 +38,8 @@ public class StorageUsageUsageRecord extends StorageStatusUsageRecord implements
|
|||
public static final String DATA_VOLUME = "dataVolume";
|
||||
@ValidInteger
|
||||
public static final String DATA_COUNT = "dataCount";
|
||||
|
||||
@ValidIP
|
||||
public static final String CALLER_IP = "callerIP";
|
||||
|
||||
public StorageUsageUsageRecord(){
|
||||
super();
|
||||
|
|
|
@ -7,9 +7,13 @@ import java.util.Calendar;
|
|||
|
||||
import org.gcube.accounting.datamodel.RawUsageRecord;
|
||||
import org.gcube.accounting.datamodel.SingleUsageRecord;
|
||||
import org.gcube.accounting.datamodel.annotations.DeprecatedField;
|
||||
import org.gcube.accounting.datamodel.annotations.NotEmpty;
|
||||
import org.gcube.accounting.datamodel.annotations.Required;
|
||||
import org.gcube.accounting.datamodel.annotations.ValidInteger;
|
||||
import org.gcube.accounting.datamodel.annotations.ValidLong;
|
||||
import org.gcube.accounting.datamodel.validators.ConvertToOperationResult;
|
||||
import org.gcube.accounting.datamodel.validators.Ignore;
|
||||
import org.gcube.accounting.exception.InvalidValueException;
|
||||
|
||||
/**
|
||||
|
@ -27,19 +31,19 @@ public class TaskUsageRecord extends RawUsageRecord implements SingleUsageRecord
|
|||
completed, failed
|
||||
};
|
||||
|
||||
@NotEmpty
|
||||
@Required @NotEmpty
|
||||
public static final String JOB_ID = "jobId";
|
||||
@NotEmpty
|
||||
@Required @NotEmpty
|
||||
public static final String REF_HOST = "refHost";
|
||||
@NotEmpty
|
||||
@Required @NotEmpty
|
||||
public static final String REF_VM = "refVM";
|
||||
@NotEmpty
|
||||
public static final String DOMAIN = "domain";
|
||||
@ValidLong
|
||||
@DeprecatedField(action=Ignore.class)
|
||||
protected static final String DOMAIN = "domain";
|
||||
@Required @ValidLong
|
||||
public static final String USAGE_START_TIME = "usageStartTime";
|
||||
@ValidLong
|
||||
@Required @ValidLong
|
||||
public static final String USAGE_END_TIME = "usageEndTime";
|
||||
@NotEmpty
|
||||
@DeprecatedField(action=ConvertToOperationResult.class) @NotEmpty
|
||||
public static final String USAGE_PHASE = "usagePhase";
|
||||
@ValidInteger
|
||||
public static final String INPUT_FILES_NUMBER = "inputFilesNumber";
|
||||
|
@ -94,13 +98,15 @@ public class TaskUsageRecord extends RawUsageRecord implements SingleUsageRecord
|
|||
setResourceSpecificProperty(REF_VM, refVM);
|
||||
}
|
||||
|
||||
public String getDomain() {
|
||||
/*
|
||||
private String getDomain() {
|
||||
return (String) this.resourceSpecificProperties.get(DOMAIN);
|
||||
}
|
||||
|
||||
public void setDomain(String domain) throws InvalidValueException {
|
||||
private void setDomain(String domain) throws InvalidValueException {
|
||||
setResourceSpecificProperty(DOMAIN, domain);
|
||||
}
|
||||
*/
|
||||
|
||||
public Calendar getUsageStartTime() {
|
||||
long millis = (Long) this.resourceSpecificProperties.get(USAGE_START_TIME);
|
||||
|
|
|
@ -0,0 +1,26 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
package org.gcube.accounting.datamodel.validators;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.gcube.accounting.datamodel.UsageRecord;
|
||||
import org.gcube.accounting.datamodel.annotations.DeprecationAction;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
|
||||
*
|
||||
*/
|
||||
public class ConvertToOperationResult implements DeprecationAction {
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public Serializable manage(String key, Serializable manage, UsageRecord usageRecord) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
|
@ -4,21 +4,20 @@
|
|||
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/
|
||||
*
|
||||
*/
|
||||
public interface FieldValidator<T extends Annotation> {
|
||||
|
||||
Class<T> annotation();
|
||||
|
||||
public boolean isValid(Serializable toValidate);
|
||||
public interface FieldValidator {
|
||||
|
||||
/**
|
||||
* Validate (and convert if needed) the provided property
|
||||
* @param toValidate the property to validate
|
||||
* @return the validated (and converted property)
|
||||
* @throws InvalidValueException if the validation (or conversion) fails
|
||||
*/
|
||||
public Serializable validate(Serializable toValidate) throws InvalidValueException;
|
||||
|
||||
String getErrorSuffix();
|
||||
}
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
package org.gcube.accounting.datamodel.validators;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.gcube.accounting.datamodel.UsageRecord;
|
||||
import org.gcube.accounting.datamodel.annotations.DeprecationAction;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
|
||||
*
|
||||
*/
|
||||
public class Ignore implements DeprecationAction {
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(Ignore.class);
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public Serializable manage(String key, Serializable manage, UsageRecord usageRecord) {
|
||||
logger.warn("The field {} is deprecated for the {}. Anyway the field will be included in the document",
|
||||
key, usageRecord.getClass().getSimpleName());
|
||||
return manage;
|
||||
}
|
||||
|
||||
}
|
|
@ -3,16 +3,9 @@ package org.gcube.accounting.datamodel.validators;
|
|||
import java.io.Serializable;
|
||||
import java.util.Map;
|
||||
|
||||
import org.gcube.accounting.datamodel.annotations.NotEmptyIfNotNull;
|
||||
import org.gcube.accounting.exception.InvalidValueException;
|
||||
public class NotEmptyIfNotNullValidator extends NotEmptyValidator {
|
||||
|
||||
public class NotEmptyIfNotNullValidator implements FieldValidator<NotEmptyIfNotNull>{
|
||||
|
||||
public Class<NotEmptyIfNotNull> annotation() {
|
||||
return NotEmptyIfNotNull.class;
|
||||
}
|
||||
|
||||
public boolean isValid(Serializable toValidate) {
|
||||
protected boolean isValid(Serializable toValidate) {
|
||||
if (toValidate == null) return true;
|
||||
if (toValidate.getClass().isArray() ){
|
||||
return ((Object[])toValidate).length>0;
|
||||
|
@ -25,19 +18,4 @@ public class NotEmptyIfNotNullValidator implements FieldValidator<NotEmptyIfNotN
|
|||
} 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";
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -3,16 +3,13 @@ package org.gcube.accounting.datamodel.validators;
|
|||
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>{
|
||||
|
||||
public Class<NotEmpty> annotation() {
|
||||
return NotEmpty.class;
|
||||
}
|
||||
|
||||
public boolean isValid(Serializable toValidate) {
|
||||
public class NotEmptyValidator implements FieldValidator{
|
||||
|
||||
private static final String ERROR = "Is Empty";
|
||||
|
||||
protected boolean isValid(Serializable toValidate) {
|
||||
if (toValidate == null) return false;
|
||||
if (toValidate.getClass().isArray() ){
|
||||
return ((Object[])toValidate).length>0;
|
||||
|
@ -25,19 +22,16 @@ public class NotEmptyValidator implements FieldValidator<NotEmpty>{
|
|||
} else return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
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(ERROR, e.getCause());
|
||||
}
|
||||
throw new InvalidValueException(getErrorSuffix());
|
||||
}
|
||||
|
||||
public String getErrorSuffix() {
|
||||
return "is empty";
|
||||
throw new InvalidValueException(ERROR);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -2,29 +2,18 @@ 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>{
|
||||
|
||||
public Class<NotNull> annotation() {
|
||||
return NotNull.class;
|
||||
}
|
||||
|
||||
public boolean isValid(Serializable toValidate) {
|
||||
return toValidate!=null;
|
||||
}
|
||||
public class NotNullValidator implements FieldValidator {
|
||||
|
||||
private static final String ERROR = "Is Null";
|
||||
|
||||
@Override
|
||||
public Serializable validate(Serializable toValidate) throws InvalidValueException {
|
||||
if(isValid(toValidate)){
|
||||
if(toValidate!=null){
|
||||
return toValidate;
|
||||
}
|
||||
throw new InvalidValueException(getErrorSuffix());
|
||||
}
|
||||
|
||||
public String getErrorSuffix() {
|
||||
return "is null";
|
||||
throw new InvalidValueException(ERROR);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -4,19 +4,16 @@ import java.io.Serializable;
|
|||
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;
|
||||
|
||||
public class ValidIPValidator implements FieldValidator<ValidLong>{
|
||||
public class ValidIPValidator implements FieldValidator{
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(ValidIPValidator.class);
|
||||
|
||||
public Class<ValidLong> annotation() {
|
||||
return ValidLong.class;
|
||||
}
|
||||
|
||||
private static final String ERROR = "Not valid IP Address";
|
||||
|
||||
private static Pattern IPV4_PATTERN = null;
|
||||
private static Pattern IPV6_PATTERN = null;
|
||||
|
||||
|
@ -44,28 +41,16 @@ public class ValidIPValidator implements FieldValidator<ValidLong>{
|
|||
return ipV6Matcher.matches();
|
||||
}
|
||||
|
||||
public boolean isValid(Serializable toValidate) {
|
||||
try {
|
||||
validate(toValidate);
|
||||
} catch (InvalidValueException e) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
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(ERROR, e.getCause());
|
||||
}
|
||||
throw new InvalidValueException(getErrorSuffix());
|
||||
}
|
||||
|
||||
public String getErrorSuffix() {
|
||||
return "not valid IP Address";
|
||||
throw new InvalidValueException(ERROR);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -2,25 +2,14 @@ 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>{
|
||||
|
||||
public Class<ValidLong> annotation() {
|
||||
return ValidLong.class;
|
||||
}
|
||||
|
||||
public boolean isValid(Serializable toValidate) {
|
||||
try {
|
||||
validate(toValidate);
|
||||
} catch (InvalidValueException e) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public class ValidIntegerValidator implements FieldValidator {
|
||||
|
||||
private static final String ERROR = String.format("Not Instance of %s", Integer.class.getSimpleName());
|
||||
|
||||
@Override
|
||||
public Serializable validate(Serializable toValidate) throws InvalidValueException {
|
||||
if(toValidate instanceof Integer){
|
||||
return toValidate;
|
||||
|
@ -30,11 +19,7 @@ public class ValidIntegerValidator implements FieldValidator<ValidLong>{
|
|||
return integerObj;
|
||||
}
|
||||
|
||||
throw new InvalidValueException(getErrorSuffix());
|
||||
}
|
||||
|
||||
public String getErrorSuffix() {
|
||||
return String.format("not instace of %s", Integer.class.getSimpleName());
|
||||
throw new InvalidValueException(ERROR);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -2,38 +2,27 @@ 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>{
|
||||
public class ValidLongValidator implements FieldValidator {
|
||||
|
||||
public Class<ValidLong> annotation() {
|
||||
return ValidLong.class;
|
||||
}
|
||||
|
||||
public boolean isValid(Serializable toValidate) {
|
||||
try {
|
||||
validate(toValidate);
|
||||
} catch (InvalidValueException e) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
private static final String ERROR = String.format("Not Instance of %s", Integer.class.getSimpleName());
|
||||
|
||||
@Override
|
||||
public Serializable validate(Serializable toValidate) throws InvalidValueException {
|
||||
if(toValidate instanceof Long){
|
||||
return toValidate;
|
||||
}
|
||||
Long longObj = Long.getLong((String) toValidate);
|
||||
if(longObj!=null){
|
||||
return longObj;
|
||||
try {
|
||||
Long longObj = Long.getLong((String) toValidate);
|
||||
if(longObj!=null){
|
||||
return longObj;
|
||||
}
|
||||
}catch (Exception e) {
|
||||
throw new InvalidValueException(ERROR, e.getCause());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public String getErrorSuffix() {
|
||||
return String.format("not instace of %s", Long.class.getSimpleName());
|
||||
throw new InvalidValueException(ERROR);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -3,38 +3,49 @@ package org.gcube.accounting.datamodel.validators;
|
|||
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>{
|
||||
public class ValidOperationResultValidator implements FieldValidator {
|
||||
|
||||
public Class<ValidOperationResult> annotation() {
|
||||
return ValidOperationResult.class;
|
||||
}
|
||||
|
||||
public boolean isValid(Serializable toValidate) {
|
||||
try {
|
||||
validate(toValidate);
|
||||
} catch (InvalidValueException e) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
private static final String ERROR = String.format("Not Instance of %s", OperationResult.class.getSimpleName());
|
||||
|
||||
@Override
|
||||
public Serializable validate(Serializable toValidate) throws InvalidValueException {
|
||||
if(toValidate instanceof OperationResult){
|
||||
return toValidate;
|
||||
}
|
||||
Integer integerObj = Integer.getInteger((String) toValidate);
|
||||
if(integerObj!=null){
|
||||
return integerObj;
|
||||
|
||||
try {
|
||||
if(toValidate instanceof String){
|
||||
try{
|
||||
OperationResult operationResult = OperationResult.valueOf((String) toValidate);
|
||||
if(operationResult !=null){
|
||||
return operationResult;
|
||||
}
|
||||
} catch(Exception e){
|
||||
// Trying another way
|
||||
}
|
||||
|
||||
try{
|
||||
Integer integer = Integer.getInteger((String) toValidate);
|
||||
if(integer!=null){
|
||||
toValidate = integer;
|
||||
}
|
||||
} catch(Exception e){
|
||||
// Trying another way
|
||||
}
|
||||
}
|
||||
|
||||
if(toValidate instanceof Integer){
|
||||
return OperationResult.values()[(Integer) toValidate];
|
||||
}
|
||||
|
||||
}catch(Exception e){
|
||||
throw new InvalidValueException(ERROR, e.getCause());
|
||||
}
|
||||
|
||||
throw new InvalidValueException(getErrorSuffix());
|
||||
}
|
||||
|
||||
public String getErrorSuffix() {
|
||||
return String.format("not instace of %s", OperationResult.class.getSimpleName());
|
||||
throw new InvalidValueException(ERROR);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,29 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
package org.gcube.accounting.exception;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
|
||||
*
|
||||
*/
|
||||
public class ExcludeException extends Exception {
|
||||
|
||||
/**
|
||||
* Generated serial Version UID
|
||||
*/
|
||||
private static final long serialVersionUID = -4512534842977294L;
|
||||
|
||||
public ExcludeException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public ExcludeException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public ExcludeException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
}
|
|
@ -22,4 +22,7 @@ public class InternallyReservedKeywordException extends Exception {
|
|||
super(message);
|
||||
}
|
||||
|
||||
public InternallyReservedKeywordException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
*/
|
||||
package org.gcube.accounting.datamodel.validators;
|
||||
|
||||
import org.gcube.accounting.exception.InvalidValueException;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
|
@ -12,17 +13,23 @@ import org.junit.Test;
|
|||
public class ValidTimeValidatorTest {
|
||||
|
||||
@Test
|
||||
public void testPrimitiveLong(){
|
||||
public void testPrimitiveLong() throws InvalidValueException{
|
||||
ValidLongValidator validTimeValidator = new ValidLongValidator();
|
||||
long myLong = 4;
|
||||
validTimeValidator.isValid(myLong);
|
||||
validTimeValidator.validate(myLong);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClassLong(){
|
||||
public void testClassLong() throws InvalidValueException{
|
||||
ValidLongValidator validTimeValidator = new ValidLongValidator();
|
||||
Long myLong = new Long(4);
|
||||
validTimeValidator.isValid(myLong);
|
||||
validTimeValidator.validate(myLong);
|
||||
}
|
||||
|
||||
@Test(expected=InvalidValueException.class)
|
||||
public void testWrongValue() throws InvalidValueException {
|
||||
ValidLongValidator validTimeValidator = new ValidLongValidator();
|
||||
validTimeValidator.validate("test");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue