01: // CookieCommand.java
02: // $Id: CookieCommand.java,v 1.4 2000/08/16 21:37:47 ylafon Exp $
03: // (c) COPYRIGHT MIT and INRIA, 1997.
04: // Please first read the full copyright statement in file COPYRIGHT.html
05:
06: package org.w3c.jigsaw.ssi.commands;
07:
08: import java.util.Dictionary;
09:
10: import org.w3c.www.http.HTTP;
11: import org.w3c.www.http.HttpCookie;
12: import org.w3c.www.http.HttpCookieList;
13: import org.w3c.www.http.HttpMessage;
14:
15: import org.w3c.jigsaw.http.Reply;
16: import org.w3c.jigsaw.http.Request;
17:
18: import org.w3c.jigsaw.ssi.SSIFrame;
19:
20: import org.w3c.util.ArrayDictionary;
21:
22: /**
23: * Cookies access from server side includes.
24: * Powerfull thingy !
25: */
26:
27: public class CookieCommand implements Command {
28: private final static String NAME = "cookie";
29: private static final boolean debug = true;
30:
31: private static final String[] keys = { "get", // get=<cookie-name> alt=<content>
32: "if", // if=<cookie-name> then=<content> alt=<content>
33: "alt", "then" };
34:
35: public String getName() {
36: return NAME;
37: }
38:
39: public Reply execute(SSIFrame ssiframe, Request request,
40: ArrayDictionary parameters, Dictionary variables) {
41: Object values[] = parameters.getMany(keys);
42: String pget = (String) values[0];
43: String pif = (String) values[1];
44: String palt = (String) values[2];
45: String pthen = (String) values[3];
46:
47: if (debug)
48: System.out.println("cookie: get=" + pget + ", if=" + pif
49: + ", alt=" + palt + ", then=" + pthen);
50: String content = null;
51:
52: if (pget != null) {
53: // Try acessing that cookie value:
54: HttpCookieList list = request.getCookie();
55: HttpCookie cookie = null;
56: if (list != null)
57: cookie = list.getCookie(pget);
58: content = (cookie == null) ? palt : cookie.getValue();
59: } else if (pif != null) {
60: HttpCookieList list = request.getCookie();
61: HttpCookie cookie = null;
62: if (list != null)
63: cookie = list.getCookie(pif);
64: content = (cookie != null) ? pthen : palt;
65: }
66: // We are NOT doing notMod hack here (tricky and useless ?)
67: Reply reply = ssiframe.createCommandReply(request, HTTP.OK);
68: reply.setContent(content);
69: return reply;
70: }
71:
72: public String getValue(Dictionary variables, String variable,
73: Request request) {
74: return "null";
75: }
76:
77: /**
78: * return true if reply can be cached.
79: * @return a boolean.
80: */
81: public boolean acceptCaching() {
82: return true;
83: }
84:
85: }
|