package at.gv.egovernment.moa.id.data; import java.util.HashMap; /** * The CookieManager is a singleton to manage a Cookie-Object for * each session * @author Stefan Knirsch * @version $Id$ * */ public class CookieManager { /** the singleton instance of the CookieManager */ private static CookieManager instance; /** a HashMap to bind a Cookie-object to every single session*/ private static HashMap cookies = new HashMap(); /** * Create a singleton of the CookieManager * @return CookieManager */ public static CookieManager getInstance() { if(instance==null) instance=new CookieManager(); return instance; } /** * Save a cookie to a specified session-id * @param String id the session id * @param String cookie_string - the complete 'Set-Cookie' String from the OnlineApplication */ public void saveCookie(String id,String cookie_string) { getCookieWithID(id).setCookie(cookie_string); } /** * Method saveOldCookies. * @param id * @param cookie_string */ public void saveOldCookies(String id,String cookie_string) { getCookieWithID(id).saveOldCookies(cookie_string); } /** * Get a Cookie-Object for a specified session-id * @param String id the session id * @return Cookie object containing all saved cookies for this session */ public Cookie getCookieWithID(String id) { Cookie c = null; if(cookies.containsKey(id)) c = (Cookie)cookies.get(id); else { c = new Cookie(); cookies.put(id,c); } return c; } /** * Get a String containing all cookies of a specified session-id * saved in that session seperated by '; ' to be sent back to * the Online-Application * @param id the session-id * @return String containing all cookies saved in that session seperated by '; ' */ public String getCookie(String id) { Cookie result = (Cookie)cookies.get((String)id); if (result==null) return null; return result.getCookies(); } /** * Adds a Cookie for a special session from a response with * response-code 401 to the cookie-pool for sending it back * to the browser / client * @param id the session-id * @param String: the complete 'Set-Cookie' - String */ public void add401(String id,String value) { getCookieWithID(id).add401(value); } /** * Clear the 401 cookie-pool of a session * @param id the session-id */ public void clear401(String id) { getCookieWithID(id).clear401(); } /** * Get the HashMap containing all cookies of a session to be sent to the browser / client * @param id the session-id * @return HashMap with all cookies */ public HashMap get401(String id) { return getCookieWithID(id).get401(); } }