Search in sources :

Example 26 with ItemManager

use of gov.usgs.cida.coastalhazards.jpa.ItemManager in project coastal-hazards by USGS-CIDA.

the class DataDomainResource method getDataDomain.

@GET
@Path("/item/{id}")
public Response getDataDomain(@PathParam("id") String id, @Context Request request) {
    Response response = null;
    try (ItemManager itemManager = new ItemManager();
        DataDomainManager domainManager = new DataDomainManager()) {
        Item item = itemManager.load(id);
        if (item == null || item.getType() != Item.Type.historical) {
            throw new NotFoundException("Only historical is supported at this time");
        }
        DataDomain domain = domainManager.getDomainForItem(item);
        Response checkModified = HTTPCachingUtil.checkModified(request, domain);
        if (checkModified != null) {
            response = checkModified;
        } else {
            Gson serializer = GsonUtil.getDefault();
            String domainJson = serializer.toJson(domain);
            response = Response.ok(domainJson, MediaType.APPLICATION_JSON_TYPE).lastModified(domain.getLastModified()).build();
        }
    }
    return response;
}
Also used : Response(javax.ws.rs.core.Response) Item(gov.usgs.cida.coastalhazards.model.Item) DataDomain(gov.usgs.cida.coastalhazards.model.util.DataDomain) ItemManager(gov.usgs.cida.coastalhazards.jpa.ItemManager) DataDomainManager(gov.usgs.cida.coastalhazards.jpa.DataDomainManager) NotFoundException(javax.ws.rs.NotFoundException) Gson(com.google.gson.Gson) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 27 with ItemManager

use of gov.usgs.cida.coastalhazards.jpa.ItemManager in project coastal-hazards by USGS-CIDA.

the class DownloadResource method downloadItem.

/**
 * Downloads a zip file containing the contents 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.lang.InterruptedException
 * @throws java.util.concurrent.ExecutionException
 */
@GET
@Path("/item/{id}")
@Produces("application/zip")
public Response downloadItem(@PathParam("id") String id) throws IOException, InterruptedException, ExecutionException {
    Response response;
    Item item;
    try (ItemManager itemManager = new ItemManager()) {
        item = itemManager.load(id);
    }
    if (item == null) {
        response = Response.status(NOT_FOUND).build();
    } else {
        Download download;
        try (DownloadManager downloadManager = new DownloadManager()) {
            download = downloadManager.load(id);
        }
        if (download == null) {
            // Download was null, so we it was not previously staged. Do so now.
            LOG.debug("Download manager could not find download for item {}. A download will be staged for this item.", id);
            DownloadUtility.stageAsyncItemDownload(id);
            response = Response.status(ACCEPTED).build();
        } else {
            LOG.debug("Download manager found a download for item id {}", id);
            if (download.isProblem()) {
                LOG.debug("Download manager found a problem with download for item id {}", id);
                response = Response.status(NOT_IMPLEMENTED).build();
            } else if (StringUtils.isBlank(download.getPersistanceURI())) {
                LOG.debug("Download manager found a download with no path id {}, Item is still being created", id);
                response = Response.status(ACCEPTED).build();
            } else {
                boolean downloadFileExists;
                try (DownloadManager downloadManager = new DownloadManager()) {
                    downloadFileExists = downloadManager.downloadFileExistsOnFilesystem(download);
                }
                // Download should be on the file system. Check that it does exist
                if (!downloadFileExists) {
                    LOG.debug("Download manager found a download path that doesn't exist for id {}. Will delete and re-stage", id);
                    try (DownloadService svc = new DownloadService()) {
                        svc.delete(id);
                    }
                    DownloadUtility.stageAsyncItemDownload(id);
                    response = Response.status(ACCEPTED).build();
                } else {
                    // File was found. Load the zip file
                    File zipFile = download.fetchZipFile();
                    if (zipFile == null) {
                        LOG.debug("Download manager found could not find the zip file that was indicated in the database for item {}. Will attempt to re-stage", id);
                        try (DownloadService svc = new DownloadService()) {
                            svc.delete(id);
                        }
                        DownloadUtility.stageAsyncItemDownload(id);
                        response = Response.status(ACCEPTED).build();
                    } else {
                        String contentDisposition = "attachment; filename=\"" + id + ".zip\"";
                        response = Response.status(OK).entity(zipFile).type("application/zip").header("Content-Disposition", contentDisposition).build();
                    }
                }
            }
        }
    }
    return response;
}
Also used : Response(javax.ws.rs.core.Response) Item(gov.usgs.cida.coastalhazards.model.Item) ItemManager(gov.usgs.cida.coastalhazards.jpa.ItemManager) Download(gov.usgs.cida.coastalhazards.model.util.Download) DownloadManager(gov.usgs.cida.coastalhazards.jpa.DownloadManager) File(java.io.File) DownloadService(gov.usgs.cida.coastalhazards.service.data.DownloadService) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 28 with ItemManager

