Created a CookieUtil for looking up cookie values.

git-svn-id: https://samskivert.googlecode.com/svn/trunk@1239 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
ray
2003-10-06 22:50:28 +00:00
parent 3748e9ea6c
commit 0b86451fb3
2 changed files with 44 additions and 17 deletions
@@ -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;
@@ -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();
}
}