Java Callout access to File Resources

Hello dear comunity,

I would like to have access to a Resource File from Java (using the JavaCallout).

The basic idea is to be able to read the JSON File that is saved as part of the proxy.

{ 
"node1": "value1",
"node2": "value2"
}

This file is provided by the clients, and we should use some of the information included on it, for example read the "node1" value, in case the Call Path in the url contains "node1".

Some other idea I had, was to pass the content of the file (maybe JSON Stringify?) as a Property to the Java Callout, but I haven´t been able to use the "ressourceURL" syntax here:

<Properties>
	<Property name="nodeValue">jsc://my-javascript.js</Property>
</Properties>

But sadly, this just passes the value string "jsc://my-javascript.js"

Does anybody knows how can we achieve this?

Thank you very much.

Oscar

Solved Solved
3 7 639
1 ACCEPTED SOLUTION

How complicated is the JSON? How large? If it is 3 or 4 properties, why not just use what you described?

<Properties>
	<Property name="nodeValue">{ 
  "node1": "value1",
  "node2": "value2"
}</Property>
</Properties>

And then in the Java callout, use Gson or Jackson or whatever to deserialize the JSON string.

String jsonString = properties.get("nodeValue");
// deserialize json here

And if that's not appropriate, thebn you could just reference it in a variable

<Properties>
	<Property name="variable">context-variable</Property>
</Properties>

And in this case the Java callout would perform

String jsonString = messageContext.getVariable(properties.get("variable"));
// deserialize json here

View solution in original post

7 REPLIES 7

How complicated is the JSON? How large? If it is 3 or 4 properties, why not just use what you described?

<Properties>
	<Property name="nodeValue">{ 
  "node1": "value1",
  "node2": "value2"
}</Property>
</Properties>

And then in the Java callout, use Gson or Jackson or whatever to deserialize the JSON string.

String jsonString = properties.get("nodeValue");
// deserialize json here

And if that's not appropriate, thebn you could just reference it in a variable

<Properties>
	<Property name="variable">context-variable</Property>
</Properties>

And in this case the Java callout would perform

String jsonString = messageContext.getVariable(properties.get("variable"));
// deserialize json here

Basically that´s what I wanted, but then how do I get the JSON content in the Property?

Meaning, the file is part of the Resources, uploaded by client, but in order to put the Info also in the property, they will have to do an extra step manually, and copy the content of the file in the JavaCallout Policy Property as you mentioned.

Also I´m kinda concerned about the Overhead of parsing a JSON by every Proxy call (if the file starts getting bigger and bigger in the future).

Yes, I understand. What I just suggested was to NOT use resources, but instead to just code the JSON into the Properties element of the Java policy configuration.

"Resources" assets in general are not accessible to policies directly, and this includes Java callout policies and jS policies.

JSC resources are directly accessible to JS code.

And similarly, java resources are accessible from Java code.

If I were doing this I think maybe I would store the settings not as a "resource" but as an entry in the KVM.

And then ... in the proxy:

  • use a KVM Get policy to retrieve the value into a context variable,
  • specify the name of the variable in a Property in the Java policy config
  • within the Java code read the context variable with MessageContext.getVariable()
  • and Gson.parse() that

We ended up packing the content of the file as a Property using a JSON String.

Luckily, the file is quite small (only two elements) and we don´t see any performance issues.

Thanks for the your help

Great! I'm glad you found this satisfactory solution.

Former Community Member
Not applicable

I would like to do the exact same thing but as my file is quite big, the suggested workaround won't work.

Is there any way to store and retrieve an OpenAPI spec using a Java Callout with the spec bundled as part of the API Proxy?

I can only think of the following:

* Package the OpenAPI spec as a classpath resource of the JAR file running the code of the Java Callout

An alternative approach would be to

* Host the OpenAPI spec on an external URL and pull it from there during runtime

This introduces a very unwelcome runtime dependency, however.

Any feedback is welcome!

There is a feature coming that will allow this. b/155436526

Today, I don't know of a better way.