1 package dolda.jsvc.util;
9 public final static DateFormat datefmt;
11 datefmt = new SimpleDateFormat("EEE, dd-MMM-yyyy HH:mm:ss z", Locale.ENGLISH);
12 datefmt.setCalendar(Calendar.getInstance(TimeZone.getTimeZone("UTC")));
14 public final String name;
17 public String domain, path;
18 public boolean secure;
20 public Cookie(String name, String value, Date expires, String domain, String path, boolean secure) {
21 if(!Http.istoken(name))
22 throw(new RuntimeException("Invalid cookie name: `" + name + "'"));
25 this.expires = expires;
31 public Cookie(String name) {
32 this(name, null, null, null, null, false);
35 public Cookie(String name, String value) {
36 this(name, value, null, null, null, false);
39 public String format() {
40 StringBuilder buf = new StringBuilder();
41 buf.append(Http.tokenquote(name));
43 buf.append(Http.tokenquote(value));
45 buf.append("; Domain=" + Http.tokenquote(domain));
47 buf.append("; Path=" + Http.tokenquote(path));
49 buf.append("; Expires=" + Http.tokenquote(datefmt.format(expires)));
51 buf.append("; Secure");
52 return(buf.toString());
55 public void addto(Request req) {
56 req.outheaders().add("Set-Cookie", format());
59 public static MultiMap<String, Cookie> parse(Request req) {
60 MultiMap<String, Cookie> ret = new WrappedMultiMap<String, Cookie>(new TreeMap<String, Collection<Cookie>>());
61 for(String in : req.inheaders().values("Cookie")) {
63 StringReader r = new StringReader(in);
66 String k = Http.tokenunquote(r);
67 String v = Http.tokenunquote(r);
70 if(k.equals("$Version")) {
71 if(Integer.parseInt(v) != 1)
72 throw(new Http.EncodingException("Unknown cookie format version"));
73 } else if(k.equals("$Path")) {
76 } else if(k.equals("$Domain")) {
84 } catch(IOException e) {
91 public String toString() {
92 StringBuilder buf = new StringBuilder();
93 buf.append("Cookie(");
96 return(buf.toString());