use of gov.usgs.cida.coastalhazards.exception.DownloadStagingUnsuccessfulException in project coastal-hazards by USGS-CIDA.
the class DownloadResource method getSession.
/**
* TODO this is in need of refactor to get it in line with item download
* Retrieves representation of an instance of
* gov.usgs.cida.coastalhazards.model.Item
*
* @param id identifier of requested item
* @return JSON representation of the item(s)
* @throws java.io.IOException
* @throws java.security.NoSuchAlgorithmException
*/
@GET
@Path("/view/{id}")
@Produces("application/zip")
public Response getSession(@PathParam("id") String id) throws IOException, NoSuchAlgorithmException {
Response response = null;
String sessionJSON = sessionManager.load(id);
try (DownloadManager downloadManager = new DownloadManager()) {
if (sessionJSON == null) {
throw new NotFoundException();
} else {
File zipFile = null;
Download download;
try {
download = downloadManager.load(id);
if (download != null && download.getPersistanceURI() != null) {
zipFile = new File(new URI(download.getPersistanceURI()));
if (!zipFile.exists()) {
throw new FileNotFoundException();
}
} else {
throw new FileNotFoundException();
}
} catch (FileNotFoundException | URISyntaxException ex) {
Session session = Session.fromJSON(sessionJSON);
File stagingDir = DownloadUtility.createDownloadStagingArea();
boolean staged = DownloadUtility.stageSessionDownload(session, stagingDir);
if (staged) {
download = new Download();
download = DownloadUtility.zipStagingAreaForDownload(stagingDir, download);
download.setSessionId(id);
zipFile = download.fetchZipFile();
if (zipFile == null) {
throw new DownloadStagingUnsuccessfulException();
}
downloadManager.save(download);
} else {
throw new DownloadStagingUnsuccessfulException();
}
}
String contentDisposition = "attachment; filename=\"" + id + ".zip\"";
response = Response.ok(zipFile, "application/zip").header("Content-Disposition", contentDisposition).build();
}
}
return response;
}
Aggregations