001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.acting;
018:
019: import java.util.ArrayList;
020: import java.util.Collections;
021: import java.util.HashMap;
022: import java.util.List;
023: import java.util.Map;
024:
025: import org.apache.avalon.framework.configuration.Configuration;
026: import org.apache.avalon.framework.configuration.ConfigurationException;
027: import org.apache.avalon.framework.parameters.Parameters;
028: import org.apache.avalon.framework.thread.ThreadSafe;
029:
030: import org.apache.cocoon.environment.ObjectModelHelper;
031: import org.apache.cocoon.environment.Redirector;
032: import org.apache.cocoon.environment.Request;
033: import org.apache.cocoon.environment.Session;
034: import org.apache.cocoon.environment.SourceResolver;
035:
036: /**
037: * This is the action used to propagate parameters into session. It
038: * simply propagates given expression to the session. If session does not
039: * exist, action fails. Additionaly it will make all propagated values
040: * available via returned Map.
041: *
042: * <pre>
043: * <map:act type="session-propagator">
044: * <paramater name="example" value="{example}">
045: * <paramater name="example1" value="xxx">
046: * </map:act>
047: * </pre>
048: *
049: * @author <a href="mailto:Martin.Man@seznam.cz">Martin Man</a>
050: * @version CVS $Id: SessionPropagatorAction.java 433543 2006-08-22 06:22:54Z crossley $
051: */
052: public class SessionPropagatorAction extends AbstractConfigurableAction
053: implements ThreadSafe {
054:
055: /**
056: * A private helper holding default parameter entries.
057: *
058: */
059: private static class Entry {
060: public String key = null;
061: public String value = null;
062:
063: public Entry(String key, String value) {
064: this .key = key;
065: this .value = value;
066: }
067: }
068:
069: private List defaults;
070:
071: public void configure(Configuration conf)
072: throws ConfigurationException {
073: super .configure(conf);
074: Configuration[] dflts = conf.getChildren();
075: if (dflts != null) {
076: this .defaults = new ArrayList(dflts.length);
077: for (int i = 0; i < dflts.length; i++) {
078: this .defaults.add(new Entry(dflts[i].getName(),
079: dflts[i].getValue()));
080: }
081: } else {
082: this .defaults = new ArrayList(0);
083: }
084: }
085:
086: /**
087: * Main invocation routine.
088: */
089: public Map act(Redirector redirector, SourceResolver resolver,
090: Map objectModel, String src, Parameters parameters)
091: throws Exception {
092: Request req = ObjectModelHelper.getRequest(objectModel);
093: HashMap actionMap = new HashMap();
094:
095: /* check session validity */
096: Session session = req.getSession(false);
097: if (session == null) {
098: if (getLogger().isDebugEnabled()) {
099: getLogger().debug("No session object");
100: }
101: return null;
102: }
103:
104: try {
105: String[] names = parameters.getNames();
106:
107: // parameters
108: for (int i = 0; i < names.length; i++) {
109: String sessionParamName = names[i];
110: String value = parameters
111: .getParameter(sessionParamName);
112: if (getLogger().isDebugEnabled()) {
113: getLogger().debug(
114: "Propagating value " + value
115: + " to session attribute "
116: + sessionParamName);
117: }
118: session.setAttribute(sessionParamName, value);
119: actionMap.put(sessionParamName, value);
120: }
121:
122: // defaults, that are not overridden
123: for (int i = 0; i < defaults.size(); i++) {
124: final Entry entry = (Entry) defaults.get(i);
125: if (!actionMap.containsKey(entry.key)) {
126: if (getLogger().isDebugEnabled()) {
127: getLogger().debug(
128: "Propagating value " + entry.value
129: + " to session attribute "
130: + entry.key);
131: }
132: session.setAttribute(entry.key, entry.value);
133: actionMap.put(entry.key, entry.value);
134: }
135: }
136: if (getLogger().isDebugEnabled()) {
137: getLogger().debug(
138: "All params propagated " + "to session");
139: }
140: return Collections.unmodifiableMap(actionMap);
141: } catch (Exception e) {
142: getLogger().warn("exception: ", e);
143: }
144: return null;
145: }
146: }
|