Java Callout - Read external files in Java

Not applicable

Hi! In Java Callout, I tried to include the java keystore file (.jks) to overcome the SSLHandShakeException but I was not able to deploy API bundle, It shows some server exception. Similarly how to read the properties in config. file in Java Callout? Please Advise. Thanks.

Solved Solved
1 3 1,557
1 ACCEPTED SOLUTION

Not applicable

Dear @P.Ganesan, for security reasons reading files from the filesystem is not allowed from Java Callout policy. However, you can still leverage Java streams to read text or binary files stored in jar files. For a full api proxy example, please leverage the following tutorial. Let me know how it goes! Cheers!

InputStream fstream = this.getClass().getResourceAsStream("/templates/stockTemplate.xml"); //read stockTemplate.xml as a class from classpath
StringWriter writer = new StringWriter();
IOUtils.copy(fstream, writer, "UTF-8");
String theString = writer.toString();
messageContext.setVariable("stockTemplateFileContent", theString); //assign file content to stockTemplateFileContent variable
messageContext.setVariable("response.content", theString); // assign file content to response.content variable
return ExecutionResult.SUCCESS;

View solution in original post

3 REPLIES 3

Not applicable

Dear @P.Ganesan, for security reasons reading files from the filesystem is not allowed from Java Callout policy. However, you can still leverage Java streams to read text or binary files stored in jar files. For a full api proxy example, please leverage the following tutorial. Let me know how it goes! Cheers!

InputStream fstream = this.getClass().getResourceAsStream("/templates/stockTemplate.xml"); //read stockTemplate.xml as a class from classpath
StringWriter writer = new StringWriter();
IOUtils.copy(fstream, writer, "UTF-8");
String theString = writer.toString();
messageContext.setVariable("stockTemplateFileContent", theString); //assign file content to stockTemplateFileContent variable
messageContext.setVariable("response.content", theString); // assign file content to response.content variable
return ExecutionResult.SUCCESS;

It would be very useful to be able to read files from the proxy resources directory, and if it was possible to store a wider range of file types in the resources folder

I agree completely, Robin!