1 64 65 package com.jcorporate.expresso.core.jsdkapi; 66 67 import com.jcorporate.expresso.core.misc.StringUtil; 68 69 import javax.servlet.ServletException ; 70 import javax.servlet.http.HttpServletRequest ; 71 import javax.servlet.http.HttpSession ; 72 import java.io.Serializable ; 73 import java.util.Enumeration ; 74 75 76 79 public class ServletAPI2_2Session 80 implements APIAwareSession { 81 private static final String thisClass = ServletAPI2_2Session.class.getName() + "."; 82 83 88 public Serializable getAttribute(HttpServletRequest req, String code) 89 throws ServletException { 90 StringUtil.assertNotBlank(code, "You must specify an attribute name"); 91 92 String myName = thisClass + 93 "getAttribute(HttpServletRequest, String)"; 94 HttpSession session = req.getSession(true); 95 96 if (session == null) { 97 return null; 98 } 99 100 Object o = session.getAttribute(code); 101 102 if (o == null) { 103 return null; 104 } 105 if (!(o instanceof Serializable )) { 106 throw new ServletException (myName + 107 ":Object stored in session with key '" + 108 code + 109 "' was not Serializable - it was of class '" + 110 o.getClass().getName() + "'"); 111 } 112 113 return (Serializable ) o; 114 } 115 116 117 public Enumeration getAttributeNames(HttpServletRequest req) 118 throws ServletException { 119 HttpSession session = req.getSession(true); 120 121 return session.getAttributeNames(); 122 } 123 124 131 public void setAttribute(HttpServletRequest req, String code, 132 Serializable value) 133 throws ServletException { 134 HttpSession session = req.getSession(true); 135 session.setAttribute(code, value); 136 } 137 138 139 145 public void removeAttribute(HttpServletRequest req, String code) 146 throws ServletException { 147 HttpSession session = req.getSession(true); 148 session.removeAttribute(code); 149 } 150 151 152 157 public void invalidate(HttpServletRequest req) { 158 HttpSession session = req.getSession(true); 159 session.invalidate(); 160 } 161 162 168 public String getId(HttpServletRequest req) { 169 HttpSession session = req.getSession(true); 170 171 return session.getId(); 172 } 173 174 180 public String getContextPath(HttpServletRequest req) { 181 return req.getContextPath(); 182 } 183 184 } 185 186 | Popular Tags |