Cookies & SSI

I thought I would share that latest hack. It's an extension to SSI
include that provides access to any cookies of the request; to install
it: 
a) copy attached file in w3c.jigsaw.ssi
b) Add the CookieCommand in the DefaultCommandRegistry
c) compile w3c.jigsaw.ssi
d) have fun !

The new 'cookie' command has two forms:
<!--#cookie get="cookie-name" alt="alt" -->
<!--#cookie if="cookie-name" then="then" alt="alt" -->

The first form will expand to the cookie-name value (if defined) or to alt
otherwise. The second form will expand to 'then' if cookie-name is
defined, 'alt' otherwise.

One nice trick is to handle customizable pages through things like:
<body bgcolor=<!--#cookie get="background" alt="white" -->>
You'll have to write a form that sets the cookie though (or use the
w3c.jigsaw.contrib.HeaderFilter, if you just want to play)

Anselm.

----- CookieCommand.java
package w3c.jigsaw.ssi;

import java.util.*;

import w3c.www.http.*;
import w3c.jigsaw.http.*;
import w3c.jigsaw.resources.*;
import w3c.util.*;

/**
 * Cookies access from server side includes.
 * Powerfull thingy !
 */

public class CookieCommand implements Command {
    private final static String NAME = "cookie";
    private static final boolean debug = true;

    private static final String[] keys = {
	"get",	// get=<cookie-name> alt=<content>
	"if",	// if=<cookie-name>  then=<content> alt=<content>
	"alt",
	"then"
    };

    public String getName() {
	return NAME;
    }

    public Reply execute(SSIResource resource
			 , Request request
			 , ArrayDictionary parameters
			 , Dictionary variables) {
	Object values[]  = parameters.getMany(keys);
	String pget  = (String) values[0];
	String pif   = (String) values[1];
	String palt  = (String) values[2];
	String pthen = (String) values[3];

	if ( debug ) 
	    System.out.println("cookie: get="+pget
			       +", if="+pif
			       +", alt="+palt
			       +", then="+pthen);
	String content   = null;

	if ( pget != null ) {
	    // Try acessing that cookie value:
	    HttpCookieList list   = request.getCookie();
	    HttpCookie     cookie = null;
	    if ( list != null )
		cookie = list.getCookie(pget);
	    content = (cookie == null) ? palt : cookie.getValue();
	} else if ( pif != null ) {
	    HttpCookieList list   = request.getCookie();
	    HttpCookie     cookie = null;
	    if ( list != null )
		cookie = list.getCookie(pif);
	    content = (cookie != null) ? pthen : palt;
	} 
	// We are NOT doing notMod hack here (tricky and useless ?)
	Reply reply = resource.createCommandReply(request, HTTP.OK);
	reply.setContent(content);
	return reply;
    }

}

Received on Tuesday, 18 February 1997 11:49:00 UTC