package org.gcube.accounting.datamodel.validations.validators; import java.util.regex.Matcher; import org.gcube.documentstore.exception.InvalidValueException; import org.gcube.documentstore.records.DSMapper; import com.fasterxml.jackson.annotation.JsonSetter; public class MatcherReplace { protected MultiMatcher multiMatcher; protected Replace replacementRegex; public MatcherReplace() {} public MultiMatcher getMultiMatcher() { return multiMatcher; } @JsonSetter(value="match") public void setMultiMatcher(MultiMatcher multiMatcher) { this.multiMatcher = multiMatcher; } protected Replace getReplacementRegex() { return replacementRegex; } @JsonSetter(value="replace") public void setReplacementRegex(Replace replacementRegex) { this.replacementRegex = replacementRegex; } public Replace check(String serviceClass, String serviceName, String calledMethod) throws InvalidValueException { boolean matched = multiMatcher.match(serviceClass, serviceName, calledMethod); Replace replace; if(matched) { replace = new Replace(); Matcher serviceClassMatcher = multiMatcher.getServiceClassPattern().matcher(serviceClass); replace.setServiceClass(serviceClassMatcher.replaceFirst(replacementRegex.getServiceClass())); Matcher serviceNameMatcher = multiMatcher.getServiceClassPattern().matcher(serviceName); replace.setServiceName(serviceNameMatcher.replaceFirst(replacementRegex.getServiceName())); Matcher calledMethodMatcher = multiMatcher.getCalledMethodPattern().matcher(calledMethod); String cm = calledMethodMatcher.replaceFirst(replacementRegex.getCalledMethod()); replace.setCalledMethod(cm); }else { replace = null; } return replace; } @Override public String toString() { try { return DSMapper.getObjectMapper().writeValueAsString(this); } catch(Exception e) { return "MatcherReplace [multiMatcher=" + multiMatcher + ", replacer=" + replacementRegex + "]"; } } }