use of gov.usgs.cida.coastalhazards.jpa.ItemManager in project coastal-hazards by USGS-CIDA.

the class DownloadResource method checkItemAvailability.

@HEAD
@Path("/item/{id}")
public Response checkItemAvailability(@PathParam("id") String id) throws IOException {
    Response response;
    Item item;
    try (ItemManager itemManager = new ItemManager()) {
        item = itemManager.load(id);
    }
    if (item == null) {
        response = Response.status(NOT_FOUND).build();
    } else {
        Download download;
        try (DownloadManager downloadManager = new DownloadManager()) {
            download = downloadManager.load(id);
        }
        if (download != null) {
            LOG.debug("Download manager found a download for item id {}", id);
            String persistenceURI = download.getPersistanceURI();
            // If it is null or blank, the download has been accepted
            if (download.isProblem()) {
                LOG.debug("Download manager found a problem with download for item id {}", id);
                response = Response.status(INTERNAL_SERVER_ERROR).build();
            } else if (StringUtils.isBlank(persistenceURI)) {
                LOG.debug("Download manager found a download with no path id {}, Item is probably still being created", id);
                response = Response.status(ACCEPTED).build();
            } else {
                // If it is has content, check whether the file exists on the server
                // If it does not, that means this is a desynchronized entry in the datbase
                // and it should be deleted and reinitialized. Otherwise, this is
                // is good to go and send an OK response
                boolean existsOnFileSystem;
                try (DownloadManager downloadManager = new DownloadManager()) {
                    existsOnFileSystem = downloadManager.downloadFileExistsOnFilesystem(download);
                }
                if (!existsOnFileSystem) {
                    LOG.debug("Download manager found a download path that doesn't exist for id {}. Will delete and re-stage", id);
                    try (DownloadService svc = new DownloadService()) {
                        svc.delete(id);
                    }
                    LOG.debug("Download path for item {} was deleted in the database. Will not attempt to re-stage", id);
                    DownloadUtility.stageAsyncItemDownload(id);
                    response = Response.status(ACCEPTED).build();
                } else {
                    LOG.debug("Download manager found the download for item {}", id);
                    response = Response.status(OK).build();
                }
            }
        } else {
            LOG.debug("Download manager could not find download for item {}. A download will be staged for this item.", id);
            DownloadUtility.stageAsyncItemDownload(id);
            response = Response.status(ACCEPTED).build();
        }
    }
    return response;
}
Also used : Response(javax.ws.rs.core.Response) Item(gov.usgs.cida.coastalhazards.model.Item) ItemManager(gov.usgs.cida.coastalhazards.jpa.ItemManager) Download(gov.usgs.cida.coastalhazards.model.util.Download) DownloadManager(gov.usgs.cida.coastalhazards.jpa.DownloadManager) DownloadService(gov.usgs.cida.coastalhazards.service.data.DownloadService) Path(javax.ws.rs.Path) HEAD(javax.ws.rs.HEAD)

