1 20 package org.apache.slide.projector.store; 21 22 import javax.servlet.http.Cookie ; 23 import javax.servlet.http.HttpServletRequest ; 24 import javax.servlet.http.HttpServletResponse ; 25 26 import org.apache.slide.projector.Store; 27 28 public class CookieStore implements Store { 29 protected HttpServletRequest request; 30 protected HttpServletResponse response; 31 32 public CookieStore(HttpServletRequest request, HttpServletResponse response) { 33 this.request = request; 34 this.response = response; 35 } 36 37 public void put(String key, Object value, long timeout) { 38 Cookie cookie = null; 39 if ( value instanceof Cookie ) { 40 cookie = (Cookie )value; 41 } 42 if ( value instanceof String ) { 43 cookie = new Cookie (key, (String )value); 44 } 45 if ( cookie != null ) { 46 cookie.setMaxAge((int) (timeout/1000)); 47 response.addCookie(cookie); 48 } 49 } 50 51 public void put(String key, Object value) { 52 Cookie cookie = null; 53 if ( value instanceof Cookie ) { 54 cookie = (Cookie )value; 55 } 56 if ( value instanceof String ) { 57 cookie = new Cookie (key, (String )value); 58 } 59 if ( cookie != null ) { 60 response.addCookie(cookie); 61 } 62 } 63 64 public Object get(String key) { 65 Cookie [] cookies = request.getCookies(); 66 for ( int i = 0; i < cookies.length; i++ ) { 67 if ( cookies[i].getName().equals(key) ) { 68 return cookies[i].getValue(); 69 } 70 } 71 return null; 72 } 73 74 public void dispose(String key) { 75 request.removeAttribute(key); 76 } 77 } | Popular Tags |