use of gov.usgs.cida.coastalhazards.model.Session in project coastal-hazards by USGS-CIDA.
the class ViewResource method postSession.
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response postSession(String content) throws SessionIOException, NoSuchAlgorithmException {
// First check to see if there's already a session saved for this item
Session session = Session.fromJSON(content);
String existingSession = sessionIo.load(session.getId());
String sid = null;
if (null != existingSession) {
sid = session.getId();
} else {
sid = sessionIo.save(content);
}
Response response;
if (null == sid) {
response = Response.status(Response.Status.BAD_REQUEST).build();
} else {
Map<String, Object> ok = new HashMap<>();
ok.put("sid", sid);
response = Response.ok(GsonUtil.getDefault().toJson(ok, HashMap.class), MediaType.APPLICATION_JSON_TYPE).build();
}
return response;
}
use of gov.usgs.cida.coastalhazards.model.Session in project coastal-hazards by USGS-CIDA.
the class ViewResource method getSession.
@GET
@Path("/{sid}")
@Produces(MediaType.APPLICATION_JSON)
public Response getSession(@PathParam("sid") String sid, @Context Request request) throws SessionIOException {
String jsonSession = sessionIo.load(sid);
Response response;
if (null == jsonSession) {
response = Response.status(Response.Status.NOT_FOUND).build();
} else {
Session session = GsonUtil.getDefault().fromJson(jsonSession, Session.class);
Response checkModified = HTTPCachingUtil.checkModified(request, session);
if (checkModified != null) {
response = checkModified;
} else {
response = Response.ok(jsonSession, MediaType.APPLICATION_JSON_TYPE).lastModified(session.getLastModified()).build();
}
}
return response;
}
use of gov.usgs.cida.coastalhazards.model.Session 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.Session in project coastal-hazards by USGS-CIDA.
the class SessionManager method removeItemFromSessions.
public synchronized boolean removeItemFromSessions(String itemId) {
EntityTransaction transaction = em.getTransaction();
for (String sessionId : getSessionIdsByItemId(itemId)) {
try {
Session sessionObj = loadSession(sessionId);
List<SessionItem> newItems = new ArrayList<>(sessionObj.getItems());
for (SessionItem item : sessionObj.getItems()) {
if (item.getItemId().equals(itemId)) {
newItems.remove(item);
}
}
transaction.begin();
em.remove(sessionObj);
sessionObj.setItems(newItems);
em.persist(sessionObj);
transaction.commit();
} catch (Exception ex) {
if (transaction.isActive()) {
transaction.rollback();
}
log.error("An error occurred while modifying the session. Error: " + ex.getMessage());
return false;
}
}
return true;
}
use of gov.usgs.cida.coastalhazards.model.Session in project coastal-hazards by USGS-CIDA.
the class SessionManager method save.
@Override
public synchronized String save(String session) {
String id = "ERR";
EntityTransaction transaction = em.getTransaction();
try {
transaction.begin();
Session sessionObj = Session.fromJSON(session);
em.persist(sessionObj);
id = sessionObj.getId();
transaction.commit();
} catch (NoSuchAlgorithmException ex) {
if (transaction.isActive()) {
transaction.rollback();
}
}
return id;
}
Aggregations