1 package com.icesoft.faces.webapp.command; 2 3 import javax.servlet.http.Cookie ; 4 import java.io.IOException ; 5 import java.io.Writer ; 6 import java.text.DateFormat ; 7 import java.text.SimpleDateFormat ; 8 import java.util.Date ; 9 import java.util.TimeZone ; 10 11 public class SetCookie implements Command { 12 private final static DateFormat CookieDateFormat = new SimpleDateFormat ("EEE, dd MMM yyyy hh:mm:ss z"); 13 static { 14 CookieDateFormat.setTimeZone(TimeZone.getTimeZone("GMT")); 15 } 16 private Cookie cookie; 17 18 public SetCookie(Cookie 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 writer) throws IOException { 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 expiryDate = new Date (System.currentTimeMillis() + maxAge * 1000); 65 writer.write("expires="); 66 writer.write(CookieDateFormat.format(expiryDate)); 67 writer.write("; "); 68 } 69 String path = cookie.getPath(); 70 if (path != null) { 71 writer.write("path="); 72 writer.write(path); 73 writer.write("; "); 74 } 75 String 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 |