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.render;
019:
020: import java.io.IOException;
021: import java.io.Writer;
022: import java.util.ArrayList;
023: import java.util.Collection;
024: import java.util.Map;
025:
026: import de.finix.contelligent.CallData;
027: import de.finix.contelligent.Component;
028: import de.finix.contelligent.ComponentManager;
029: import de.finix.contelligent.Contelligent;
030: import de.finix.contelligent.action.Action;
031: import de.finix.contelligent.core.ContelligentSession;
032: import de.finix.contelligent.core.security.ComponentPermission;
033: import de.finix.contelligent.core.security.User;
034: import de.finix.contelligent.exception.ContelligentException;
035: import de.finix.contelligent.exception.NoExecutePermissionException;
036: import de.finix.contelligent.logging.LoggingService;
037:
038: public class ActionRenderer implements Renderer {
039:
040: final static public String MODE_LINK = "link"; // creates the <a href... >
041:
042: // link
043:
044: public final static String MODE_REF_PARAMETER = "refAppendParameter";
045:
046: // HTTP variant switch
047: public final static String FORCE_PROTOCOL = "forceProtocol";
048:
049: public final static String FORCE_PROTOCOL_HTTP = "HTTP";
050:
051: public final static String FORCE_PROTOCOL_HTTPS = "HTTPS";
052:
053: public final static String FORCE_PROTOCOL_PARAM_DESCRIPTION = "force_protocol_description";
054:
055: // HTTP Auth switch
056: public final static String USE_HTTP_AUTH = "forceHTTPAuth";
057:
058: public final static String USE_HTTP_AUTH_WEB = "WEB";
059:
060: public final static String USE_HTTP_AUTH_HTTP = "HTTP";
061:
062: public final static String USE_HTTP_AUTH_PARAM_DESCRIPTION = "use_http_auth_param_description";
063:
064: final static public String TEXT = "text";
065:
066: final static public String TARGET = "target";
067:
068: final static public String SELF = "_self";
069:
070: final static public String BLANK = "_blank";
071:
072: final static public String TOP = "_top";
073:
074: final static public String PARENT = "_parent";
075:
076: final static org.apache.log4j.Logger log = LoggingService
077: .getLogger(ActionRenderer.class);
078:
079: protected Action action;
080:
081: public ActionRenderer(Action action) throws Exception {
082: this .action = action;
083: }
084:
085: public ParameterDescription[] getParameterDescription() {
086: ParameterDescription[] parameters = new ParameterDescription[] {
087: new ParameterDescription(PageRenderer.MODE,
088: "mode_description",
089: ParameterDescription.OPTIONAL,
090: ParameterDescription.CONSTRAINED, new String[] {
091: PageRenderer.MODE_REF,
092: PageRenderer.MODE_MARKUP, MODE_LINK,
093: MODE_REF_PARAMETER }),
094: new ParameterDescription(TARGET, "target_description",
095: ParameterDescription.OPTIONAL,
096: ParameterDescription.CONSTRAINED, new String[] {
097: SELF, BLANK, TOP, PARENT }),
098: new ParameterDescription(TEXT, "text_description",
099: ParameterDescription.OPTIONAL),
100: new ParameterDescription(FORCE_PROTOCOL,
101: FORCE_PROTOCOL_PARAM_DESCRIPTION,
102: ParameterDescription.OPTIONAL,
103: ParameterDescription.CONSTRAINED, new String[] {
104: FORCE_PROTOCOL_HTTP,
105: FORCE_PROTOCOL_HTTPS }),
106: new ParameterDescription(USE_HTTP_AUTH,
107: USE_HTTP_AUTH_PARAM_DESCRIPTION,
108: ParameterDescription.OPTIONAL,
109: ParameterDescription.CONSTRAINED, new String[] {
110: USE_HTTP_AUTH_HTTP, USE_HTTP_AUTH_WEB }) };
111: return parameters;
112: }
113:
114: public void renderReference(Writer writer, CallData callData,
115: boolean refParameter) throws ContelligentException,
116: IOException {
117: renderReference(writer, callData, refParameter, false, false,
118: false, false);
119: }
120:
121: public void renderReference(Writer writer, CallData callData,
122: boolean refParameter, boolean forceProtocolHttp,
123: boolean forceProtocolHttps, boolean forceAuthHttp,
124: boolean forceAuthWeb) throws ContelligentException,
125: IOException {
126: StringBuffer url = new StringBuffer(256);
127: String base = callData.getCurrentBaseURL(forceProtocolHttp,
128: forceProtocolHttps, forceAuthHttp, forceAuthWeb);
129:
130: url.append(base).append(action.getComponentContext().getPath())
131: .append(Contelligent.EXT_ACTION);
132: if (refParameter) {
133: url.append("?");
134: }
135: writer.write(callData.encodeURL(url.toString()));
136: }
137:
138: public void render(Writer writer, Map parameterMap,
139: CallData callData) throws ContelligentException,
140: IOException {
141: final boolean debugEnabled = log.isDebugEnabled();
142: boolean forceProtocolHttp = false;
143: boolean forceProtocolHttps = false;
144:
145: if (parameterMap != null
146: && parameterMap.containsKey(FORCE_PROTOCOL)) {
147: if (((String[]) parameterMap.get(FORCE_PROTOCOL))[0]
148: .equals(FORCE_PROTOCOL_HTTPS)) {
149: forceProtocolHttps = true;
150: } else if (((String[]) parameterMap.get(FORCE_PROTOCOL))[0]
151: .equals(FORCE_PROTOCOL_HTTP)) {
152: forceProtocolHttp = true;
153: } else {
154: log
155: .error("FORCE_PROTOCOL parameter map value set to illegal Value! [Source Component: "
156: + action.getComponentContext()
157: .getPath().toString() + "]");
158: }
159: }
160:
161: boolean useAuthWeb = false;
162: boolean useAuthHttp = false;
163:
164: if (parameterMap != null
165: && parameterMap.containsKey(USE_HTTP_AUTH)) {
166: if (((String[]) parameterMap.get(USE_HTTP_AUTH))[0]
167: .equals(USE_HTTP_AUTH_HTTP)) {
168: useAuthHttp = true;
169: } else if (((String[]) parameterMap.get(USE_HTTP_AUTH))[0]
170: .equals(USE_HTTP_AUTH_WEB)) {
171: useAuthWeb = true;
172: } else {
173: log
174: .error("USE_AUTH parameter map value set to illegal Value! [Source Component: "
175: + action.getComponentContext()
176: .getPath().toString() + "]");
177: }
178: }
179:
180: String renderMode = null;
181:
182: if (parameterMap != null
183: && parameterMap.containsKey(PageRenderer.MODE)) {
184: if (debugEnabled) {
185: log.debug("render() - parameter '"
186: + PageRenderer.MODE
187: + "' found with value '"
188: + ((String[]) parameterMap
189: .get(PageRenderer.MODE))[0] + "'");
190: }
191: renderMode = ((String[]) parameterMap
192: .get(PageRenderer.MODE))[0];
193: if (renderMode.equals(PageRenderer.MODE_MARKUP)) {
194: ContelligentSession session = (ContelligentSession) callData
195: .getContelligentSession();
196: User caller = session.getUser();
197: if (!callData.getActualManager().callerHasPermission(
198: caller, callData, action,
199: ComponentPermission.EXECUTE)) {
200: throw new NoExecutePermissionException(caller,
201: action.getComponentContext().getPath());
202: }
203: ComponentManager manager = callData.getActualManager();
204: String result = action.perform(callData);
205: Component actionResult = manager.getSubcomponent(
206: action, "routing/" + result, callData);
207: if (debugEnabled) {
208: log.debug("... action '" + action + "' returned '"
209: + actionResult + "' ...");
210: log.debug("... action '" + action
211: + "' channel is '"
212: + callData.getCategoryMap().get("channel")
213: + "' ...");
214: }
215: if (actionResult instanceof Renderable) {
216: ((Renderable) actionResult).getRenderer().render(
217: writer, parameterMap, callData);
218: } else {
219: log.error("result '" + actionResult
220: + "' of action '" + action
221: + "' is not renderable!");
222: }
223: return;
224: } else if (renderMode.equals(MODE_LINK)) {
225: writer.write("<a href=\"");
226: renderReference(writer, callData, false,
227: forceProtocolHttp, forceProtocolHttps,
228: useAuthHttp, useAuthWeb);
229: if ((String[]) parameterMap.get(TARGET) != null) {
230: writer.write("\" target=\"");
231: writer
232: .write(((String[]) parameterMap.get(TARGET))[0]);
233: }
234: writer.write("\">");
235: if ((String[]) parameterMap.get(TEXT) != null) {
236: writer
237: .write(((String[]) parameterMap.get(TEXT))[0]);
238: }
239: writer.write("</a>");
240: return;
241: }
242:
243: }
244: // if we reach this point mode was either not set or is
245: // PageRenderer.MODE_REF
246: renderReference(writer, callData, MODE_REF_PARAMETER
247: .equals(renderMode), forceProtocolHttp,
248: forceProtocolHttps, useAuthHttp, useAuthWeb);
249: }
250:
251: public Collection getSensitiveCategories() {
252: return new ArrayList();
253: }
254: }
|