how to check payload size in scripting component in mule 3 - mule

I'm trying to check the size of payload in scripting component in mule 3 but it's not working and getting script error. Please suggest the correct code.
if(sizeOf (payload) > 0){
flowVars.username = "aaaaaaaaa"
}
else{
flowVars.username = "bbbbbb"
}

In Mule 3 you need to be aware of the Java class used to implement the payload. If it is a String you can use the method length() (ie payload.length(). If it is a collection use size(). Some classes or interfaces don't have an actual size.
The function sizeOf() seems to be the Mule 3 DataWeave operator, which will not work in Mule 3 expressions or the scripting component. If you need to understand the language that you are using.

Related

Getting error "unbalanced braces" in data weave in mule 3.9.3

I am using choice router to evalute expression. Here is the expression
and I tested this expression in dataweave here is the result.
but when I use the choice router to evalute the expression I am getting this result
and another thing the value of "payload.relations.rel" is "Microsoft.VSTS.Common.TestedBy-Reverse" why I am getting false for this expression
bench : payload.relations.rel == "Microsoft.VSTS.Common.TestedBy-Reverse"
The error is not from DataWeave. In Mule 3.x the expression language used is MEL (Mule Expression Language). DataWeave is only used in Transform components. This is different from Mule 4.x where DataWeave 2 is used as the expression language. Testing the expression of the choice in DataWeave is not a good test.
Also your tests show that you are comparing an array (payload.relations.rel) to a String. Try fixing the comparison first. Then if you still have the error try putting it into a logger component before the choice and see if it prints the right result.

Unable to understand Mule 3 expressions

I know Mule 4 but came across Mule 3 application and trying to understand some expressions and connectors. Can somebody explain.
Set Variable expression 1:
#[dw("p(flowVars.someKey)")]
Set Variable expression 2: (input XML payload)
#[xpath3('local-name(//*:Body/*[1])')]
What does DOM to XML do?
I'll assume these are 3 questions:
What does this expression do?
#[dw("p(flowVars.someKey)")]
This is a MEL expression (MEL is the expression language in Mule 3) which executes a dynamic DataWeave expression, to obtain a configuration property, where the name of the configuration property is dynamically obtained from the value of flow variable someKey.
What does this expression do?
#[xpath3('local-name(//:Body/1)')]
This MEL expression call the xpath3() function to evaluate an XPath 3.0 expression on the payload.
What does DOM to XML do?
This question has been answered previously.

How to handle the null scenario with kotlinx-coroutines-reactive?

kotlinx-coroutines-reactive makes org.reactivestreams.Publisher to have awaitXXX methods:
val person = peopleReactiveRepository.findById(personId).awaitSingle()
If there is no person can be found by a person ID, this invocation will throw NoSuchElementException and this exception cannot be handled in the user code directly. And Spring MVC ExceptionHandler can not translate this exception into a user-friendly response.
java.util.NoSuchElementException: No value received via onNext for awaitSingle
at kotlinx.coroutines.experimental.reactive.AwaitKt$awaitOne$$inlined$suspendCancellableCoroutine$lambda$1.onComplete(Await.kt:131) ~[kotlinx-coroutines-reactive-0.22.1.jar:na]
at reactor.core.publisher.StrictSubscriber.onComplete(StrictSubscriber.java:123) ~[reactor-core-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at reactor.core.publisher.Operators$MultiSubscriptionSubscriber.onComplete(Operators.java:1327) ~[reactor-core-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at reactor.core.publisher.FluxHide$SuppressFuseableSubscriber.onComplete(FluxHide.java:137) ~[reactor-core-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at reactor.core.publisher.FluxMap$MapSubscriber.onComplete(FluxMap.java:130) ~[reactor-core-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at reactor.core.publisher.MonoNext$NextSubscriber.onComplete(MonoNext.java:96) ~[reactor-core-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at com.mongodb.reactivestreams.client.internal.ObservableToPublisher$1.onComplete(ObservableToPublisher.java:78) ~[mongodb-driver-reactivestreams-1.6.0.jar:na]
One of approaches I can figure out is in the following:
val person = peopleRepository.findById(personId).awaitFirstOrDefault(null)
if (person == null) {
// do something
}
But I do not think it is an elegant way. For example, can provide a method named awaitSingleOptional.
Is there any better Kotlin way to handle this scenario?
There are no standard Optional wrappers in Kotlin. You can use the let function for such cases:
val person = peopleRepository.findById(personId).awaitFirstOrDefault(null)?.let {
// do
}
If the await-expression evaluates to the default null, the let invocation will also evaluate to null. If you need to handle this case, the Elvis operator can be used:
.let {...} ?: throw IllegalStateException()
An Extension awaitFirstOrNull() has been made available in the recent kotlinx.coroutines release 0.22.2. See this PR.
Taken from the release notes:
Reactive: Added awaitFirstOrDefault and awaitFirstOrNull extensions (see #224, PR by #konrad-kaminski).
You can definitely use Optional wrappers if you are programming in a functional way.
If you want to get your toes wet with functional programming, you can pick up Arrow which has the Option and the Try data types for this purpose.
Using ?.let is another option but it won't help you much with reactive programming.
There is also the Notification class in ReactiveX which will lets you handle an erroneous scenario if you are doing Railway Oriented Programming.

Unexpected behaviour of MEL function after upgrade to Mule Runtime 3.8.1

I have the following MEL function:
def createSomething(foo){
if (org.springframework.util.StringUtils.isEmpty(foo)){
return org.apache.commons.lang.StringUtils.remove(java.util.UUID.randomUUID().toString(), '-');
}
if (foo.toString().length() <= 32){
return foo;
}
String fooWithoutHyphens = org.apache.commons.lang.StringUtils.remove(foo.toString(), '-');
if (fooWithoutHyphens.length() <= 32){
return fooWithoutHyphens;
}
return foo.toString().substring(0, 32);
}
that is being called from a DWL file:
%var mySomething = createSomething("foo")
This is working fine on Mule Runtime 3.7.2.
However after an upgrade to Mule Runtime 3.8.1 I receive the following exception:
com.mulesoft.weave.mule.exception.WeaveExecutionException: Exception while executing:
Unknown
Not enough arguments (1) for function with parameters (foo,
fooWithoutHyphens)..
at com.mulesoft.weave.mule.exception.WeaveExecutionException$.apply(WeaveExecutionException.scala:12)
at com.mulesoft.weave.mule.WeaveMessageProcessor.execute(WeaveMessageProcessor.scala:121)
at com.mulesoft.weave.mule.WeaveMessageProcessor.process(WeaveMessageProcessor.scala:67)
at com.mulesoft.weave.mule.WeaveMessageProcessor$$FastClassByCGLIB$$216b1542.invoke(<generated>)
When I provide anything as a second argument to the function, e.g.
%var mySomething = createSomething("foo", 0)
the exception doesn't occur, although as far as I can tell the function doesn't work as expected.
What is the reason of this behaviour and how can it be fixed?
UPDATE: If the following part:
String fooWithoutHyphens = org.apache.commons.lang.StringUtils.remove(foo.toString(), '-');
if (fooWithoutHyphens.length() <= 32){
return fooWithoutHyphens;
}
is removed or replaced with:
if (org.apache.commons.lang.StringUtils.remove(foo.toString(), '-').length() <= 32){
return org.apache.commons.lang.StringUtils.remove(foo.toString(), '-');
}
the exception is not thrown. It seems that the declarated string fooWithoutHyphens is now treated as an argument, but I don't know why.
The official doc is succinct on the use of global MEL function, and nowhere is mentioned variable declaration such as String fooWithoutHyphens = .... Related documentation can be found on the MEL Docs and DataWeave docs for 3.8.
Even if your example worked on Mule 3.7 there's no guarantee it will continue to work on later versions considering it is not documented. I think the work around you used in your example may be a good way to go, or if you require more complex transformation using global MEL function may not be the best way to go.

Confusion about the Argument< T > and Variable< T > in .NET 4.0 Workflow Foundation

I am using Windows Workflow Foundation in .NET 4.0. Below is some syntax/semantic confusion I have.
I have 2 equivalent way to declare an Assign activity to assign a value to a workflow variable (varIsFreeShipping).
(1) Using XAML in the designer.
(2) Using code.
But in approach 2, the it seems I am creating a new OutArgument< Boolean > and assign value to it, not to the original Variable< Boolean> varIsFreeShipping. And OutArgument and Variable are totally different types.
So how could the value assigned to this new Argument finally reach the original Variable?
This pattern seems common in WF 4.0. Could anybody shed some light on this?
Thanks!
As a matter of fact, the second (2) method can be written just as:
Then = new Assign<bool>
{
To = varIsFreeShipping,
Value = true
}
This all works because OutArgument<T> can be initialized through a Variable<T> using an implicit operator.
In your first (1) assign, using the editor, that's what's happening behind the scene; the variable is being implicitly converted from Variable to OutArgument.
WF4 uses alot of implicit operators mainly on Activity<T> from/to Variable<T>, OutArgument<T> from/to Variable<T>, etc. If you look at it, they all represent a piece of data (already evaluated or not), that is located somewhere. It's exactly the same as in C#, for example:
public int SomeMethod(int a)
{
var b = a;
return a;
}
You can assign an argument to a variable, but you can also return that same variable as an out argument. That's what you're doing with that Assign<T> activity (using the variable varIsFreeShipping as the activity's out argument).
This answers your question?