1 16 package com.blandware.atleap.webapp.util.core; 17 18 import org.apache.commons.logging.Log; 19 import org.apache.commons.logging.LogFactory; 20 import org.apache.struts.action.ActionMapping; 21 import org.apache.struts.Globals; 22 import org.apache.struts.util.TokenProcessor; 23 24 import javax.servlet.http.HttpServletRequest ; 25 import javax.servlet.http.HttpSession ; 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 29 37 public class TokenUtil { 38 39 42 public static final String TOKENS_KEY = "com.blandware.atleap.webapp.action.core.TOKENS"; 43 44 47 protected transient final Log log = LogFactory.getLog(TokenUtil.class); 48 49 52 private static TokenUtil instance = new TokenUtil(); 53 54 59 public static TokenUtil getInstance() { 60 return instance; 61 } 62 63 67 protected TokenUtil() { 68 super(); 69 } 70 71 77 protected String getDefaultTokenScope(HttpServletRequest request) { 78 ActionMapping mapping = (ActionMapping) request.getAttribute(Globals.MAPPING_KEY); 79 String path = mapping.getPath(); 80 int slash = path.lastIndexOf("/"); 81 if (slash != -1) { 82 return path.substring(0, slash + 1); 83 } else { 84 return path; 85 } 86 } 87 88 105 public synchronized boolean isTokenValid(HttpServletRequest request, boolean reset) { 106 return isTokenValid(null, request, reset); 107 } 108 109 125 public synchronized boolean isTokenValid(HttpServletRequest request) { 126 return isTokenValid(null, request, false); 127 } 128 129 130 147 public synchronized boolean isTokenValid(String [] scopes, HttpServletRequest request) { 148 return isTokenValid(scopes, request, false); 149 } 150 151 169 public synchronized boolean isTokenValid(String [] scopes, HttpServletRequest request, boolean reset) { 170 HttpSession session = request.getSession(false); 172 if (session == null) { 173 return false; 174 } 175 176 String requestToken = request.getParameter(org.apache.struts.taglib.html.Constants.TOKEN_KEY); 178 if (requestToken == null) { 179 return false; 180 } 181 182 String sessionToken = (String ) session.getAttribute(Globals.TRANSACTION_TOKEN_KEY); 184 185 if (requestToken.equals(sessionToken)) { 186 if (reset) { 187 resetTokenByToken(requestToken, request); 188 } 189 return true; 190 } 191 192 HashMap tokens = (HashMap ) session.getAttribute(TOKENS_KEY); 193 if (tokens == null) { 194 tokens = new HashMap (); 195 } 196 197 if (scopes != null && scopes.length > 0) { 198 for (int i = 0; i < scopes.length; i++) { 200 String scope = scopes[i]; 201 String scopeToken = (String ) tokens.get(scope); 202 if (requestToken.equals(scopeToken)) { 203 if (reset) { 204 resetToken(scope, request); 205 } 206 return true; 207 } 208 } 209 } else { 210 212 String defaultScope = getDefaultTokenScope(request); 214 String token = (String ) tokens.get(defaultScope); 215 if (requestToken.equals(token)) { 216 if (reset) { 217 resetToken(defaultScope, request); 218 } 219 return true; 220 } 221 222 for (Iterator iterator = tokens.keySet().iterator(); iterator.hasNext();) { 224 String scope = (String ) iterator.next(); 225 if (requestToken.equals(tokens.get(scope))) { 226 if (reset) { 227 resetToken(scope, request); 228 } 229 return true; 230 } 231 } 232 } 233 234 return false; 235 } 236 237 245 public synchronized void resetToken(String tokenScope, HttpServletRequest request) { 246 HttpSession session = request.getSession(false); 247 if (session == null) { 248 return; 249 } 250 if (tokenScope == null) { 251 tokenScope = getDefaultTokenScope(request); 252 } 253 session.removeAttribute(Globals.TRANSACTION_TOKEN_KEY); 254 HashMap tokens = (HashMap ) session.getAttribute(TOKENS_KEY); 255 if (tokens == null) { 256 tokens = new HashMap (); 257 } 258 tokens.remove(tokenScope); 259 session.setAttribute(TOKENS_KEY, tokens); 260 } 261 262 270 protected synchronized void resetTokenByToken(String token, HttpServletRequest request) { 271 HttpSession session = request.getSession(false); 272 if (session == null) { 273 return; 274 } 275 session.removeAttribute(Globals.TRANSACTION_TOKEN_KEY); 276 HashMap tokens = (HashMap ) session.getAttribute(TOKENS_KEY); 277 if (tokens == null) { 278 tokens = new HashMap (); 279 } 280 281 String origScope = null; 282 for (Iterator iterator = tokens.keySet().iterator(); iterator.hasNext();) { 283 String scope = (String ) iterator.next(); 284 if (token.equals(tokens.get(scope))) { 285 origScope = scope; 286 break; 287 } 288 } 289 if (origScope != null) { 290 tokens.remove(origScope); 291 session.setAttribute(TOKENS_KEY, tokens); 292 } 293 } 294 295 296 303 public synchronized void resetToken(HttpServletRequest request) { 304 resetToken(null, request); 305 } 306 307 314 public synchronized void saveToken(String tokenScope, HttpServletRequest request) { 315 String token = TokenProcessor.getInstance().generateToken(request); 316 if (token != null) { 317 if (tokenScope == null) { 318 tokenScope = getDefaultTokenScope(request); 319 } 320 321 HttpSession session = request.getSession(); 322 session.setAttribute(Globals.TRANSACTION_TOKEN_KEY, token); 323 324 HashMap tokens = (HashMap ) session.getAttribute(TOKENS_KEY); 325 if (tokens == null) { 326 tokens = new HashMap (); 327 } 328 tokens.put(tokenScope, token); 329 session.setAttribute(TOKENS_KEY, tokens); 330 } 331 } 332 333 339 public synchronized void saveToken(HttpServletRequest request) { 340 saveToken(null, request); 341 } 342 343 } 344 | Popular Tags |