From 3aadd103b2e1a68ead06acbdee7b881d193acb02 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Thu, 14 Dec 2017 10:09:58 +0000 Subject: [PATCH] Refs #10646: Add the posisbility to rewrite the calledMethod which match a regular expression Task-Url: https://support.d4science.org/issues/10646 git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/accounting/accounting-lib@160493 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../basetypes/AbstractServiceUsageRecord.java | 3 +- .../annotations/CalledMethodRegexReplace.java | 19 ++++ .../CalledMethodRegexReplaceValidator.java | 86 +++++++++++++++++++ .../validations/validators/RegexReplace.java | 44 ++++++++++ 4 files changed, 151 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/gcube/accounting/datamodel/validations/annotations/CalledMethodRegexReplace.java create mode 100644 src/main/java/org/gcube/accounting/datamodel/validations/validators/CalledMethodRegexReplaceValidator.java create mode 100644 src/main/java/org/gcube/accounting/datamodel/validations/validators/RegexReplace.java diff --git a/src/main/java/org/gcube/accounting/datamodel/basetypes/AbstractServiceUsageRecord.java b/src/main/java/org/gcube/accounting/datamodel/basetypes/AbstractServiceUsageRecord.java index 8e1ff21..8f2e5fd 100644 --- a/src/main/java/org/gcube/accounting/datamodel/basetypes/AbstractServiceUsageRecord.java +++ b/src/main/java/org/gcube/accounting/datamodel/basetypes/AbstractServiceUsageRecord.java @@ -7,6 +7,7 @@ import java.io.Serializable; import java.util.Map; import org.gcube.accounting.datamodel.BasicUsageRecord; +import org.gcube.accounting.datamodel.validations.annotations.CalledMethodRegexReplace; import org.gcube.documentstore.exception.InvalidValueException; import org.gcube.documentstore.records.implementation.RequiredField; import org.gcube.documentstore.records.implementation.validations.annotations.NotEmpty; @@ -52,7 +53,7 @@ public abstract class AbstractServiceUsageRecord extends BasicUsageRecord { /** * KEY for : Called Method */ - @RequiredField @NotEmpty + @RequiredField @NotEmpty @CalledMethodRegexReplace public static final String CALLED_METHOD = "calledMethod"; /** diff --git a/src/main/java/org/gcube/accounting/datamodel/validations/annotations/CalledMethodRegexReplace.java b/src/main/java/org/gcube/accounting/datamodel/validations/annotations/CalledMethodRegexReplace.java new file mode 100644 index 0000000..c8fb9b7 --- /dev/null +++ b/src/main/java/org/gcube/accounting/datamodel/validations/annotations/CalledMethodRegexReplace.java @@ -0,0 +1,19 @@ +package org.gcube.accounting.datamodel.validations.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.gcube.accounting.datamodel.validations.validators.CalledMethodRegexReplaceValidator; +import org.gcube.documentstore.records.implementation.FieldDecorator; + +/** + * @author Luca Frosini (ISTI - CNR) + */ +@Target(ElementType.FIELD) +@Retention(RetentionPolicy.RUNTIME) +@FieldDecorator(action=CalledMethodRegexReplaceValidator.class) +public @interface CalledMethodRegexReplace { + +} diff --git a/src/main/java/org/gcube/accounting/datamodel/validations/validators/CalledMethodRegexReplaceValidator.java b/src/main/java/org/gcube/accounting/datamodel/validations/validators/CalledMethodRegexReplaceValidator.java new file mode 100644 index 0000000..63a10ce --- /dev/null +++ b/src/main/java/org/gcube/accounting/datamodel/validations/validators/CalledMethodRegexReplaceValidator.java @@ -0,0 +1,86 @@ +package org.gcube.accounting.datamodel.validations.validators; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Matcher; + +import org.gcube.accounting.datamodel.basetypes.AbstractServiceUsageRecord; +import org.gcube.documentstore.exception.InvalidValueException; +import org.gcube.documentstore.records.Record; +import org.gcube.documentstore.records.implementation.FieldAction; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * @author Luca Frosini (ISTI - CNR) + */ +public class CalledMethodRegexReplaceValidator implements FieldAction { + + private static Logger logger = LoggerFactory.getLogger(CalledMethodRegexReplaceValidator.class); + + protected static final List regexReplaceList; + + public static List getRegexReplacelist() { + return regexReplaceList; + } + + public static RegexReplace addRegexReplace(String serviceClass, String serviceName, String regex, String replace) { + RegexReplace regexReplace = new RegexReplace(serviceClass, serviceName, regex, replace); + regexReplaceList.add(regexReplace); + return regexReplace; + } + + public static RegexReplace addRegexReplace(RegexReplace regexReplace) { + regexReplaceList.add(regexReplace); + return regexReplace; + } + + static { + regexReplaceList = new ArrayList<>(); + } + + /** + * {@inheritDoc} + */ + @Override + public Serializable validate(String key, Serializable value, Record record) throws InvalidValueException { + + if(!(record instanceof AbstractServiceUsageRecord)) { + throw new RuntimeException(record.toString() + "is not an instace of " + AbstractServiceUsageRecord.class.getSimpleName()); + } + + if(!(value instanceof String)) { + throw new InvalidValueException(value.toString() + "is not a " + String.class.getSimpleName()); + } + + String stringValue = (String) value; + + AbstractServiceUsageRecord serviceUsageRecord = (AbstractServiceUsageRecord) record; + + String serviceClass = serviceUsageRecord.getServiceClass(); + if(serviceClass==null) { + logger.debug("{} is not already set. The check will be postponed to validation phase", AbstractServiceUsageRecord.SERVICE_CLASS); + return value; + } + + String serviceName = serviceUsageRecord.getServiceName(); + if(serviceName==null) { + logger.debug("{} is not already set. The check will be postponed to validation phase", AbstractServiceUsageRecord.SERVICE_NAME); + return value; + } + + for(RegexReplace regexReplace : getRegexReplacelist()) { + if(serviceClass.compareTo(regexReplace.getServiceClass())==0 && serviceName.compareTo(regexReplace.getServiceName())==0) { + Matcher matcher = regexReplace.regexPattern.matcher(stringValue); + if(matcher.matches()) { + // TODO allow regex replace using matcher + return regexReplace.getReplace(); + } + } + } + + return value; + } + +} diff --git a/src/main/java/org/gcube/accounting/datamodel/validations/validators/RegexReplace.java b/src/main/java/org/gcube/accounting/datamodel/validations/validators/RegexReplace.java new file mode 100644 index 0000000..698cdc3 --- /dev/null +++ b/src/main/java/org/gcube/accounting/datamodel/validations/validators/RegexReplace.java @@ -0,0 +1,44 @@ +package org.gcube.accounting.datamodel.validations.validators; + +import java.util.regex.Pattern; + +public class RegexReplace { + + protected final String serviceClass; + protected final String serviceName; + + protected final String regex; + protected final Pattern regexPattern; + + protected final String replace; + + public RegexReplace(String serviceClass, String serviceName, String regex, String replace) { + super(); + this.serviceClass = serviceClass; + this.serviceName = serviceName; + this.regex = regex; + this.regexPattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE); + this.replace = replace; + } + + public String getServiceClass() { + return serviceClass; + } + + public String getServiceName() { + return serviceName; + } + + public String getRegex() { + return regex; + } + + public Pattern getRegexPattern() { + return regexPattern; + } + + public String getReplace() { + return replace; + } + +} \ No newline at end of file