1 11 package org.eclipse.help.internal.webapp.servlet; 12 13 import java.io.*; 14 15 import javax.servlet.http.*; 16 17 import org.eclipse.help.internal.webapp.*; 18 import org.eclipse.help.internal.webapp.data.*; 19 20 25 public class CookieUtil { 26 private static final int COOKIE_LIFE = 5 * 365 * 24 * 60 * 60; 27 private static final int MAX_COOKIE_PAYLOAD = 4096 28 - "wset01=".length() - "81920<".length() - 1; 30 33 public static String getCookieValue(String name, HttpServletRequest request) { 34 String ret = null; 35 Cookie[] cookies = request.getCookies(); 36 if (cookies != null) { 37 for (int i = 0; i < cookies.length; i++) { 38 if (name.equals(cookies[i].getName())) { 39 ret = cookies[i].getValue(); 40 break; 41 } 42 } 43 } 44 if (HelpWebappPlugin.DEBUG_WORKINGSETS) { 45 System.out.println("CookieUtil.getCookieValue(" + name + ", " + request.getRequestURI() + ") returning " + ret); 49 } 50 return ret; 51 } 52 public static void setCookieValue(String name, String value, 53 HttpServletResponse response) { 54 Cookie cookie = new Cookie(name, value); 55 cookie.setMaxAge(COOKIE_LIFE); 56 response.addCookie(cookie); 57 if (HelpWebappPlugin.DEBUG_WORKINGSETS) { 58 System.out 59 .println("CookieUtil.setCookieValue(" + name + ", " + value + ",...)"); } 61 } 62 63 public static void deleteCookie(String name, HttpServletResponse response) { 64 Cookie cookie = new Cookie(name, ""); cookie.setMaxAge(0); 66 response.addCookie(cookie); 67 } 68 79 public static void saveString(String name, String data, int maxCookies, 80 HttpServletRequest request, HttpServletResponse response) 81 throws IOException { 82 int len = data.length(); 83 int n = len / MAX_COOKIE_PAYLOAD; 84 if (n > maxCookies) { 85 throw new IOException(WebappResources.getString( 86 "CookieUtil.tooManyCookiesNeeded", UrlUtil.getLocaleObj( request, response))); 88 } 89 for (int i = 1; i <= n; i++) { 90 if (i == 1) { 91 setCookieValue(name + "1", len + "<" + data.substring(0, MAX_COOKIE_PAYLOAD), response); 94 } else { 95 setCookieValue(name + i, data.substring(MAX_COOKIE_PAYLOAD 96 * (i - 1), MAX_COOKIE_PAYLOAD * i), response); 97 } 98 } 99 if (len % MAX_COOKIE_PAYLOAD > 0) { 100 if (n == 0) { 101 setCookieValue(name + "1", len + "<" + data.substring(0, len), response); 104 } else { 105 setCookieValue(name + (n + 1), data.substring( 106 MAX_COOKIE_PAYLOAD * n, len), response); 107 } 108 } 109 110 for (int i = n + 1; i <= maxCookies; i++) { 113 if (i == n + 1 && len % MAX_COOKIE_PAYLOAD > 0) { 114 continue; 115 } 116 if (getCookieValue(name + i, request) != null) { 117 deleteCookie(name + i, response); 118 } else { 119 break; 120 } 121 } 122 } 123 126 public static String restoreString(String name, HttpServletRequest request) { 127 String value1 = CookieUtil.getCookieValue(name + "1", request); if (value1 == null) { 129 return null; 131 } 132 String lengthAndSubstring1[] = value1.split("<"); if (lengthAndSubstring1.length < 2) { 134 return null; 135 } 136 int len = 0; 137 try { 138 len = Integer.parseInt(lengthAndSubstring1[0]); 139 } catch (NumberFormatException nfe) { 140 return null; 141 } 142 if (len <= 0) { 143 return null; 144 } 145 StringBuffer data = new StringBuffer (len); 146 data.append(lengthAndSubstring1[1]); 147 int n = len / MAX_COOKIE_PAYLOAD; 148 for (int i = 2; i <= n; i++) { 149 String substring = CookieUtil.getCookieValue(name + i, request); 150 if (substring == null) { 151 return null; 152 } 153 data.append(substring); 154 } 155 if (len % MAX_COOKIE_PAYLOAD > 0 && n > 0) { 156 String substring = CookieUtil.getCookieValue(name + (n + 1), 157 request); 158 if (substring == null) { 159 return null; 160 } 161 data.append(substring); 162 } 163 164 if (data.length() != len) { 165 return null; 166 } 167 168 return data.toString(); 169 } 170 } 171 | Popular Tags |