01: package com.technoetic.xplanner.security;
02:
03: import javax.servlet.http.Cookie;
04: import javax.servlet.http.HttpServletRequest;
05: import javax.servlet.http.HttpServletResponse;
06:
07: import com.technoetic.xplanner.security.util.Base64;
08: import com.technoetic.xplanner.util.CookieSupport;
09:
10: public class CredentialCookie {
11: private static final String USERID_COOKIE_NAME = "userid";
12: private static final String PASSWORD_COOKIE_NAME = "password";
13: private HttpServletResponse response;
14: private Cookie userId;
15: private Cookie password;
16:
17: public CredentialCookie(HttpServletRequest request,
18: HttpServletResponse response) {
19: this .response = response;
20: userId = CookieSupport.getCookie(USERID_COOKIE_NAME, request);
21: password = CookieSupport.getCookie(PASSWORD_COOKIE_NAME,
22: request);
23: }
24:
25: public void set(String userId, String password) {
26: CookieSupport
27: .createCookie(USERID_COOKIE_NAME, userId, response);
28: CookieSupport.createCookie(PASSWORD_COOKIE_NAME, new String(
29: Base64.encode(password.getBytes())), response);
30: }
31:
32: public void remove() {
33: CookieSupport.deleteCookie(USERID_COOKIE_NAME, response);
34: CookieSupport.deleteCookie(PASSWORD_COOKIE_NAME, response);
35: }
36:
37: public boolean isPresent() {
38: return userId != null && password != null;
39: }
40:
41: public String getUserId() {
42: return userId != null ? userId.getValue() : null;
43: }
44:
45: public String getPassword() {
46: return password != null ? new String(Base64.decode(password
47: .getValue().getBytes())) : null;
48: }
49:
50: }
|