@GET @Path("retrieveFile") @Produces(MediaType.TEXT_PLAIN) public Response retrieveFileFromImmutableStorage(@QueryParam("address") String address) { try (InputStream data = getAccountabilityManager().retrieveState(address).get()) { String fileAsString = IOUtils.toString(data, Charset.defaultCharset().toString()); return Response .ok(fileAsString, MediaType.TEXT_PLAIN) .build(); } catch (IOException | InterruptedException | ExecutionException | AccountabilityException e) { LOGGER.error("Cannot retrieve file ({}) from immutable storage. Reason: {}", address, e.getMessage()); throw createException(e); } } }
@GET @Path("downloadFile") @Produces(MediaType.APPLICATION_OCTET_STREAM) public Response downloadFileFromImmutableStorage(@QueryParam("address") String address, @QueryParam("filename") String filename) { StreamingOutput fileStream = output -> { try (InputStream data = getAccountabilityManager().retrieveState(address).get()) { IOUtils.copy(data, output); output.flush(); } catch (Exception e) { LOGGER.error("Cannot download file ({}) from immutable storage. Reason: {}", address, e.getMessage()); throw createException(e); } }; return Response .ok(fileStream, MediaType.APPLICATION_OCTET_STREAM) .header("content-disposition", "attachment; filename = " + filename) .build(); }