01: /*
02: * (C) Copyright 2006 Nabh Information Systems, Inc.
03: *
04: * This program is free software; you can redistribute it and/or
05: * modify it under the terms of the GNU General Public License
06: * as published by the Free Software Foundation; either version 2
07: * of the License, or (at your option) any later version.
08: *
09: * This program is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12: * GNU General Public License for more details.
13: *
14: * You should have received a copy of the GNU General Public License
15: * along with this program; if not, write to the Free Software
16: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17: *
18: */
19: package com.nabhinc.portal.login;
20:
21: import javax.servlet.http.Cookie;
22: import javax.servlet.http.HttpServletRequest;
23: import javax.servlet.http.HttpServletResponse;
24:
25: /**
26: *
27: *
28: * @author Padmanabh Dabke
29: * (c) 2006 Nabh Information Systems, Inc. All Rights Reserved.
30: */
31: public class LoginUtil {
32: public static String getUsernameFromCookie(HttpServletRequest req) {
33: String username = null;
34: Cookie[] cookies = req.getCookies();
35: if (cookies != null) {
36: for (int i = 0; i < cookies.length; i++) {
37: Cookie cookie = cookies[i];
38: if (cookie.getName().equals(
39: LoginConstants.USER_NAME_COOKIE)) {
40: username = cookie.getValue();
41: break;
42: }
43: }
44: }
45: return username;
46: }
47:
48: public static String getUserCredFromCookie(HttpServletRequest req) {
49: String cred = null;
50: Cookie[] cookies = req.getCookies();
51: if (cookies != null) {
52: for (int i = 0; i < cookies.length; i++) {
53: Cookie cookie = cookies[i];
54: if (cookie.getName().equals(
55: LoginConstants.USER_CREDENTIAL_COOKIE)) {
56: cred = cookie.getValue();
57: break;
58: }
59: }
60: }
61: return cred;
62: }
63:
64: public static void removeUserCookie(HttpServletRequest req,
65: HttpServletResponse resp) {
66: if (getUserCredFromCookie(req) != null) { //only remember me option has password cookie.
67: Cookie usernameCookie = new Cookie(
68: LoginConstants.USER_NAME_COOKIE, "");
69: usernameCookie.setMaxAge(0);// delete this cookie
70: usernameCookie.setPath("/");
71: resp.addCookie(usernameCookie);
72: }
73:
74: Cookie credCookie = new Cookie(
75: LoginConstants.USER_CREDENTIAL_COOKIE, "");
76: credCookie.setMaxAge(0);// delete this cookie
77: credCookie.setPath("/");
78: resp.addCookie(credCookie);
79: }
80:
81: }
|