1 25 package org.nemesis.forum.util; 26 27 import javax.servlet.http.Cookie ; 28 import javax.servlet.http.HttpServletRequest ; 29 import javax.servlet.http.HttpServletResponse ; 30 31 36 public class CookieManager { 37 38 private static final long SECOND = 1000; 39 private static final long MINUTE = 60 * SECOND; 40 private static final long HOUR = 60 * MINUTE; 41 private static final long DAY = 24 * HOUR; 42 private static final long WEEK = 7 * DAY; 43 public static final int MAX_COOKIE_AGE = (int) (WEEK / 1000) * 8; 44 45 private final static int ENCODE_XORMASK = 0x5A; 46 private final static char ENCODE_DELIMETER = '\002'; 47 private final static char ENCODE_CHAR_OFFSET1 = 'A'; 48 private final static char ENCODE_CHAR_OFFSET2 = 'h'; 49 50 public static void setCookie(HttpServletResponse res, String name, String value, int maxAge) { 51 Cookie oneCookie = new Cookie (name, value); 52 oneCookie.setMaxAge(maxAge); 53 oneCookie.setPath("/"); 54 res.addCookie(oneCookie); 55 } 56 57 65 public static Cookie getCookie(HttpServletRequest request, String name) { 66 Cookie cookies[] = request.getCookies(); 67 if (cookies == null || name == null || name.length() == 0) { 68 return null; 69 } 70 for (int i = 0; i < cookies.length; i++) { 72 if (cookies[i].getName().equals(name)) { 73 return cookies[i]; 74 } 75 } 76 return null; 77 } 78 79 88 public static String getCookieValue(HttpServletRequest request, String name) { 89 Cookie cookie = getCookie(request, name); 90 if (cookie != null) { 91 return cookie.getValue(); 92 } 93 return null; 94 } 95 96 103 public static void invalidateCookie(HttpServletRequest request, HttpServletResponse response, String cookieName) { 104 Cookie cookie = new Cookie (cookieName, null); cookie.setMaxAge(0); cookie.setPath("/"); 107 response.addCookie(cookie); 108 } 109 110 122 public static String encodePasswordCookie(String username, String password) { 123 StringBuffer buf = new StringBuffer (); 124 if (username != null && password != null) { 125 byte[] bytes = (username + ENCODE_DELIMETER + password).getBytes(); 126 int b; 127 128 for (int n = 0; n < bytes.length; n++) { 129 b = bytes[n] ^ (ENCODE_XORMASK + n); 130 buf.append((char) (ENCODE_CHAR_OFFSET1 + (b & 0x0F))); 131 buf.append((char) (ENCODE_CHAR_OFFSET2 + ((b >> 4) & 0x0F))); 132 } 133 } 134 return buf.toString(); 135 } 136 137 144 public static String [] decodePasswordCookie(String cookieVal) { 145 146 if (cookieVal == null || cookieVal.length() <= 0) { 148 return null; 149 } 150 151 char[] chars = cookieVal.toCharArray(); 153 byte[] bytes = new byte[chars.length / 2]; 154 int b; 155 for (int n = 0, m = 0; n < bytes.length; n++) { 156 b = chars[m++] - ENCODE_CHAR_OFFSET1; 157 b |= (chars[m++] - ENCODE_CHAR_OFFSET2) << 4; 158 bytes[n] = (byte) (b ^ (ENCODE_XORMASK + n)); 159 } 160 cookieVal = new String (bytes); 161 int pos = cookieVal.indexOf(ENCODE_DELIMETER); 162 String username = (pos < 0) ? "" : cookieVal.substring(0, pos); 163 String password = (pos < 0) ? "" : cookieVal.substring(pos + 1); 164 165 return new String [] { username, password }; 166 } 167 } 168 | Popular Tags |