Can't get all parts of a chunked based web service


(Yarichard) #1

Hi everyone,
we’re calling a web service on an Axis brand camera to get some infos. It serves the response as a plain/text chunked response, and we can’t manage to get all data in a play web service call.

Here is the code we’re using:

public CompletionStage<Result> getAxisDeviceInfosTest(String axisUrl){
    // Make the request
    CompletionStage<WSResponse> futureResponse = ws.url(axisUrl).setMethod("GET").stream();
    CompletionStage<Result> result = futureResponse.thenApply(response -> {
        Source<ByteString, ?> body = response.getBodyAsSource();
        Logger.info(body.toString());
        // Check that the response was successful
        if (response.getStatus() == 200) {
            // Get the content type
            String contentType =
                    Optional.ofNullable(response.getHeaders().get("Content-Type"))
                            .map(contentTypes -> contentTypes.get(0))
                            .orElse("application/octet-stream");

            // If there's a content length, send that, otherwise return the body chunked
            Optional<String> contentLength = Optional.ofNullable(response.getHeaders()
                    .get("Content-Length"))
                    .map(contentLengths -> contentLengths.get(0));
            if (contentLength.isPresent()) {
                return Results.ok().sendEntity(new HttpEntity.Streamed(
                        body,
                        Optional.of(Long.parseLong(contentLength.get())),
                        Optional.of(contentType)
                ));
            } else {
                return Results.ok().chunked(body).as(contentType);
            }
        } else {
            return new Result(Http.Status.BAD_GATEWAY);
        }
    });
    return result;
}

everytime this route returns only the first part of the camera result, not the full content.
Calling the web service directly from a tool that handles chunked responses work.

We also tried the simple way by calling a

public CompletionStage<Result> getAxisDeviceInfos(String axisUrl){
    return ws.url(axisUrl).get().thenApply(r -> {
        Logger.info(r.getBody().toString());
        return Results.ok(r.getBody());
    });
}

Same result. Any idea of what we’re doing wrong ?
Thanks in advance,
Yann