0 votes
2.3k views
in Process Center by (140 points)

Hi, need suggestion how to read file from shared folder in ibm bpm server. i have create script to write file in service flow but there is no sample to read file from share folder.

//Below code converts the file content string in to byte[] (byte array).

var byteValue = Packages.java.lang.String(tw.local.ContentStream.content).getBytes();

// Below code, decodes the above Base64 byte array in to use format of byte[] (byte array)

var content64 = Packages.org.apache.commons.codec.binary.Base64.decodeBase64(byteValue);

    var fileName="/opt/docs/" + tw.local.ContentStream.fileName ;

            var file = new Packages.java.io.File(fileName);

             // if file doesnt exists, then create it

            if (!file.exists()) {

                file.createNewFile();

            }

            

          //change permission to 777 for all the users

        //no option for group and others file.setExecutable(true, false);

        file.setReadable(true, false);

       file.setWritable(true, false);

            var fop = new Packages.java.io.FileOutputStream(file);

            fop.write(content64);

Thank you,
 

1 Answer

0 votes
by (30.6k points)

Server-side JavaScript in BPM / BAW has no file API of its own, but it can call Java classes directly (Rhino "LiveConnect"), so reading a file from a folder the server can reach is a few lines - no jar required:

// service flow script task - read a text file from a shared folder mounted on the server
var path = tw.env.inboundFolder + "/" + tw.local.fileName;      // e.g. /mnt/share/in/order-123.csv
var file = new java.io.File(path);
if (!file.exists()) throw new Error("File not found: " + path);
var bytes = java.nio.file.Files.readAllBytes(file.toPath());
tw.local.content = String(new java.lang.String(bytes, "UTF-8"));  // text
// binary (PDF, image): keep it as Base64 for the document store
tw.local.contentBase64 = String(java.util.Base64.getEncoder().encodeToString(bytes));

// list a folder
var names = new java.io.File(tw.env.inboundFolder).list();
tw.local.files = new tw.object.listOf.String();
for (var i = 0; i < names.length; i++) tw.local.files[i] = String(names[i]);

Write works the same way (java.nio.file.Files.write(java.nio.file.Paths.get(path), new java.lang.String(tw.local.content).getBytes("UTF-8"))). Notes:

  • The path is on the server that runs the service (every cluster member must see the share - mount it identically on all nodes, or use a Java integration on one node).
  • Wrap Java strings with String(...) before storing them in tw.local, otherwise you keep a java.lang.String object inside the variable.
  • Large files: stream with java.io.BufferedReader line by line instead of readAllBytes.
  • On CP4BA (containers) there is no shared folder unless you mount a persistent volume into the BAW pods - the supported pattern there is to read from an ECM object store or an object storage (S3) through REST.
  • Some hardened servers disable Java access from scripts (CWTBG0529E / script security); then use a Java integration with a small class in a managed jar.

References

Related questions

0 votes
1 answer 2.0k views
0 votes
1 answer 1.4k views
0 votes
2 answers 4.6k views
0 votes
1 answer 2.4k views
0 votes
1 answer 1.4k views
asked Jun 15, 2017 by lavanyas (320 points)
0 votes
1 answer 1.1k views
0 votes
1 answer 2.3k views
0 votes
1 answer 740 views
0 votes
1 answer 2.6k views
+1 vote
1 answer 1.8k views

723 questions

807 answers

98 comments

4.9k users

Join BPM Community Discord Channel

Welcome to BPM Tips Q&A, Community wiki/forum where you can ask questions and receive answers from other IBM BPM experts and members of the community. Users with 2000 points will automatically be promoted to expert level.
Created by Dosvak LLC
Our Youtube Channel
...