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
This commit is contained in:
parent
2e4c076bd2
commit
3aadd103b2
|
@ -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";
|
||||
|
||||
/**
|
||||
|
|
|
@ -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 {
|
||||
|
||||
}
|
|
@ -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<RegexReplace> regexReplaceList;
|
||||
|
||||
public static List<RegexReplace> 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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue