1 64 package com.jcorporate.expresso.core.controller.session; 65 66 import com.jcorporate.expresso.core.jsdkapi.GenericSession; 67 import com.jcorporate.expresso.core.misc.CookieUtil; 68 import com.jcorporate.expresso.core.misc.SerializableString; 69 import com.jcorporate.expresso.core.misc.StringUtil; 70 import com.jcorporate.expresso.kernel.exception.ChainedException; 71 import org.apache.log4j.Logger; 72 73 import javax.servlet.ServletException ; 74 import javax.servlet.http.Cookie ; 75 import javax.servlet.http.HttpServletRequest ; 76 import javax.servlet.http.HttpServletResponse ; 77 import java.io.Serializable ; 78 import java.util.Enumeration ; 79 80 81 100 public class HTTPPersistentSession implements PersistentSession { 101 104 private static final transient Logger log = Logger.getLogger(HTTPPersistentSession.class); 105 106 109 private transient HttpServletRequest request = null; 110 111 114 private transient HttpServletResponse response = null; 115 116 119 public HTTPPersistentSession() { 120 } 121 122 128 public HTTPPersistentSession(HttpServletRequest req, HttpServletResponse res) { 129 this(); 130 setRequest(req); 131 setResponse(res); 132 } 133 134 142 public void setAttribute(String attribName, Object attribValue) { 143 if (request == null) { 144 throw new IllegalArgumentException ("HTTPPersistentSession not initialized correctly"); 145 } 146 147 request.setAttribute(attribName, attribValue); 148 } 149 150 156 public Object getAttribute(String attribName) { 157 if (request == null) { 158 return null; 159 } 160 161 return request.getAttribute(attribName); 162 } 163 164 169 public Enumeration getAttributeNames() { 170 return request.getAttributeNames(); 171 } 172 173 183 public void setClientAttribute(String attribName, String attribValue) { 184 Cookie c1; 185 186 try { 187 if ((attribValue != null) && (attribValue.length() > 0)) { 188 c1 = new Cookie (attribName, CookieUtil.cookieEncode(attribValue)); 189 c1.setMaxAge(2592000); 190 } else { 191 c1 = new Cookie (attribName, ""); 192 c1.setMaxAge(0); 193 } 194 195 c1.setPath("/"); 196 response.addCookie(c1); 197 } catch (ChainedException ce) { 198 log.error(ce); 199 throw new IllegalArgumentException (ce.getMessage()); 200 } 201 } 202 203 204 216 public String getClientAttribute(String attribName) { 217 Cookie [] cookies = request.getCookies(); 218 219 try { 220 if (cookies != null) { 221 for (int i = 0; i < cookies.length; i++) { 222 String name = StringUtil.notNull(cookies[i].getName()); 223 String value = StringUtil.notNull(cookies[i].getValue()); 224 225 if (name.equalsIgnoreCase(attribName)) { 226 return CookieUtil.cookieDecode(value); 227 } 228 } 229 230 } 231 232 } catch (Exception ce) { 233 log.error(ce); 234 throw new IllegalArgumentException (ce.getMessage()); 235 } 236 237 return null; 238 } 239 240 245 public Enumeration getPeristentAttributeNames() { 246 try { 247 return GenericSession.getAttributeNames(request); 248 } catch (ServletException se) { 249 log.error(se); 250 } 251 252 return null; 253 } 254 255 267 public void setPersistentAttribute(String attribName, Object attribValue) { 268 if (attribValue == null) { 269 throw new IllegalArgumentException ("Value must not be null"); 270 } 271 272 if (attribValue instanceof String ) { 273 attribValue = new SerializableString((String ) attribValue); 274 } 275 276 if (attribValue instanceof Serializable ) { 277 try { 278 GenericSession.setAttribute(request, attribName, 279 (Serializable ) attribValue); 280 } catch (ServletException se) { 281 log.error(se); 282 throw new IllegalArgumentException ("Unable to store value in session:" + se.getMessage()); 283 } 284 } else { 285 throw new IllegalArgumentException ("Attribute value object must be serializable. '" + 286 attribValue.getClass().getName() + "' is not serializable."); 287 } 288 } 289 290 296 public Object getPersistentAttribute(String attribName) { 297 try { 298 return GenericSession.getAttribute(request, attribName); 299 } catch (ServletException se) { 300 log.error(se); 301 302 return null; 303 } 304 } 305 306 311 public void setRequest(HttpServletRequest req) { 312 request = req; 313 } 314 315 320 public void setResponse(HttpServletResponse res) { 321 response = res; 322 } 323 324 327 public void invalidate() { 328 try { 329 GenericSession.invalidate(request); 330 } catch (ServletException ce) { 331 log.error(ce); 332 } 333 334 request = null; 335 } 336 337 342 public void removeAttribute(String attribName) { 343 request.removeAttribute(attribName); 344 } 345 346 351 public void removePersistentAttribute(String attribName) { 352 try { 353 GenericSession.removeAttribute(request, attribName); 354 } catch (ServletException se) { 355 log.error(se); 356 } 357 } 358 } 359 | Popular Tags |