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 javax.servlet.http.Cookie;
027:
028: import de.finix.contelligent.AbstractComponent;
029: import de.finix.contelligent.CallData;
030: import de.finix.contelligent.Component;
031: import de.finix.contelligent.ComponentPath;
032: import de.finix.contelligent.content.ContentProvider;
033: import de.finix.contelligent.exception.ContelligentException;
034: import de.finix.contelligent.logging.LoggingService;
035: import de.finix.contelligent.render.DefaultRenderer;
036: import de.finix.contelligent.render.ParameterDescription;
037: import de.finix.contelligent.render.Renderable;
038: import de.finix.contelligent.render.Renderer;
039: import de.finix.contelligent.util.StringUtils;
040:
041: public class ParameterRenderer extends AbstractComponent implements
042: Renderable, Renderer {
043: final static private org.apache.log4j.Logger log = LoggingService
044: .getLogger(ParameterRenderer.class);
045:
046: final static private String PARAM_KEY = "key";
047:
048: final static private String PARAM_SCOPE = "scope";
049:
050: final static private String PARAM_EVAL = "evaluate";
051:
052: final static private String PARAM_REQUESTSESSION = "requestsession";
053:
054: final static private String[] scopeValues = new String[] {
055: "session", "request", "cookie", "attribute",
056: "requestsession" };
057:
058: final static private int SESSION = 0, REQUEST = 1, COOKIE = 2,
059: ATTRIBUTE = 3, REQUESTSESSION = 4;
060:
061: final static private String[] renderValues = new String[] { "true",
062: "false" };
063:
064: final static private ParameterDescription[] parameters = new ParameterDescription[] {
065: new ParameterDescription(PARAM_KEY,
066: "The key to lookup for", false),
067: new ParameterDescription(
068: PARAM_SCOPE,
069: "The scope where the key should be looked up, one of ["
070: + StringUtils.createCSV(scopeValues, ',')
071: + "]. If no defined property 'scope' is used.",
072: false, true, scopeValues),
073: new ParameterDescription(PARAM_EVAL, "Evaluate value",
074: false, true, renderValues) };
075:
076: private String scope, key, defaultValue;
077:
078: private boolean renderTarget;
079:
080: public boolean getRenderTarget() {
081: return renderTarget;
082: }
083:
084: public void setRenderTarget(boolean renderTarget) {
085: this .renderTarget = renderTarget;
086: }
087:
088: public String getDefaultValue() {
089: return this .defaultValue;
090: }
091:
092: public void setDefaultValue(String defaultValue) {
093: this .defaultValue = defaultValue;
094: }
095:
096: public String getKey() {
097: return key;
098: }
099:
100: public void setKey(String key) {
101: this .key = key;
102: }
103:
104: public String getScope() {
105: return scope;
106: }
107:
108: public void setScope(String scope) {
109: this .scope = scope;
110: }
111:
112: public ParameterDescription[] getParameterDescription() {
113: return parameters;
114: }
115:
116: public Collection getSensitiveCategories() {
117: return Collections.EMPTY_SET;
118: }
119:
120: public Renderer getRenderer() {
121: return this ;
122: }
123:
124: public void render(Writer writer, Map parameterMap,
125: CallData callData) throws IOException {
126: int scopeId = 0;
127: if (parameterMap != null
128: && parameterMap.containsKey(PARAM_SCOPE)) {
129: scopeId = scopeStringToInt(((String[]) parameterMap
130: .get(PARAM_SCOPE))[0]);
131: } else {
132: scopeId = scopeStringToInt(scope);
133: }
134: String key = this .key;
135: if (parameterMap != null && parameterMap.containsKey(PARAM_KEY)) {
136: key = ((String[]) parameterMap.get(PARAM_KEY))[0];
137: }
138: boolean localRenderTarget = renderTarget;
139: if (parameterMap != null
140: && parameterMap.containsKey(PARAM_EVAL)) {
141: localRenderTarget = Boolean.valueOf(
142: ((String[]) parameterMap.get(PARAM_EVAL))[0])
143: .booleanValue();
144: }
145:
146: Object tmp = null;
147: switch (scopeId) {
148: case SESSION: // session
149: tmp = callData.getSessionAttribute(key);
150: break;
151: case REQUEST: // request
152: tmp = callData.getParameter(key);
153: break;
154: case REQUESTSESSION: // request attribute
155: tmp = callData.getParameter(key);
156: if (tmp == null) {
157: tmp = callData.getSessionAttribute(key);
158: }
159: callData.setSessionAttribute(key, tmp);
160: break;
161: case ATTRIBUTE: // request attribute
162: tmp = callData.getRequestAttribute(key);
163: break;
164: case COOKIE: // cookie
165: // the data is hold in a cookie, which is accessable via the
166: // callData-object. In this case, callData can be castesd to
167: // an HTTPCallData (actually (June 03), there is just one
168: // implementation for CallData, which is HTTPCallDataImpl)
169:
170: // get all cookies, then search for the correct key (cookie:
171: // key - value pair)
172: Cookie[] cookies = callData.getCookies();
173: if (cookies != null) {
174:
175: for (int i = 0; i < cookies.length; i++) {
176:
177: log.debug("trying cookie nr " + i + " (of "
178: + cookies.length + ") with key : >"
179: + cookies[i].getName() + "<");
180: if (cookies[i].getName().equals(key)) {
181: tmp = cookies[i].getValue();
182: log
183: .debug("'"
184: + this
185: + "':render() - found cookie with value "
186: + cookies[i].getValue());
187: break;
188: }
189: }
190: break;
191: } else {
192: log
193: .error("'"
194: + this
195: + "':render() -failure while looking for cookie (key: "
196: + key
197: + ") : no cookies in HTTP-request. ");
198: }
199: }
200:
201: // use the default
202: if (tmp == null) {
203: log.debug("'" + this
204: + "':render() - no value found for key '" + key
205: + "' ... using default '" + getDefaultValue()
206: + "' !");
207: tmp = getDefaultValue();
208: }
209:
210: if (tmp != null) {
211: // if (log.isDebugEnabled())
212: log.debug("'" + this + "':render() - writing value=" + tmp
213: + ".");
214: if (tmp instanceof String[]) {
215: writer
216: .write(StringUtils.createCSV((String[]) tmp,
217: ','));
218: } else {
219: if (localRenderTarget) {
220: // resolve component with given path and render it
221: ComponentPath path = new ComponentPath(tmp
222: .toString());
223: try {
224: Component target = callData.getActualManager()
225: .getComponent(path, callData);
226: if (target instanceof Renderable) {
227: ((Renderable) target).getRenderer().render(
228: writer, Collections.EMPTY_MAP,
229: callData);
230: } else if (target instanceof ContentProvider) {
231: writer.write(new DefaultRenderer(
232: ((ContentProvider) target)
233: .getContent())
234: .getContentAsString(callData));
235: }
236: } catch (ContelligentException e) {
237: log
238: .error("'"
239: + this
240: + "':render() - could not render component with path '"
241: + path
242: + "', rendering path instead!");
243: writer.write(tmp.toString());
244: }
245: } else {
246: writer.write(tmp.toString());
247: }
248: }
249: } else {
250: log.debug("'" + this
251: + "':render() - no value found for key '" + key
252: + "'!");
253: }
254: }
255:
256: private int scopeStringToInt(String scope) {
257: for (int i = 0; i < scopeValues.length; i++) {
258: if (scope.equals(scopeValues[i])) {
259: return i;
260: }
261: }
262: return 0;
263: }
264: }
|