Search in sources :

Example 1 with DownloadStagingUnsuccessfulException

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;
}
Also used : Response(javax.ws.rs.core.Response) FileNotFoundException(java.io.FileNotFoundException) FileNotFoundException(java.io.FileNotFoundException) NotFoundException(javax.ws.rs.NotFoundException) DownloadStagingUnsuccessfulException(gov.usgs.cida.coastalhazards.exception.DownloadStagingUnsuccessfulException) URISyntaxException(java.net.URISyntaxException) DownloadManager(gov.usgs.cida.coastalhazards.jpa.DownloadManager) File(java.io.File) Download(gov.usgs.cida.coastalhazards.model.util.Download) URI(java.net.URI) Session(gov.usgs.cida.coastalhazards.model.Session) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

DownloadStagingUnsuccessfulException (gov.usgs.cida.coastalhazards.exception.DownloadStagingUnsuccessfulException)1 DownloadManager (gov.usgs.cida.coastalhazards.jpa.DownloadManager)1 Session (gov.usgs.cida.coastalhazards.model.Session)1 Download (gov.usgs.cida.coastalhazards.model.util.Download)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 GET (javax.ws.rs.GET)1 NotFoundException (javax.ws.rs.NotFoundException)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 Response (javax.ws.rs.core.Response)1