1 package com.opensymphony.webwork.portlet.util; 2 3 import org.apache.log4j.Category; 4 5 import javax.servlet.http.Cookie ; 6 import javax.servlet.http.HttpServletRequest ; 7 import javax.servlet.http.HttpServletResponse ; 8 9 14 public class CookieUtils { 15 private static final Category log = Category.getInstance(CookieUtils.class); 16 17 public static final String COOKIES_TO_SEND = "atlassian.core.web.cookies.unsent"; 19 20 private static final char DELIMITER = 0x13; 23 24 private final static int ENCODE_XORMASK = 0x5A; 27 private final static char ENCODE_CHAR_OFFSET1 = 'C'; 28 private final static char ENCODE_CHAR_OFFSET2 = 'i'; 29 30 37 public static void invalidateCookie(HttpServletRequest request, HttpServletResponse response, String cookieName, String path) { 38 log.debug("CookieUtils.invalidateCookie " + cookieName + " for path " + path); 39 setCookie(request, response, cookieName, null, 0, path); 40 } 41 42 51 public static void invalidateCookie(HttpServletRequest request, HttpServletResponse response, String cookieName) { 52 invalidateCookie(request, response, cookieName, "/"); 53 } 54 55 63 public static Cookie getCookie(HttpServletRequest request, String name) { 64 Cookie cookies[] = request.getCookies(); 65 if (cookies == null || name == null || name.length() == 0) { 66 return null; 67 } 68 for (int i = 0; i < cookies.length; i++) { 70 if (cookies[i].getName().equals(name)) { 71 return cookies[i]; 72 } 73 } 74 return null; 75 } 76 77 84 public static Cookie setCookie(HttpServletRequest request, HttpServletResponse response, String name, String value, int maxAge, String path) { 85 log.debug("CookieUtils.setCookie " + name + ":" + value); 86 Cookie cookie = new Cookie (name, value); 87 cookie.setMaxAge(maxAge); 88 cookie.setPath(path); 89 response.addCookie(cookie); 90 91 return cookie; 92 } 93 94 103 public static String getCookieValue(HttpServletRequest request, String name) { 104 Cookie cookie = getCookie(request, name); 105 if (cookie != null) { 106 return cookie.getValue(); 107 } 108 return null; 109 } 110 111 123 public static String encodePasswordCookie(String username, String password) { 124 return encodePasswordCookie(username, password, new String (new char[]{DELIMITER, ENCODE_CHAR_OFFSET1, ENCODE_CHAR_OFFSET2})); 125 } 126 127 140 public static String encodePasswordCookie(String username, String password, String encoding) { 141 StringBuffer buf = new StringBuffer (); 142 if (username != null && password != null) { 143 char offset1 = (encoding != null && encoding.length() > 1) ? encoding.charAt(1) : ENCODE_CHAR_OFFSET1; 144 char offset2 = (encoding != null && encoding.length() > 2) ? encoding.charAt(2) : ENCODE_CHAR_OFFSET2; 145 146 byte[] bytes = (username + DELIMITER + password).getBytes(); 147 int b; 148 149 for (int n = 0; n < bytes.length; n++) { 150 b = bytes[n] ^ (ENCODE_XORMASK + n); 151 buf.append((char) (offset1 + (b & 0x0F))); 152 buf.append((char) (offset2 + ((b >> 4) & 0x0F))); 153 } 154 } 155 return buf.toString(); 156 } 157 158 166 public static String [] decodePasswordCookie(String cookieVal) { 167 return decodePasswordCookie(cookieVal, new String (new char[]{DELIMITER, ENCODE_CHAR_OFFSET1, ENCODE_CHAR_OFFSET2})); 168 } 169 170 179 public static String [] decodePasswordCookie(String cookieVal, String encoding) { 180 if (cookieVal == null || cookieVal.length() <= 0) { 182 return null; 183 } 184 185 char offset1 = (encoding != null && encoding.length() > 1) ? encoding.charAt(1) : ENCODE_CHAR_OFFSET1; 186 char offset2 = (encoding != null && encoding.length() > 2) ? encoding.charAt(2) : ENCODE_CHAR_OFFSET2; 187 188 char[] chars = cookieVal.toCharArray(); 190 byte[] bytes = new byte[chars.length / 2]; 191 int b; 192 for (int n = 0, m = 0; n < bytes.length; n++) { 193 b = chars[m++] - offset1; 194 b |= (chars[m++] - offset2) << 4; 195 bytes[n] = (byte) (b ^ (ENCODE_XORMASK + n)); 196 } 197 cookieVal = new String (bytes); 198 int pos = cookieVal.indexOf(DELIMITER); 199 String username = (pos < 0) ? "" : cookieVal.substring(0, pos); 200 String password = (pos < 0) ? "" : cookieVal.substring(pos + 1); 201 202 return new String []{username, password}; 203 } 204 } 205 | Popular Tags |