Example 29 with ItemManager

use of gov.usgs.cida.coastalhazards.jpa.ItemManager in project coastal-hazards by USGS-CIDA.

the class PrintRouter method useInfoPrintViewJsp.

@GET
@Produces(MediaType.TEXT_HTML)
@Path("/item/{id}")
public Response useInfoPrintViewJsp(@PathParam("id") String id) {
    Map<String, Object> map = new HashMap<>();
    try (ItemManager mgr = new ItemManager()) {
        Item item = mgr.load(id);
        if (item == null) {
            return Response.status(Response.Status.NOT_FOUND).build();
        }
        map.put("item", item);
        return Response.ok(new Viewable("/WEB-INF/jsp/ui/back/index-print.jsp", map)).build();
    }
}
Also used : Item(gov.usgs.cida.coastalhazards.model.Item) HashMap(java.util.HashMap) ItemManager(gov.usgs.cida.coastalhazards.jpa.ItemManager) Viewable(org.glassfish.jersey.server.mvc.Viewable) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 30 with ItemManager

use of gov.usgs.cida.coastalhazards.jpa.ItemManager in project coastal-hazards by USGS-CIDA.

the class PrintRouter method useAliasInfoPrintViewJsp.

@GET
@Produces(MediaType.TEXT_HTML)
@Path("/alias/{aliasId}")
public Response useAliasInfoPrintViewJsp(@PathParam("aliasId") String aliasId) {
    Map<String, Object> map = new HashMap<>();
    try (ItemManager mgr = new ItemManager();
        AliasManager amgr = new AliasManager()) {
        Alias alias = amgr.load(aliasId);
        if (alias != null) {
            Item item = mgr.load(alias.getItemId());
            if (item == null) {
                return Response.status(Response.Status.NOT_FOUND).build();
            }
            map.put("item", item);
            map.put("alias", alias);
            return Response.ok(new Viewable("/WEB-INF/jsp/ui/back/index-print.jsp", map)).build();
        } else {
            return Response.status(Response.Status.NOT_FOUND).build();
        }
    }
}
Also used : AliasManager(gov.usgs.cida.coastalhazards.jpa.AliasManager) Item(gov.usgs.cida.coastalhazards.model.Item) HashMap(java.util.HashMap) ItemManager(gov.usgs.cida.coastalhazards.jpa.ItemManager) Alias(gov.usgs.cida.coastalhazards.model.Alias) Viewable(org.glassfish.jersey.server.mvc.Viewable) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

ItemManager (gov.usgs.cida.coastalhazards.jpa.ItemManager)31 Item (gov.usgs.cida.coastalhazards.model.Item)26 Path (javax.ws.rs.Path)24 GET (javax.ws.rs.GET)22 Produces (javax.ws.rs.Produces)21 Response (javax.ws.rs.core.Response)21 HashMap (java.util.HashMap)11 NotFoundException (javax.ws.rs.NotFoundException)9 StatusManager (gov.usgs.cida.coastalhazards.jpa.StatusManager)8 Status (gov.usgs.cida.coastalhazards.model.util.Status)8 JsonSyntaxException (com.google.gson.JsonSyntaxException)7 BadRequestException (gov.usgs.cida.coastalhazards.exception.BadRequestException)7 Viewable (org.glassfish.jersey.server.mvc.Viewable)7 Gson (com.google.gson.Gson)6 JsonObject (com.google.gson.JsonObject)5 AliasManager (gov.usgs.cida.coastalhazards.jpa.AliasManager)5 Alias (gov.usgs.cida.coastalhazards.model.Alias)5 RolesAllowed (javax.annotation.security.RolesAllowed)5 ThumbnailManager (gov.usgs.cida.coastalhazards.jpa.ThumbnailManager)4 IOException (java.io.IOException)4