@Override public void persistSessions(String deploymentName, Map<String, PersistentSession> sessionData) { try { final Marshaller marshaller = createMarshaller(); try { final Map<String, SessionEntry> serializedData = new HashMap<String, SessionEntry>(); for (Map.Entry<String, PersistentSession> sessionEntry : sessionData.entrySet()) { Map<String, byte[]> data = new HashMap<String, byte[]>(); for (Map.Entry<String, Object> sessionAttribute : sessionEntry.getValue().getSessionData().entrySet()) { try { final ByteArrayOutputStream out = new ByteArrayOutputStream(); marshaller.start(new OutputStreamByteOutput(out)); marshaller.writeObject(sessionAttribute.getValue()); marshaller.finish(); data.put(sessionAttribute.getKey(), out.toByteArray()); } catch (Exception e) { UndertowLogger.ROOT_LOGGER.failedToPersistSessionAttribute(sessionAttribute.getKey(), sessionAttribute.getValue(), sessionEntry.getKey(), e); } } serializedData.put(sessionEntry.getKey(), new SessionEntry(sessionEntry.getValue().getExpiration(), data)); } persistSerializedSessions(deploymentName, serializedData); } finally { marshaller.close(); } } catch (Exception e) { UndertowServletLogger.ROOT_LOGGER.failedToPersistSessions(e); } }
SerializablePersistentSession(PersistentSession session) { this.expiration = session.getExpiration(); this.sessionData = new LinkedHashMap<>(session.getSessionData()); }
@Override public void persistSessions(String deploymentName, Map<String, PersistentSession> sessionData) { try { final Map<String, SessionEntry> serializedData = new HashMap<>(); for (Map.Entry<String, PersistentSession> sessionEntry : sessionData.entrySet()) { Map<String, byte[]> data = new HashMap<>(); for (Map.Entry<String, Object> sessionAttribute : sessionEntry.getValue().getSessionData().entrySet()) { try { final ByteArrayOutputStream out = new ByteArrayOutputStream(); final ObjectOutputStream objectOutputStream = new ObjectOutputStream(out); objectOutputStream.writeObject(sessionAttribute.getValue()); objectOutputStream.close(); data.put(sessionAttribute.getKey(), out.toByteArray()); } catch (Exception e) { UndertowServletLogger.ROOT_LOGGER.failedToPersistSessionAttribute(sessionAttribute.getKey(), sessionAttribute.getValue(), sessionEntry.getKey(), e); } } serializedData.put(sessionEntry.getKey(), new SessionEntry(sessionEntry.getValue().getExpiration(), data)); } data.put(deploymentName, serializedData); } catch (Exception e) { UndertowServletLogger.ROOT_LOGGER.failedToPersistSessions(e); } }
@Override public void persistSessions(String deploymentName, Map<String, PersistentSession> sessionData) { try { final Marshaller marshaller = createMarshaller(); try { final Map<String, SessionEntry> serializedData = new HashMap<String, SessionEntry>(); for (Map.Entry<String, PersistentSession> sessionEntry : sessionData.entrySet()) { Map<String, byte[]> data = new HashMap<String, byte[]>(); for (Map.Entry<String, Object> sessionAttribute : sessionEntry.getValue().getSessionData().entrySet()) { try { final ByteArrayOutputStream out = new ByteArrayOutputStream(); marshaller.start(new OutputStreamByteOutput(out)); marshaller.writeObject(sessionAttribute.getValue()); marshaller.finish(); data.put(sessionAttribute.getKey(), out.toByteArray()); } catch (Exception e) { UndertowLogger.ROOT_LOGGER.failedToPersistSessionAttribute(sessionAttribute.getKey(), sessionAttribute.getValue(), sessionEntry.getKey(), e); } } serializedData.put(sessionEntry.getKey(), new SessionEntry(sessionEntry.getValue().getExpiration(), data)); } persistSerializedSessions(deploymentName, serializedData); } finally { marshaller.close(); } } catch (Exception e) { UndertowServletLogger.ROOT_LOGGER.failedToPersistSessions(e); } }
@Override public void persistSessions(String deploymentName, Map<String, PersistentSession> sessionData) { try { final Marshaller marshaller = createMarshaller(); try { final Map<String, SessionEntry> serializedData = new HashMap<String, SessionEntry>(); for (Map.Entry<String, PersistentSession> sessionEntry : sessionData.entrySet()) { Map<String, byte[]> data = new HashMap<String, byte[]>(); for (Map.Entry<String, Object> sessionAttribute : sessionEntry.getValue().getSessionData().entrySet()) { try { final ByteArrayOutputStream out = new ByteArrayOutputStream(); marshaller.start(new OutputStreamByteOutput(out)); marshaller.writeObject(sessionAttribute.getValue()); marshaller.finish(); data.put(sessionAttribute.getKey(), out.toByteArray()); } catch (Exception e) { UndertowLogger.ROOT_LOGGER.failedToPersistSessionAttribute(sessionAttribute.getKey(), sessionAttribute.getValue(), sessionEntry.getKey(), e); } } serializedData.put(sessionEntry.getKey(), new SessionEntry(sessionEntry.getValue().getExpiration(), data)); } persistSerializedSessions(deploymentName, serializedData); } finally { marshaller.close(); } } catch (Exception e) { UndertowServletLogger.ROOT_LOGGER.failedToPersistSessions(e); } }
@Override public void handleRequest(HttpServerExchange exchange) throws Exception { final String incomingSessionId = servletContext.getSessionConfig().findSessionId(exchange); if (incomingSessionId == null || !data.containsKey(incomingSessionId)) { next.handleRequest(exchange); return; } //we have some old data PersistentSession result = data.remove(incomingSessionId); if (result != null) { long time = System.currentTimeMillis(); if (time < result.getExpiration().getTime()) { final HttpSessionImpl session = servletContext.getSession(exchange, true); final HttpSessionEvent event = new HttpSessionEvent(session); for (Map.Entry<String, Object> entry : result.getSessionData().entrySet()) { if (entry.getValue() instanceof HttpSessionActivationListener) { ((HttpSessionActivationListener) entry.getValue()).sessionDidActivate(event); } if(entry.getKey().startsWith(HttpSessionImpl.IO_UNDERTOW)) { session.getSession().setAttribute(entry.getKey(), entry.getValue()); } else { session.setAttribute(entry.getKey(), entry.getValue()); } } } } next.handleRequest(exchange); }
private void writeSession(final Path deploymentDir, final String sessionId, final PersistentSession persistentSession) { final Date expDate = persistentSession.getExpiration(); if (expDate == null) return; // No expiry date? no serialization final Map<String, Object> sessionData = persistentSession.getSessionData(); if (sessionData == null) return; // No sessionData? no serialization final File sessionFile = deploymentDir.resolve(sessionId).toFile(); try (final ObjectOutputStream draftOutputStream = new ObjectOutputStream(new NullOutputStream())) { try (final ObjectOutputStream sessionOutputStream = new ObjectOutputStream( new FileOutputStream(sessionFile))) { sessionOutputStream.writeLong(expDate.getTime()); // The date is stored as long sessionData.forEach( (attribute, object) -> writeSessionAttribute(draftOutputStream, sessionOutputStream, attribute, object)); } } catch (IOException | CancellationException e) { LOGGER.log(Level.SEVERE, e, () -> "Cannot save sessions in " + sessionFile); } }