templates.evaluatorHeader.mustache Maven / Gradle / Ivy
The newest version!
/**
* Copyright (C) 2015 The Gravitee team (http://gravitee.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package {{packageName}};
import com.fasterxml.jackson.databind.ObjectMapper;
import io.gravitee.gateway.reactive.api.ExecutionFailure;
import io.gravitee.gateway.reactive.api.context.ExecutionContext;
import io.reactivex.rxjava3.core.Completable;
import io.reactivex.rxjava3.core.Flowable;
import io.reactivex.rxjava3.core.Maybe;
import io.reactivex.rxjava3.core.Single;
import jakarta.validation.ConstraintViolation;
import jakarta.validation.Validation;
import jakarta.validation.Validator;
import jakarta.validation.ValidatorFactory;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import org.hibernate.validator.HibernateValidator;
import org.hibernate.validator.messageinterpolation.ParameterMessageInterpolator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class {{evaluatorSimpleClassName}} {
private static final String FAILURE_CONFIGURATION_INVALID = "FAILURE_CONFIGURATION_INVALID";
private final Logger logger = LoggerFactory.getLogger({{evaluatorSimpleClassName}}.class);
private final ObjectMapper objectMapper = new ObjectMapper();
private final {{simpleClassName}} configuration;
private static final Validator validator;
private final String attributePrefix = "{{attributePrefix}}";
private final String internalId;
static {
ValidatorFactory factory = Validation.byProvider(HibernateValidator.class).configure()
.messageInterpolator(new ParameterMessageInterpolator()).buildValidatorFactory();
validator = factory.getValidator();
}
public {{evaluatorSimpleClassName}} (
{{simpleClassName}} configuration)
{
this.configuration = configuration;
this.internalId = UUID.randomUUID().toString();
}
// Utility methods
private String buildAttributeName(String attributePrefix, String name) {
return attributePrefix.concat(".").concat(name);
}
private Maybe evalStringProperty(String name, String value, String attributePrefix, ExecutionContext ctx) {
//First check attributes
String attribute = ctx.getAttribute(buildAttributeName(attributePrefix, name));
if (attribute != null) {
//If a value is found for this attribute, override the original value with it
value = attribute;
}
//If value is null, return empty
if(value == null) {
return Maybe.empty();
}
//Then check EL
String finalValue = value;
return ctx
.getTemplateEngine()
.eval(value, String.class)
.doOnError(throwable -> logger.error("Unable to evaluate property [{}] with expression [{}].", name, finalValue));
}
private > T evalEnumProperty(String name, T value, Class enumClass, String attributePrefix, ExecutionContext ctx) {
String attribute = ctx.getAttribute(buildAttributeName(attributePrefix, name));
if (attribute != null) {
return T.valueOf(enumClass, attribute);
}
return value;
}
private Integer evalIntegerProperty(String name, int value, String attributePrefix, ExecutionContext ctx) {
Integer attribute = ctx.getAttribute(buildAttributeName(attributePrefix, name));
if (attribute != null) {
return attribute;
}
return value;
}
private Long evalLongProperty(String name, long value, String attributePrefix, ExecutionContext ctx) {
Long attribute = ctx.getAttribute(buildAttributeName(attributePrefix, name));
if (attribute != null) {
return attribute;
}
return value;
}
private boolean evalBooleanProperty(String name, boolean value, String attributePrefix, ExecutionContext ctx) {
Object attribute = ctx.getAttribute(buildAttributeName(attributePrefix, name));
if (attribute != null) {
return (boolean) attribute;
}
return value;
}
private Set evalSetProperty(String name, Set value, String attributePrefix, ExecutionContext ctx) {
//Try to get a Set and if it fails try to get a String and split it
try {
Set attribute = ctx.getAttribute(buildAttributeName(attributePrefix, name));
if (attribute != null) {
return attribute;
}
} catch (ClassCastException cce) {
List attribute = ctx.getAttributeAsList(buildAttributeName(attributePrefix, name));
if(attribute != null) {
return Set.copyOf(attribute);
}
}
return value;
}
private List evalListProperty(String name, List value, String attributePrefix, ExecutionContext ctx) {
List attribute = ctx.getAttributeAsList(buildAttributeName(attributePrefix, name));
if (attribute != null) {
return attribute;
}
return value;
}
private void validateConfiguration(T configuration) {
Set> constraintViolations = validator.validate(configuration);
if (!constraintViolations.isEmpty()) {
StringBuilder exceptionMessage = new StringBuilder(constraintViolations.size() + " constraint violations found : ");
constraintViolations.forEach(violation ->
exceptionMessage
.append("[attribute[")
.append(violation.getPropertyPath())
.append("] reason[")
.append(violation.getMessage())
.append("]]")
);
//LOG
logger.error(exceptionMessage.toString());
//Throw exception with info
throw new IllegalStateException(exceptionMessage.toString());
}
}
/**
* Blocking eval method (see {@link #eval(ExecutionContext)} Eval})
* Caution when using this method if the evaluation involves EL expressions that trigger blocking fetches
* @param ctx the current context
* @return configuration with all dynamic configuration parameters updated
*/
public {{simpleClassName}} evalNow(ExecutionContext ctx) {
return eval(ctx).blockingGet();
}
/**
* Evaluates the configuration using the context to update parameters using attributes or EL
* and stores it as an internal attributes to avoid multiple evaluation
* @param ctx the current context
* @return configuration with all dynamic configuration parameters updated
*/
public Single<{{simpleClassName}}> eval(ExecutionContext ctx) {
//First check if the configuration has not been already evaluated
{{simpleClassName}} evaluatedConf = ctx.getInternalAttribute("{{evaluatedConfigurationName}}-"+this.internalId);
if(evaluatedConf != null) {
return Single.just(evaluatedConf);
}
{{simpleClassName}} evaluatedConfiguration;
try {
evaluatedConfiguration = objectMapper.readValue(objectMapper.writeValueAsString(configuration), {{simpleClassName}}.class);
} catch (com.fasterxml.jackson.core.JsonProcessingException e) {
logger.error("Unable to clone configuration", e);
return Single.error(e);
}
String currentAttributePrefix = attributePrefix;
List> toEval = new ArrayList<>();
© 2015 - 2025 Weber Informatics LLC | Privacy Policy