JavaScript Callout Followed by JavaCallOut

Appreciate if some one can help.

I am having an issue when I try to execute javascript callout followed by java callout. The issue is I am setting up some String variables in Java Call out but when I read them in java script which follows java callout  I see variables with double quotes in trace like "JWT" (instead of JWT) even if I concatenate two strings in javascript after reading variables that I set in java callout it appears as ("JWT""nsdvksjd").

My another problem is that if I set a variable as array of strings  and when I read same variable in javascript and implement .indexOf("x") I see -1 for every string even if string exists.

Solved Solved
0 3 342
1 ACCEPTED SOLUTION

That sounds amazing!  and frustrating. I have been using Java callouts for a long long time, and what you are describing is not how things work. In fact I just tried this myself, and it's working as expected. there are no extra double quotes.

My java code

 

 

  public ExecutionResult execute(final MessageContext msgCtxt, final ExecutionContext execContext) {
    // set some variables.
    Instant stamp = Instant.now();
    String formattedStamp = DateTimeFormatter.ISO_INSTANT.format(stamp);
    msgCtxt.setVariable("baloney.stamp", formattedStamp);
    String x = "abc";
    msgCtxt.setVariable("x", x);
    String y = "def";
    msgCtxt.setVariable("y", y);
    return ExecutionResult.SUCCESS;
  }

 

 

my JS code

 

 

var x = context.getVariable('x');
var y = context.getVariable('y');
context.setVariable('xy', x + y);
print(context.getVariable('xy'));

 

 

And here are the screenshots showing .... the expected results.  You can see, there are no double quotes.

The Java callout:

screenshot-20210727-182104.png

the JS callout: 

screenshot-20210727-182113.png

 

So I think you need to double-check your work. Double check that your code is doing what you say it is doing.  

Please find the bundle attached. 

Start a trace session, then invoke the proxy  with:

 

 

curl -i $endpoint/baloney-callout/example

 

 

 ...and then view the results in trace.

 

View solution in original post

3 REPLIES 3

I cannot imagine what is causing quotes to be injected where you do not want them.  Maybe you are using JSON.stringify() or it equivalent in your Java code?  In Java, when you run a string through a JSON serializer, it will get quoted.  Ex: 

$ jshell --class-path ~/dev/java/lib/gson-2.3.jar 
|  Welcome to JShell -- Version 11.0.10
|  For an introduction type: /help intro

jshell> import com.google.gson.Gson;

jshell> Gson gson = new Gson();
gson ==> {serializeNulls:falsefactories:[Factory[typeHiera ... 4799],instanceCreators:{}}

jshell> String s = "foo";
s ==> "foo"

jshell> gson.toJson(s);
$4 ==> "\"foo\""

jshell> 

I suggest that, in your java callout, you should serialize things to strings when you set them into context variables.  For example, if you have a List<String>, then you should join them into a single string.  like this; 

messageContext.setVariable("my_new_variable", 
                            String.join(",", listOfStrings)); ​

 

Then, In JavaScript, read the resulting variable as normal.  

var s = context.getVariable('my_new_variable');

If the thing you are storing into a context variable in the Java code is a complex object, then, you may want to use a JSON formatter (like Gson).  In that case you will need to perform a JSON.parse() in JavaScript. 

// Java code: set an object into a variable
messageContext.setVariable("my_var",
                            gson.toJson(my_object));

// Javascript code: read the object
var obj = JSON.parse(context.getVariable('my_var'));

If you show the code from a simple example Java callout and JavaScript policy that exhibits the behavior you are describing, I will better be able to diagnose and guide you. 

 

Thanks for the quick response. Now I am able to serialize complex objects in Java and deserialize in JavaScript using JSON.parse().

But I have a problem with simple objects like string. Let me give you a small example

for example if java callout have following code:

String x = "abc"; String y = "def";

context.setVariable("x",x); context.setVariable("y", y);

Now inside JavaScript if I do

var x = context.getVariable(x);

var y = context.getVariable(y);

//concatenate

context.setVarialbe("xy",x+y);

print(context.getVariable(xy));  -----> prints "abc""def" instead of abcdef

 

 

 

 

 

 

 

 

 

 

That sounds amazing!  and frustrating. I have been using Java callouts for a long long time, and what you are describing is not how things work. In fact I just tried this myself, and it's working as expected. there are no extra double quotes.

My java code

 

 

  public ExecutionResult execute(final MessageContext msgCtxt, final ExecutionContext execContext) {
    // set some variables.
    Instant stamp = Instant.now();
    String formattedStamp = DateTimeFormatter.ISO_INSTANT.format(stamp);
    msgCtxt.setVariable("baloney.stamp", formattedStamp);
    String x = "abc";
    msgCtxt.setVariable("x", x);
    String y = "def";
    msgCtxt.setVariable("y", y);
    return ExecutionResult.SUCCESS;
  }

 

 

my JS code

 

 

var x = context.getVariable('x');
var y = context.getVariable('y');
context.setVariable('xy', x + y);
print(context.getVariable('xy'));

 

 

And here are the screenshots showing .... the expected results.  You can see, there are no double quotes.

The Java callout:

screenshot-20210727-182104.png

the JS callout: 

screenshot-20210727-182113.png

 

So I think you need to double-check your work. Double check that your code is doing what you say it is doing.  

Please find the bundle attached. 

Start a trace session, then invoke the proxy  with:

 

 

curl -i $endpoint/baloney-callout/example

 

 

 ...and then view the results in trace.