How to return binary data through REST API extension?

1
0
-1

Hello,

I'm trynig to make a process which involves file handling (uploading files in a case to be downloaded in a later case by a separate user) and prepared everything by consuming the file input from the uploading case in an out connector that uploads the raw binary content to a separate service that is on localhost only. However now on the downloading side I'm trying to implement this using a REST API extension, since I want to be able to verify the accessing user before letting them download the file from the separate service.

So for the downloader this is my code so far:

logger.info("Downloading file $uuid in activity ${activity.name} (ID: $activityId)")
val client = newBuilder()
    .version(Version.HTTP_1_1)
    .followRedirects(Redirect.NEVER)
    .connectTimeout(Duration.ofSeconds(20))
    .build()

val request = HttpRequest.newBuilder()
    .uri(URI.create("$serviceHost/file/$uuid"))
    .timeout(Duration.ofMinutes(1))
    .GET()
    .build()

val res = client.send(request, BodyHandlers.ofByteArray())
return responseBuilder
    .withResponseStatus(res.statusCode())
    .withMediaType(
        res.headers().firstValue("Content-Type").orElse("application/octet-stream")
    )
    .withResponse(res.body())
    .build()

However the withResponse API takes in a "Serializable", indicating I can only really pass through Strings, since the Java serialization format for complex objects is otherwise useless outside the Java ecosystem.

Also since the RestApiResponseBuilder contains a withCharacterSet, indicating it can only describe text, I don't think it's the right API for me to use when I want to send out binary and would probably need some kind of binary response builder instead.

I looked online for this and found in https://community.bonitasoft.com/questions-and-answers/how-can-i-return-binary-data-part-rest-extension that this wasn't possible 3 years ago, but there was an idea board linked that now doesn't seem to exist anymore / require some login now. Did anything there change to make it now possible?

My alternatives right now are either making another service / extending the file upload service with some temporary public sharing that can be triggered from my REST API extension, which is a lot of extra administration and security overhead with making a public facing service or alternatively encode the binary file to a string using e.g. Base64, and decode in the browser, which seems super inefficient and wasteful for the simple task at hand.

Any ideas how to solve this? Is my file handling approach itself fundamentally flawed? I'm only doing all this extra work with a separate file hosting service since it looks like Bonita is unable to store the files anywhere outside a single task, specifically not inside the BDM.

Kind regards,
Jan

No answers yet.
Notifications