private void configureSession(WebAppContext context) { SessionHandler handler = context.getSessionHandler(); Duration sessionTimeout = getSession().getTimeout(); handler.setMaxInactiveInterval( isNegative(sessionTimeout) ? -1 : (int) sessionTimeout.getSeconds()); if (getSession().isPersistent()) { DefaultSessionCache cache = new DefaultSessionCache(handler); FileSessionDataStore store = new FileSessionDataStore(); store.setStoreDir(getValidSessionStoreDir()); cache.setSessionDataStore(store); handler.setSessionCache(cache); } }
/** * @see org.eclipse.jetty.server.session.SessionDataStoreFactory#getSessionDataStore(org.eclipse.jetty.server.session.SessionHandler) */ @Override public SessionDataStore getSessionDataStore(SessionHandler handler) { FileSessionDataStore fsds = new FileSessionDataStore(); fsds.setDeleteUnrestorableFiles(isDeleteUnrestorableFiles()); fsds.setStoreDir(getStoreDir()); fsds.setGracePeriodSec(getGracePeriodSec()); fsds.setSavePeriodSec(getSavePeriodSec()); return fsds; }
.filter(p->!Files.isDirectory(p)).filter(p->isSessionFilename(p.getFileName().toString())) .forEach(p->{ sweepFile(now, p); String context = getContextFromFilename(filename); String sessionIdWithContext = getIdWithContextFromFilename(filename); if (sessionIdWithContext != null) long existingExpiry = getExpiryFromFilename(existing); long thisExpiry = getExpiryFromFilename(filename);
/** * @see org.eclipse.jetty.server.session.AbstractSessionDataStore#doStore(java.lang.String, org.eclipse.jetty.server.session.SessionData, long) */ @Override public void doStore(String id, SessionData data, long lastSaveTime) throws Exception { File file = null; if (_storeDir != null) { delete(id); //make a fresh file using the latest session expiry String filename = getIdWithContextAndExpiry(data); String idWithContext = getIdWithContext(id); file = new File(_storeDir, filename); try(FileOutputStream fos = new FileOutputStream(file,false)) { save(fos, id, data); _sessionFileMap.put(idWithContext, filename); } catch (Exception e) { if (file != null) file.delete(); // No point keeping the file if we didn't save the whole session throw new UnwriteableSessionDataException(id, _context,e); } } }
long expiry = getExpiryFromFilename(filename); if (expiry > 0 && expiry < now) expired.add(getIdFromFilename(filename)); String filename = _sessionFileMap.get(getIdWithContext(c)); if (filename == null) expired.add(c); sweepDisk();
String idWithContext = getIdWithContext(id); String filename = _sessionFileMap.get(idWithContext); if (filename == null) SessionData data = load(in, id); data.setLastSaved(file.lastModified()); reference.set(data); if (isDeleteUnrestorableFiles() && file.exists() && file.getParentFile().equals(_storeDir)) delete(id); LOG.warn("Deleted unrestorable file for session {}", id);
/** * Delete a session * * @param id session id */ @Override public boolean delete(String id) throws Exception { if (_storeDir != null) { //remove from our map String filename = _sessionFileMap.remove(getIdWithContext(id)); if (filename == null) return false; //remove the file return deleteFile(filename); } return false; }
/** * @see org.eclipse.jetty.server.session.SessionDataStore#exists(java.lang.String) */ @Override public boolean exists(String id) throws Exception { String idWithContext = getIdWithContext(id); String filename = _sessionFileMap.get(idWithContext); if (filename == null) return false; //check the expiry long expiry = getExpiryFromFilename(filename); if (expiry <= 0) return true; //never expires else return (expiry > System.currentTimeMillis()); //hasn't yet expired }
public void setStoreDir(File storeDir) { checkStarted(); _storeDir = storeDir; }
/** * Check if the filename matches our session pattern * and is a session for our context. * * @param filename the filename to check * @return true if the filename has the correct filename format and is for this context */ protected boolean isOurContextSessionFilename (String filename) { if (StringUtil.isBlank(filename)) return false; String[] parts = filename.split("_"); //Need at least 4 parts for a valid filename if (parts.length < 4) return false; //Also needs to be for our context String context = getContextFromFilename(filename); if (context == null) return false; return (_contextString.equals(context)); }
/** * Get the session id with its context and its expiry time * @param data * @return the session id plus context and expiry */ protected String getIdWithContextAndExpiry (SessionData data) { return ""+data.getExpiry()+"_"+getIdWithContext(data.getId()); }
/** * Check to see if the expiry on the file is very old, and * delete the file if so. "Old" means that it expired at least * 5 gracePeriods ago. The session can belong to any context. * * @param now the time now in msec * @param p the file to check * * @throws Exception indicating error in sweep */ public void sweepFile (long now, Path p) throws Exception { if (p == null) return; try { long expiry = getExpiryFromFilename(p.getFileName().toString()); //files with 0 expiry never expire if (expiry >0 && ((now - expiry) >= (5*TimeUnit.SECONDS.toMillis(_gracePeriodSec)))) { Files.deleteIfExists(p); if (LOG.isDebugEnabled()) LOG.debug("Sweep deleted {}", p.getFileName()); } } catch (NumberFormatException e) { LOG.warn("Not valid session filename {}", p.getFileName(), e); } }
public void setDeleteUnrestorableFiles(boolean deleteUnrestorableFiles) { checkStarted(); _deleteUnrestorableFiles = deleteUnrestorableFiles; }