Sidebar

Returning a Binary or Image File as the Response When Using qie.postMessageResponse()

0 votes
66 views
asked May 14 by rich-c-2789 (17,430 points)
How do I return a binary file when using qie.postMessageResponse()?

1 Answer

0 votes

The qie.postMessageResponse() can take either a string or a byte[].  To return a binary file we can pass a binary file to qie.postMessageResponse() as a byte[].  Here are a couple ways to do that depending on what needs to be returned in your case.

Return a file:

The simplest example is to just return the file without any other information.

qie.postMessageResponse(qie.readFile('/Users/dir/Pictures/dog.jpeg'));   

The above line uses qie.readFile() to load the file as a byte[] and passes the byte[] to qie.postMessageResponse().

Add HTTP response headers and return file:

We might also need to add response headers to tell the server responsible for processing the response how to handle the files bytes. In this example the header text will need to be converted to bytes to combine the headers with the file bytes. 

//Build the header info.  Remember to prefix the headers with "http.header." and to end it with a new line.
var headers = "http.header.encoding = ISO-8859-1\r\n";
headers += "http.header.Content-Type = image/jpeg\r\n";
headers += "http.header.content-transfer-encoding = binary\r\n";

// Convert the JavaScript string to a java.lang.String
headers = new java.lang.String(headers);

// Get the bytes for the headers
var headerBytes = headers.getBytes();

// Get the bytes for the binary file.  An image in this case.
var imageBytes = qie.readFile('/Users/dir/Pictures/dog.jpeg');

// Create a new ByteArrayOutputStream that is used to combine the bytes for the headers and file
var baos = new java.io.ByteArrayOutputStream();

// Combine the bytes
baos.write(headerBytes, 0, headerBytes.length);
baos.write(imageBytes, 0, imageBytes.length);

// Post the combined bytes as the response
qie.postMessageResponse(baos.toByteArray());

We have to be careful when we combine the header and file bytes so the bytes are recognized on the other end. We use a java.lang.String and not a JavaScript string.  The above code builds the headers with a JavaScript string then converts them to a java.lang.String.  We could have also used a java.lang.String and the concat() function (only available on java.lang.String) like this:

...
//Build the header info.  Remember to prefix the headers with "http.header." and to end it with a new line.
var headers = new java.lang.String("http.header.encoding = ISO-8859-1\r\n");
headers.concat("http.header.Content-Type = image/jpeg\r\n");
headers.concat("http.header.content-transfer-encoding = binary\r\n");

// Get the bytes for the headers
var headerBytes = headers.getBytes();
...

Note: Using the + or += operators for concatenation in this case is not recomended.

The above examples are iintended to demonstrate using a byte[] with qie.postMessageResponse().  To return a file you might also consider returning a file using a multipart or MTOM format and/or using base64 to encode the file bytes as a string.

 

See also:

Handling HTTP requests and responses

Posting Responses via qie.postMessageResponse() in a channel that uses an HTTP Listener source node

 

answered May 14 by rich-c-2789 (17,430 points)
edited May 16 by rich-c-2789
...