Return-Path: Delivered-To: apmail-cocoon-cvs-archive@www.apache.org Received: (qmail 15373 invoked from network); 3 Nov 2005 14:40:14 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 3 Nov 2005 14:40:14 -0000 Received: (qmail 27090 invoked by uid 500); 3 Nov 2005 14:36:29 -0000 Delivered-To: apmail-cocoon-cvs-archive@cocoon.apache.org Received: (qmail 26150 invoked by uid 500); 3 Nov 2005 14:35:41 -0000 Mailing-List: contact cvs-help@cocoon.apache.org; run by ezmlm Precedence: bulk Reply-To: dev@cocoon.apache.org list-help: list-unsubscribe: List-Post: List-Id: Delivered-To: mailing list cvs@cocoon.apache.org Received: (qmail 25977 invoked by uid 99); 3 Nov 2005 14:35:01 -0000 X-ASF-Spam-Status: No, hits=-9.4 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [209.237.227.194] (HELO minotaur.apache.org) (209.237.227.194) by apache.org (qpsmtpd/0.29) with SMTP; Thu, 03 Nov 2005 06:34:18 -0800 Received: (qmail 11297 invoked by uid 65534); 3 Nov 2005 14:33:57 -0000 Message-ID: <20051103143357.11280.qmail@minotaur.apache.org> Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r330548 [9/132] - in /cocoon/whiteboard/maven2/cocoon-flat-layout: ./ cocoon-ajax-block/ cocoon-ajax-block/api/ cocoon-ajax-block/api/src/ cocoon-ajax-block/api/src/main/ cocoon-ajax-block/api/src/main/java/ cocoon-ajax-block/api/src/main/r... Date: Thu, 03 Nov 2005 14:00:48 -0000 To: cvs@cocoon.apache.org From: jheymans@apache.org X-Mailer: svnmailer-1.0.5 X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Added: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/LocaleAction.java URL: http://svn.apache.org/viewcvs/cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/LocaleAction.java?rev=330548&view=auto ============================================================================== --- cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/LocaleAction.java (added) +++ cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/LocaleAction.java Thu Nov 3 05:41:06 2005 @@ -0,0 +1,292 @@ +/* + * Copyright 1999-2004 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.cocoon.acting; + +import org.apache.avalon.framework.configuration.Configurable; +import org.apache.avalon.framework.configuration.Configuration; +import org.apache.avalon.framework.configuration.ConfigurationException; +import org.apache.avalon.framework.parameters.Parameters; +import org.apache.avalon.framework.thread.ThreadSafe; + +import org.apache.cocoon.environment.Redirector; +import org.apache.cocoon.environment.SourceResolver; +import org.apache.cocoon.i18n.I18nUtils; + +import java.util.HashMap; +import java.util.Locale; +import java.util.Map; + +/** + * An action that locates and provides to the pipeline locale information + * looked up in a range of ways. + * + *

Configuration

+ *

A sample configuration (given in the <map:matchers> section of the + * sitemap) is given below. This configuration shows default values. + *

+ *
+ *   <map:action name="locale" src="org.apache.cocoon.acting.LocaleAction">
+ *     <locale-attribute>locale</locale-attribute>
+ *     <use-locale>true</use-locale>
+ *     <default-locale language="en" country="US"/>
+ *     <store-in-request>false<store-in-request>
+ *     <create-session>false<create-session>
+ *     <store-in-session>false<store-in-session>
+ *     <store-in-cookie>false<store-in-cookie>
+ *   </map:action>
+ * 
+ * + *

Above configuration parameters mean: + *

    + *
  • locale-attribute specifies the name of the request + * parameter / session attribute / cookie that is to be used as a locale + * (defaults to locale)
  • + *
  • use-locale specifies whether the primary locale provided + * by the user agent (or server default, is no locale passed by the agent) + * is to be used
  • + *
  • default-locale specifies the default locale to be used when + * none found.
  • + *
  • store-in-request specifies whether found locale should be + * stored as request attribute.
  • + *
  • create-session specifies whether session should be created + * when storing found locale as session attribute.
  • + *
  • store-in-session specifies whether found locale should be + * stored as session attribute.
  • + *
  • store-in-cookie specifies whether found locale should be + * stored as cookie.
  • + *
+ *

+ * + *

Usage

+ *

This action will be used in a pipeline like so:

+ *
+ *   <map:act type="locale">
+ *     <map:generate src="file_{language}_{country}_{variant}.xml"/>
+ *     ...
+ *   </map:match>
+ * 
+ *

or

+ *
+ *   <map:act type="locale">
+ *     <map:generate src="file_{locale}.xml"/>
+ *     ...
+ *   </map:match>
+ * 
+ * + *

Locale Identification

+ *

Locales will be tested in following order:

+ *
    + *
  • Locale provided as a request parameter
  • + *
  • Locale provided as a session attribute
  • + *
  • Locale provided as a cookie
  • + *
  • Locale provided using a sitemap parameter
    + * (<map:parameter name="locale" value="{1}"/> style parameter within + * the <map:match> node)
  • + *
  • Locale provided by the user agent, or server default, + * if use-locale is set to true
  • + *
  • The default locale, if specified in the matcher's configuration
  • + *
+ *

First found locale will be returned.

+ * + *

Sitemap Variables

+ *

Once locale has been found, the following sitemap variables + * will be available to sitemap elements contained within the action:

+ *
    + *
  • {locale}: The locale string
  • + *
  • {language}: The language of the found locale
  • + *
  • {country}: The country of the found locale
  • + *
  • {variant}: The variant of the found locale
  • + *
