KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > faces > webapp > command > SetCookie


1 package com.icesoft.faces.webapp.command;
2
3 import javax.servlet.http.Cookie JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.io.Writer JavaDoc;
6 import java.text.DateFormat JavaDoc;
7 import java.text.SimpleDateFormat JavaDoc;
8 import java.util.Date JavaDoc;
9 import java.util.TimeZone JavaDoc;
10
11 public class SetCookie implements Command {
12     private final static DateFormat JavaDoc CookieDateFormat = new SimpleDateFormat JavaDoc("EEE, dd MMM yyyy hh:mm:ss z");
13     static {
14         CookieDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
15     }
16     private Cookie JavaDoc cookie;
17
18     public SetCookie(Cookie JavaDoc cookie) {
19         this.cookie = cookie;
20     }
21
22     public Command coalesceWith(Command command) {
23
24         return command.coalesceWith(this);
25     }
26
27     public Command coalesceWith(Macro macro) {
28         macro.addCommand(this);
29         return macro;
30     }
31
32     public Command coalesceWith(UpdateElements updateElements) {
33         return new Macro(updateElements, this);
34     }
35
36     public Command coalesceWith(Redirect redirect) {
37         return new Macro(redirect, this);
38     }
39
40     public Command coalesceWith(SessionExpired sessionExpired) {
41         return sessionExpired;
42     }
43
44     public Command coalesceWith(SetCookie setCookie) {
45         return new Macro(setCookie, this);
46     }
47
48     public Command coalesceWith(Pong pong) {
49         return new Macro(pong, this);
50     }
51
52     public Command coalesceWith(NOOP noop) {
53         return this;
54     }
55
56     public void serializeTo(Writer JavaDoc writer) throws IOException JavaDoc {
57         writer.write("<set-cookie>");
58         writer.write(cookie.getName());
59         writer.write("=");
60         writer.write(cookie.getValue());
61         writer.write("; ");
62         int maxAge = cookie.getMaxAge();
63         if (maxAge >= 0) {
64             Date JavaDoc expiryDate = new Date JavaDoc(System.currentTimeMillis() + maxAge * 1000);
65             writer.write("expires=");
66             writer.write(CookieDateFormat.format(expiryDate));
67             writer.write("; ");
68         }
69         String JavaDoc path = cookie.getPath();
70         if (path != null) {
71             writer.write("path=");
72             writer.write(path);
73             writer.write("; ");
74         }
75         String JavaDoc domain = cookie.getDomain();
76         if (domain != null) {
77             writer.write("domain=");
78             writer.write(domain);
79             writer.write("; ");
80         }
81         if (cookie.getSecure()) {
82             writer.write("secure;");
83         }
84         writer.write("</set-cookie>");
85     }
86 }
87
Popular Tags