protected void onExchange()

in extensions/qute/component/src/main/java/org/apache/camel/component/qute/QuteEndpoint.java [151:214]


    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());
    }