Java 类org.glassfish.jersey.server.CloseableService 实例源码

项目:proarc    文件:ExportResource.java   
/**
 * Gets the exported package built by {@link #newDesaExport() } with {@code forDownload=true}.
 * The package data are removed after completion of the response.
 *
 * @param token token to identify the prepared package
 * @return the package contents in ZIP format
 */
@GET
@Path(ExportResourceApi.DESA_PATH)
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response getDesaExport(
        @QueryParam(ExportResourceApi.RESULT_TOKEN) String token,
        @Context CloseableService finalizer
        ) {

    URI exportUri = user.getExportFolder();
    File exportFolder = new File(exportUri);
    final File file = DesaExport.findExportedPackage(exportFolder, token);
    if (file == null) {
        return Response.status(Status.NOT_FOUND).type(MediaType.TEXT_PLAIN_TYPE)
                .entity("The contents not found!").build();
    }
    finalizer.add(new Closeable() {

        @Override
        public void close() throws IOException {
            FileUtils.deleteQuietly(file.getParentFile());
        }
    });
    return Response.ok(file, MediaType.APPLICATION_OCTET_STREAM)
            .header("Content-Disposition", "attachment; filename=\"" + file.getName() + '"')
            .build();
}