0001: /*
0002: * Licensed to the Apache Software Foundation (ASF) under one or more
0003: * contributor license agreements. See the NOTICE file distributed with
0004: * this work for additional information regarding copyright ownership.
0005: * The ASF licenses this file to You under the Apache License, Version 2.0
0006: * (the "License"); you may not use this file except in compliance with
0007: * the License. You may obtain a copy of the License at
0008: *
0009: * http://www.apache.org/licenses/LICENSE-2.0
0010: *
0011: * Unless required by applicable law or agreed to in writing, software
0012: * distributed under the License is distributed on an "AS IS" BASIS,
0013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014: * See the License for the specific language governing permissions and
0015: * limitations under the License.
0016: */
0017: package org.apache.cocoon.webapps.authentication.generation;
0018:
0019: import org.apache.avalon.framework.configuration.Configuration;
0020: import org.apache.avalon.framework.configuration.ConfigurationException;
0021: import org.apache.avalon.framework.service.ServiceException;
0022: import org.apache.avalon.framework.service.ServiceManager;
0023:
0024: import org.apache.cocoon.ProcessingException;
0025: import org.apache.cocoon.components.source.SourceUtil;
0026: import org.apache.cocoon.environment.ObjectModelHelper;
0027: import org.apache.cocoon.environment.Request;
0028: import org.apache.cocoon.environment.Response;
0029: import org.apache.cocoon.environment.Session;
0030: import org.apache.cocoon.generation.ServiceableGenerator;
0031: import org.apache.cocoon.webapps.authentication.AuthenticationManager;
0032: import org.apache.cocoon.webapps.authentication.context.AuthenticationContext;
0033: import org.apache.cocoon.webapps.authentication.user.RequestState;
0034: import org.apache.cocoon.webapps.authentication.user.UserHandler;
0035: import org.apache.cocoon.xml.IncludeXMLConsumer;
0036: import org.apache.cocoon.xml.XMLConsumer;
0037: import org.apache.cocoon.xml.XMLUtils;
0038: import org.apache.cocoon.xml.dom.DOMUtil;
0039:
0040: import org.apache.excalibur.source.Source;
0041: import org.apache.excalibur.source.SourceException;
0042: import org.apache.excalibur.source.SourceParameters;
0043: import org.apache.excalibur.xml.xpath.XPathProcessor;
0044: import org.w3c.dom.Document;
0045: import org.w3c.dom.Element;
0046: import org.w3c.dom.Node;
0047: import org.w3c.dom.NodeList;
0048: import org.xml.sax.SAXException;
0049: import org.xml.sax.helpers.DefaultHandler;
0050:
0051: import java.io.IOException;
0052: import java.util.Enumeration;
0053: import java.util.Map;
0054:
0055: /**
0056: * This is the authentication Configuration Generator.
0057: *
0058: * @author <a href="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
0059: * @deprecated This block is deprecated and will be removed in future versions.
0060: * @version $Id: ConfigurationGenerator.java 433543 2006-08-22 06:22:54Z crossley $
0061: */
0062: public final class ConfigurationGenerator extends ServiceableGenerator {
0063:
0064: /** Request parameter */
0065: public static final String REQ_PARAMETER_STATE = "authstate";
0066: public static final String REQ_PARAMETER_ROLE = "authrole";
0067: public static final String REQ_PARAMETER_ID = "authid";
0068: public static final String REQ_PARAMETER_USER = "authuser";
0069:
0070: private static final String SESSION_CONTEXT_ATTRIBUTE_ADMIN_ROLE = "org.apache.cocoon.webapps.generation.ConfigurationGenerator.simple-role";
0071:
0072: /** The XPath Processor */
0073: protected XPathProcessor xpathProcessor;
0074:
0075: /* (non-Javadoc)
0076: * @see org.apache.avalon.framework.activity.Disposable#dispose()
0077: */
0078: public void dispose() {
0079: if (this .manager != null) {
0080: this .manager.release(this .xpathProcessor);
0081: this .xpathProcessor = null;
0082: }
0083: super .dispose();
0084: }
0085:
0086: /* (non-Javadoc)
0087: * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
0088: */
0089: public void service(ServiceManager manager) throws ServiceException {
0090: super .service(manager);
0091: this .xpathProcessor = (XPathProcessor) this .manager
0092: .lookup(XPathProcessor.ROLE);
0093: }
0094:
0095: /**
0096: * Generate the configuration
0097: */
0098: public void generate() throws IOException, SAXException,
0099: ProcessingException {
0100:
0101: AuthenticationManager authManager = null;
0102: RequestState state = null;
0103: try {
0104: authManager = (AuthenticationManager) this .manager
0105: .lookup(AuthenticationManager.ROLE);
0106: state = authManager.getState();
0107:
0108: } catch (Exception ignore) {
0109: }
0110:
0111: this .xmlConsumer.startDocument();
0112: if (state != null) {
0113: try {
0114: UserHandler userhandler = state.getHandler();
0115:
0116: Configuration conf = state
0117: .getModuleConfiguration("single-role-user-management");
0118: if (conf == null) {
0119: throw new ProcessingException(
0120: "Module configuration 'single-role-user-management' for authentication user management generator not found.");
0121: }
0122: UserManagementHandler handler = new UserManagementHandler(
0123: conf, state.getApplicationName());
0124: this .showConfiguration(this .xmlConsumer, this .source,
0125: handler, userhandler.getContext());
0126:
0127: } catch (ConfigurationException ex) {
0128: throw new ProcessingException(
0129: "ConfigurationException: " + ex, ex);
0130: }
0131: }
0132:
0133: this .xmlConsumer.endDocument();
0134: }
0135:
0136: /**
0137: * Show the configuration for the admin.
0138: * If <code>src</code> is "admin" or null the admin configuration is shown.
0139: * If <code>src</code> is "user" the configuration of the current user
0140: * is shown.
0141: */
0142: public void showConfiguration(XMLConsumer consumer, String src,
0143: UserManagementHandler handler, AuthenticationContext context)
0144: throws ProcessingException, SAXException, IOException {
0145: // synchronized
0146: if (this .getLogger().isDebugEnabled() == true) {
0147: this .getLogger().debug(
0148: "BEGIN showConfiguration consumer=" + consumer
0149: + ", src=" + src);
0150: }
0151:
0152: // get some important information
0153: Request request = ObjectModelHelper
0154: .getRequest(this .objectModel);
0155: Response response = ObjectModelHelper
0156: .getResponse(this .objectModel);
0157: Session session = request.getSession();
0158:
0159: boolean isAdmin = (src == null || src.equals("admin"));
0160:
0161: // now start producing xml:
0162: consumer.startElement("", "configuration", "configuration",
0163: XMLUtils.EMPTY_ATTRIBUTES);
0164:
0165: // set the conf uri:
0166: // This is a bug in the servlet 2.2 API!!!
0167: // It does not contain the context: String uri = HttpUtils.getRequestURL(this.request).toString();
0168: // So: ABSOLUTELY USELESS
0169: String uri = response.encodeURL(request.getRequestURI());
0170: consumer.startElement("", "uri", "uri",
0171: XMLUtils.EMPTY_ATTRIBUTES);
0172: consumer.characters(uri.toCharArray(), 0, uri.length());
0173: consumer.endElement("", "uri", "uri");
0174:
0175: if (isAdmin == true) {
0176: // build the menue
0177: consumer.startElement("", "menue", "menue",
0178: XMLUtils.EMPTY_ATTRIBUTES);
0179:
0180: if (handler.getNewRoleResource() != null) {
0181: consumer.startElement("", "addrole", "addrole",
0182: XMLUtils.EMPTY_ATTRIBUTES);
0183: consumer.endElement("", "addrole", "addrole");
0184: }
0185: if (handler.getDeleteRoleResource() != null) {
0186: consumer.startElement("", "delrole", "delrole",
0187: XMLUtils.EMPTY_ATTRIBUTES);
0188: consumer.endElement("", "delrole", "delrole");
0189: }
0190:
0191: consumer.endElement("", "menue", "menue");
0192: }
0193:
0194: synchronized (session) {
0195:
0196: String state = request.getParameter(REQ_PARAMETER_STATE);
0197: if (state == null) {
0198: state = (isAdmin == true ? "main" : "seluser");
0199: }
0200:
0201: if (state.equals("addrole") == true) {
0202: String role = request.getParameter(REQ_PARAMETER_ROLE);
0203: if (role != null && role.trim().length() > 0) {
0204: SourceParameters pars = new SourceParameters();
0205: // first include all request parameters
0206: Enumeration requestParameters = request
0207: .getParameterNames();
0208: String current;
0209: while (requestParameters.hasMoreElements() == true) {
0210: current = (String) requestParameters
0211: .nextElement();
0212: pars.setParameter(current, request
0213: .getParameter(current));
0214: }
0215: this .addRole(role, pars, handler);
0216: } else {
0217: role = null;
0218: }
0219: session
0220: .removeAttribute(SESSION_CONTEXT_ATTRIBUTE_ADMIN_ROLE);
0221: }
0222:
0223: if (state.equals("delrole") == true) {
0224: try {
0225: String role = request
0226: .getParameter(REQ_PARAMETER_ROLE);
0227: if (role != null) {
0228: // first delete user
0229: Document userDF = this .getUsers(role, null,
0230: handler);
0231: NodeList users = null;
0232: if (userDF != null)
0233: users = DOMUtil.selectNodeList(userDF,
0234: "users/user", this .xpathProcessor);
0235: if (users != null) {
0236: for (int i = 0; i < users.getLength(); i++) {
0237: this .deleteUser(role, DOMUtil
0238: .getValueOf(users.item(i),
0239: "ID",
0240: this .xpathProcessor),
0241: null, handler);
0242: }
0243: }
0244: this .deleteRole(role, null, handler);
0245: }
0246: session
0247: .removeAttribute(SESSION_CONTEXT_ATTRIBUTE_ADMIN_ROLE);
0248: } catch (javax.xml.transform.TransformerException local) {
0249: throw new ProcessingException(
0250: "TransformerException: " + local, local);
0251: }
0252: }
0253:
0254: if (state.equals("chguser") == true) {
0255: String role;
0256: String id;
0257: String user;
0258:
0259: if (isAdmin == false) {
0260: Map pars = context.getContextInfo();
0261: id = (String) pars.get("ID");
0262: role = (String) pars.get("role");
0263: user = "old";
0264: } else {
0265: role = request.getParameter(REQ_PARAMETER_ROLE);
0266: id = request.getParameter(REQ_PARAMETER_ID);
0267: user = request.getParameter(REQ_PARAMETER_USER);
0268: }
0269:
0270: boolean addingNewUserFailed = false;
0271: if (role != null && id != null && user != null) {
0272: if (user.equals("new") == true) {
0273: SourceParameters pars = new SourceParameters();
0274: // first include all request parameters
0275: Enumeration requestParameters = request
0276: .getParameterNames();
0277: String current;
0278: while (requestParameters.hasMoreElements() == true) {
0279: current = (String) requestParameters
0280: .nextElement();
0281: pars.setParameter(current, request
0282: .getParameter(current));
0283: }
0284: addingNewUserFailed = !this .addUser(role, id,
0285: pars, handler);
0286: if (addingNewUserFailed == false) {
0287: consumer.startElement("", "addeduser",
0288: "addeduser",
0289: XMLUtils.EMPTY_ATTRIBUTES);
0290: consumer.characters(id.toCharArray(), 0, id
0291: .length());
0292: consumer.endElement("", "addeduser",
0293: "addeduser");
0294: }
0295: } else {
0296: String delete = request
0297: .getParameter("authdeluser");
0298: if (delete != null
0299: && delete.equals("true") == true) {
0300: this .deleteUser(role, id, null, handler);
0301: } else {
0302: SourceParameters pars = new SourceParameters();
0303: // first include all request parameters
0304: Enumeration requestParameters = request
0305: .getParameterNames();
0306: String current;
0307: while (requestParameters.hasMoreElements() == true) {
0308: current = (String) requestParameters
0309: .nextElement();
0310: pars.setParameter(current, request
0311: .getParameter(current));
0312: }
0313: this .changeUser(role, id, pars, handler);
0314: }
0315: }
0316: session
0317: .removeAttribute(SESSION_CONTEXT_ATTRIBUTE_ADMIN_ROLE);
0318: }
0319: if (addingNewUserFailed == false) {
0320: state = (isAdmin == true ? "adduser" : "seluser");
0321: } else {
0322: state = "erruser";
0323: }
0324: }
0325:
0326: if (state.equals("seluser") == true) {
0327: String role;
0328: String id;
0329:
0330: if (isAdmin == false) {
0331: Map pars = context.getContextInfo();
0332: id = (String) pars.get("ID");
0333: role = (String) pars.get("role");
0334: } else {
0335: role = request.getParameter(REQ_PARAMETER_ROLE);
0336: id = request.getParameter(REQ_PARAMETER_ID);
0337: }
0338: if (role != null && id != null) {
0339: session.setAttribute(
0340: SESSION_CONTEXT_ATTRIBUTE_ADMIN_ROLE, role);
0341:
0342: // include users
0343: Document userDF = this .getUsers(role, id, handler);
0344: Element users = null;
0345: try {
0346: if (userDF != null)
0347: users = (Element) DOMUtil.getSingleNode(
0348: userDF, "users/user",
0349: this .xpathProcessor);
0350: } catch (javax.xml.transform.TransformerException local) {
0351: throw new ProcessingException(
0352: "TransformerException: " + local, local);
0353: }
0354: consumer.startElement("", "uservalues",
0355: "uservalues", XMLUtils.EMPTY_ATTRIBUTES);
0356: if (users != null && users.hasChildNodes() == true) {
0357: NodeList childs = users.getChildNodes();
0358: for (int i = 0; i < childs.getLength(); i++) {
0359: if (childs.item(i).getNodeType() == Node.ELEMENT_NODE)
0360: IncludeXMLConsumer.includeNode(childs
0361: .item(i), consumer, consumer);
0362: }
0363: }
0364: consumer.endElement("", "uservalues", "uservalues");
0365: }
0366: consumer.startElement("", "user", "user",
0367: XMLUtils.EMPTY_ATTRIBUTES);
0368: consumer.characters("old".toCharArray(), 0, 3);
0369: consumer.endElement("", "user", "user");
0370: if (isAdmin == false) {
0371: consumer.startElement("", "role", "role",
0372: XMLUtils.EMPTY_ATTRIBUTES);
0373: consumer.characters(role.toCharArray(), 0, role
0374: .length());
0375: consumer.endElement("", "role", "role");
0376: }
0377: }
0378:
0379: if (state.equals("erruser") == true) {
0380: String role;
0381: String id;
0382:
0383: if (isAdmin == false) {
0384: Map pars = context.getContextInfo();
0385: id = (String) pars.get("ID");
0386: role = (String) pars.get("role");
0387: } else {
0388: role = request.getParameter(REQ_PARAMETER_ROLE);
0389: id = request.getParameter(REQ_PARAMETER_ID);
0390: }
0391: if (role != null && id != null) {
0392: session.setAttribute(
0393: SESSION_CONTEXT_ATTRIBUTE_ADMIN_ROLE, role);
0394:
0395: // include users
0396: Document userDF = this .getUsers(role, id, handler);
0397: Element users = null;
0398: try {
0399: if (userDF != null)
0400: users = (Element) DOMUtil.getSingleNode(
0401: userDF, "users/user",
0402: this .xpathProcessor);
0403: } catch (javax.xml.transform.TransformerException local) {
0404: throw new ProcessingException(
0405: "TransformerException: " + local, local);
0406: }
0407: consumer.startElement("", "uservalues",
0408: "uservalues", XMLUtils.EMPTY_ATTRIBUTES);
0409: if (users != null && users.hasChildNodes() == true) {
0410: NodeList childs = users.getChildNodes();
0411: for (int i = 0; i < childs.getLength(); i++) {
0412: if (childs.item(i).getNodeType() == Node.ELEMENT_NODE)
0413: IncludeXMLConsumer.includeNode(childs
0414: .item(i), consumer, consumer);
0415: }
0416: }
0417: consumer.endElement("", "uservalues", "uservalues");
0418: }
0419: consumer.startElement("", "user", "user",
0420: XMLUtils.EMPTY_ATTRIBUTES);
0421: consumer.characters("error".toCharArray(), 0, 5);
0422: consumer.endElement("", "user", "user");
0423: if (isAdmin == false) {
0424: consumer.startElement("", "role", "role",
0425: XMLUtils.EMPTY_ATTRIBUTES);
0426: consumer.characters(role.toCharArray(), 0, role
0427: .length());
0428: consumer.endElement("", "role", "role");
0429: }
0430: }
0431:
0432: if (state.equals("adduser") == true) {
0433: consumer.startElement("", "user", "user",
0434: XMLUtils.EMPTY_ATTRIBUTES);
0435: consumer.characters("new".toCharArray(), 0, 3);
0436: consumer.endElement("", "user", "user");
0437: }
0438:
0439: if (state.equals("selrole") == true) {
0440: String role = request.getParameter(REQ_PARAMETER_ROLE);
0441: session.setAttribute(
0442: SESSION_CONTEXT_ATTRIBUTE_ADMIN_ROLE, role);
0443: // include users
0444: Document userDF = this .getUsers(role, null, handler);
0445: Node users = null;
0446: try {
0447: if (userDF != null)
0448: users = DOMUtil.getSingleNode(userDF, "users",
0449: this .xpathProcessor);
0450: } catch (javax.xml.transform.TransformerException local) {
0451: throw new ProcessingException(
0452: "TransformerException: " + local, local);
0453: }
0454: IncludeXMLConsumer.includeNode(users, consumer,
0455: consumer);
0456: }
0457:
0458: if (isAdmin == true) {
0459: // include roles
0460: Document rolesDF = this .getRoles(handler);
0461: Node roles = null;
0462: try {
0463: if (rolesDF != null)
0464: roles = DOMUtil.getSingleNode(rolesDF, "roles",
0465: this .xpathProcessor);
0466: } catch (javax.xml.transform.TransformerException local) {
0467: throw new ProcessingException(
0468: "TransformerException: " + local, local);
0469: }
0470: IncludeXMLConsumer.includeNode(roles, consumer,
0471: consumer);
0472:
0473: // include selected role
0474: String role = (String) session
0475: .getAttribute(SESSION_CONTEXT_ATTRIBUTE_ADMIN_ROLE);
0476: if (role != null) {
0477: consumer.startElement("", "role", "role",
0478: XMLUtils.EMPTY_ATTRIBUTES);
0479: consumer.characters(role.toCharArray(), 0, role
0480: .length());
0481: consumer.endElement("", "role", "role");
0482: }
0483: }
0484: } // end synchronized(context)
0485:
0486: consumer.endElement("", "configuration", "configuration");
0487: if (this .getLogger().isDebugEnabled() == true) {
0488: this .getLogger().debug("END showConfiguration");
0489: }
0490: }
0491:
0492: /**
0493: * Get all users in a document fragment with the following children:
0494: * <users>
0495: * <user>
0496: * <ID>...</ID>
0497: * <role>...</role> <!-- optional -->
0498: * <data>
0499: * ...
0500: * </data>
0501: * </user>
0502: * ....
0503: * </users>
0504: * The document fragment might contain further nodes at the root!
0505: * If <code>role</code> is <code>null</code> all users are fetched,
0506: * otherwise only the users for this role.
0507: * If also ID is not null only the single user is fetched.
0508: */
0509: public Document getUsers(String role, String ID,
0510: UserManagementHandler handler) throws IOException,
0511: ProcessingException, SAXException {
0512: // calling method is syned
0513: if (this .getLogger().isDebugEnabled() == true) {
0514: this .getLogger().debug(
0515: "BEGIN getUsers role=" + role + ", ID=" + ID);
0516: }
0517: Document frag = null;
0518:
0519: if (handler.getLoadUsersResource() != null) {
0520: final String loadUsersResource = handler
0521: .getLoadUsersResource();
0522: final SourceParameters loadParameters = handler
0523: .getLoadUsersResourceParameters();
0524: SourceParameters parameters = (loadParameters == null) ? new SourceParameters()
0525: : (SourceParameters) loadParameters;
0526: if (handler.getApplicationName() != null) {
0527: parameters.setSingleParameterValue("application",
0528: handler.getApplicationName());
0529: }
0530: if (ID != null) {
0531: parameters.setSingleParameterValue("type", "user");
0532: parameters.setSingleParameterValue("ID", ID);
0533: } else {
0534: parameters.setSingleParameterValue("type", "users");
0535: }
0536: if (role != null)
0537: parameters.setSingleParameterValue("role", role);
0538: frag = this .loadResource(loadUsersResource, parameters);
0539:
0540: }
0541:
0542: if (this .getLogger().isDebugEnabled() == true) {
0543: this
0544: .getLogger()
0545: .debug(
0546: "END getUsers fragment="
0547: + (frag == null ? "null"
0548: : XMLUtils
0549: .serializeNode(
0550: frag,
0551: XMLUtils
0552: .createPropertiesForXML(false))));
0553: }
0554: return frag;
0555: }
0556:
0557: /**
0558: * Get all roles in a document fragment with the following children:
0559: * <roles>
0560: * <role>...</role>
0561: * ....
0562: * </roles>
0563: * The document fragment might contain further nodes at the root!
0564: */
0565: public Document getRoles(UserManagementHandler handler)
0566: throws IOException, ProcessingException, SAXException {
0567: // calling method is syned
0568: if (this .getLogger().isDebugEnabled() == true) {
0569: this .getLogger().debug("BEGIN getRoles");
0570: }
0571: Document frag = null;
0572:
0573: if (handler.getLoadRolesResource() != null) {
0574: final String loadRolesResource = handler
0575: .getLoadRolesResource();
0576: final SourceParameters loadParameters = handler
0577: .getLoadRolesResourceParameters();
0578: SourceParameters parameters = (loadParameters == null) ? new SourceParameters()
0579: : (SourceParameters) loadParameters.clone();
0580: if (handler.getApplicationName() != null)
0581: parameters.setSingleParameterValue("application",
0582: handler.getApplicationName());
0583: parameters.setSingleParameterValue("type", "roles");
0584: frag = this .loadResource(loadRolesResource, parameters);
0585: }
0586:
0587: if (this .getLogger().isDebugEnabled() == true) {
0588: this .getLogger().debug("END getRoles fragment=" + frag);
0589: }
0590: return frag;
0591: }
0592:
0593: /**
0594: * Add a role
0595: */
0596: private void addRole(String name, SourceParameters parameters,
0597: UserManagementHandler handler) throws IOException,
0598: ProcessingException, SAXException {
0599: // calling method is syned
0600: if (this .getLogger().isDebugEnabled() == true) {
0601: this .getLogger().debug(
0602: "BEGIN addRole role=" + name + ", parameters="
0603: + parameters);
0604: }
0605: if (handler.getNewRoleResource() != null) {
0606: final String newRoleResource = handler.getNewRoleResource();
0607: final SourceParameters handlerPars = handler
0608: .getNewRoleResourceParameters();
0609: if (parameters == null)
0610: parameters = new SourceParameters();
0611: parameters.add(handlerPars);
0612:
0613: if (handler.getApplicationName() != null)
0614: parameters.setSingleParameterValue("application",
0615: handler.getApplicationName());
0616: parameters.setSingleParameterValue("type", "role");
0617: parameters.setSingleParameterValue("role", name);
0618:
0619: this .invokeResource(newRoleResource, parameters);
0620: }
0621:
0622: if (this .getLogger().isDebugEnabled() == true) {
0623: this .getLogger().debug("END addRole");
0624: }
0625: }
0626:
0627: /**
0628: * Add a user.
0629: * @return If a user with ID already exists <code>false</code> is returned.
0630: */
0631: public boolean addUser(String role, String ID,
0632: SourceParameters parameters, UserManagementHandler handler)
0633: throws IOException, ProcessingException, SAXException {
0634: // calling method is syned
0635: if (this .getLogger().isDebugEnabled() == true) {
0636: this .getLogger().debug(
0637: "BEGIN addUser role=" + role + ", ID=" + ID
0638: + ", parameters=" + parameters);
0639: }
0640: boolean result = false;
0641:
0642: if (handler.getNewUserResource() != null && ID != null
0643: && ID.trim().length() > 0) {
0644: // first test if a user with this ID already exists
0645: Document user = this .getUsers(null, null, handler);
0646: Node node = null;
0647: if (user != null) {
0648: try {
0649: node = DOMUtil.getSingleNode(user,
0650: "users/user/ID[text()='" + ID + "']",
0651: this .xpathProcessor);
0652: } catch (javax.xml.transform.TransformerException local) {
0653: throw new ProcessingException(
0654: "Transformer exception: " + local, local);
0655: }
0656: }
0657: if (user == null || node == null) {
0658: final String newUserResource = handler
0659: .getNewUserResource();
0660: final SourceParameters newUsersPars = handler
0661: .getNewUserResourceParameters();
0662: if (parameters == null)
0663: parameters = new SourceParameters();
0664: parameters.add(newUsersPars);
0665:
0666: if (handler.getApplicationName() != null)
0667: parameters.setSingleParameterValue("application",
0668: handler.getApplicationName());
0669: parameters.setSingleParameterValue("type", "user");
0670: parameters.setSingleParameterValue("role", role);
0671: parameters.setSingleParameterValue("ID", ID);
0672:
0673: this .invokeResource(newUserResource, parameters);
0674: result = true;
0675: }
0676: }
0677:
0678: if (this .getLogger().isDebugEnabled() == true) {
0679: this .getLogger().debug("END addUser success=" + result);
0680: }
0681: return result;
0682: }
0683:
0684: /**
0685: * Delete a role
0686: */
0687: private void deleteRole(String name, SourceParameters parameters,
0688: UserManagementHandler handler) throws IOException,
0689: ProcessingException, SAXException {
0690: // calling method is syned
0691: if (this .getLogger().isDebugEnabled() == true) {
0692: this .getLogger().debug(
0693: "BEGIN deleteRole role=" + name + ", parameters="
0694: + parameters);
0695: }
0696: if (handler.getDeleteRoleResource() != null) {
0697: final String deleteRoleResource = handler
0698: .getDeleteRoleResource();
0699: final SourceParameters handlerPars = handler
0700: .getDeleteRoleResourceParameters();
0701: if (parameters == null)
0702: parameters = new SourceParameters();
0703: parameters.add(handlerPars);
0704:
0705: if (handler.getApplicationName() != null)
0706: parameters.setSingleParameterValue("application",
0707: handler.getApplicationName());
0708: parameters.setSingleParameterValue("type", "role");
0709: parameters.setSingleParameterValue("role", name);
0710:
0711: this .invokeResource(deleteRoleResource, parameters);
0712: }
0713:
0714: if (this .getLogger().isDebugEnabled() == true) {
0715: this .getLogger().debug("END deleteRole");
0716: }
0717: }
0718:
0719: /**
0720: * Delete a user
0721: */
0722: private void deleteUser(String role, String name,
0723: SourceParameters parameters, UserManagementHandler handler)
0724: throws IOException, ProcessingException, SAXException {
0725: // calling method is syned
0726: if (this .getLogger().isDebugEnabled() == true) {
0727: this .getLogger().debug(
0728: "BEGIN deleteUser role=" + role + ", ID=" + name
0729: + ", parameters=" + parameters);
0730: }
0731: if (handler.getDeleteUserResource() != null) {
0732: final String deleteUserResource = handler
0733: .getDeleteUserResource();
0734: final SourceParameters handlerPars = handler
0735: .getDeleteUserResourceParameters();
0736: if (parameters == null)
0737: parameters = new SourceParameters();
0738: parameters.add(handlerPars);
0739:
0740: if (handler.getApplicationName() != null)
0741: parameters.setSingleParameterValue("application",
0742: handler.getApplicationName());
0743: parameters.setSingleParameterValue("type", "user");
0744: parameters.setSingleParameterValue("role", role);
0745: parameters.setSingleParameterValue("ID", name);
0746:
0747: this .invokeResource(deleteUserResource, parameters);
0748: }
0749:
0750: if (this .getLogger().isDebugEnabled() == true) {
0751: this .getLogger().debug("END deleteUser");
0752: }
0753: }
0754:
0755: /**
0756: * Change a user
0757: */
0758: private void changeUser(String role, String name,
0759: SourceParameters parameters, UserManagementHandler handler)
0760: throws IOException, ProcessingException, SAXException {
0761: // calling method is syned
0762: if (this .getLogger().isDebugEnabled() == true) {
0763: this .getLogger().debug(
0764: "BEGIN changeUser role=" + role + ", ID=" + name
0765: + ", parameters=" + parameters);
0766: }
0767: if (handler.getChangeUserResource() != null) {
0768: final String changeUserResource = handler
0769: .getChangeUserResource();
0770: final SourceParameters handlerPars = handler
0771: .getChangeUserResourceParameters();
0772: if (parameters == null)
0773: parameters = new SourceParameters();
0774: parameters.add(handlerPars);
0775:
0776: if (handler.getApplicationName() != null)
0777: parameters.setSingleParameterValue("application",
0778: handler.getApplicationName());
0779: parameters.setSingleParameterValue("type", "user");
0780: parameters.setSingleParameterValue("role", role);
0781: parameters.setSingleParameterValue("ID", name);
0782:
0783: this .invokeResource(changeUserResource, parameters);
0784: }
0785:
0786: if (this .getLogger().isDebugEnabled() == true) {
0787: this .getLogger().debug("END changeUser");
0788: }
0789: }
0790:
0791: /**
0792: * Invoke resource
0793: */
0794: private void invokeResource(String resource,
0795: SourceParameters parameters) throws IOException,
0796: ProcessingException, SAXException {
0797: Source source = null;
0798: try {
0799: source = SourceUtil.getSource(resource, null, parameters,
0800: this .resolver);
0801: SourceUtil
0802: .parse(this .manager, source, new DefaultHandler());
0803: } catch (SourceException se) {
0804: throw SourceUtil.handle(se);
0805: } finally {
0806: this .resolver.release(source);
0807: }
0808: }
0809:
0810: /**
0811: * Load XML resource
0812: */
0813: private Document loadResource(String resource,
0814: SourceParameters parameters) throws IOException,
0815: ProcessingException, SAXException {
0816: Source source = null;
0817: try {
0818: source = SourceUtil.getSource(resource, null, parameters,
0819: this .resolver);
0820: return SourceUtil.toDOM(source);
0821: } catch (SourceException se) {
0822: throw SourceUtil.handle(se);
0823: } finally {
0824: this .resolver.release(source);
0825: }
0826: }
0827: }
0828:
0829: final class UserManagementHandler {
0830:
0831: /** The name of the current application */
0832: private String applicationName;
0833:
0834: /** The load-users resource */
0835: private String loadUsersResource;
0836: private SourceParameters loadUsersResourceParameters;
0837:
0838: /** The load-roles resource */
0839: private String loadRolesResource;
0840: private SourceParameters loadRolesResourceParameters;
0841:
0842: /** The new-user resource */
0843: private String newUserResource;
0844: private SourceParameters newUserResourceParameters;
0845:
0846: /** The new-role resource */
0847: private String newRoleResource;
0848: private SourceParameters newRoleResourceParameters;
0849:
0850: /** The delete-role resource */
0851: private String deleteRoleResource;
0852: private SourceParameters deleteRoleResourceParameters;
0853:
0854: /** The delete-user resource */
0855: private String deleteUserResource;
0856: private SourceParameters deleteUserResourceParameters;
0857:
0858: /** The change-user resource */
0859: private String changeUserResource;
0860: private SourceParameters changeUserResourceParameters;
0861:
0862: /**
0863: * Create a new handler object.
0864: */
0865: public UserManagementHandler(Configuration conf, String appName)
0866: throws ConfigurationException {
0867: Configuration child;
0868:
0869: this .applicationName = appName;
0870:
0871: // get load-users resource (optional)
0872: child = conf.getChild("load-users", false);
0873: if (child != null) {
0874: this .loadUsersResource = child.getAttribute("uri");
0875: this .loadUsersResourceParameters = SourceParameters
0876: .create(child);
0877: }
0878:
0879: // get load-roles resource (optional)
0880: child = conf.getChild("load-roles", false);
0881: if (child != null) {
0882: this .loadRolesResource = child.getAttribute("uri");
0883: this .loadRolesResourceParameters = SourceParameters
0884: .create(child);
0885: }
0886:
0887: // get new user resource (optional)
0888: child = conf.getChild("new-user", false);
0889: if (child != null) {
0890: this .newUserResource = child.getAttribute("uri");
0891: this .newUserResourceParameters = SourceParameters
0892: .create(child);
0893: }
0894:
0895: // get new role resource (optional)
0896: child = conf.getChild("new-role", false);
0897: if (child != null) {
0898: this .newRoleResource = child.getAttribute("uri");
0899: this .newRoleResourceParameters = SourceParameters
0900: .create(child);
0901: }
0902:
0903: // get delete user resource (optional)
0904: child = conf.getChild("delete-user", false);
0905: if (child != null) {
0906: this .deleteUserResource = child.getAttribute("uri");
0907: this .deleteUserResourceParameters = SourceParameters
0908: .create(child);
0909: }
0910:
0911: // get delete role resource (optional)
0912: child = conf.getChild("delete-role", false);
0913: if (child != null) {
0914: this .deleteRoleResource = child.getAttribute("uri");
0915: this .deleteRoleResourceParameters = SourceParameters
0916: .create(child);
0917: }
0918:
0919: // get change user resource (optional)
0920: child = conf.getChild("change-user", false);
0921: if (child != null) {
0922: this .changeUserResource = child.getAttribute("uri");
0923: this .changeUserResourceParameters = SourceParameters
0924: .create(child);
0925: }
0926: }
0927:
0928: /**
0929: * Get the name of the current application
0930: */
0931: public String getApplicationName() {
0932: return this .applicationName;
0933: }
0934:
0935: /**
0936: * Get the load users resource
0937: */
0938: public String getLoadUsersResource() {
0939: return this .loadUsersResource;
0940: }
0941:
0942: public SourceParameters getLoadUsersResourceParameters() {
0943: return this .loadUsersResourceParameters;
0944: }
0945:
0946: /**
0947: * Get the load roles resource
0948: */
0949: public String getLoadRolesResource() {
0950: return this .loadRolesResource;
0951: }
0952:
0953: public SourceParameters getLoadRolesResourceParameters() {
0954: return this .loadRolesResourceParameters;
0955: }
0956:
0957: /**
0958: * Get the new user resource
0959: */
0960: public String getNewUserResource() {
0961: return this .newUserResource;
0962: }
0963:
0964: public SourceParameters getNewUserResourceParameters() {
0965: return this .newUserResourceParameters;
0966: }
0967:
0968: /**
0969: * Get the new role resource
0970: */
0971: public String getNewRoleResource() {
0972: return this .newRoleResource;
0973: }
0974:
0975: public SourceParameters getNewRoleResourceParameters() {
0976: return this .newRoleResourceParameters;
0977: }
0978:
0979: /** Get the delete user resource */
0980: public String getDeleteUserResource() {
0981: return this .deleteUserResource;
0982: }
0983:
0984: public SourceParameters getDeleteUserResourceParameters() {
0985: return this .deleteUserResourceParameters;
0986: }
0987:
0988: /** Get the delete role resource */
0989: public String getDeleteRoleResource() {
0990: return this .deleteRoleResource;
0991: }
0992:
0993: public SourceParameters getDeleteRoleResourceParameters() {
0994: return this .deleteRoleResourceParameters;
0995: }
0996:
0997: /** Get the change user resource */
0998: public String getChangeUserResource() {
0999: return this .changeUserResource;
1000: }
1001:
1002: public SourceParameters getChangeUserResourceParameters() {
1003: return this.changeUserResourceParameters;
1004: }
1005: }
|