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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations