Search in sources :

Example 1 with Session

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;
}
Also used : Response(javax.ws.rs.core.Response) HashMap(java.util.HashMap) Session(gov.usgs.cida.coastalhazards.model.Session) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Example 2 with Session

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;
}
Also used : Response(javax.ws.rs.core.Response) Session(gov.usgs.cida.coastalhazards.model.Session) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 3 with Session

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;
}
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 4 with Session

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;
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) SessionItem(gov.usgs.cida.coastalhazards.model.SessionItem) ArrayList(java.util.ArrayList) PersistenceException(javax.persistence.PersistenceException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Session(gov.usgs.cida.coastalhazards.model.Session)

Example 5 with Session

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;
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Session(gov.usgs.cida.coastalhazards.model.Session)

Aggregations

Session (gov.usgs.cida.coastalhazards.model.Session)5 Produces (javax.ws.rs.Produces)3 Response (javax.ws.rs.core.Response)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 EntityTransaction (javax.persistence.EntityTransaction)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 DownloadStagingUnsuccessfulException (gov.usgs.cida.coastalhazards.exception.DownloadStagingUnsuccessfulException)1 DownloadManager (gov.usgs.cida.coastalhazards.jpa.DownloadManager)1 SessionItem (gov.usgs.cida.coastalhazards.model.SessionItem)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 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 PersistenceException (javax.persistence.PersistenceException)1 Consumes (javax.ws.rs.Consumes)1 NotFoundException (javax.ws.rs.NotFoundException)1