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.content.BinaryContent;
029: import de.finix.contelligent.content.Content;
030: import de.finix.contelligent.content.ContentProvider;
031: import de.finix.contelligent.core.ContelligentImpl;
032: import de.finix.contelligent.exception.ContelligentException;
033: import de.finix.contelligent.logging.LoggingService;
034:
035: public class BinaryRenderer implements Renderer {
036:
037: final static public String MODE_LINK = "link"; // creates the <a href... >
038:
039: // link
040:
041: final static public String MODE_REF = "ref";
042:
043: final static public String MODE_REF_PARAMETER = "refAppendParameter";
044:
045: final static public String MODE_EXTENSION = "extension";
046:
047: final static public String MODE_CONTENTTYPE = "contentType";
048:
049: final static public String MODE_PATH = "path";
050:
051: final static public String MODE = "mode";
052:
053: // HTTP variant switch
054: public final static String FORCE_PROTOCOL = "forceProtocol";
055:
056: public final static String FORCE_PROTOCOL_HTTP = "HTTP";
057:
058: public final static String FORCE_PROTOCOL_HTTPS = "HTTPS";
059:
060: public final static String FORCE_PROTOCOL_PARAM_DESCRIPTION = "force_protocol_description";
061:
062: // HTTP Auth switch
063: public final static String USE_HTTP_AUTH = "forceHTTPAuth";
064:
065: public final static String USE_HTTP_AUTH_WEB = "WEB";
066:
067: public final static String USE_HTTP_AUTH_HTTP = "HTTP";
068:
069: public final static String USE_HTTP_AUTH_PARAM_DESCRIPTION = "use_http_auth_param_description";
070:
071: final static public String TEXT = "text";
072:
073: final static public String TARGET = "target";
074:
075: final static public String SELF = "_self";
076:
077: final static public String BLANK = "_blank";
078:
079: final static public String TOP = "_top";
080:
081: final static public String PARENT = "_parent";
082:
083: final static org.apache.log4j.Logger log = LoggingService
084: .getLogger(BinaryRenderer.class);
085:
086: protected Component component;
087:
088: public BinaryRenderer(Component component) {
089: this .component = component;
090: }
091:
092: public ParameterDescription[] getParameterDescription() {
093: ParameterDescription[] parameters = new ParameterDescription[] {
094: new ParameterDescription(MODE, "mode_description",
095: ParameterDescription.OPTIONAL,
096: ParameterDescription.CONSTRAINED, new String[] {
097: MODE_REF, MODE_LINK,
098: MODE_REF_PARAMETER, MODE_EXTENSION,
099: MODE_CONTENTTYPE, MODE_PATH }),
100: new ParameterDescription(TARGET, "target_description",
101: ParameterDescription.OPTIONAL,
102: ParameterDescription.CONSTRAINED, new String[] {
103: SELF, BLANK, TOP, PARENT }),
104: new ParameterDescription(TEXT, "text_description",
105: ParameterDescription.OPTIONAL),
106: new ParameterDescription(FORCE_PROTOCOL,
107: FORCE_PROTOCOL_PARAM_DESCRIPTION,
108: ParameterDescription.OPTIONAL,
109: ParameterDescription.CONSTRAINED, new String[] {
110: FORCE_PROTOCOL_HTTP,
111: FORCE_PROTOCOL_HTTPS }),
112: new ParameterDescription(USE_HTTP_AUTH,
113: USE_HTTP_AUTH_PARAM_DESCRIPTION,
114: ParameterDescription.OPTIONAL,
115: ParameterDescription.CONSTRAINED, new String[] {
116: USE_HTTP_AUTH_HTTP, USE_HTTP_AUTH_WEB }) };
117: return parameters;
118: }
119:
120: public void renderReference(Writer writer, CallData callData,
121: boolean refParameter) throws ContelligentException,
122: IOException {
123: renderReference(writer, callData, refParameter, false, false,
124: false, false);
125: }
126:
127: public void renderReference(Writer writer, CallData callData,
128: boolean refParameter, boolean forceProtocolHttp,
129: boolean forceProtocolHttps, boolean forceAuthHttp,
130: boolean forceAuthWeb) throws ContelligentException,
131: IOException {
132: StringBuffer url = new StringBuffer(256);
133: String base = callData.getCurrentBaseURL(forceProtocolHttp,
134: forceProtocolHttps, forceAuthHttp, forceAuthWeb);
135:
136: url.append(base).append(
137: component.getComponentContext().getPath());
138: String ext = ".cstr";
139:
140: if (component instanceof ContentProvider) {
141: Content content = ((ContentProvider) component)
142: .getContent();
143:
144: if (content instanceof BinaryContent) {
145: try {
146: ext = "."
147: + ((BinaryContent) content)
148: .getExtension(callData);
149: } catch (ContelligentException e) {
150: ext = ContelligentImpl.EXT_BINARY;
151: }
152: }
153: }
154: url.append(ext);
155: if (refParameter) {
156: url.append("?");
157: }
158: writer.write(callData.encodeURL(url.toString()));
159: }
160:
161: public void render(Writer writer, Map parameterMap,
162: CallData callData) throws ContelligentException,
163: IOException {
164: String renderMode = null;
165: boolean forceProtocolHttp = false;
166: boolean forceProtocolHttps = false;
167:
168: if (parameterMap != null
169: && parameterMap.containsKey(FORCE_PROTOCOL)) {
170: if (((String[]) parameterMap.get(FORCE_PROTOCOL))[0]
171: .equals(FORCE_PROTOCOL_HTTPS)) {
172: forceProtocolHttps = true;
173: } else if (((String[]) parameterMap.get(FORCE_PROTOCOL))[0]
174: .equals(FORCE_PROTOCOL_HTTP)) {
175: forceProtocolHttp = true;
176: } else {
177: log
178: .error("FORCE_PROTOCOL parameter map value set to illegal Value! [Source Component: "
179: + component.getComponentContext()
180: .getPath().toString() + "]");
181: }
182: }
183:
184: boolean useAuthWeb = false;
185: boolean useAuthHttp = false;
186:
187: if (parameterMap != null
188: && parameterMap.containsKey(USE_HTTP_AUTH)) {
189: if (((String[]) parameterMap.get(USE_HTTP_AUTH))[0]
190: .equals(USE_HTTP_AUTH_HTTP)) {
191: useAuthHttp = true;
192: } else if (((String[]) parameterMap.get(USE_HTTP_AUTH))[0]
193: .equals(USE_HTTP_AUTH_WEB)) {
194: useAuthWeb = true;
195: } else {
196: log
197: .error("USE_AUTH parameter map value set to illegal Value! [Source Component: "
198: + component.getComponentContext()
199: .getPath().toString() + "]");
200: }
201: }
202:
203: if (parameterMap != null && parameterMap.containsKey(MODE)) {
204: renderMode = ((String[]) parameterMap.get(MODE))[0];
205: if (renderMode.equals(MODE_LINK)) {
206: writer.write("<a href=\"");
207: renderReference(writer, callData, true,
208: forceProtocolHttp, forceProtocolHttps,
209: useAuthHttp, useAuthWeb);
210: if ((String[]) parameterMap.get(TARGET) != null) {
211: writer.write("\" target=\"");
212: writer
213: .write(((String[]) parameterMap.get(TARGET))[0]);
214: }
215: writer.write("\">");
216: if ((String[]) parameterMap.get(TEXT) != null) {
217: writer
218: .write(((String[]) parameterMap.get(TEXT))[0]);
219: }
220: writer.write("</a>");
221: return;
222: } else if (renderMode.equals(MODE_EXTENSION)) {
223: if (component instanceof ContentProvider) {
224: Content content = ((ContentProvider) component)
225: .getContent();
226:
227: if (content instanceof BinaryContent) {
228: try {
229: writer.write(((BinaryContent) content)
230: .getExtension(callData));
231: } catch (ContelligentException e) {
232: // Ignore
233: }
234: }
235: }
236: return;
237: } else if (renderMode.equals(MODE_CONTENTTYPE)) {
238: if (component instanceof ContentProvider) {
239: Content content = ((ContentProvider) component)
240: .getContent();
241:
242: if (content instanceof BinaryContent) {
243: try {
244: writer.write(((BinaryContent) content)
245: .getContentType(callData));
246: } catch (ContelligentException e) {
247: // Ignore
248: }
249: }
250: }
251: return;
252: } else if (renderMode.equals(MODE_PATH)) {
253: renderPath(writer, callData, true);
254: return;
255: }
256: }
257: renderReference(writer, callData, MODE_REF_PARAMETER
258: .equals(renderMode), forceProtocolHttp,
259: forceProtocolHttps, useAuthHttp, useAuthWeb);
260: }
261:
262: public Collection getSensitiveCategories() {
263: return new ArrayList();
264: }
265:
266: protected void renderPath(Writer writer, CallData callData,
267: boolean refParameter) throws IOException {
268: StringBuffer url = new StringBuffer(256);
269: url.append(component.getComponentContext().getPath());
270: writer.write(callData.encodeURL(url.toString()));
271: }
272: }
|