001: /*
002: * SupplyHandler.java
003: *
004: * Brazil project web application Framework,
005: * export version: 1.1
006: * Copyright (c) 1998-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.8
024: * Created by suhler on 98/09/14
025: * Last modified by suhler on 00/12/11 13:27:02
026: */
027:
028: package sunlabs.brazil.handler;
029:
030: import java.io.FileInputStream;
031: import java.util.Properties;
032: import java.util.StringTokenizer;
033: import sunlabs.brazil.server.Handler;
034: import sunlabs.brazil.server.Request;
035: import sunlabs.brazil.server.Server;
036:
037: /**
038: * Sample Handler for dispatching different users to different spots
039: * This is a re-implementation of the supplier.net content server
040: * using the new server apis (e.g. its not used for anything anymore).
041: * This handler was originally designed to be a "virtual web site", where
042: * credentials are passed in from an upstream proxy. Those credentials are
043: * used to provide different views based on the particular credentials supplied.
044: *
045: * The following configuration properties are used:
046: * <dl class=props>
047: * <dt>mapFile <dd> properties file
048: * <dt>prefix <dd> url prefix
049: * <dt>default <dd> default map
050: * <dt>header <dd> http header (authentication)
051: * <dt>realm <dd> The authentication realm (basic)
052: * </dl>
053: *
054: * @author Stephen Uhler
055: * @version 1.8, 00/12/11
056: */
057:
058: public class SupplyHandler implements Handler {
059: private Properties map; // The authorization mapping table
060: private String propsPrefix; // My prefix in the global properties file
061: private String urlPrefix; // The prefix to look for to map
062: private String authHeader; // header containing auth info
063: private String realm; // The authentication realm
064: private static final String MAP = "mapFile"; // properties file
065: private static final String PREFIX = "prefix"; // url prefix
066: private static final String DEFAULT = "default"; // default map
067: private static final String HEADER = "header"; // http header
068: private static final String REALM = "realm"; // realm to look for
069:
070: public boolean init(Server server, String prefix) {
071: propsPrefix = prefix;
072: authHeader = server.props.getProperty(propsPrefix + HEADER,
073: "authorization");
074: realm = server.props.getProperty(propsPrefix + REALM, "basic");
075: urlPrefix = server.props.getProperty(propsPrefix + PREFIX, "");
076: if (urlPrefix.equals("")) {
077: server.log(Server.LOG_WARNING, prefix,
078: "handler can't find " + PREFIX);
079: return false;
080: }
081: if (!urlPrefix.endsWith("/")) {
082: urlPrefix += "/";
083: }
084: String mapFile = server.props
085: .getProperty(propsPrefix + MAP, "");
086: try {
087: FileInputStream in = new FileInputStream(mapFile);
088: map = new Properties();
089: map.load(in);
090: in.close();
091: } catch (Exception e) {
092: server.log(Server.LOG_ERROR, propsPrefix + MAP, ": ("
093: + mapFile + ") " + e.toString());
094: return false;
095: }
096: return true;
097: }
098:
099: /**
100: * Dispatch and handle the request.
101: * This version looks at the supplier id, rewrites the url based on
102: * that supplier, then lets the default handler do it.
103: */
104:
105: public boolean respond(Request request) {
106: if (!request.url.startsWith(urlPrefix)) {
107: return false;
108: }
109:
110: request.log(Server.LOG_INFORMATIONAL, propsPrefix
111: + ".. handling request");
112: if (request.headers.get(authHeader) == null) {
113: request.sendError(404,
114: "Not Authorized - no credentials supplied", "");
115: return true;
116: }
117:
118: StringTokenizer auth = new StringTokenizer(
119: (String) request.headers.get(authHeader));
120: String realm = auth.nextToken();
121: if (!realm.equals(realm)) {
122: request.sendError(404, "Not Authorized - Invalid realm",
123: "Realm: " + auth + " need: " + realm);
124: return true;
125: }
126:
127: String src = auth.nextToken();
128: String dst = map.getProperty(src, request.props.getProperty(
129: propsPrefix + DEFAULT, ""));
130: if (dst.equals("")) {
131: request.sendError(404, "Not Authorized - Invalid id :"
132: + src, "");
133: return true;
134: }
135:
136: // If we have a supplier url, change it to the correct supplier,
137: // based on the id of the user, otherwise handle it as a normal url.
138:
139: String url = "/" + dst
140: + request.url.substring(request.url.indexOf("/", 1));
141: request.log(Server.LOG_INFORMATIONAL, propsPrefix
142: + ": mapping " + request.url + " -> " + url);
143: request.url = url;
144: return false;
145: }
146: }
|