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.ArrayList;
023: import java.util.Collection;
024: import java.util.Map;
025:
026: import de.finix.contelligent.AbstractComponent;
027: import de.finix.contelligent.CallData;
028: import de.finix.contelligent.exception.ContelligentException;
029: import de.finix.contelligent.logging.LoggingService;
030: import de.finix.contelligent.render.ParameterDescription;
031: import de.finix.contelligent.render.Renderable;
032: import de.finix.contelligent.render.Renderer;
033:
034: public class BaseUrl extends AbstractComponent implements Renderable,
035: Renderer {
036:
037: final static org.apache.log4j.Logger log = LoggingService
038: .getLogger(BaseUrl.class);
039:
040: final static private String PARAM_MODE = "mode";
041:
042: final static private String PARAM_SLASH = "trailingSlash";
043:
044: final static private String[] modeValues = new String[] { "full",
045: "path" };
046:
047: final static private String[] slashValues = new String[] { "true",
048: "false" };
049:
050: final static private ParameterDescription[] parameters = new ParameterDescription[] {
051: new ParameterDescription(PARAM_MODE,
052: "Which base path type to render", false, true,
053: modeValues),
054: new ParameterDescription(
055: PARAM_SLASH,
056: "Whether to include a slash at the end of the path",
057: false, true, slashValues) };
058:
059: private Collection sensitiveCategories = new ArrayList();
060:
061: public Renderer getRenderer() {
062: return this ;
063: }
064:
065: public ParameterDescription[] getParameterDescription() {
066: return parameters;
067: }
068:
069: public void render(Writer writer, Map parameterMap,
070: CallData callData) throws ContelligentException,
071: IOException {
072: String paramMode = "full";
073: if (parameterMap != null
074: && parameterMap.containsKey(PARAM_MODE)) {
075: paramMode = ((String[]) parameterMap.get(PARAM_MODE))[0];
076: }
077: String paramSlash = "default";
078: if (parameterMap != null
079: && parameterMap.containsKey(PARAM_SLASH)) {
080: paramSlash = ((String[]) parameterMap.get(PARAM_SLASH))[0];
081: }
082:
083: String baseURL = callData.getCurrentBaseURL();
084: if ("path".equals(paramMode)) {
085: // First slash after http(s)://
086: baseURL = baseURL.substring(baseURL.indexOf('/', 8));
087: } else {
088: baseURL = baseURL + "/";
089: }
090: if (paramSlash.equals("true")) {
091: if (!baseURL.endsWith("/")) {
092: baseURL = baseURL + "/";
093: }
094: } else if (paramSlash.equals("false")) {
095: while (baseURL.endsWith("/")) {
096: baseURL = baseURL.substring(0, baseURL.length() - 1);
097: }
098: } // The default mode uses the old/mixed behaviour
099:
100: writer.write(baseURL);
101: }
102:
103: public Collection getSensitiveCategories() {
104: return sensitiveCategories;
105: }
106: }
|