Skip to content

Commit

Permalink
[KOGITO-9555] Adding generic endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
fjtirado committed Jul 7, 2023
1 parent 4431aed commit 65ba3d8
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@ public void registerCloseable(AutoCloseable closeable) {
closeables.add(closeable);
}

public Optional<Process<JsonNodeModel>> findProcessById(String id) {
return Optional.ofNullable((Process<JsonNodeModel>) processes.processById(id));
}

private Optional<ProcessInstance<JsonNodeModel>> findProcessInstance(String id) {
for (Process<JsonNodeModel> process : processes.map.values()) {
Optional<ProcessInstance<JsonNodeModel>> pi = process.instances().findById(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-cache</artifactId>
</dependency>
<dependency>
<groupId>org.kie.kogito</groupId>
<artifactId>kogito-serverless-workflow-executor-core</artifactId>
</dependency>
<dependency>
<groupId>org.graalvm.nativeimage</groupId>
<artifactId>svm</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2023 Red Hat, Inc. and/or its affiliates.
*
* 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 org.kie.kogito.serverless.workflow;

import java.io.IOException;
import java.io.StringReader;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.kie.kogito.serverless.workflow.executor.StaticWorkflowApplication;
import org.kie.kogito.serverless.workflow.models.JsonNodeModelInput;
import org.kie.kogito.serverless.workflow.utils.ServerlessWorkflowUtils;
import org.kie.kogito.serverless.workflow.utils.WorkflowFormat;

@Path("/")
public class KogitoGenericResource {

private StaticWorkflowApplication application;

@PostConstruct
void init() {
application = StaticWorkflowApplication.create();
}

@PreDestroy
void cleanup() {
application.close();
}

@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Path("{id}")
public Response executeProcess(@PathParam("id") String processId, JsonNodeModelInput input) {
return Response.status(200).entity(
application.execute(application.findProcessById(processId).orElseThrow(() -> new IllegalArgumentException("Cannot find process id " + processId)), input.toModel())).build();
}

@POST
@Consumes(MediaType.TEXT_PLAIN)
@Path("{id}/upload")
public Response uploadProcess(@PathParam("id") String processId, String content) throws IOException {
application.process(ServerlessWorkflowUtils.getWorkflow(new StringReader(content), content.startsWith("{") ? WorkflowFormat.JSON : WorkflowFormat.YAML));
return Response.ok().build();
}
}

0 comments on commit 65ba3d8

Please sign in to comment.