-
Notifications
You must be signed in to change notification settings - Fork 209
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Adding new dynamic addon * [Fix #3615] Moving common stuff to common addons
- Loading branch information
Showing
26 changed files
with
983 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<!-- | ||
Licensed to the Apache Software Foundation (ASF) under one | ||
or more contributor license agreements. See the NOTICE file | ||
distributed with this work for additional information | ||
regarding copyright ownership. The ASF licenses this file | ||
to you 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. | ||
--> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>org.kie</groupId> | ||
<artifactId>kogito-addons-common-parent</artifactId> | ||
<version>999-SNAPSHOT</version> | ||
</parent> | ||
<artifactId>kie-addons-process-dynamic</artifactId> | ||
<name>KIE Add-On Process Instance Dynamic Calls</name> | ||
<description>Allow performing dynamic calls for existing process instances</description> | ||
<properties> | ||
<java.module.name>org.kie.process.dynamic</java.module.name> | ||
</properties> | ||
<dependencies> | ||
|
||
<dependency> | ||
<groupId>org.kie.kogito</groupId> | ||
<artifactId>kogito-rest-workitem</artifactId> | ||
</dependency> | ||
</dependencies> | ||
</project> |
37 changes: 37 additions & 0 deletions
37
...rocess-dynamic/src/main/java/org/kie/kogito/process/dynamic/DynamicPathParamResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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.process.dynamic; | ||
|
||
import java.util.Map; | ||
|
||
import org.kogito.workitem.rest.pathresolvers.DefaultPathParamResolver; | ||
|
||
public class DynamicPathParamResolver extends DefaultPathParamResolver { | ||
|
||
private String processInstanceId; | ||
|
||
public DynamicPathParamResolver(String processInstanceId) { | ||
this.processInstanceId = processInstanceId; | ||
} | ||
|
||
@Override | ||
protected Object getParam(Map<String, Object> parameters, String key) { | ||
return key.equals("processInstanceId") ? this.processInstanceId : super.getParam(parameters, key); | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
...ynamic/src/main/java/org/kie/kogito/process/dynamic/ProcessInstanceDynamicCallHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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.process.dynamic; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.stream.Stream; | ||
|
||
import org.drools.core.common.InternalKnowledgeRuntime; | ||
import org.jbpm.process.instance.InternalProcessRuntime; | ||
import org.jbpm.process.instance.KogitoProcessContextImpl; | ||
import org.jbpm.process.instance.ProcessInstance; | ||
import org.jbpm.util.ContextFactory; | ||
import org.jbpm.workflow.core.impl.WorkflowProcessImpl; | ||
import org.jbpm.workflow.instance.WorkflowProcessInstance; | ||
import org.jbpm.workflow.instance.node.DynamicUtils; | ||
import org.kie.kogito.Model; | ||
import org.kie.kogito.process.Process; | ||
import org.kie.kogito.process.expr.ExpressionHandlerFactory; | ||
import org.kie.kogito.process.impl.AbstractProcessInstance; | ||
import org.kogito.workitem.rest.RestWorkItemHandler; | ||
|
||
public class ProcessInstanceDynamicCallHelper { | ||
|
||
private ProcessInstanceDynamicCallHelper() { | ||
} | ||
|
||
public static void executeRestCall(RestWorkItemHandler handler, Stream<Process<?>> processes, String processId, String processInstanceId, RestCallInfo input) { | ||
Process<?> processDef = processes.filter(p -> p.id().equals(processId)).findAny().orElseThrow(() -> new IllegalArgumentException("Cannot find process " + processId)); | ||
WorkflowProcessInstance pi = (WorkflowProcessInstance) findProcessInstance(processDef, processInstanceId); | ||
WorkflowProcessImpl process = (WorkflowProcessImpl) pi.getProcess(); | ||
if (!process.isDynamic()) { | ||
process.setDynamic(true); | ||
} | ||
InternalKnowledgeRuntime runtime = pi.getKnowledgeRuntime(); | ||
InternalProcessRuntime.asKogitoProcessRuntime(runtime).getKogitoWorkItemManager().registerWorkItemHandler(RestWorkItemHandler.REST_TASK_TYPE, handler); | ||
Map<String, Object> parameters = input.getArguments() == null ? new HashMap<>() : new HashMap<>(input.getArguments()); | ||
if (input.getHost() != null) { | ||
parameters.put(RestWorkItemHandler.HOST, input.getHost()); | ||
} | ||
if (input.getPort() != null) { | ||
parameters.put(RestWorkItemHandler.PORT, input.getPort()); | ||
} | ||
if (input.getMethod() != null) { | ||
parameters.put(RestWorkItemHandler.METHOD, input.getMethod()); | ||
} | ||
if (input.getEndpoint() != null) { | ||
parameters.put(RestWorkItemHandler.URL, input.getEndpoint()); | ||
} | ||
parameters.put(RestWorkItemHandler.PATH_PARAM_RESOLVER, new DynamicPathParamResolver(processInstanceId)); | ||
WorkItemHandlerResultHolder holder = new WorkItemHandlerResultHolder(); | ||
parameters.put(RestWorkItemHandler.RESULT_HANDLER, holder); | ||
KogitoProcessContextImpl context = ContextFactory.fromItem(DynamicUtils.addDynamicWorkItem(pi, runtime, RestWorkItemHandler.REST_TASK_TYPE, parameters)); | ||
Model model = ((Model) processDef.createModel()); | ||
model.update(input.getOutputExpression() != null | ||
? ExpressionHandlerFactory.get(input.getOutputExpressionLang(), input.getOutputExpression()).eval(holder.getResult(), Map.class, context) | ||
: holder.getResult()); | ||
((AbstractProcessInstance) pi.unwrap()).updateVariablesPartially(model); | ||
} | ||
|
||
private static ProcessInstance findProcessInstance(Process<?> process, String processInstanceId) { | ||
return process.instances() | ||
.findById(processInstanceId).map(AbstractProcessInstance.class::cast) | ||
.map(AbstractProcessInstance::internalGetProcessInstance) | ||
.orElseThrow(() -> new IllegalArgumentException("Cannot find process instance " + processInstanceId + " for process " + process.id())); | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
addons/common/process-dynamic/src/main/java/org/kie/kogito/process/dynamic/RestCallInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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.process.dynamic; | ||
|
||
import java.util.Map; | ||
|
||
public class RestCallInfo { | ||
|
||
private String endpoint; | ||
private String host; | ||
private Integer port; | ||
private String method; | ||
private Map<String, Object> arguments; | ||
private String outputExpression; | ||
private String outputExpressionLang = "jq"; | ||
|
||
public String getEndpoint() { | ||
return endpoint; | ||
} | ||
|
||
public void setEndpoint(String endpoint) { | ||
this.endpoint = endpoint; | ||
} | ||
|
||
public String getHost() { | ||
return host; | ||
} | ||
|
||
public void setHost(String host) { | ||
this.host = host; | ||
} | ||
|
||
public Integer getPort() { | ||
return port; | ||
} | ||
|
||
public void setPort(Integer port) { | ||
this.port = port; | ||
} | ||
|
||
public String getMethod() { | ||
return method; | ||
} | ||
|
||
public void setMethod(String method) { | ||
this.method = method; | ||
} | ||
|
||
public Map<String, Object> getArguments() { | ||
return arguments; | ||
} | ||
|
||
public void setArguments(Map<String, Object> arguments) { | ||
this.arguments = arguments; | ||
} | ||
|
||
public String getOutputExpression() { | ||
return outputExpression; | ||
} | ||
|
||
public void setOutputExpression(String outputExpression) { | ||
this.outputExpression = outputExpression; | ||
} | ||
|
||
public String getOutputExpressionLang() { | ||
return outputExpressionLang; | ||
} | ||
|
||
public void setOutputExpressionLang(String outputExpressionLang) { | ||
this.outputExpressionLang = outputExpressionLang; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "RestCallInfo [endpoint=" + endpoint + ", host=" + host + ", port=" + port + ", method=" + method | ||
+ ", arguments=" + arguments + ", outputExpression=" + outputExpression + ", outputExpressionLang=" | ||
+ outputExpressionLang + "]"; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...ess-dynamic/src/main/java/org/kie/kogito/process/dynamic/WorkItemHandlerResultHolder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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.process.dynamic; | ||
|
||
import java.util.Map; | ||
|
||
import org.kogito.workitem.rest.resulthandlers.RestWorkItemHandlerResult; | ||
|
||
import io.vertx.mutiny.core.buffer.Buffer; | ||
import io.vertx.mutiny.ext.web.client.HttpResponse; | ||
|
||
public class WorkItemHandlerResultHolder implements RestWorkItemHandlerResult { | ||
|
||
private Map<String, Object> result; | ||
|
||
@Override | ||
public Object apply(HttpResponse<Buffer> buffer, Class<?> clazz) { | ||
result = buffer.bodyAsJson(Map.class); | ||
return result; | ||
} | ||
|
||
public Map<String, Object> getResult() { | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.