@@ -0,0 +1,1800 @@
+package com.github.ddth.plommon.utils;%0A%0Aimport java.util.HashMap;%0Aimport java.util.Map;%0A%0Aimport play.mvc.Controller;%0A%0A/**%0A * Session utilities with enhanced functionality.%0A * %0A * %3Cul%3E%0A * %3Cli%3EComplex data structure for session items. Session items are automatically%0A * serialized/deserialized using JSON format.%3C/li%3E%0A * %3Cli%3ETTL: session items can have expiry.%3C/li%3E%0A * %3C/ul%3E%0A * %0A * @author Thanh Nguyen %3Cbtnguyen2k@gmail.com%3E%0A * @since 0.1.0%0A */%0Apublic class SessionUtils %7B%0A%09/**%0A%09 * Gets a session item.%0A%09 * %0A%09 * @param key%0A%09 * @return the session item, or %3Ccode%3Enull%3C/code%3E if the item does not exist%0A%09 *         or it has been expired%0A%09 */%0A%09@SuppressWarnings(%22unchecked%22)%0A%09public static Object getSession(String key) %7B%0A%09%09String sValue = Controller.session(key);%0A%09%09if (sValue == null) %7B%0A%09%09%09return null;%0A%09%09%7D%0A%09%09try %7B%0A%09%09%09Map%3CString, Object%3E sEntry = JsonUtils.fromJsonString(sValue,%0A%09%09%09%09%09Map.class);%0A%09%09%09Long expiry = DPathUtils.getValue(sEntry, %22expiry%22, Long.class);%0A%09%09%09if (expiry == null%0A%09%09%09%09%09%7C%7C expiry.longValue() %3E System.currentTimeMillis()) %7B%0A%09%09%09%09return sEntry.get(%22value%22);%0A%09%09%09%7D else %7B%0A%09%09%09%09Controller.session().remove(key);%0A%09%09%09%09return null;%0A%09%09%09%7D%0A%09%09%7D catch (Exception e) %7B%0A%09%09%09return null;%0A%09%09%7D%0A%09%7D%0A%0A%09/**%0A%09 * Sets a session item.%0A%09 * %0A%09 * @param key%0A%09 * @param value%0A%09 */%0A%09public static void setSession(String key, Object value) %7B%0A%09%09setSession(key, value, 0);%0A%09%7D%0A%0A%09/**%0A%09 * Sets a session item.%0A%09 * %0A%09 * @param key%0A%09 * @param value%0A%09 * @param ttl%0A%09 *            time-to-live in seconds%0A%09 */%0A%09public static void setSession(String key, Object value, long ttl) %7B%0A%09%09Map%3CString, Object%3E sEntry = new HashMap%3CString, Object%3E();%0A%09%09sEntry.put(%22value%22, value);%0A%09%09if (ttl %3E 0) %7B%0A%09%09%09sEntry.put(%22expiry%22, System.currentTimeMillis() + ttl * 1000);%0A%09%09%7D%0A%09%09Controller.session(key, JsonUtils.toJsonString(value));%0A%09%7D%0A%7D%0A

Add class SessionUtils