Search in sources :

Example 1 with Download

use of gov.usgs.cida.coastalhazards.model.util.Download 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)

Example 2 with Download

use of gov.usgs.cida.coastalhazards.model.util.Download in project coastal-hazards by USGS-CIDA.

the class DownloadService method delete.

public boolean delete(String id) {
    boolean deleted = false;
    EntityTransaction transaction = manager.getTransaction();
    try {
        transaction.begin();
        Download download = manager.load(id);
        if (download == null) {
            throw new NotFoundException();
        }
        deleted = deleteDownload(download);
        transaction.commit();
    } catch (Exception ex) {
        log.error("Unable to delete item", ex);
        if (transaction.isActive()) {
            transaction.rollback();
        }
    }
    return deleted;
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) NotFoundException(javax.ws.rs.NotFoundException) Download(gov.usgs.cida.coastalhazards.model.util.Download) URISyntaxException(java.net.URISyntaxException) NotFoundException(javax.ws.rs.NotFoundException)

Example 3 with Download

use of gov.usgs.cida.coastalhazards.model.util.Download in project coastal-hazards by USGS-CIDA.

the class DownloadService method deleteAll.

public int deleteAll(List<String> downloadIds) {
    int deleted = 0;
    EntityTransaction transaction = manager.getTransaction();
    try {
        transaction.begin();
        for (String id : downloadIds) {
            Download download = manager.load(id);
            if (download != null) {
                if (deleteDownload(download)) {
                    deleted++;
                }
            }
        }
        transaction.commit();
    } catch (Exception ex) {
        log.error("Unable to delete items", ex);
        if (transaction.isActive()) {
            transaction.rollback();
        }
    }
    return deleted;
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) Download(gov.usgs.cida.coastalhazards.model.util.Download) URISyntaxException(java.net.URISyntaxException) NotFoundException(javax.ws.rs.NotFoundException)

Example 4 with Download

use of gov.usgs.cida.coastalhazards.model.util.Download in project coastal-hazards by USGS-CIDA.

the class DownloadManager method load.

/**
 * Attempts to load the Download object which specifies, among other things,
 * where on the file system the file sits.
 *
 * @param id
 * @return
 */
public Download load(String id) {
    Download download = null;
    Query selectQuery = em.createQuery(HQL_SELECT_BY_ID);
    selectQuery.setParameter("id", id);
    List<Download> resultList = selectQuery.getResultList();
    if (!resultList.isEmpty()) {
        download = resultList.get(0);
    }
    return download;
}
Also used : Query(javax.persistence.Query) Download(gov.usgs.cida.coastalhazards.model.util.Download)

Example 5 with Download

use of gov.usgs.cida.coastalhazards.model.util.Download in project coastal-hazards by USGS-CIDA.

the class DownloadResource method deleteStagedItem.

@RolesAllowed({ CoastalHazardsTokenBasedSecurityFilter.CCH_ADMIN_ROLE })
@DELETE
@Produces("application/json")
@Path("/item/{id}")
public Response deleteStagedItem(@PathParam("id") String itemId, @Context HttpServletRequest request) {
    Response response = null;
    try (DownloadService downloadService = new DownloadService()) {
        Download download = downloadService.get(itemId);
        if (download == null) {
            throw new NotFoundException();
        }
        boolean deleted = downloadService.delete(itemId);
        response = Response.ok("{\"deleted\":\"" + deleted + "\"}", MediaType.APPLICATION_JSON_TYPE).build();
    }
    return response;
}
Also used : Response(javax.ws.rs.core.Response) FileNotFoundException(java.io.FileNotFoundException) NotFoundException(javax.ws.rs.NotFoundException) Download(gov.usgs.cida.coastalhazards.model.util.Download) DownloadService(gov.usgs.cida.coastalhazards.service.data.DownloadService) Path(javax.ws.rs.Path) RolesAllowed(javax.annotation.security.RolesAllowed) DELETE(javax.ws.rs.DELETE) Produces(javax.ws.rs.Produces)

Aggregations

Download (gov.usgs.cida.coastalhazards.model.util.Download)8 DownloadManager (gov.usgs.cida.coastalhazards.jpa.DownloadManager)4 NotFoundException (javax.ws.rs.NotFoundException)4 Path (javax.ws.rs.Path)4 Produces (javax.ws.rs.Produces)4 Response (javax.ws.rs.core.Response)4 DownloadService (gov.usgs.cida.coastalhazards.service.data.DownloadService)3 URISyntaxException (java.net.URISyntaxException)3 GET (javax.ws.rs.GET)3 ItemManager (gov.usgs.cida.coastalhazards.jpa.ItemManager)2 Item (gov.usgs.cida.coastalhazards.model.Item)2 File (java.io.File)2 FileNotFoundException (java.io.FileNotFoundException)2 EntityTransaction (javax.persistence.EntityTransaction)2 Gson (com.google.gson.Gson)1 DownloadStagingUnsuccessfulException (gov.usgs.cida.coastalhazards.exception.DownloadStagingUnsuccessfulException)1 Session (gov.usgs.cida.coastalhazards.model.Session)1 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 RolesAllowed (javax.annotation.security.RolesAllowed)1