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();
}
Aggregations