diff --git a/projects/samskivert/src/java/com/samskivert/servlet/user/UserManager.java b/projects/samskivert/src/java/com/samskivert/servlet/user/UserManager.java index ed6f539b..56797b70 100644 --- a/projects/samskivert/src/java/com/samskivert/servlet/user/UserManager.java +++ b/projects/samskivert/src/java/com/samskivert/servlet/user/UserManager.java @@ -1,5 +1,5 @@ // -// $Id: UserManager.java,v 1.17 2003/09/19 02:51:25 eric Exp $ +// $Id: UserManager.java,v 1.18 2003/10/06 22:50:28 ray Exp $ // // samskivert library - useful routines for java programs // Copyright (C) 2001 Michael Bayne @@ -30,6 +30,7 @@ import com.samskivert.Log; import com.samskivert.io.PersistenceException; import com.samskivert.jdbc.ConnectionProvider; import com.samskivert.servlet.RedirectException; +import com.samskivert.servlet.util.CookieUtil; import com.samskivert.servlet.util.RequestUtils; import com.samskivert.util.Interval; import com.samskivert.util.IntervalManager; @@ -172,7 +173,7 @@ public class UserManager public User loadUser (HttpServletRequest req) throws PersistenceException { - String authcode = getAuthCode(req); + String authcode = CookieUtil.getCookieValue(req, USERAUTH_COOKIE); if (authcode != null) { return _repository.loadUserBySession(authcode); } else { @@ -253,7 +254,7 @@ public class UserManager public void logout (HttpServletRequest req, HttpServletResponse rsp) { - String authcode = getAuthCode(req); + String authcode = CookieUtil.getCookieValue(req, USERAUTH_COOKIE); // nothing to do if they don't already have an auth cookie if (authcode == null) { @@ -267,20 +268,6 @@ public class UserManager rsp.addCookie(rmcookie); } - protected static String getAuthCode (HttpServletRequest req) - { - Cookie[] cookies = req.getCookies(); - if (cookies == null) { - return null; - } - for (int i = 0; i < cookies.length; i++) { - if (cookies[i].getName().equals(USERAUTH_COOKIE)) { - return cookies[i].getValue(); - } - } - return null; - } - /** The user repository. */ protected UserRepository _repository; diff --git a/projects/samskivert/src/java/com/samskivert/servlet/util/CookieUtil.java b/projects/samskivert/src/java/com/samskivert/servlet/util/CookieUtil.java new file mode 100644 index 00000000..21c3c21b --- /dev/null +++ b/projects/samskivert/src/java/com/samskivert/servlet/util/CookieUtil.java @@ -0,0 +1,40 @@ +// +// $Id: CookieUtil.java,v 1.1 2003/10/06 22:50:28 ray Exp $ + +package com.samskivert.servlet.util; + +import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServletRequest; + +/** + * Utility methods for dealing with cookies. + */ +public class CookieUtil +{ + /** + * Get the cookie of the specified name, or null if not found. + */ + public static Cookie getCookie (HttpServletRequest req, String name) + { + Cookie[] cookies = req.getCookies(); + if (cookies != null) { + for (int ii=0, nn=cookies.length; ii < nn; ii++) { + if (cookies[ii].getName().equals(name)) { + return cookies[ii]; + } + } + } + + return null; // not found + } + + /** + * Get the value of the cookie for the cookie of the specified name, + * or null if not found. + */ + public static String getCookieValue (HttpServletRequest req, String name) + { + Cookie c = getCookie(req, name); + return (c == null) ? null : c.getValue(); + } +}