KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jdon > security > web > CookieUtil


1 /**
2  * Copyright 2003-2006 the original author or authors.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6
7        http://www.apache.org/licenses/LICENSE-2.0
8
9   * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */

15
16 package com.jdon.security.web;
17
18 import javax.servlet.http.*;
19
20 import com.jdon.util.RequestUtil;
21 import com.jdon.util.StringUtil;
22 import java.net.*;
23 import com.jdon.util.Debug;
24
25 public class CookieUtil {
26   private final static String JavaDoc module = CookieUtil.class.getName();
27
28   public static void deleteAllCookie(HttpServletRequest request,
29                                      HttpServletResponse response) {
30     Cookie rememberMe = RequestUtil.getCookie(request, "rememberMe");
31     if (rememberMe != null)
32       RequestUtil.deleteCookie(response, rememberMe, "/");
33     Cookie userCookie = RequestUtil.getCookie(request, "username");
34     if (userCookie != null)
35       RequestUtil.deleteCookie(response, userCookie, "/");
36     Cookie passCookie = RequestUtil.getCookie(request, "password");
37     if (passCookie != null)
38       RequestUtil.deleteCookie(response, passCookie, "/");
39
40   }
41
42   public static void setUsername(HttpServletResponse response, String JavaDoc username) {
43
44     RequestUtil.setCookie(response, "username",
45                           StringUtil.encodeString(username),
46                           "/");
47
48   }
49
50   public static String JavaDoc getUsername(HttpServletRequest request) {
51     String JavaDoc username = "";
52     try {
53       Cookie userCookie = RequestUtil.getCookie(request, "username");
54       username = (userCookie != null)
55           ? StringUtil.decodeString(userCookie.getValue()) : null;
56     } catch (Exception JavaDoc e) {
57       System.err.print("getUsername from cookie" + e);
58     }
59
60     return username;
61   }
62
63   public static void setUPassword(HttpServletResponse response, String JavaDoc password) {
64     RequestUtil.setCookie(response, "password",
65                           StringUtil.encodeString(password),
66                           "/");
67
68   }
69
70   public static String JavaDoc getPassword(HttpServletRequest request) {
71     String JavaDoc password = "";
72     try {
73       Cookie passCookie = RequestUtil.getCookie(request, "password");
74       password = (passCookie != null)
75           ? StringUtil.decodeString(passCookie.getValue()) : null;
76     } catch (Exception JavaDoc e) {
77       System.err.print("getpassCookie from cookie" + e);
78     }
79
80     return password;
81
82   }
83
84   public static void setRememberMe(HttpServletResponse response) {
85     RequestUtil.setCookie(response, "rememberMe", "true", "/");
86   }
87
88   public static String JavaDoc getRememberMe(HttpServletRequest request) {
89     String JavaDoc rememberMe = "";
90     try {
91       Cookie rememberMeie = RequestUtil.getCookie(request, "rememberMe");
92       rememberMe = (rememberMeie != null)
93           ? URLDecoder.decode(rememberMeie.getValue(), "UTF-8") : null;
94     } catch (Exception JavaDoc e) {
95       System.err.print("getrememberMe from cookie" + e);
96     }
97     Debug.logVerbose("[JdonFramework]--> get RememberMe from cookier :" + rememberMe, module);
98     return rememberMe;
99
100   }
101
102 }
103
Popular Tags