001: /*
002: * UrlSessionFilter.java
003: *
004: * Brazil project web application Framework,
005: * export version: 1.1
006: * Copyright (c) 1999-2000 Sun Microsystems, Inc.
007: *
008: * Sun Public License Notice
009: *
010: * The contents of this file are subject to the Sun Public License Version
011: * 1.0 (the "License"). You may not use this file except in compliance with
012: * the License. A copy of the License is included as the file "license.terms",
013: * and also available at http://www.sun.com/
014: *
015: * The Original Code is from:
016: * Brazil project web application Framework release 1.1.
017: * The Initial Developer of the Original Code is: suhler.
018: * Portions created by suhler are Copyright (C) Sun Microsystems, Inc.
019: * All Rights Reserved.
020: *
021: * Contributor(s): cstevens, suhler.
022: *
023: * Version: 1.9
024: * Created by suhler on 99/08/06
025: * Last modified by suhler on 00/12/08 16:47:39
026: */
027:
028: package sunlabs.brazil.filter;
029:
030: import sunlabs.brazil.handler.MapPage;
031: import sunlabs.brazil.server.Request;
032: import sunlabs.brazil.server.Server;
033: import sunlabs.brazil.util.http.MimeHeaders;
034:
035: import java.util.Random;
036: import java.util.Properties;
037:
038: /**
039: * Sample filter to use url's instead of cookies for sessions.
040: * When html files are delivered, all URL's back to this host are
041: * changed to add in the session information.
042: *
043: * When requests are made, the session info is stripped off the URL,
044: * which is passed to the rest of the handlers.
045: * <p>
046: * Note: This fiter has been superceded by the
047: * {@link SessionFilter}. It is
048: * included for illustrative purposes only.
049: *
050: * @author Stephen Uhler
051: * @version 1.9, 00/12/08
052: */
053:
054: public class UrlSessionFilter implements Filter {
055: private static final String SESSION = "session";
056:
057: public String session = "SessionID";
058:
059: private static Random random = new Random();
060:
061: public boolean init(Server server, String propsPrefix) {
062: Properties props = server.props;
063:
064: session = props.getProperty(propsPrefix + SESSION, session);
065:
066: return true;
067: }
068:
069: /**
070: * Extract the cookie out of the URL, rewriting the url as needed.
071: * Add the session info at the end of the url:
072: * /a/b.html -> /a/b.html$xxxx where xxx is the session
073: * This gets called before the original request is made.
074: */
075:
076: public boolean respond(Request request) {
077: String id;
078:
079: int dollar = request.url.indexOf('$');
080: if (dollar > 0) {
081: id = request.url.substring(dollar + 1);
082: request.url = request.url.substring(0, dollar);
083: } else {
084: id = Long.toHexString(random.nextLong());
085: }
086: request.props.put(session, id);
087: return false;
088: }
089:
090: /**
091: * We have the results, only filter if html
092: */
093:
094: public boolean shouldFilter(Request request, MimeHeaders headers) {
095: String type = headers.get("content-type");
096: return (type != null && type.equals("text/html"));
097: }
098:
099: /**
100: * Rewrite all the url's, adding the session id to the end
101: */
102:
103: public byte[] filter(Request request, MimeHeaders headers,
104: byte[] content) {
105: String id = request.props.getProperty(session);
106: if (id == null) {
107: return content;
108: }
109: Map map = new Map(id);
110: return map.convertHtml(new String(content)).getBytes();
111: }
112:
113: /**
114: * The mapPage class was designed for virtual web page re-mapping
115: * We use it for session attachment instead.
116: */
117:
118: static class Map extends MapPage {
119: Map(String s) {
120: super (s);
121: }
122:
123: /**
124: * If the url doesn't start with http://, tack the session info
125: * on to the end of the URL
126: */
127:
128: public String convertString(String fix) {
129: if (fix.startsWith("http://")) {
130: return null;
131: }
132:
133: int index = fix.indexOf('#');
134: if (index < 0) {
135: index = fix.indexOf('?');
136: }
137: if (index == 0) {
138: return null;
139: } else if (index < 0) {
140: index = fix.length();
141: }
142: String result = fix.substring(0, index) + "$" + prefix
143: + fix.substring(index);
144:
145: return result;
146: }
147: }
148: }
|