001: /*
002: * Copyright 2001-2006 C:1 Financial Services GmbH
003: *
004: * This software is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License Version 2.1, as published by the Free Software Foundation.
007: *
008: * This software is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011: * Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public
014: * License along with this library; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
016: */
017:
018: package de.finix.contelligent.components.util;
019:
020: import java.io.IOException;
021: import java.io.Writer;
022: import java.util.Collection;
023: import java.util.Collections;
024: import java.util.Map;
025:
026: import de.finix.contelligent.CallData;
027: import de.finix.contelligent.Component;
028: import de.finix.contelligent.ComponentContext;
029: import de.finix.contelligent.core.security.User;
030: import de.finix.contelligent.logging.LoggingService;
031: import de.finix.contelligent.render.ParameterDescription;
032: import de.finix.contelligent.render.Renderable;
033: import de.finix.contelligent.render.Renderer;
034: import de.finix.contelligent.search.Metainfo;
035: import de.finix.contelligent.util.StringUtils;
036:
037: /**
038: * A <code>CurrentUser</code> may be used to render some information about the
039: * current user of the actual session. If rendered without parameter the
040: * {@link User#getDisplayName display-name} of the user gets rendered, if the
041: * parameter {@link #PARAM_KEY key} is used, the value of the parameter
042: * determines what information gets rendered:
043: * <UL>
044: * <LI>'name' - the {@link User#getDisplayName display-name} of the user
045: * <LI>'id' - the {@link User#getName id} of the user
046: * <LI>'groupId' - the {@link User#getGroupId group-id} of the user
047: * <LI>'roles' - the {@link User#getRoles roles} of the user in comma separated
048: * value format
049: * <LI>'principalString' - the {@link User#toPrincipalString full-id} of the
050: * user that is user-id plus group-id
051: * </UL>
052: * <BR>
053: * To render any additional user data use {@link UserHomeRenderer}.
054: */
055: public class CurrentUser implements Component, Renderable, Renderer {
056: final static org.apache.log4j.Logger log = LoggingService
057: .getLogger(CurrentUser.class);
058:
059: final static private String PARAM_KEY = "key";
060:
061: // default at index 0
062: final static private String[] validKeys = new String[] { "name",
063: "id", "groupId", "roles", "principalString" };
064:
065: final static private ParameterDescription[] parameters = new ParameterDescription[] { new ParameterDescription(
066: PARAM_KEY,
067: ("The key of the data to render, one of ["
068: + StringUtils.createCSV(validKeys, ',')
069: + "]. If rendered without parameter the display-name of the user gets rendered"
070: + " or else the value of the parameter determines what information gets rendered:\n"
071: + " 'name' - the display-name of the user\n"
072: + " 'id' - the id of the user\n"
073: + " 'groupId' - the group-id of the user\n"
074: + " 'roles' - the roles of the user in comma separated value format\n" + " 'principalString' - the full-id of the user that is user-id plus group-id"),
075: false, true, validKeys) };
076:
077: protected ComponentContext ctx;
078:
079: public ParameterDescription[] getParameterDescription() {
080: return parameters;
081: }
082:
083: /**
084: * Implementation of {@link Component#postCreate}. This implementation
085: * doesn't do anything.
086: */
087: public void postCreate() {
088: }
089:
090: /**
091: * Implementation of {@link Component#setComponentContext}.
092: */
093: public void setComponentContext(ComponentContext ctx) {
094: this .ctx = ctx;
095: }
096:
097: /**
098: * Implementation of {@link Component#getComponentContext}.
099: */
100: public ComponentContext getComponentContext() {
101: return ctx;
102: }
103:
104: /**
105: * Implementation of {@link Component#isDynamic}. This implementation
106: * always returns true.
107: */
108: public boolean isDynamic() {
109: return true;
110: }
111:
112: /**
113: * Implementation of {@link Component#mayChangeContent}. This
114: * implementation always returns false.
115: */
116: public boolean mayChangeContent() {
117: return false;
118: }
119:
120: /**
121: * Implementation of {@link Component#putSearchMetainfo}. This
122: * implementation doesn't do anything.
123: */
124: public void putSearchMetainfo(Metainfo metainfo, CallData callData) {
125: }
126:
127: /**
128: * returns the path of this component as string. (without a trailing
129: * separator!)
130: */
131: final public String toString() {
132: if (ctx != null) {
133: return ctx.getPath().toString();
134: } else {
135: return "CurrentUser (name unknown)";
136: }
137: }
138:
139: public Renderer getRenderer() {
140: return this ;
141: }
142:
143: public Collection getSensitiveCategories() {
144: return Collections.EMPTY_SET;
145: }
146:
147: public void render(Writer writer, Map parameterMap,
148: CallData callData) throws IOException {
149: String key = validKeys[0];
150: if (parameterMap != null && parameterMap.containsKey(PARAM_KEY)) {
151: key = ((String[]) parameterMap.get(PARAM_KEY))[0];
152: }
153: User currentUser = callData.getUser();
154: if ("name".equals(key)) {
155: if (log.isDebugEnabled())
156: log.debug("writing display-name of user '"
157: + currentUser + "' ...");
158: writer.write(currentUser.getDisplayName());
159: } else if ("id".equals(key)) {
160: if (log.isDebugEnabled())
161: log.debug("writing id of user '" + currentUser
162: + "' ...");
163: writer.write(currentUser.getName());
164: } else if ("groupId".equals(key)) {
165: if (log.isDebugEnabled())
166: log.debug("writing group-id of user '" + currentUser
167: + "' ...");
168: writer.write(currentUser.getGroupId());
169: } else if ("roles".equals(key)) {
170: if (log.isDebugEnabled())
171: log.debug("writing roles of user '" + currentUser
172: + "' (in CSV format) ...");
173: writer.write(StringUtils.createCSV(currentUser.getRoles(),
174: ','));
175: } else if ("principalString".equals(key)) {
176: if (log.isDebugEnabled())
177: log.debug("writing principal-string of user '"
178: + currentUser + "' ...");
179: writer.write(currentUser.toPrincipalString());
180: } else {
181: // this should never be reached because system checks valid
182: // parameter values.
183: writer.write(currentUser.getDisplayName());
184: }
185: }
186:
187: }
|