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.AbstractComponent;
027: import de.finix.contelligent.CallData;
028: import de.finix.contelligent.logging.LoggingService;
029: import de.finix.contelligent.render.ParameterDescription;
030: import de.finix.contelligent.render.Renderable;
031: import de.finix.contelligent.render.Renderer;
032: import de.finix.contelligent.util.StringUtils;
033:
034: public class SessionDataRenderer extends AbstractComponent implements
035: Renderable, Renderer {
036: final static private org.apache.log4j.Logger log = LoggingService
037: .getLogger(SessionDataRenderer.class);
038:
039: final static private String PARAM_KEY = "key";
040:
041: final static private String PARAM_SCOPE = "scope";
042:
043: final static private String[] scopeValues = new String[] {
044: "session", "request" };
045:
046: final static private ParameterDescription[] parameters = new ParameterDescription[] {
047: new ParameterDescription(PARAM_KEY,
048: "The key to lookup for", false),
049: new ParameterDescription(
050: PARAM_SCOPE,
051: "The scope where the key should be looked up, one of ["
052: + StringUtils.createCSV(scopeValues, ',')
053: + "]. If no defined property 'scope' is used.",
054: false, true, scopeValues) };
055:
056: private String scope, key;
057:
058: public String getKey() {
059: return key;
060: }
061:
062: public void setKey(String key) {
063: this .key = key;
064: }
065:
066: public String getScope() {
067: return scope;
068: }
069:
070: public void setScope(String scope) {
071: this .scope = scope;
072: }
073:
074: public ParameterDescription[] getParameterDescription() {
075: return parameters;
076: }
077:
078: public Collection getSensitiveCategories() {
079: return Collections.EMPTY_SET;
080: }
081:
082: public Renderer getRenderer() {
083: return this ;
084: }
085:
086: public void render(Writer writer, Map parameterMap,
087: CallData callData) throws IOException {
088: int scopeId = 0;
089: if (parameterMap != null
090: && parameterMap.containsKey(PARAM_SCOPE)) {
091: scopeId = scopeStringToInt(((String[]) parameterMap
092: .get(PARAM_SCOPE))[0]);
093: } else {
094: scopeId = scopeStringToInt(scope);
095: }
096: String key = this .key;
097: if (parameterMap != null && parameterMap.containsKey(PARAM_KEY)) {
098: key = ((String[]) parameterMap.get(PARAM_KEY))[0];
099: }
100:
101: Object tmp = null;
102: switch (scopeId) {
103: case 0: // session
104: tmp = callData.getSessionAttribute(key);
105: break;
106: case 1: // request
107: tmp = callData.getRequestAttribute(key);
108: break;
109: }
110:
111: if (tmp != null) {
112: if (log.isDebugEnabled())
113: log.debug("'" + this + "':render() - writing value="
114: + tmp + ".");
115: if (tmp instanceof String[])
116: writer
117: .write(StringUtils.createCSV((String[]) tmp,
118: ','));
119: else
120: writer.write(tmp.toString());
121: } else {
122: log.error("'" + this
123: + "':render() - no value found for key '" + key
124: + "'!");
125: }
126: }
127:
128: private int scopeStringToInt(String scope) {
129: for (int i = 0; i < scopeValues.length; i++) {
130: if (scope.equals(scopeValues[i])) {
131: return i;
132: }
133: }
134: return 0;
135: }
136:
137: }
|