001: /*
002: * RolesHandler.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.11
024: * Created by suhler on 98/09/30
025: * Last modified by suhler on 00/07/07 17:01:22
026: */
027:
028: package sunlabs.brazil.handler;
029:
030: import java.io.FileInputStream;
031: import java.io.IOException;
032: import java.util.Enumeration;
033: import java.util.Hashtable;
034: import java.util.Properties;
035: import sunlabs.brazil.server.Handler;
036: import sunlabs.brazil.server.Request;
037: import sunlabs.brazil.server.Server;
038:
039: /**
040: * Handler for associating roles with an id. This is a placeholder
041: * until the SunEconomy gets integrated in. It looks for an "id" in the
042: * request, looks it up in a property file, then add the value of the
043: * id into the request. It may be used in conjuntion with
044: * {@link AclSwitchHandler} to provide role based web access.
045: *
046: * @author Stephen Uhler
047: * @version @(#) RolesHandler.java 1.11 00/07/07 17:01:22
048: */
049:
050: public class RolesHandler implements Handler {
051: Properties map; // The authorization mapping table
052: String propsPrefix; // My prefix in the global properties file
053: String urlPrefix; // The prefix to look for to map
054: String idKey; // The property name for the token id
055: String roleKey; // The property name for the roles
056: boolean check = true; // only put results in request, don't return
057:
058: /**
059: * Handler configuration property <b>SessionID</b>.
060: * The request property name to find the id string.
061: * Defaults to id.
062: */
063:
064: public static final String ID_KEY = "SessionID"; // property key for token id
065:
066: /**
067: * Handler configuration property <b>roleName</b>.
068: * The request property name to place the roles into.
069: * Defaults to roles.
070: */
071:
072: public static final String ROLE_KEY = "roleName"; // property key for token roles
073:
074: /**
075: * Handler configuration property <b>mapFile</b>.
076: * The path to the java properties file containing the id
077: * to roles mapping. The roles are a whitespace delimited list
078: * of ascii role names.
079: */
080:
081: public static final String MAP = "mapFile"; // properties file
082:
083: /**
084: * Handler configuration property <b>prefix</b>.
085: * Only URL's that begin with this string are considered by this handler.
086: * The default is (/).
087: */
088:
089: static final String PREFIX = "prefix"; // url prefix
090:
091: /**
092: * Handler configuration property <b>check</b>.
093: * If true, the results are out into the request object.
094: * Otherwise, they are returned in a text/plain java properties
095: * formatted document, which can be used with the
096: * @see RemoteStsHandler.
097: */
098:
099: static final String CHECK = "check"; // how results are returned
100:
101: public boolean init(Server server, String prefix) {
102: propsPrefix = prefix;
103: urlPrefix = server.props.getProperty(propsPrefix + PREFIX, "/");
104: String mapFile = server.props
105: .getProperty(propsPrefix + MAP, "");
106: map = new Properties();
107: try {
108: FileInputStream in = new FileInputStream(mapFile);
109: map.load(in);
110: in.close();
111: } catch (Exception e) {
112: server.log(Server.LOG_ERROR, prefix, propsPrefix + MAP
113: + ": (" + mapFile + ") " + e.toString());
114: return false;
115: }
116: idKey = server.props.getProperty(propsPrefix + ID_KEY, "id");
117: roleKey = server.props.getProperty(propsPrefix + ROLE_KEY,
118: "roles");
119: check = server.props.getProperty(propsPrefix + CHECK, "y")
120: .startsWith("y");
121: return true;
122: }
123:
124: /**
125: * Dispatch and handle the request.
126: * This version looks at the request for the id, looks it up in the
127: * table, and adds the value, if available
128: */
129:
130: public boolean respond(Request request) throws IOException {
131: if (!request.url.startsWith(urlPrefix)) {
132: return false;
133: }
134: String id = (String) request.props.get(idKey);
135: // System.out.println("ids: " + map);
136: // System.out.println("props: " + request.props);
137: if (id != null && map.containsKey(id)) {
138: String value = (String) map.get(id);
139: request.log(Server.LOG_DIAGNOSTIC, "Mapping: " + id + "->"
140: + value);
141: request.props.put(roleKey, value);
142: }
143: if (!check) {
144: StringBuffer result = new StringBuffer("");
145: Enumeration keys = request.props.keys();
146: while (keys.hasMoreElements()) {
147: String key = (String) keys.nextElement();
148: result
149: .append(key + "=" + request.props.get(key)
150: + "\n");
151: }
152: request.sendResponse(result.toString(), "text/plain");
153: return true;
154: } else {
155: return false;
156: }
157: }
158: }
|