I'm working on a service to PUT and GET InputStream objects based on a key - these can be anything from basic Strings to files. Here is the PUT method which appears to work just fine:
@PUT
@Path("{key}")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response addData(
final @PathParam("key") String key,
final InputStream data) {
final Service service = new Service();
try {
service.addData(key, data);
} finally {
IOUtils.closeQuietly(data
}
return Response.status(204).build();
}
Now when I want to get the data (based on a key) I want to return the InputStream which is how the object is stored and retrieved. This is what I have currently:
@GET
@Path("/{key}")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public InputStream getData(
@PathParam("key") String key) {
// get service and do other stuff ...
return service.getData(); //This is an InputStream
}
When I send a GET, I get a status 200 and a 'Response Does Not Contain Any Data' message ... when I examine the request in Fiddler, it doesn't look like the data is attached. Am I missing something simple here? Thanks.
Edit: Here are the responses from:
Advanced Rest Client: https://i.stack.imgur.com/VabeK.jpg
Fiddler: https://i.stack.imgur.com/v1KTc.jpg
It seems like there's nothing attached to the response, but I'm not sure. Is there a good way to test whether or not it's returning the InputStream?
Edit2: Interesting. Per peeskillet's suggestion of reading the data to see if it's even there I did this:
final String data = IOUtils.toString(stream);
This returns ""
when it should be returning "test"
. Now I'm not super familiar with IOUtils so maybe their toString(InputStream)
is causing it to be "test"
, but that would suggest that it's not getting set properly in the service.
My service for getting the InputStream data looks something like this:
final InputStream data = getData(_key);
try {
if (data == null) {
return null;
}
return object.newBuilder()
.setKey(_key)
.setData(data)
.build();
} finally {
IOUtils.closeQuietly(data);
}
Would anything in the service be causing the stream to be read therefore making it non-accessible?
Edit3: The problem is in the service when I set the data to be returned. If I remove IOUtils.closeQuietly(data);
I'm able to get the data back just fine. However this causes issues because I leave an InputStream open... any workaround suggestions?