4

I want to add a GET request to my Dropwizard app so that a file is returned which was retrieved from a Minio server.

Consider

@Path("/file")
public class FileResource {

    @GET
    @Produces(MediaType.APPLICATION_OCTET_STREAM)
    public Response getFile() throws Exception {
        InputStream is = minioClient.getObject("mybucket", "myobject");
  // timeout?
        return Response.ok(is)
                .header(HttpHeaders.CONTENT_DISPOSITION, 
                        "attachment; filename=\"file.txt\"")
                .build();
    }
}

What happens with the Dropwizard GET request then retrieving the file from Minio takes takes to long e.g. slow network?

Is it correct that the servlet container copies the file from Minio to the client and if I add the content length to the response the request styas open until the copy is finished?

A.Dumas
  • 2,619
  • 3
  • 28
  • 51
  • Might be worth trying a StreamingOutput instead of returning the InputStream, e.g. https://stackoverflow.com/questions/12012724/example-of-using-streamingoutput-as-response-entity-in-jersey – Michael Barnwell Apr 13 '18 at 07:05
  • my following questions would be: Is the InputStream chunked or is the whole input stream loaded into the memory? – A.Dumas Apr 13 '18 at 07:50
  • Seems that doing proxying in a rest call is rather out of spec for REST. – Joakim Erdfelt Apr 15 '18 at 12:15
  • Could you elaborate a little on this so I could then marked this as accepted answer. – A.Dumas Apr 17 '18 at 09:09

1 Answers1

1

Jersey automatically translates an InputStream to a StreamingOutput in the overload you're using, so ignore the suggestion from Michael. It's already happening behind the scenes.

You are setting the incorrect mime type. If you know a file is text, you need to define your @Produces to match. Some frameworks will misbehave if they detect a mismatch between the data and the mime type provided, so try to be as specific as possible when defining your interface.

Take a look at this Question for a discussion on using the correct mime type.

A timeout causes a Response to be returned to the client with an error code. You are responsible for registering a callback to handle error responses.

Stephan
  • 666
  • 8
  • 23