Search in sources :

Example 1 with TemplateException

use of io.quarkus.qute.TemplateException in project camel-quarkus by apache.

the class QuteEndpoint method onExchange.

@Override
protected void onExchange(Exchange exchange) throws Exception {
    String path = getResourceUri();
    ObjectHelper.notNull(path, "resourceUri");
    if (allowTemplateFromHeader) {
        String newResourceUri = exchange.getIn().getHeader(QuteConstants.QUTE_RESOURCE_URI, String.class);
        if (newResourceUri != null) {
            exchange.getIn().removeHeader(QuteConstants.QUTE_RESOURCE_URI);
            log.debug("{} set to {} creating new endpoint to handle exchange", QuteConstants.QUTE_RESOURCE_URI, newResourceUri);
            QuteEndpoint newEndpoint = findOrCreateEndpoint(getEndpointUri(), newResourceUri);
            newEndpoint.onExchange(exchange);
            return;
        }
    }
    String content = null;
    if (allowTemplateFromHeader) {
        content = exchange.getIn().getHeader(QuteConstants.QUTE_TEMPLATE, String.class);
        if (content != null) {
            // remove the header to avoid it being propagated in the routing
            exchange.getIn().removeHeader(QuteConstants.QUTE_TEMPLATE);
        }
    }
    TemplateInstance instance = null;
    if (allowTemplateFromHeader) {
        instance = exchange.getIn().getHeader(QuteConstants.QUTE_TEMPLATE_INSTANCE, TemplateInstance.class);
    }
    if (instance != null) {
        // use template instance from header
        if (log.isDebugEnabled()) {
            log.debug("Qute template instance is from header {} for endpoint {}", QuteConstants.QUTE_TEMPLATE_INSTANCE, getEndpointUri());
        }
        exchange.getIn().removeHeader(QuteConstants.QUTE_TEMPLATE_INSTANCE);
    } else {
        Template template;
        Engine engine = getQuteEngine();
        if (content == null) {
            template = engine.getTemplate(path);
            if (template == null) {
                throw new TemplateException("Unable to parse Qute template from path: " + path);
            }
        } else {
            // This is the first time to parse the content
            template = engine.parse(content);
            if (template == null) {
                throw new TemplateException("Unable to parse Qute template");
            }
        }
        instance = template.instance();
    }
    ExchangeHelper.createVariableMap(exchange, isAllowContextMapAll()).forEach(instance::data);
    Map<String, Object> map = exchange.getIn().getHeader(QuteConstants.QUTE_TEMPLATE_DATA, Map.class);
    if (map != null) {
        map.forEach(instance::data);
    }
    exchange.getMessage().setBody(instance.render().trim());
}
Also used : TemplateException(io.quarkus.qute.TemplateException) Engine(io.quarkus.qute.Engine) TemplateInstance(io.quarkus.qute.TemplateInstance) Template(io.quarkus.qute.Template)

Aggregations

Engine (io.quarkus.qute.Engine)1 Template (io.quarkus.qute.Template)1 TemplateException (io.quarkus.qute.TemplateException)1 TemplateInstance (io.quarkus.qute.TemplateInstance)1