Search in sources :

Example 6 with SessionData

use of org.eclipse.jetty.server.session.SessionData 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

SessionData (org.eclipse.jetty.server.session.SessionData)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 Blob (com.google.cloud.datastore.Blob)1 DatastoreException (com.google.cloud.datastore.DatastoreException)1 BasicDBObject (com.mongodb.BasicDBObject)1 DBObject (com.mongodb.DBObject)1 MongoException (com.mongodb.MongoException)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpClient (org.eclipse.jetty.client.HttpClient)1 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)1 Request (org.eclipse.jetty.client.api.Request)1 MockDataStore (org.eclipse.jetty.memcached.sessions.MemcachedTestHelper.MockDataStore)1 CachingSessionDataStore (org.eclipse.jetty.server.session.CachingSessionDataStore)1 DefaultSessionCacheFactory (org.eclipse.jetty.server.session.DefaultSessionCacheFactory)1 SessionDataMap (org.eclipse.jetty.server.session.SessionDataMap)1 SessionDataStore (org.eclipse.jetty.server.session.SessionDataStore)1 SessionDataStoreFactory (org.eclipse.jetty.server.session.SessionDataStoreFactory)1