1 package com.atlassian.seraph.util; 2 3 import org.apache.log4j.Category; 4 5 import javax.servlet.http.HttpServletRequest ; 6 import javax.servlet.http.HttpServletResponse ; 7 import javax.servlet.http.Cookie ; 8 9 14 public class CookieUtils 15 { 16 private static final Category log = Category.getInstance(CookieUtils.class); 17 18 public static final String COOKIES_TO_SEND = "atlassian.core.web.cookies.unsent"; 20 21 private static final char DELIMITER = 0x13; 24 25 private final static int ENCODE_XORMASK = 0x5A; 28 private final static char ENCODE_CHAR_OFFSET1 = 'C'; 29 private final static char ENCODE_CHAR_OFFSET2 = 'i'; 30 31 38 public static void invalidateCookie(HttpServletRequest request, HttpServletResponse response, String cookieName, String path) 39 { 40 log.debug("CookieUtils.invalidateCookie " + cookieName + " for path " + path); 41 setCookie(request, response, cookieName, null, 0, path); 42 } 43 44 53 public static void invalidateCookie(HttpServletRequest request, HttpServletResponse response, String cookieName) 54 { 55 invalidateCookie(request, response, cookieName, "/"); 56 } 57 58 66 public static Cookie getCookie(HttpServletRequest request, String name) 67 { 68 Cookie cookies[] = request.getCookies(); 69 if (cookies == null || name == null || name.length() == 0) 70 { 71 return null; 72 } 73 for (int i = 0; i < cookies.length; i++) 75 { 76 if (cookies[i].getName().equals(name)) 77 { 78 return cookies[i]; 79 } 80 } 81 return null; 82 } 83 84 91 public static Cookie setCookie(HttpServletRequest request, HttpServletResponse response, String name, String value, int maxAge, String path) 92 { 93 log.debug("CookieUtils.setCookie " + name + ":" + value); 94 Cookie cookie = new Cookie (name, value); 95 cookie.setMaxAge(maxAge); 96 cookie.setPath(path); 97 response.addCookie(cookie); 98 99 return cookie; 100 } 101 102 111 public static String getCookieValue(HttpServletRequest request, String name) 112 { 113 Cookie cookie = getCookie(request, name); 114 if (cookie != null) 115 { 116 return cookie.getValue(); 117 } 118 return null; 119 } 120 121 133 public static String encodePasswordCookie(String username, String password) 134 { 135 return encodePasswordCookie(username, password, new String (new char[]{DELIMITER, ENCODE_CHAR_OFFSET1, ENCODE_CHAR_OFFSET2})); 136 } 137 138 151 public static String encodePasswordCookie(String username, String password, String encoding) 152 { 153 StringBuffer buf = new StringBuffer (); 154 if (username != null && password != null) 155 { 156 char offset1 = (encoding != null && encoding.length() > 1) ? encoding.charAt(1) : ENCODE_CHAR_OFFSET1; 157 char offset2 = (encoding != null && encoding.length() > 2) ? encoding.charAt(2) : ENCODE_CHAR_OFFSET2; 158 159 byte[] bytes = (username + DELIMITER + password).getBytes(); 160 int b; 161 162 for (int n = 0; n < bytes.length; n++) 163 { 164 b = bytes[n] ^ (ENCODE_XORMASK + n); 165 buf.append((char) (offset1 + (b & 0x0F))); 166 buf.append((char) (offset2 + ((b >> 4) & 0x0F))); 167 } 168 } 169 return buf.toString(); 170 } 171 172 179 public static String [] decodePasswordCookie(String cookieVal) 180 { 181 return decodePasswordCookie(cookieVal, new String (new char[]{DELIMITER, ENCODE_CHAR_OFFSET1, ENCODE_CHAR_OFFSET2})); 182 } 183 184 192 public static String [] decodePasswordCookie(String cookieVal, String encoding) 193 { 194 if (cookieVal == null || cookieVal.length() <= 0) 196 { 197 return null; 198 } 199 200 char offset1 = (encoding != null && encoding.length() > 1) ? encoding.charAt(1) : ENCODE_CHAR_OFFSET1; 201 char offset2 = (encoding != null && encoding.length() > 2) ? encoding.charAt(2) : ENCODE_CHAR_OFFSET2; 202 203 char[] chars = cookieVal.toCharArray(); 205 byte[] bytes = new byte[chars.length / 2]; 206 int b; 207 for (int n = 0, m = 0; n < bytes.length; n++) 208 { 209 b = chars[m++] - offset1; 210 b |= (chars[m++] - offset2) << 4; 211 bytes[n] = (byte) (b ^ (ENCODE_XORMASK + n)); 212 } 213 cookieVal = new String (bytes); 214 int pos = cookieVal.indexOf(DELIMITER); 215 String username = (pos < 0) ? "" : cookieVal.substring(0, pos); 216 String password = (pos < 0) ? "" : cookieVal.substring(pos + 1); 217 218 return new String []{username, password}; 219 } 220 } 221 | Popular Tags |