Search in sources :

Example 1 with TemplateInstance

use of io.quarkus.qute.TemplateInstance 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)

Example 2 with TemplateInstance

use of io.quarkus.qute.TemplateInstance in project jbang by jbangdev.

the class Init method renderQuteTemplate.

void renderQuteTemplate(Path outFile, String templatePath, Map<String, Object> properties) throws IOException {
    Template template = TemplateEngine.instance().getTemplate(templatePath);
    if (template == null) {
        throw new ExitException(EXIT_INVALID_INPUT, "Could not find or load template: " + templatePath);
    }
    if (outFile.toString().endsWith(".java")) {
        String basename = Util.getBaseName(outFile.getFileName().toString());
        if (!SourceVersion.isIdentifier(basename)) {
            throw new ExitException(EXIT_INVALID_INPUT, "'" + basename + "' is not a valid class name in java. Remove the special characters");
        }
    }
    Files.createDirectories(outFile.getParent());
    try (BufferedWriter writer = Files.newBufferedWriter(outFile)) {
        TemplateInstance templateWithData = template.instance();
        properties.forEach(templateWithData::data);
        String result = templateWithData.render();
        writer.write(result);
        outFile.toFile().setExecutable(true);
    }
}
Also used : Template(io.quarkus.qute.Template) BufferedWriter(java.io.BufferedWriter) TemplateInstance(io.quarkus.qute.TemplateInstance)

Example 3 with TemplateInstance

use of io.quarkus.qute.TemplateInstance in project automatiko-engine by automatiko-io.

the class UserTaskManagementResource method get.

@APIResponses(value = { @APIResponse(responseCode = "404", description = "In case of instance with given id was not found", content = @Content(mediaType = "text/html")), @APIResponse(responseCode = "200", description = "List of available processes", content = @Content(mediaType = "text/html")) })
@Operation(summary = "Retrives user task form for given user task", operationId = "getUserTaskForm")
@SuppressWarnings("unchecked")
@GET
@Path("{processId}/{pInstanceId}/{taskId}")
@Produces(MediaType.TEXT_HTML)
public TemplateInstance get(@Context UriInfo uriInfo, @Parameter(description = "Unique identifier of the process", required = true) @PathParam("processId") String processId, @Parameter(description = "Unique identifier of the instance", required = true) @PathParam("pInstanceId") String pInstanceId, @Parameter(description = "Unique identifier of the task", required = true) @PathParam("taskId") String taskId, @Parameter(description = "User identifier as alternative autroization info", required = false, hidden = true) @QueryParam("user") final String user, @Parameter(description = "Groups as alternative autroization info", required = false, hidden = true) @QueryParam("group") final List<String> groups) {
    IdentityProvider identityProvider = identitySupplier.buildIdentityProvider(user, groups);
    try {
        Process<?> process = processData.get(processId);
        if (process == null) {
            return getTemplate("process-not-found", PROCESS_INSTANCE_NOT_FOUND).instance().data("instanceId", pInstanceId);
        }
        Optional<ProcessInstance<?>> instance = (Optional<ProcessInstance<?>>) process.instances().findById(pInstanceId, ProcessInstanceReadMode.READ_ONLY);
        if (instance.isEmpty()) {
            return getTemplate("process-instance-not-found", PROCESS_INSTANCE_NOT_FOUND).instance().data("instanceId", pInstanceId);
        }
        ProcessInstance<?> pi = instance.get();
        WorkItem task = pi.workItem(taskId, SecurityPolicy.of(identityProvider));
        Template template = getTemplate(process.id(), task);
        if (template == null) {
            template = engine.getTemplate(DEFAULT_TEMPLATE);
        }
        String link = task.getReferenceId() + "?redirect=true";
        if (user != null && !user.isEmpty()) {
            link += "&user=" + user;
        }
        if (groups != null && !groups.isEmpty()) {
            for (String group : groups) {
                link += "&group=" + group;
            }
        }
        TemplateInstance templateInstance = template.data("task", task).data("url", new RawString(link));
        templateInstance.data("inputs", process.taskInputs(task.getId(), task.getName(), task.getParameters()));
        Map<String, Object> results = task.getResults();
        task.getParameters().entrySet().stream().forEach(e -> results.putIfAbsent(e.getKey(), e.getValue()));
        templateInstance.data("results", process.taskOutputs(task.getId(), task.getName(), results));
        return templateInstance;
    } catch (WorkItemNotFoundException e) {
        return getTemplate("task-not-found", TASK_INSTANCE_NOT_FOUND).instance().data("taskId", taskId);
    } finally {
        IdentityProvider.set(null);
    }
}
Also used : Optional(java.util.Optional) RawString(io.quarkus.qute.RawString) IdentityProvider(io.automatiko.engine.api.auth.IdentityProvider) RawString(io.quarkus.qute.RawString) WorkItem(io.automatiko.engine.api.workflow.WorkItem) Template(io.quarkus.qute.Template) TemplateInstance(io.quarkus.qute.TemplateInstance) WorkItemNotFoundException(io.automatiko.engine.api.runtime.process.WorkItemNotFoundException) ProcessInstance(io.automatiko.engine.api.workflow.ProcessInstance) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) APIResponses(org.eclipse.microprofile.openapi.annotations.responses.APIResponses) Operation(org.eclipse.microprofile.openapi.annotations.Operation)

Aggregations

Template (io.quarkus.qute.Template)3 TemplateInstance (io.quarkus.qute.TemplateInstance)3 IdentityProvider (io.automatiko.engine.api.auth.IdentityProvider)1 WorkItemNotFoundException (io.automatiko.engine.api.runtime.process.WorkItemNotFoundException)1 ProcessInstance (io.automatiko.engine.api.workflow.ProcessInstance)1 WorkItem (io.automatiko.engine.api.workflow.WorkItem)1 Engine (io.quarkus.qute.Engine)1 RawString (io.quarkus.qute.RawString)1 TemplateException (io.quarkus.qute.TemplateException)1 BufferedWriter (java.io.BufferedWriter)1 Optional (java.util.Optional)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 Operation (org.eclipse.microprofile.openapi.annotations.Operation)1 APIResponses (org.eclipse.microprofile.openapi.annotations.responses.APIResponses)1