+ * + * @author Marcus Crafter + * @author Konstantin Piroumian + * @author Lassi Immonen + * @author Vadim Gritsenko + * @version CVS $Id: LocaleAction.java 36371 2004-08-14 00:48:04Z vgritsenko $ + */ +public class LocaleAction extends ServiceableAction implements ThreadSafe, Configurable { + + private static final String DEFAULT_DEFAULT_LANG = "en"; + private static final String DEFAULT_DEFAULT_COUNTRY = "US"; + private static final String DEFAULT_DEFAULT_VARIANT = ""; + + /** + * Default locale attribute name. + */ + public static final String LOCALE = "locale"; + + /** + * Configuration element name for locale attribute name. + */ + public static final String LOCALE_ATTR = "locale-attribute"; + + + /** + * Constant representing the request storage configuration attribute + */ + public static final String STORE_REQUEST = "store-in-request"; + + /** + * Constant representing the session creation configuration attribute + */ + public static final String CREATE_SESSION = "create-session"; + + /** + * Constant representing the session storage configuration attribute + */ + public static final String STORE_SESSION = "store-in-session"; + + /** + * Constant representing the cookie storage configuration attribute + */ + public static final String STORE_COOKIE = "store-in-cookie"; + + + /** + * Name of the locale request parameter, session attribute, cookie. + */ + private String localeAttribute; + + /** + * Whether to query locale provided by the user agent or not. + */ + private boolean useLocale; + + /** + * Default locale if no other found and {@link #useLocale} is false. + */ + private Locale defaultLocale; + + /** + * Store the locale in request. Default is not to do this. + */ + private boolean storeInRequest; + + /** + * Store the locale in session, if available. Default is not to do this. + */ + private boolean storeInSession; + + /** + * Should we create a session if needed. Default is not to do this. + */ + private boolean createSession; + + /** + * Should we add a cookie with the locale. Default is not to do this. + */ + private boolean storeInCookie; + + /** + * Configure this action. + * + * @param config configuration information (if any) + */ + public void configure(Configuration config) + throws ConfigurationException { + this.storeInRequest = config.getChild(STORE_REQUEST).getValueAsBoolean(false); + this.createSession = config.getChild(CREATE_SESSION).getValueAsBoolean(false); + this.storeInSession = config.getChild(STORE_SESSION).getValueAsBoolean(false); + this.storeInCookie = config.getChild(STORE_COOKIE).getValueAsBoolean(false); + if (getLogger().isDebugEnabled()) { + getLogger().debug((this.storeInRequest ? "will" : "won't") + " set values in request"); + getLogger().debug((this.createSession ? "will" : "won't") + " create session"); + getLogger().debug((this.storeInSession ? "will" : "won't") + " set values in session"); + getLogger().debug((this.storeInCookie ? "will" : "won't") + " set values in cookies"); + } + + this.localeAttribute = config.getChild(LOCALE_ATTR).getValue(LOCALE); + this.useLocale = config.getChild("use-locale").getValueAsBoolean(true); + + Configuration child = config.getChild("default-locale", false); + if (child != null) { + this.defaultLocale = new Locale(child.getAttribute("language", DEFAULT_DEFAULT_LANG), + child.getAttribute("country", DEFAULT_DEFAULT_COUNTRY), + child.getAttribute("variant", DEFAULT_DEFAULT_VARIANT)); + } + + if (getLogger().isDebugEnabled()) { + getLogger().debug("Locale attribute name is " + this.localeAttribute); + getLogger().debug((this.useLocale ? "will" : "won't") + " use request locale"); + getLogger().debug("default locale " + this.defaultLocale); + } + } + + /** + * Action which obtains the current environments locale information, and + * places it in the objectModel (and optionally in a session/cookie). + */ + public Map act(Redirector redirector, + SourceResolver resolver, + Map objectModel, + String source, + Parameters params) + throws Exception { + // Obtain locale information from request, session, cookies, or params + Locale locale = I18nUtils.findLocale(objectModel, + localeAttribute, + params, + defaultLocale, + useLocale); + + if (locale == null) { + if (getLogger().isDebugEnabled()) { + getLogger().debug("No locale found."); + } + + return null; + } + + String localeStr = locale.toString(); + if (getLogger().isDebugEnabled()) { + getLogger().debug("Found locale: " + localeStr); + } + + I18nUtils.storeLocale(objectModel, + localeAttribute, + localeStr, + storeInRequest, + storeInSession, + storeInCookie, + createSession); + + // Set up a map for sitemap parameters + Map map = new HashMap(); + map.put("language", locale.getLanguage()); + map.put("country", locale.getCountry()); + map.put("variant", locale.getVariant()); + map.put("locale", localeStr); + return map; + } + + /** + * Helper method to retreive the attribute value containing locale + * information. See class documentation for locale determination algorythm. + * + * @deprecated See I18nUtils.findLocale + * @param objectModel requesting object's environment + * @return locale value or null if no locale was found + */ + public static String getLocaleAttribute(Map objectModel, + String localeAttrName) { + Locale locale = I18nUtils.findLocale(objectModel, + localeAttrName, + null, + null, + true); + return locale.toString(); + } +} Propchange: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/LocaleAction.java ------------------------------------------------------------------------------ svn:eol-style = native Added: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/PropagatorAction.java URL: http://svn.apache.org/viewcvs/cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/PropagatorAction.java?rev=330548&view=auto ============================================================================== --- cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/PropagatorAction.java (added) +++ cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/PropagatorAction.java Thu Nov 3 05:41:06 2005 @@ -0,0 +1,265 @@ +/* + * Copyright 1999-2004 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.cocoon.acting; + +import org.apache.avalon.framework.configuration.Configurable; +import org.apache.avalon.framework.configuration.Configuration; +import org.apache.avalon.framework.configuration.ConfigurationException; +import org.apache.avalon.framework.parameters.Parameters; +import org.apache.avalon.framework.service.ServiceSelector; +import org.apache.avalon.framework.thread.ThreadSafe; + +import org.apache.cocoon.components.modules.output.OutputModule; +import org.apache.cocoon.environment.Redirector; +import org.apache.cocoon.environment.SourceResolver; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +/** + * This is the action used to propagate parameters into a store using an + * {@link org.apache.cocoon.components.modules.output.OutputModule}. It + * simply propagates given expression. Additionaly it will make all propagated values + * available via returned Map. + * + *

Example configuration:

+ *
+ * <map:action type="...." name="...." logger="...">
+ *   <output-module name="session-attr">
+ *      <!-- optional configuration for output module -->
+ *   </output-module>
+ *   <store-empty-parameters>true</store-empty-parameters>
+ *   <defaults>
+ *     <default name="..." value="...."/>
+ *     <default name="..." value="..."/>
+ *   </defaults>
+ * </map:action>
+ * 
+ * + *

Example use:

+ *
+ * <map:act type="session-propagator">
+ *      <paramater name="example" value="{example}"/>
+ *      <paramater name="example1" value="xxx"/>
+ *      <parameter name="PropagatorAction:store-empty-parameters" value="true"/>
+ *      <parameter name="PropagatorAction:output-module" value="session-attr"/>
+ * </map:act>
+ * 
+ * + *

Configuration

+ * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + *
output-moduleNested element configuring output to use. Name attribute holds + * output module hint.XMLrequest-attr
store-empty-parametersPropagate parameters with empty values.booleantrue
defaultsParent for default parameters to propagate.XML
defaults/defaultName attribute holds parameter name, value attribute holds + * parameter value. Will be used when not set on use.parameter
+ * + *

Parameters

+ * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + *
PropagatorAction:output-moduleAlternative output module hint to use. A null configuration + * will be passed to a module selected this way.Stringas determined by configuration
PropagatorAction:store-empty-parametersPropagate parameters with empty values.booleanas determined by configuration
any otherAny other parameter will be propagated.String
+ * + * @author Christian Haul + * @author Martin Man + * @version CVS $Id: PropagatorAction.java 126305 2005-01-24 18:42:30Z vgritsenko $ + */ +public class PropagatorAction extends ServiceableAction + implements Configurable, ThreadSafe { + + /** Prefix for sitemap parameters targeted at this action. */ + private static final String ACTION_PREFIX = "PropagatorAction:"; + + /** Configuration parameter name. */ + private static final String CONFIG_STORE_EMPTY = "store-empty-parameters"; + + /** Configuration parameter name. */ + private static final String CONFIG_OUTPUT_MODULE = "output-module"; + + /** Default output module name. */ + private static final String OUTPUT_HINT = "request-attr"; // defaults to request attributes + + + /** Should empty parameter values be propagated? */ + private boolean storeEmpty = true; + + /** Configuration object for output module. */ + private Configuration outputConf; + + /** Name of output module to use. */ + private String outputName; + + /** List of {@link Entry}s holding default values. */ + private List defaults; + + /** + * A private helper holding default parameter entries. + * + */ + private static class Entry { + public String key; + public String value; + + public Entry(String key, String value) { + this.key = key; + this.value = value; + } + } + + /* (non-Javadoc) + * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration) + */ + public void configure(Configuration config) throws ConfigurationException { + this.outputConf = config.getChild(CONFIG_OUTPUT_MODULE); + this.outputName = this.outputConf.getAttribute("name", OUTPUT_HINT); + this.storeEmpty = + config.getChild(CONFIG_STORE_EMPTY).getValueAsBoolean(this.storeEmpty); + + Configuration[] dflts = config.getChild("defaults").getChildren("default"); + if (dflts != null) { + this.defaults = new ArrayList(dflts.length); + for (int i = 0; i < dflts.length; i++) { + this.defaults.add( + new Entry(dflts[i].getAttribute("name"), + dflts[i].getAttribute("value"))); + } + } else { + this.defaults = new ArrayList(0); + } + } + + /* (non-Javadoc) + * @see org.apache.cocoon.acting.Action#act(Redirector, SourceResolver, Map, String, Parameters) + */ + public Map act(Redirector redirector, + SourceResolver resolver, + Map objectModel, + String source, + Parameters parameters) + throws Exception { + // Read action parameters + String outputName = parameters.getParameter(ACTION_PREFIX + CONFIG_OUTPUT_MODULE, + null); + boolean storeEmpty = parameters.getParameterAsBoolean(ACTION_PREFIX + CONFIG_STORE_EMPTY, + this.storeEmpty); + parameters.removeParameter(ACTION_PREFIX + CONFIG_OUTPUT_MODULE); + parameters.removeParameter(ACTION_PREFIX + CONFIG_STORE_EMPTY); + + Configuration outputConf = null; + if (outputName == null) { + outputName = this.outputName; + outputConf = this.outputConf; + } + + // Action results map + final Map results = new HashMap(); + + OutputModule output = null; + ServiceSelector selector = null; + try { + selector = (ServiceSelector) this.manager.lookup(OutputModule.ROLE + "Selector"); + if (outputName != null + && selector != null + && selector.isSelectable(outputName)) { + + output = (OutputModule) selector.select(outputName); + + String[] names = parameters.getNames(); + for (int i = 0; i < names.length; i++) { + String name = names[i]; + String value = parameters.getParameter(name); + if (storeEmpty || (value != null && !value.equals(""))) { + if (getLogger().isDebugEnabled()) { + getLogger().debug("Propagating <" + name + "> value <" + value + ">"); + } + output.setAttribute(outputConf, + objectModel, + name, + value); + results.put(name, value); + } + } + + // Defaults, that are not overridden + for (Iterator i = defaults.iterator(); i.hasNext();) { + Entry entry = (Entry) i.next(); + if (!results.containsKey(entry.key)) { + if (getLogger().isDebugEnabled()) { + getLogger().debug("Propagating default <" + entry.key + "> value <" + entry.value + ">"); + } + output.setAttribute(outputConf, + objectModel, + entry.key, + entry.value); + results.put(entry.key, entry.value); + } + } + + output.commit(outputConf, objectModel); + } + } catch (Exception e) { + if (output != null) { + output.rollback(outputConf, objectModel, e); + } + throw e; + } finally { + if (selector != null) { + if (output != null) { + selector.release(output); + } + this.manager.release(selector); + } + } + + return Collections.unmodifiableMap(results); + } +} Propchange: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/PropagatorAction.java ------------------------------------------------------------------------------ svn:eol-style = native Added: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/RequestParamAction.java URL: http://svn.apache.org/viewcvs/cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/RequestParamAction.java?rev=330548&view=auto ============================================================================== --- cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/RequestParamAction.java (added) +++ cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/RequestParamAction.java Thu Nov 3 05:41:06 2005 @@ -0,0 +1,135 @@ +/* + * Copyright 1999-2004 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.cocoon.acting; + +import java.util.Enumeration; +import java.util.HashMap; +import java.util.Map; + +import org.apache.avalon.framework.parameters.Parameters; +import org.apache.avalon.framework.thread.ThreadSafe; + +import org.apache.cocoon.environment.ObjectModelHelper; +import org.apache.cocoon.environment.Redirector; +import org.apache.cocoon.environment.Request; +import org.apache.cocoon.environment.SourceResolver; + +/** + * This action makes some request details available to the sitemap via parameter + * substitution. + * + * {context} - is the context path of the servlet (usually "/cocoon") + * {requestURI} - is the requested URI without parameters + * {requestQuery} - is the query string like "?param1=test" if there is one + * + * Additionlly all request parameters can be made available for use in the sitemap. + * if the parameter "parameters" is set to true. + * (A variable is created for each request parameter (only if it doesn't exist) + * with the same name as the parameter itself) + * + * Default values can be set for request parameters, by including sitemap parameters + * named "default.". + * + * Sitemap definition: + * + *
+ * <map:action name="request" src="org.apache.cocoon.acting.RequestParamAction"/>
+ * 
+ * + *

+ * + * Example use: + * + *

+ * <map:match pattern="some-resource">
+ *  <map:act type="request">
+ *     <map:parameter name="parameters" value="true"/>
+ *     <map:parameter name="default.dest" value="invalid-destination.html"/>
+ *     <map:redirect-to uri="{context}/somewhereelse/{dest}"/>
+ *  </map:act>
+ * </map:match>
+ * 
+ * + * Redirection is only one example, another use: + * + *
+ * <map:match pattern="some-resource">
+ *  <map:act type="request">
+ *     <map:parameter name="parameters" value="true"/>
+ *     <map:generate src="users/menu-{id}.xml"/>
+ *  </map:act>
+ *  <map:transform src="menus/personalisation.xsl"/>
+ *  <map:serialize/>
+ * </map:match>
+ * 
+ * + * etc, etc. + * + * @author Marcus Crafter + * @author Torsten Curdt + * @version CVS $Id: RequestParamAction.java 30941 2004-07-29 19:56:58Z vgritsenko $ + */ +public class RequestParamAction extends ServiceableAction implements ThreadSafe { + + public final static String MAP_URI = "requestURI"; + public final static String MAP_QUERY = "requestQuery"; + public final static String MAP_CONTEXTPATH = "context"; + + public final static String PARAM_PARAMETERS = "parameters"; + public final static String PARAM_DEFAULT_PREFIX = "default."; + + public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, + String source, Parameters param) throws Exception { + + Request request = ObjectModelHelper.getRequest(objectModel); + + Map map = new HashMap(); + + map.put(MAP_URI, request.getRequestURI()); + + String query = request.getQueryString(); + if (query != null && query.length() > 0) { + map.put(MAP_QUERY, "?" + query); + } else { + map.put(MAP_QUERY, ""); + } + + map.put(MAP_CONTEXTPATH, request.getContextPath()); + + if ("true".equalsIgnoreCase(param.getParameter(PARAM_PARAMETERS, null))) { + Enumeration e = request.getParameterNames(); + while (e.hasMoreElements()) { + String name = (String)e.nextElement(); + String value = request.getParameter(name); + + if (value != null && !map.containsKey(name)) { + map.put(name, value); + } + } + + String[] paramNames = param.getNames(); + for (int i = 0; i < paramNames.length; i++) { + if (paramNames[i].startsWith(PARAM_DEFAULT_PREFIX) + && (request.getParameter(paramNames[i].substring(PARAM_DEFAULT_PREFIX.length())) == null)) { + map.put(paramNames[i].substring(PARAM_DEFAULT_PREFIX.length()), + param.getParameter(paramNames[i])); + } + } + } + return (map); + } + +} Propchange: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/RequestParamAction.java ------------------------------------------------------------------------------ svn:eol-style = native Added: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/RequestParameterExistsAction.java URL: http://svn.apache.org/viewcvs/cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/RequestParameterExistsAction.java?rev=330548&view=auto ============================================================================== --- cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/RequestParameterExistsAction.java (added) +++ cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/RequestParameterExistsAction.java Thu Nov 3 05:41:06 2005 @@ -0,0 +1,345 @@ +/* + * Copyright 1999-2004 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.cocoon.acting; + +import java.util.Collections; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.Map; +import java.util.StringTokenizer; +import java.util.Iterator; +import org.apache.avalon.framework.parameters.Parameters; +import org.apache.avalon.framework.thread.ThreadSafe; +import org.apache.cocoon.environment.ObjectModelHelper; +import org.apache.cocoon.environment.Redirector; +import org.apache.cocoon.environment.Request; +import org.apache.cocoon.environment.SourceResolver; + +/** + * This action simply checks to see if a given request parameter + * exists. It takes an arbitrary number of default parameters to check + * named 'parameter-name'. Non-default parameters need to be separated + * by spaces and passed as value of a sitemap parameter named + * 'parameters'. The action returns a map with all parameters if all + * of them exist and null otherwise. Parameter names can only be added + * to this list but no default parameters can be overridden by + * specific ones. + * + *

This action is very closely related to @link{RequestParamAction} + * and {@link FormValidatorAction}. However this action is considerably + * simpler in that it tests only for existence of a parameter and it + * doesn't need a descriptor. Besides it doesn't propagate all request + * parameters to the sitemap but only those that are marked as + * required.

One special feature is, however, that parameters + * can contain one wildcard ("*"). It will be + * checked, whether all parameters with a wildcard have the same + * matches. E.g. "id_* name_*" enforces, that if "id_1" exists, + * "name_1" must also exist and vice versa.

+ * + * @author Christian Haul + * @version CVS $Id: RequestParameterExistsAction.java 293457 2005-10-03 21:52:54Z danielf $ + */ +public class RequestParameterExistsAction extends AbstractConfigurableAction implements ThreadSafe +{ + + protected class StringParts { + String prefix = null; + String pstfix = null; + int count = 0; + + public StringParts ( String pre, String post ) { + prefix = pre; + pstfix = post; + } + } + + + + public Map act( Redirector redirector, + SourceResolver resolver, + Map objectModel, + String source, + Parameters parameters + ) + throws Exception { + Request request = ObjectModelHelper.getRequest(objectModel); + HashMap results = new HashMap(); + HashMap items = new HashMap(); + int wildcards = 0; + + // check default parameters for existence + if (this.getLogger().isDebugEnabled()) { + getLogger().debug("checking default parameters"); + } + Iterator reqParams = settings.values().iterator(); + while (reqParams.hasNext()) { + String paramName = (String) reqParams.next(); + StringParts sp = splitParameter(paramName); + if (sp != null) { + // wildcard: special care required (deferred until later) + items.put(new Integer(wildcards++), sp); + if (this.getLogger().isDebugEnabled()) { + getLogger().debug("(default) deferring " + paramName); + } + } else { + String paramValue = request.getParameter(paramName); + if (paramValue == null) { + return null; + } + results.put(paramName, paramValue); + } + } + + // check parameters for existence + if (this.getLogger().isDebugEnabled()) { + getLogger().debug("checking sitemap parameters"); + } + String params = parameters.getParameter("parameters", null); + if (params != null) { + StringTokenizer st = new StringTokenizer(params); + while (st.hasMoreTokens()) { + String paramName = st.nextToken(); + StringParts sp = splitParameter(paramName); + if (sp != null) { + // wildcard: special care required (deferred until later) + items.put(new Integer(wildcards++), sp); + if (this.getLogger().isDebugEnabled()) { + getLogger().debug("deferring " + paramName); + } + } else { + + String paramValue = request.getParameter(paramName); + if (paramValue == null) { + return null; + } + results.put(paramName, paramValue); + } + } + } + + if (wildcards != 0) { + // special care for parameters with wildcard + // + if (this.getLogger().isDebugEnabled()) { + getLogger().debug("deferred checking for parameters: " + wildcards); + } + + // first one + // + if (this.getLogger().isDebugEnabled()) { + getLogger().debug(" checking first"); + } + HashMap values = new HashMap(); + StringParts sp1 = (StringParts) items.get(new Integer(0)); + if (this.getLogger().isDebugEnabled()) { + getLogger().debug( + " Parameter is [" + sp1.prefix + " * " + sp1.pstfix + "] "); + } + Enumeration requestParams = request.getParameterNames(); + Boolean dummy = Boolean.TRUE; + while (requestParams.hasMoreElements()) { + String paramName = (String) requestParams.nextElement(); + String match = getMatch(paramName, sp1); + if (match != null) { + if (this.getLogger().isDebugEnabled()) { + getLogger().debug( + " value is >" + + match + + "< " + + sp1.prefix.length() + + " " + + paramName.length() + + " " + + sp1.pstfix.length()); + } + values.put(match, dummy); + sp1.count++; + if (this.getLogger().isDebugEnabled()) { + getLogger().debug( + " Parameter " + + sp1.prefix + + "*" + + sp1.pstfix + + " matches " + + paramName + + " (" + + sp1.count + + " so far)"); + } + String paramValue = request.getParameter(paramName); + if (paramValue == null) { + return null; + } + results.put(paramName, paramValue); + } + } + + if (sp1.count == 0) { + if (this.getLogger().isDebugEnabled()) { + getLogger().debug( + " Parameter " + + sp1.prefix + + "*" + + sp1.pstfix + + " matches " + + sp1.count); + } + return null; + } + + // all other ones + // + if (this.getLogger().isDebugEnabled()) { + getLogger().debug(" checking others"); + } + requestParams = request.getParameterNames(); + while (requestParams.hasMoreElements()) { + String paramName = (String) requestParams.nextElement(); + if (this.getLogger().isDebugEnabled()) { + getLogger().debug(" checking request parameter " + paramName); + } + for (int i = wildcards - 1; i > 0; i--) { + if (this.getLogger().isDebugEnabled()) { + getLogger().debug(" checking against " + i); + } + StringParts sp = (StringParts) items.get(new Integer(i)); + String match = getMatch(paramName, sp); + if (this.getLogger().isDebugEnabled()) { + getLogger().debug( + " Parameter is [" + + sp.prefix + + " * " + + sp.pstfix + + "] "); + } + if (match != null) { + if (this.getLogger().isDebugEnabled()) { + getLogger().debug( + " Parameter " + + sp.prefix + + "*" + + sp.pstfix + + " matches " + + paramName + + " (" + + sp.count + + " so far)"); + } + if (values.containsKey(match)) { + sp.count++; + if (this.getLogger().isDebugEnabled()) { + getLogger().debug( + " " + paramName + " (verified)"); + } + String paramValue = request.getParameter(paramName); + if (paramValue == null) { + return null; + } + results.put(paramName, paramValue); + + } else { + if (this.getLogger().isDebugEnabled()) { + getLogger().debug( + "Match " + + match + + "not found for " + + sp1.prefix + + "*" + + sp1.pstfix + + " but for " + + sp.prefix + + "*" + + sp.pstfix); + } + return null; + } + } + } + } + + // since we enforce that only matches are counted, that exist for + // the first parameter as well, check if every parameter has an + // equal number of matches. + // + if (this.getLogger().isDebugEnabled()) { + getLogger().debug("checking number of matches"); + } + for (int i = wildcards - 1; i > 0; i--) { + StringParts sp = (StringParts) items.get(new Integer(i)); + if (sp.count != sp1.count) { + if (this.getLogger().isDebugEnabled()) { + getLogger().debug( + "Found " + + sp.count + + " matches for " + + sp.prefix + + "*" + + sp.pstfix + + " but expected " + + sp1.count); + } + return null; + } else { + if (this.getLogger().isDebugEnabled()) { + getLogger().debug( + "Found " + + sp.count + + " matches for " + + sp.prefix + + "*" + + sp.pstfix + + " as expected"); + } + } + } + + } + + return Collections.unmodifiableMap(results); + } + + + /** + * Find first "*" in a String and split it into the substring + * before and after the "*". Returns null if no "*" is present. + */ + protected StringParts splitParameter( String paramName ) + { + int idx = paramName.indexOf("*"); + if ( idx != -1 ) { + return new StringParts(paramName.substring(0,idx), paramName.substring(idx+1)); + } else { + return null; + } + } + + /** + * If a String matches a StringPart spec, return the substring + * between the specified prefix and postfix. Returns null if it + * doesn't match. + */ + protected String getMatch( String paramName, + StringParts sp + ) + { + if ( paramName.startsWith( sp.prefix ) && paramName.endsWith( sp.pstfix ) ) { + return paramName.substring( sp.prefix.length(), ( paramName.length() - sp.pstfix.length() ) ); + } else { + return null; + } + } + +} Propchange: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/RequestParameterExistsAction.java ------------------------------------------------------------------------------ svn:eol-style = native Added: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/ResourceExistsAction.java URL: http://svn.apache.org/viewcvs/cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/ResourceExistsAction.java?rev=330548&view=auto ============================================================================== --- cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/ResourceExistsAction.java (added) +++ cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/ResourceExistsAction.java Thu Nov 3 05:41:06 2005 @@ -0,0 +1,63 @@ +/* + * Copyright 1999-2004 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.cocoon.acting; + +import org.apache.avalon.framework.parameters.Parameters; +import org.apache.avalon.framework.thread.ThreadSafe; +import org.apache.cocoon.environment.Redirector; +import org.apache.cocoon.environment.SourceResolver; +import org.apache.excalibur.source.Source; +import org.apache.excalibur.source.SourceNotFoundException; + +import java.util.Map; + +/** + * This action simply checks to see if a resource identified by the src + * sitemap attribute exists or not. The action returns empty Map if + * resource exists, null otherwise. + * + *

Instead of src attribute, source can be specified using + * parameter named url (this is old syntax, should be removed soon). + * + *

NOTE: {@link org.apache.cocoon.selection.ResourceExistsSelector} + * should be preferred to this component, as the semantics of a Selector better + * matches the supplied functionality. + * + * @author Donald Ball + * @version CVS $Id: ResourceExistsAction.java 30941 2004-07-29 19:56:58Z vgritsenko $ + */ +public class ResourceExistsAction extends ServiceableAction implements ThreadSafe { + + public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String src, Parameters parameters) throws Exception { + String resourceURI = parameters.getParameter("url", src); + Source source = null; + try { + source = resolver.resolveURI(resourceURI); + if (source.exists()) { + return EMPTY_MAP; + } + } catch (SourceNotFoundException e) { + // Do not log + } catch (Exception e) { + getLogger().warn("Exception resolving resource " + resourceURI, e); + } finally { + if (source != null) { + resolver.release(source); + } + } + return null; + } +} Propchange: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/ResourceExistsAction.java ------------------------------------------------------------------------------ svn:eol-style = native Added: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/ServiceableAction.java URL: http://svn.apache.org/viewcvs/cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/ServiceableAction.java?rev=330548&view=auto ============================================================================== --- cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/ServiceableAction.java (added) +++ cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/ServiceableAction.java Thu Nov 3 05:41:06 2005 @@ -0,0 +1,42 @@ +/* + * Copyright 1999-2004 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.cocoon.acting; + +import org.apache.avalon.framework.service.ServiceException; +import org.apache.avalon.framework.service.ServiceManager; +import org.apache.avalon.framework.service.Serviceable; + +/** + * The ServiceableAction will allow any Action + * that extends this to access SitemapComponents. + * + * @author Carsten Ziegeler + * @version CVS $Id: ServiceableAction.java 30941 2004-07-29 19:56:58Z vgritsenko $ + */ +public abstract class ServiceableAction + extends AbstractAction implements Serviceable { + + /** The service manager instance */ + protected ServiceManager manager; + + /** + * Set the current ServiceManager instance used by this + * Serviceable. + */ + public void service(ServiceManager manager) throws ServiceException { + this.manager = manager; + } +} Propchange: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/ServiceableAction.java ------------------------------------------------------------------------------ svn:eol-style = native Added: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/SessionInvalidatorAction.java URL: http://svn.apache.org/viewcvs/cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/SessionInvalidatorAction.java?rev=330548&view=auto ============================================================================== --- cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/SessionInvalidatorAction.java (added) +++ cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/SessionInvalidatorAction.java Thu Nov 3 05:41:06 2005 @@ -0,0 +1,59 @@ +/* + * Copyright 1999-2004 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.cocoon.acting; + +import org.apache.avalon.framework.parameters.Parameters; +import org.apache.avalon.framework.thread.ThreadSafe; +import org.apache.cocoon.environment.ObjectModelHelper; +import org.apache.cocoon.environment.Redirector; +import org.apache.cocoon.environment.Request; +import org.apache.cocoon.environment.Session; +import org.apache.cocoon.environment.SourceResolver; + +import java.util.Map; + +/** + * This is the action used to invalidate an HTTP session. The action returns + * empty map if everything is ok, null otherwise. + * + * @author Martin Man + * @version CVS $Id: SessionInvalidatorAction.java 30941 2004-07-29 19:56:58Z vgritsenko $ + */ +public class SessionInvalidatorAction extends AbstractAction implements ThreadSafe +{ + /** + * Main invocation routine. + */ + public Map act (Redirector redirector, SourceResolver resolver, Map objectModel, String src, + Parameters parameters) throws Exception { + Request req = ObjectModelHelper.getRequest(objectModel); + + /* check session validity */ + Session session = req.getSession (false); + if (session != null) { + session.invalidate (); + if (this.getLogger().isDebugEnabled()) { + getLogger ().debug ("SESSIONINVALIDATOR: session invalidated"); + } + } else { + if (this.getLogger().isDebugEnabled()) { + getLogger ().debug ("SESSIONINVALIDATOR: no session object"); + } + } + + return EMPTY_MAP; // cut down on object creation + } +} Propchange: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/SessionInvalidatorAction.java ------------------------------------------------------------------------------ svn:eol-style = native Added: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/SessionIsValidAction.java URL: http://svn.apache.org/viewcvs/cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/SessionIsValidAction.java?rev=330548&view=auto ============================================================================== --- cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/SessionIsValidAction.java (added) +++ cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/SessionIsValidAction.java Thu Nov 3 05:41:06 2005 @@ -0,0 +1,62 @@ +/* + * Copyright 1999-2004 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.cocoon.acting; + +import org.apache.avalon.framework.parameters.Parameters; +import org.apache.avalon.framework.thread.ThreadSafe; +import org.apache.cocoon.environment.ObjectModelHelper; +import org.apache.cocoon.environment.Redirector; +import org.apache.cocoon.environment.Request; +import org.apache.cocoon.environment.Session; +import org.apache.cocoon.environment.SourceResolver; + +import java.util.Map; + + +/** + * This action just checks if a session exists and whether the current + * seesion is still valid. + * + * @author Christian Haul + * @version CVS $Id: SessionIsValidAction.java 30941 2004-07-29 19:56:58Z vgritsenko $ + */ +public class SessionIsValidAction extends AbstractAction implements ThreadSafe +{ + /** + * Main invocation routine. + */ + public Map act (Redirector redirector, SourceResolver resolver, Map objectModel, String src, + Parameters parameters) throws Exception { + Request req = ObjectModelHelper.getRequest(objectModel); + + /* check session validity */ + Session session = req.getSession (false); + if (session == null) { + if (this.getLogger().isDebugEnabled()) { + getLogger().debug("No session object"); + } + return null; + } + if (!req.isRequestedSessionIdValid()) { + if (this.getLogger().isDebugEnabled()) { + getLogger().debug("Requested session id is invalid"); + } + return null; + } + + return EMPTY_MAP; + } +} Propchange: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/SessionIsValidAction.java ------------------------------------------------------------------------------ svn:eol-style = native Added: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/SessionPropagatorAction.java URL: http://svn.apache.org/viewcvs/cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/SessionPropagatorAction.java?rev=330548&view=auto ============================================================================== --- cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/SessionPropagatorAction.java (added) +++ cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/SessionPropagatorAction.java Thu Nov 3 05:41:06 2005 @@ -0,0 +1,143 @@ +/* + * Copyright 1999-2004 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.cocoon.acting; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.avalon.framework.configuration.Configuration; +import org.apache.avalon.framework.configuration.ConfigurationException; +import org.apache.avalon.framework.parameters.Parameters; +import org.apache.avalon.framework.thread.ThreadSafe; + +import org.apache.cocoon.environment.ObjectModelHelper; +import org.apache.cocoon.environment.Redirector; +import org.apache.cocoon.environment.Request; +import org.apache.cocoon.environment.Session; +import org.apache.cocoon.environment.SourceResolver; + +/** + * This is the action used to propagate parameters into session. It + * simply propagates given expression to the session. If session does not + * exist, action fails. Additionaly it will make all propagated values + * available via returned Map. + * + *

+ * <map:act type="session-propagator">
+ *      <paramater name="example" value="{example}">
+ *      <paramater name="example1" value="xxx">
+ * </map:act>
+ * 
+ * + * @author Martin Man + * @version CVS $Id: SessionPropagatorAction.java 54551 2004-10-11 10:33:49Z cziegeler $ + */ +public class SessionPropagatorAction extends AbstractConfigurableAction implements ThreadSafe { + + /** + * A private helper holding default parameter entries. + * + */ + private static class Entry { + public String key = null; + public String value = null; + + public Entry(String key, String value) { + this.key = key; + this.value = value; + } + } + + private List defaults; + + public void configure(Configuration conf) throws ConfigurationException { + super.configure(conf); + Configuration[] dflts = conf.getChildren(); + if (dflts != null) { + this.defaults = new ArrayList(dflts.length); + for (int i = 0; i < dflts.length; i++) { + this.defaults.add( + new Entry( + dflts[i].getName(), + dflts[i].getValue())); + } + } else { + this.defaults = new ArrayList(0); + } + } + + /** + * Main invocation routine. + */ + public Map act (Redirector redirector, SourceResolver resolver, Map objectModel, String src, + Parameters parameters) throws Exception { + Request req = ObjectModelHelper.getRequest(objectModel); + HashMap actionMap = new HashMap (); + + /* check session validity */ + Session session = req.getSession (false); + if (session == null) { + if (getLogger().isDebugEnabled()) { + getLogger().debug("No session object"); + } + return null; + } + + try { + String[] names = parameters.getNames(); + + // parameters + for (int i = 0; i < names.length; i++) { + String sessionParamName = names[i]; + String value = parameters.getParameter(sessionParamName); + if (getLogger().isDebugEnabled()) { + getLogger().debug("Propagating value " + + value + + " to session attribute " + + sessionParamName); + } + session.setAttribute(sessionParamName, value); + actionMap.put(sessionParamName, value); + } + + // defaults, that are not overridden + for (int i = 0; i < defaults.size(); i++) { + final Entry entry = (Entry)defaults.get(i); + if (!actionMap.containsKey(entry.key)) { + if (getLogger().isDebugEnabled()) { + getLogger().debug("Propagating value " + + entry.value + + " to session attribute " + + entry.key); + } + session.setAttribute(entry.key, entry.value); + actionMap.put(entry.key, entry.value); + } + } + if (getLogger().isDebugEnabled()) { + getLogger().debug("All params propagated " + "to session"); + } + return Collections.unmodifiableMap(actionMap); + } catch (Exception e) { + getLogger().warn("exception: ", e); + } + return null; + } +} + Propchange: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/SessionPropagatorAction.java ------------------------------------------------------------------------------ svn:eol-style = native Added: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/SessionStateAction.java URL: http://svn.apache.org/viewcvs/cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/SessionStateAction.java?rev=330548&view=auto ============================================================================== --- cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/SessionStateAction.java (added) +++ cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/SessionStateAction.java Thu Nov 3 05:41:06 2005 @@ -0,0 +1,174 @@ +/* + * Copyright 1999-2004 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.cocoon.acting; + +import org.apache.avalon.framework.configuration.Configuration; +import org.apache.avalon.framework.configuration.ConfigurationException; +import org.apache.avalon.framework.parameters.Parameters; +import org.apache.avalon.framework.thread.ThreadSafe; +import org.apache.cocoon.environment.ObjectModelHelper; +import org.apache.cocoon.environment.Redirector; +import org.apache.cocoon.environment.Request; +import org.apache.cocoon.environment.Session; +import org.apache.cocoon.environment.SourceResolver; + +import java.util.HashMap; +import java.util.Map; + +/** + * Store the session's current state in a session attribute. + * + *

To keep track of the state of a user's session, a string is + * stored in a session attribute in order to allow to chose between + * different pipelines in the sitemap accordingly.

+ * + *

For added flexibility it is possible to use sub states as + * well. For this declare your own name for the session state + * attribute and give the number of sublevels plus the level to + * modify. (This is one based!) Sub states below the current + * one are removed from the session so that the default sub state will + * be reentered when the user returns. If you don't like this + * behaviour and prefer independent sub states, use this action + * several times with different attribute names rather than sub + * levels.

+ * + *

Global and local parameters:

+ * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + *
state-key-prefixString that identifies the attribute that stores the session state in the + * session object. When sublevels are used, this is a prefix ie. the + * number of the level is appended to the prefix. Example prefix is + * "__sessionState", sub-levels is 2, attributes + * "__sessionState1", "__sessionState2", and + * "__sessionState3" will be used to store the + * information. + *
new-stateString that identifies the current state
sub-levelsNumber of sub levels to use
state-levelSub level to modify, this is one based
+ * + * @see org.apache.cocoon.matching.WildcardSessionAttributeMatcher + * @see org.apache.cocoon.selection.SessionAttributeSelector + * + * @author Christian Haul + * @version CVS $Id: SessionStateAction.java 30941 2004-07-29 19:56:58Z vgritsenko $ + */ +public class SessionStateAction + extends AbstractConfigurableAction + implements ThreadSafe { + + protected String statekey = "org.apache.cocoon.SessionState"; + protected String newstate = null; + protected int sublevels = 0; + protected int mylevel = 0; + + /** + * Configures the Action. + */ + public void configure(Configuration conf) throws ConfigurationException { + super.configure(conf); + + if (settings.containsKey("state-key-prefix")) { + statekey = (String) settings.get("state-key-prefix"); + } + if (settings.containsKey("new-state")) { + newstate = (String) settings.get("new-state"); + } + if (settings.containsKey("sub-levels")) { + sublevels = Integer.parseInt((String) settings.get("sub-levels")); + } + if (settings.containsKey("state-level")) { + mylevel = Integer.parseInt((String) settings.get("state-level")); + } + } + + public Map act(Redirector redirector, + SourceResolver resolver, + Map objectModel, + String src, + Parameters par) throws Exception { + + Request request = ObjectModelHelper.getRequest(objectModel); + + // read local settings + String newstate = par.getParameter("new-state", this.newstate); + String statekey = par.getParameter("state-key", this.statekey); + int sublevels = par.getParameterAsInteger("sublevels", this.sublevels); + int mylevel = par.getParameterAsInteger("state-level", this.mylevel); + + if (newstate == null) { + if (this.getLogger().isDebugEnabled()) { + getLogger().error("new-state is null"); + } + return null; + } + + if (request != null) { + Session session = request.getSession(false); + + if (session != null && request.isRequestedSessionIdValid()) { + String oldstate = null; + if (sublevels == 0) { + oldstate = (String) session.getAttribute(statekey); + session.setAttribute(statekey, newstate); + if (this.getLogger().isDebugEnabled()) { + getLogger().debug(statekey + "=" + newstate); + } + + } else { // sublevels != 0 + oldstate = (String)session.getAttribute(statekey + mylevel); + for (int i = mylevel + 1; i <= sublevels; i++) { + session.removeAttribute(statekey + i); + if (this.getLogger().isDebugEnabled()) { + getLogger().debug("Remove " + statekey + i); + } + } + session.setAttribute(statekey + mylevel, newstate); + if (this.getLogger().isDebugEnabled()) { + getLogger().debug(statekey + mylevel + "=" + newstate); + } + } + if (this.getLogger().isDebugEnabled()) { + getLogger().debug("Transition " + oldstate + " -> " + newstate); + } + + HashMap map = new HashMap(1); + map.put("newstate", newstate); + return map; + } else { + getLogger().warn( + "A session object was not present or no longer valid"); + return null; + } + } else { + getLogger().warn("No request object"); + return null; + } + } +} Propchange: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/SessionStateAction.java ------------------------------------------------------------------------------ svn:eol-style = native Added: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/SessionValidatorAction.java URL: http://svn.apache.org/viewcvs/cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/SessionValidatorAction.java?rev=330548&view=auto ============================================================================== --- cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/SessionValidatorAction.java (added) +++ cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/SessionValidatorAction.java Thu Nov 3 05:41:06 2005 @@ -0,0 +1,81 @@ +/* + * Copyright 1999-2004 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.cocoon.acting; + +import org.apache.avalon.framework.configuration.Configuration; +import org.apache.avalon.framework.thread.ThreadSafe; + +import org.apache.cocoon.environment.ObjectModelHelper; +import org.apache.cocoon.environment.Session; + +import java.util.Collection; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + +/** + * This is the action used to validate Session parameters (attributes). + * The parameters are described via the external xml + * file. + * + * @see org.apache.cocoon.acting.AbstractValidatorAction + * + * @author Martin Man + * @author Christian Haul + * @version CVS $Id: SessionValidatorAction.java 30941 2004-07-29 19:56:58Z vgritsenko $ + */ +public class SessionValidatorAction extends AbstractValidatorAction implements ThreadSafe { + + /* (non-Javadoc) + * @see org.apache.cocoon.acting.AbstractValidatorAction#createMapOfParameters(java.util.Map, java.util.Collection) + */ + protected HashMap createMapOfParameters(Map objectModel, Collection set) { + String name; + HashMap params = new HashMap(set.size()); + // put required params into hash + Session session = ObjectModelHelper.getRequest(objectModel).getSession(); + for (Iterator i = set.iterator(); i.hasNext();) { + name = ((Configuration) i.next()).getAttribute("name", "").trim(); + Object value = session.getAttribute(name); + params.put(name, value); + } + return params; + } + + + /* (non-Javadoc) + * @see org.apache.cocoon.acting.AbstractValidatorAction#setResult(java.util.Map, java.util.Map, java.util.Map, boolean) + */ + protected Map setResult(Map objectModel, Map actionMap, Map resultMap, boolean allOK) { + if (allOK){ + Session session = ObjectModelHelper.getRequest(objectModel).getSession(); + for (Iterator i = actionMap.keySet().iterator(); i.hasNext(); ){ + String name = (String) i.next(); + session.setAttribute(name, actionMap.get(name)); + } + } + return super.setResult(objectModel, actionMap, resultMap, allOK); + } + + + /* (non-Javadoc) + * @see org.apache.cocoon.acting.AbstractValidatorAction#isStringEncoded() + */ + boolean isStringEncoded() { + return false; + } + +} Propchange: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/SessionValidatorAction.java ------------------------------------------------------------------------------ svn:eol-style = native Added: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/SetCharacterEncodingAction.java URL: http://svn.apache.org/viewcvs/cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/SetCharacterEncodingAction.java?rev=330548&view=auto ============================================================================== --- cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/SetCharacterEncodingAction.java (added) +++ cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/SetCharacterEncodingAction.java Thu Nov 3 05:41:06 2005 @@ -0,0 +1,69 @@ +/* + * Copyright 1999-2004 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.cocoon.acting; + +import org.apache.avalon.framework.parameters.ParameterException; +import org.apache.avalon.framework.parameters.Parameterizable; +import org.apache.avalon.framework.parameters.Parameters; +import org.apache.avalon.framework.thread.ThreadSafe; + +import org.apache.cocoon.environment.ObjectModelHelper; +import org.apache.cocoon.environment.Redirector; +import org.apache.cocoon.environment.Request; +import org.apache.cocoon.environment.SourceResolver; + +import java.util.Map; + +/** + * Sets the character encoding of parameters. + * Components use this encoding as default after the action. + *

+ * Configuration parameters: + *

+ *
form-encoding (optional) + *
The supposed encoding of the request parameter. + *
+ * These configuration options supported in both declaration and use time. + *

If no encoding specified, the action does nothing. + * + * @author MIYABE Tatsuhiko + * @version CVS $Id: SetCharacterEncodingAction.java 30941 2004-07-29 19:56:58Z vgritsenko $ + */ +public class SetCharacterEncodingAction extends ServiceableAction implements ThreadSafe, Parameterizable { + private String global_form_encoding = null; + + public void parameterize(Parameters parameters) + throws ParameterException { + // super.parameterize(parameters); + + global_form_encoding = parameters.getParameter("form-encoding", null); + } + + /** + * Set character encoding of current request. + */ + public Map act (Redirector redirector, SourceResolver resolver, Map objectModel, String src, Parameters par) throws Exception { + Request request = ObjectModelHelper.getRequest(objectModel); + if (request != null) { + String form_encoding = par.getParameter("form-encoding", global_form_encoding); + if (form_encoding != null) { + request.setCharacterEncoding(form_encoding); + } + } + + return null; + } +} Propchange: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/SetCharacterEncodingAction.java ------------------------------------------------------------------------------ svn:eol-style = native Added: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/SetterAction.java URL: http://svn.apache.org/viewcvs/cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/SetterAction.java?rev=330548&view=auto ============================================================================== --- cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/SetterAction.java (added) +++ cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/SetterAction.java Thu Nov 3 05:41:06 2005 @@ -0,0 +1,93 @@ +/* + * Copyright 2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.cocoon.acting; + +import java.util.Map; + +import org.apache.avalon.framework.parameters.ParameterException; +import org.apache.avalon.framework.parameters.Parameterizable; +import org.apache.avalon.framework.parameters.Parameters; +import org.apache.avalon.framework.thread.ThreadSafe; +import org.apache.cocoon.environment.ObjectModelHelper; +import org.apache.cocoon.environment.Redirector; +import org.apache.cocoon.environment.SourceResolver; + +/** + * This action can be used to set information in either the object model, + * the request or the session. + * All parameters set for this action are set in the according location + * whereas the parameter name is the key and the value of the parameter + * will be set as a string value for this key. + * + * @version SVN $Id: SetterAction.java 289177 2005-09-15 08:24:23Z cziegeler $ + */ +public class SetterAction + extends AbstractAction + implements Parameterizable, ThreadSafe { + + public static final int MODE_OBJECT_MODEL = 1; + public static final int MODE_REQUEST_ATTR = 2; + public static final int MODE_SESSION_ATTR = 3; + + public static final String MODEDEF_OBJECT_MODEL = "object-model"; + public static final String MODEDEF_REQUEST_ATTR = "request-attribute"; + public static final String MODEDEF_SESSION_ATTR = "session-attribute"; + + protected int mode = MODE_OBJECT_MODEL; + + /** + * @see Parameterizable#parameterize(org.apache.avalon.framework.parameters.Parameters) + * @throws ParameterException + */ + public void parameterize(Parameters params) + throws ParameterException { + String modeDef = params.getParameter("mode", null); + if ( modeDef != null ) { + if ( MODEDEF_OBJECT_MODEL.equals(modeDef) ) { + this.mode = MODE_OBJECT_MODEL; + } else if ( MODEDEF_REQUEST_ATTR.equals(modeDef) ) { + this.mode = MODE_REQUEST_ATTR; + } else if ( MODEDEF_SESSION_ATTR.equals(modeDef) ) { + this.mode = MODE_SESSION_ATTR; + } else { + throw new ParameterException("Unknown mode: " + this.mode); + } + } + } + + /** + * @see org.apache.cocoon.acting.Action#act(org.apache.cocoon.environment.Redirector, org.apache.cocoon.environment.SourceResolver, java.util.Map, java.lang.String, org.apache.avalon.framework.parameters.Parameters) + */ + public Map act(Redirector redirector, + SourceResolver resolver, + Map objectModel, + String source, + Parameters parameters) + throws Exception { + final String[] names = parameters.getNames(); + for(int i = 0; i < names.length; i++) { + final String name = names[i]; + if ( this.mode == MODE_OBJECT_MODEL ) { + objectModel.put(name, parameters.getParameter(name)); + } else if ( this.mode == MODE_REQUEST_ATTR ) { + ObjectModelHelper.getRequest(objectModel).setAttribute(name, parameters.getParameter(name)); + } else if ( this.mode == MODE_SESSION_ATTR ) { + ObjectModelHelper.getRequest(objectModel).getSession().setAttribute(name, parameters.getParameter(name)); + } + } + return EMPTY_MAP; + } +} Propchange: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/SetterAction.java ------------------------------------------------------------------------------ svn:eol-style = native Added: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/ValidatorActionHelper.java URL: http://svn.apache.org/viewcvs/cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/ValidatorActionHelper.java?rev=330548&view=auto ============================================================================== --- cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/ValidatorActionHelper.java (added) +++ cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/ValidatorActionHelper.java Thu Nov 3 05:41:06 2005 @@ -0,0 +1,124 @@ +/* + * Copyright 1999-2004 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.cocoon.acting; + + +/** + * Helper class to pass the result of a validation back along with + * the validated object itself. + * + * @author Christian Haul + * @version CVS $Id: ValidatorActionHelper.java 30941 2004-07-29 19:56:58Z vgritsenko $ + */ +public class ValidatorActionHelper +{ + protected ValidatorActionResult result = ValidatorActionResult.OK; + protected Object object = null; + + /** + * Create a ValidatorActionHelper object that contains just the + * object. Defaults to OK as validation result. + * + * @param validatedObject object that has been validated + */ + public ValidatorActionHelper ( Object validatedObject ) { + this.object = validatedObject; + this.result = ValidatorActionResult.OK; + } + + /** + * Create a ValidatorActionHelper object that contains just the + * object. Defaults to OK as validation result. + * + * @param validatedObject object that has been validated + * @param validationResult result of the validation + */ + public ValidatorActionHelper ( Object validatedObject, ValidatorActionResult validationResult ) { + this.object = validatedObject; + this.result = validationResult; + } + + /** + * Tests if the validation result is OK + * + */ + public boolean isOK() { + return (result.equals(ValidatorActionResult.OK)); + } + + /** + * Tests if the validation result is NOTPRESENT, + * e.g. when the value is null and is allowed to be null. + * + */ + public boolean isNotPresent() { + return (result.equals(ValidatorActionResult.NOTPRESENT)); + } + + /** + * Tests if the validation result is ISNULL, + * e.g. when the value is null but is not supposed to be null. + * + */ + public boolean isNull() { + return (result.equals(ValidatorActionResult.ISNULL)); + } + + /** + * Tests if the validation result is TOOLARGE, + * e.g. in case of a double or long the value is too large or in + * case of a string it is too long. + * + */ + public boolean isTooLarge() { + return (result.equals(ValidatorActionResult.TOOLARGE)); + } + + /** + * Tests if the validation result is TOOSMALL, + * e.g. in case of a double or long the value is too small or in + * case of a string it is too short. + * + */ + public boolean isTooSmall() { + return (result.equals(ValidatorActionResult.TOOSMALL)); + } + + /** + * Tests if the validation result is NOMATCH, can + * only occur when + * + */ + public boolean doesNotMatch() { + return (result.equals(ValidatorActionResult.NOMATCH)); + } + + /** + * Returns the tested object. + * + */ + public Object getObject() { + return object; + } + + /** + * Returns the result. + * + */ + public ValidatorActionResult getResult() { + return result; + } +} Propchange: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/acting/ValidatorActionHelper.java ------------------------------------------------------------------------------ svn:eol-style = native