1 17 package javax.servlet.http; 18 19 import java.text.MessageFormat ; 20 import java.util.ResourceBundle ; 21 22 61 62 66 public class Cookie implements Cloneable { 67 68 private static final String LSTRING_FILE = 69 "javax.servlet.http.LocalStrings"; 70 private static ResourceBundle lStrings = 71 ResourceBundle.getBundle(LSTRING_FILE); 72 73 77 private String name; private String value; 80 84 private String comment; private String domain; private int maxAge = -1; private String path; private boolean secure; private int version = 0; 92 93 94 124 125 public Cookie(String name, String value) { 126 if (!isToken(name) 127 || name.equalsIgnoreCase("Comment") || name.equalsIgnoreCase("Discard") || name.equalsIgnoreCase("Domain") 130 || name.equalsIgnoreCase("Expires") || name.equalsIgnoreCase("Max-Age") || name.equalsIgnoreCase("Path") 133 || name.equalsIgnoreCase("Secure") 134 || name.equalsIgnoreCase("Version") 135 || name.startsWith("$") 136 ) { 137 String errMsg = lStrings.getString("err.cookie_name_is_token"); 138 Object [] errArgs = new Object [1]; 139 errArgs[0] = name; 140 errMsg = MessageFormat.format(errMsg, errArgs); 141 throw new IllegalArgumentException (errMsg); 142 } 143 144 this.name = name; 145 this.value = value; 146 } 147 148 149 150 151 152 165 166 public void setComment(String purpose) { 167 comment = purpose; 168 } 169 170 171 172 173 183 184 public String getComment() { 185 return comment; 186 } 187 188 189 190 191 192 211 212 public void setDomain(String pattern) { 213 domain = pattern.toLowerCase(); } 215 216 217 218 219 220 229 230 public String getDomain() { 231 return domain; 232 } 233 234 235 236 237 259 260 public void setMaxAge(int expiry) { 261 maxAge = expiry; 262 } 263 264 265 266 267 281 282 public int getMaxAge() { 283 return maxAge; 284 } 285 286 287 288 289 309 310 public void setPath(String uri) { 311 path = uri; 312 } 313 314 315 316 317 329 330 public String getPath() { 331 return path; 332 } 333 334 335 336 337 338 351 352 public void setSecure(boolean flag) { 353 secure = flag; 354 } 355 356 357 358 359 370 371 public boolean getSecure() { 372 return secure; 373 } 374 375 376 377 378 379 386 387 public String getName() { 388 return name; 389 } 390 391 392 393 394 395 413 414 public void setValue(String newValue) { 415 value = newValue; 416 } 417 418 419 420 421 431 432 public String getValue() { 433 return value; 434 } 435 436 437 438 439 454 455 public int getVersion() { 456 return version; 457 } 458 459 460 461 462 478 479 public void setVersion(int v) { 480 version = v; 481 } 482 483 488 private static final String tspecials = ",; "; 489 490 491 492 493 503 504 private boolean isToken(String value) { 505 int len = value.length(); 506 507 for (int i = 0; i < len; i++) { 508 char c = value.charAt(i); 509 510 if (c < 0x20 || c >= 0x7f || tspecials.indexOf(c) != -1) 511 return false; 512 } 513 return true; 514 } 515 516 517 518 519 520 521 528 529 public Object clone() { 530 try { 531 return super.clone(); 532 } catch (CloneNotSupportedException e) { 533 throw new RuntimeException (e.getMessage()); 534 } 535 } 536 } 537 538 | Popular Tags |