Search in sources :

Example 1 with Blob

use of com.google.cloud.datastore.Blob in project jetty.project by eclipse.

the class GCloudSessionDataStore method sessionFromEntity.

/**
     * Generate SessionData from an Entity retrieved from gcloud datastore.
     * @param entity the entity
     * @return the session data
     * @throws Exception if unable to get the entity
     */
protected SessionData sessionFromEntity(Entity entity) throws Exception {
    if (entity == null)
        return null;
    final AtomicReference<SessionData> reference = new AtomicReference<SessionData>();
    final AtomicReference<Exception> exception = new AtomicReference<Exception>();
    Runnable load = new Runnable() {

        public void run() {
            try {
                //turn an Entity into a Session
                String id = entity.getString(_model.getId());
                String contextPath = entity.getString(_model.getContextPath());
                String vhost = entity.getString(_model.getVhost());
                long accessed = entity.getLong(_model.getAccessed());
                long lastAccessed = entity.getLong(_model.getLastAccessed());
                long createTime = entity.getLong(_model.getCreateTime());
                long cookieSet = entity.getLong(_model.getCookieSetTime());
                String lastNode = entity.getString(_model.getLastNode());
                long lastSaved = 0;
                //for compatibility with previously saved sessions, lastSaved may not be present
                try {
                    lastSaved = entity.getLong(_model.getLastSaved());
                } catch (DatastoreException e) {
                    LOG.ignore(e);
                }
                long expiry = entity.getLong(_model.getExpiry());
                long maxInactive = entity.getLong(_model.getMaxInactive());
                Blob blob = (Blob) entity.getBlob(_model.getAttributes());
                SessionData session = newSessionData(id, createTime, accessed, lastAccessed, maxInactive);
                session.setLastNode(lastNode);
                session.setContextPath(contextPath);
                session.setVhost(vhost);
                session.setCookieSet(cookieSet);
                session.setLastNode(lastNode);
                session.setLastSaved(lastSaved);
                session.setExpiry(expiry);
                try (ClassLoadingObjectInputStream ois = new ClassLoadingObjectInputStream(blob.asInputStream())) {
                    Object o = ois.readObject();
                    session.putAllAttributes((Map<String, Object>) o);
                } catch (Exception e) {
                    throw new UnreadableSessionDataException(id, _context, e);
                }
                reference.set(session);
            } catch (Exception e) {
                exception.set(e);
            }
        }
    };
    //ensure this runs in the context classloader
    _context.run(load);
    if (exception.get() != null)
        throw exception.get();
    return reference.get();
}
Also used : Blob(com.google.cloud.datastore.Blob) ClassLoadingObjectInputStream(org.eclipse.jetty.util.ClassLoadingObjectInputStream) AtomicReference(java.util.concurrent.atomic.AtomicReference) SessionData(org.eclipse.jetty.server.session.SessionData) DatastoreException(com.google.cloud.datastore.DatastoreException) DatastoreException(com.google.cloud.datastore.DatastoreException) UnreadableSessionDataException(org.eclipse.jetty.server.session.UnreadableSessionDataException) UnwriteableSessionDataException(org.eclipse.jetty.server.session.UnwriteableSessionDataException) UnreadableSessionDataException(org.eclipse.jetty.server.session.UnreadableSessionDataException)

Aggregations

Blob (com.google.cloud.datastore.Blob)1 DatastoreException (com.google.cloud.datastore.DatastoreException)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 SessionData (org.eclipse.jetty.server.session.SessionData)1 UnreadableSessionDataException (org.eclipse.jetty.server.session.UnreadableSessionDataException)1 UnwriteableSessionDataException (org.eclipse.jetty.server.session.UnwriteableSessionDataException)1 ClassLoadingObjectInputStream (org.eclipse.jetty.util.ClassLoadingObjectInputStream)1