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