How copy selected property (not all) from camel to activiti - variables

Camel scenario:
read file
do something e.g store content in database
run activiti process and pass variable from previous step
RouteBuilder:
from("file:/home/work/Inbox")
.to("bean:sourceFileService?method=storeFile")
.to("activiti:Receive?copyVariablesFromProperties=true")
During call bean sourceFileService property 'sourceFileId' is set.
This variable should be transferred to the process Receive as variable.
If I don't use copyVariablesFromProperties any variable is not set in process.
One the other side when copyVariablesFromProperties=true then camel try pass all properties and exception occurs:
ActivitiException: couldn't find a variable type that is able to serialize GenericFile
(because one of property is object represents read file, there are 7 other unwanted property )
How pass only selected property to activiti endpoint or in camel any next 'to' ?

change sourceFileService's storeFile method signature as follows;
public String storeFile(... your other params, #OutHeaders Map headers) {
...
headers.put("sourceFileId", "32132132");
....
}
and you can access the set sourceFileId in your activiti endpoint

I have found that the use of copyVariablesFromProperties is not necessary.
The same is achieved by
.setBody().properties()
.to("activiti:Receive")
When in body is Map camel set variables for activiti process using that map.
But I still get exception for the same reason (pass unwanted, not serializable object).
The only solution I have found is
from("file:/home/work/Inbox")
.to("bean:sourceFileService?method=storeFile")
.setBody(method(Helper.class))
.to("activiti:Receive")
where
public class Helper {
#Handler
public Map getProcessVariables(Exchange exchange) {
Map<String, Object> variables = new HashMap<String, Object>();
variables.put("sourceFileId, exchange.getProperty("sourceFileId"));
return variables;
}
}

Related

FLOWABLE: Executing Service Tasks from java code without using xml

I am using Flowable 6.4.1 in spring-boot to create processes and run from my java code, but requirement is to not use any xml, so due to this I have hit a blockade.
I have a user task, taking input from user, depending on that input, call to corresponding service task is made.
Below is a short example of what I am going to do:
basic-process.bpmn20.xml:
<process id="basicprocess" name="Basic Process" isExecutable="true">
<startEvent id="startEvent"/>
<sequenceFlow sourceRef="startEvent" targetRef="getInput"/>
<userTask id="getInput" name="Get input from user" />
<sequenceFlow sourceRef="getInput" targetRef="decision"/>
<exclusiveGateway id="decision"/>
<sequenceFlow sourceRef="decision" targetRef="firstServiceTask">
<conditionExpression xsi:type="tFormalExpression">
<![CDATA[
${number>100}
]]>
</conditionExpression>
</sequenceFlow>
<sequenceFlow sourceRef="decision" targetRef="secondServiceTask">
<conditionExpression xsi:type="tFormalExpression">
<![CDATA[
${number<=100}
]]>
</conditionExpression>
</sequenceFlow>
<serviceTask id="firstServiceTask" name="Number is greater than predefined target"
flowable:class="demo.service.tasks.FirstServiceTask"/>
<sequenceFlow sourceRef="firstServiceTask" targetRef="greaterEnd"/>
<serviceTask id="secondServiceTask" name="Number is less than predefined target"
flowable:class="demo.service.tasks.SecondServiceTask"/>
<sequenceFlow sourceRef="secondServiceTask" targetRef="lesserEnd"/>
<endEvent id="greaterEnd"/>
<endEvent id="lesserEnd"/>
</process>
Above, XML shows the process and I'm starting the process using REST API
Below is the controller:
DefinitionsController.java:
#RestController
#SuppressWarnings("rawtypes")
public class DefinitionsController {
#Autowired
private RepositoryService mRepositoryService;
#Autowired
private RuntimeService mRuntimeService;
#Autowired
private TaskService mTaskService;
#PostMapping("/start-service")
public String startService(#RequestBody String input) {
Integer request = Integer.parseInt(input);
Map<String, Object> variables = new HashMap<String, Object>();
variables.put("number", request);
ProcessInstance instance = mRuntimeService.startProcessInstanceByKey("basicprocess", variables);
Task userTask = mTaskService.createTaskQuery().processInstanceId(instance.getId()).taskDefinitionKey("getInput").singleResult();
mTaskService.complete(userTask.getId());
return "ProcessInstance id is "+instance.getProcessInstanceId();
}
}
FirstServiceTask.java:
public class FirstServiceTask implements JavaDelegate{
#Override
public void execute(DelegateExecution execution) {
System.err.println("Came in first service task");
}
}
Same for SecondServiceTask.java except the sysout statement.
REST RESPONSE: I get the processInstance Id and sysout statement of respective service task gets printed in console..
Pretty easy to wire the Service Task classes from xml, however if I were to not use XML, I would need to create the same process using flowable-modeler api of FLOWABLE.
So, basically I want to have control over those service tasks from my java code and in order to do that how do I wire the Service Tasks that are created using flowable-modeler with my java code ?
I have gone through docs, but found the xml way only.
Configuring Service Tasks (created using flowable-modeler) with Java code can be done by the 4 ways shown here.
The delegate expression which is going to be configured should be either present on the classpath or should have a spring-bean created.
I created bean using a method in main class, and put the name of method in delegate expressionattribute in flowable-modeler/process api and that's what was needed to do that.
Attached image should clarify things, which shows the way to wire Service Tasks (created using flowable-modeler API) with Java classes in workspace.
firstServiceTask in highlighted field is the method which returns bean of FirstServiceTask
EDIT: Apart from above solution, we can also specify class name in class field alone and all configuration is done. Forex: I have a class called TestClass.java in package org.flowable.learning, so I'll just specify org.flowable.learning.TestClass in class field, which is just above highlighted Delegate expression field in attached screenshot

Retrieve WS request in CXF Web service

I got a CXF OSGi Web service (based on the example demo in servicemix: https://github.com/apache/servicemix/tree/master/examples/cxf/cxf-jaxws-blueprint)
The Web service works fine and i call all the available implemented methods of the service.
My question is how can i retrieve the request inside a WS method and parse in a string XML format.
I have found that this is possible inside interceptors for logging, but i want also to the WS-Request inside my methods.
For storing the request in the database I suggest to extend the new CXF message logging.
You can implement a custom LogEventSender that writes into the database.
I had similar requirement where I need to save data into DB once method is invoked. I had used ThreadLocal with LoggingInInterceptor and LoggingOutInterceptor. For example in LoggingInInterceptor I used to set the message into ThreadContext and in webservice method get the message using LoggingContext.getMessage() and in LoggingOutInterceptor I used to removed the message(NOTE: Need to be careful here you need to explictly remove the message from thread context else you will end up with memory leak, and also incase of client side code interceptors get reversed.
public class LoggingContext {
private static ThreadLocal<String> message;
public static Optional<String> getMessage() {
return Optional.ofNullable(message.get());
}
public static void setMessage(final String message) {
LoggingContext.message = new ThreadLocal<>();
LoggingContext.message.set(message);
}
}
Not an answer to this question but i achieved to do my task by using JAXB in the end and do some manipulations there.

Extract org.restlet.http.headers value from Camel headers inside a .choice()

I'm trying to extract a value from org.restlet.http.headers header collection in a Camel route.
My incoming POST has a http header property called IncomingRequestType: ABCD.
Camel moves this inside the exchange headers collection, but it is buried inside org.restlet.http.headers which is in-itself a collection of headers.
I can extract the value in a process using the code below:
.process(new Processor() {
public void process(Exchange exchange) throws Exception {
org.restlet.util.Series<Header> httpHeaders = null;
httpHeaders = (Series<Header>) exchange.getIn().getHeader("org.restlet.http.headers");
String reqType = httpHeaders.getValues("IncomingRequestType").toString();
}})
Outside of a process I need to access the IncomingRequestType inside a .choice().when()
e.g. i want to be able to do:
.choice()
.when(header("org.restlet.http.headers")["IncomingRequestType"]).isEqualTo("ABCD"))
Any suggestions on how this can be done. I've tried creating predicates but cannot get a suitable solution.
This can be done in the simple language:
.choice()
.when(simple("${in.header.org.restlet.http.headers[IncomingRequestType]} == 'ABCD'"))

Apache camel nested routes

I am new to Apache camel. I have very common use case that i am struggling to configure camel route. The use case is to take execution context
Update database using execution context.
Then using event on the execution context, create a byte message and send over MQ.
Then in the next step again use execution context and perform event processing.
Update database using execution context.
So basically its kind of nested routes. In the below configuration I need to have access to the executionContext that executionController has created in the updateSchedulerState, sendNotification, processEvent and updateSchedulerState i.e. steps annotated as 1,2, 3 and 4 respectively.
from("direct:processMessage")
.routeId("MessageExecutionRoute")
.beanRef("executionController", "getEvent", true)
.beanRef("executionController", "updateSchedulerState", true) (1)
.beanRef("executionController", "sendNotification", true) (2)
.beanRef("messageTransformer", "transform", true)
.to("wmq:NOTIFICATION")
.beanRef("executionController", "processEvent", true) (3)
.beanRef("eventProcessor", "process", true)
.beanRef("messageTransformer", "transform", true)
.to("wmq:EVENT")
.beanRef("executionController", "updateSchedulerState", true); (4)
Kindly let me know how should i configure the route for the above use case.
Thanks,
Vaibhav
So you need to access this executionContext in your beans at various points in the route?
If I understand correctly, you can put this executionContext in an exchange Property, and it will persist throughout the route.
Setting the exchange property can be done via the Exchange.setProperty() method or various camel dsl functions such as like this:
from("direct:xyz)
.setProperty("awesome", constant("YES"))
//...
You can access exchange properties from a bean by adding a method argument of type Exchange, like this:
public class MyBean {
public void foo(Something something, Exchange exchange) {
if ("YES".equals(exchange.getProperty("awesome"))) {
// ...
}
}
}
Or via #Property like this:
public class MyBean {
public void foo(Something something, #Property String awesome) {
if ("YES".equals(awesome)) {
// ...
}
}
}
This presumes you are using later versions of camel.
Does this help?

An interesting Restlet Attribute behavior

Using Restlet 2.1 for Java EE, I am discovering an interesting problem with its ability to handle attributes.
Suppose you have code like the following:
cmp.getDefaultHost().attach("/testpath/{attr}",SomeServerResource.class);
and on your browser you provide the following URL:
http://localhost:8100/testpath/command
then, of course, the attr attribute gets set to "command".
Unfortunately, suppose you want the attribute to be something like command/test, as in the following URL:
http://localhost:8100/testpath/command/test
or if you want to dynamically add things with different levels, like:
http://localhost:800/testpath/command/test/subsystems/network/security
in both cases the attr attribute is still set to "command"!
Is there some way in a restlet application to make an attribute that can retain the "slash", so that one can, for example, make the attr attribute be set to "command/test"? I would like to be able to just grab everything after testpath and have the entire string be the attribute.
Is this possible? Someone please advise.
For the same case I usually change the type of the variable :
Route route = cmp.getDefaultHost().attach("/testpath/{attr}",SomeServerResource.class);
route.getTemplate().getVariables().get("attr") = new Variable(Variable.TYPE_URI_PATH);
You can do this by using url encoding.
I made the following attachment in my router:
router.attach("/test/{cmd}", TestResource.class);
My test resource class looks like this, with a little help from Apache Commons Codec URLCodec
#Override
protected Representation get() {
try {
String raw = ResourceWrapper.get(this, "cmd");
String decoded = new String(URLCodec.decodeUrl(raw.getBytes()));
return ResourceWrapper.wrap(raw + " " + decoded);
} catch(Exception e) { throw new RuntimeException(e); }
}
Note my resource wrapper class is simply utility methods. The get returns the string of the url param, and the wrap returns a StringRepresentation.
Now if I do something like this:
http://127.0.0.1/test/haha/awesome
I get a 404.
Instead, I do this:
http://127.0.0.1/test/haha%2fawesome
I have URLEncoded the folder path. This results in my browser saying:
haha%2fawesome haha/awesome
The first is the raw string, the second is the result. I don't know if this is suitable for your needs as it's a simplistic example, but as long as you URLEncode your attribute, you can decode it on the other end.