Search in sources :

Example 1 with ClassLoadingObjectInputStream

use of org.eclipse.jetty.util.ClassLoadingObjectInputStream in project jetty.project by eclipse.

the class JDBCSessionDataStore method load.

/** 
     * @see org.eclipse.jetty.server.session.SessionDataStore#load(java.lang.String)
     */
@Override
public SessionData load(String id) throws Exception {
    final AtomicReference<SessionData> reference = new AtomicReference<SessionData>();
    final AtomicReference<Exception> exception = new AtomicReference<Exception>();
    Runnable r = new Runnable() {

        public void run() {
            try (Connection connection = _dbAdaptor.getConnection();
                PreparedStatement statement = _sessionTableSchema.getLoadStatement(connection, id, _context);
                ResultSet result = statement.executeQuery()) {
                SessionData data = null;
                if (result.next()) {
                    data = newSessionData(id, result.getLong(_sessionTableSchema.getCreateTimeColumn()), result.getLong(_sessionTableSchema.getAccessTimeColumn()), result.getLong(_sessionTableSchema.getLastAccessTimeColumn()), result.getLong(_sessionTableSchema.getMaxIntervalColumn()));
                    data.setCookieSet(result.getLong(_sessionTableSchema.getCookieTimeColumn()));
                    data.setLastNode(result.getString(_sessionTableSchema.getLastNodeColumn()));
                    data.setLastSaved(result.getLong(_sessionTableSchema.getLastSavedTimeColumn()));
                    data.setExpiry(result.getLong(_sessionTableSchema.getExpiryTimeColumn()));
                    //TODO needed? this is part of the key now
                    data.setContextPath(result.getString(_sessionTableSchema.getContextPathColumn()));
                    //TODO needed??? this is part of the key now
                    data.setVhost(result.getString(_sessionTableSchema.getVirtualHostColumn()));
                    try (InputStream is = _dbAdaptor.getBlobInputStream(result, _sessionTableSchema.getMapColumn());
                        ClassLoadingObjectInputStream ois = new ClassLoadingObjectInputStream(is)) {
                        Object o = ois.readObject();
                        data.putAllAttributes((Map<String, Object>) o);
                    } catch (Exception e) {
                        throw new UnreadableSessionDataException(id, _context, e);
                    }
                    if (LOG.isDebugEnabled())
                        LOG.debug("LOADED session {}", data);
                } else if (LOG.isDebugEnabled())
                    LOG.debug("No session {}", id);
                reference.set(data);
            } catch (Exception e) {
                exception.set(e);
            }
        }
    };
    //ensure this runs with context classloader set
    _context.run(r);
    if (exception.get() != null)
        throw exception.get();
    return reference.get();
}
Also used : ClassLoadingObjectInputStream(org.eclipse.jetty.util.ClassLoadingObjectInputStream) ClassLoadingObjectInputStream(org.eclipse.jetty.util.ClassLoadingObjectInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Connection(java.sql.Connection) AtomicReference(java.util.concurrent.atomic.AtomicReference) PreparedStatement(java.sql.PreparedStatement) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet)

Example 2 with ClassLoadingObjectInputStream

use of org.eclipse.jetty.util.ClassLoadingObjectInputStream in project jetty.project by eclipse.

the class FileSessionDataStore method restoreAttributes.

/**
     * @param is inputstream containing session data
     * @param size number of attributes
     * @param data the data to restore to
     * @throws Exception
     */
private void restoreAttributes(InputStream is, int size, SessionData data) throws Exception {
    if (size > 0) {
        // input stream should not be closed here
        Map<String, Object> attributes = new HashMap<String, Object>();
        ClassLoadingObjectInputStream ois = new ClassLoadingObjectInputStream(is);
        for (int i = 0; i < size; i++) {
            String key = ois.readUTF();
            Object value = ois.readObject();
            attributes.put(key, value);
        }
        data.putAllAttributes(attributes);
    }
}
Also used : HashMap(java.util.HashMap) ClassLoadingObjectInputStream(org.eclipse.jetty.util.ClassLoadingObjectInputStream)

Example 3 with ClassLoadingObjectInputStream

use of org.eclipse.jetty.util.ClassLoadingObjectInputStream in project jetty.project by eclipse.

the class MongoSessionDataStore method decodeValue.

/*------------------------------------------------------------ */
protected Object decodeValue(final Object valueToDecode) throws IOException, ClassNotFoundException {
    if (valueToDecode == null || valueToDecode instanceof Number || valueToDecode instanceof String || valueToDecode instanceof Boolean || valueToDecode instanceof Date) {
        return valueToDecode;
    } else if (valueToDecode instanceof byte[]) {
        final byte[] decodeObject = (byte[]) valueToDecode;
        final ByteArrayInputStream bais = new ByteArrayInputStream(decodeObject);
        final ClassLoadingObjectInputStream objectInputStream = new ClassLoadingObjectInputStream(bais);
        return objectInputStream.readUnshared();
    } else if (valueToDecode instanceof DBObject) {
        Map<String, Object> map = new HashMap<String, Object>();
        for (String name : ((DBObject) valueToDecode).keySet()) {
            String attr = decodeName(name);
            map.put(attr, decodeValue(((DBObject) valueToDecode).get(name)));
        }
        return map;
    } else {
        throw new IllegalStateException(valueToDecode.getClass().toString());
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ClassLoadingObjectInputStream(org.eclipse.jetty.util.ClassLoadingObjectInputStream) HashMap(java.util.HashMap) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) Date(java.util.Date)

Example 4 with ClassLoadingObjectInputStream

use of org.eclipse.jetty.util.ClassLoadingObjectInputStream 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

ClassLoadingObjectInputStream (org.eclipse.jetty.util.ClassLoadingObjectInputStream)4 ByteArrayInputStream (java.io.ByteArrayInputStream)2 HashMap (java.util.HashMap)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Blob (com.google.cloud.datastore.Blob)1 DatastoreException (com.google.cloud.datastore.DatastoreException)1 BasicDBObject (com.mongodb.BasicDBObject)1 DBObject (com.mongodb.DBObject)1 InputStream (java.io.InputStream)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Date (java.util.Date)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