01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.cocoon.acting;
18:
19: import java.util.Map;
20:
21: import org.apache.avalon.framework.parameters.ParameterException;
22: import org.apache.avalon.framework.parameters.Parameterizable;
23: import org.apache.avalon.framework.parameters.Parameters;
24: import org.apache.avalon.framework.thread.ThreadSafe;
25: import org.apache.cocoon.environment.ObjectModelHelper;
26: import org.apache.cocoon.environment.Redirector;
27: import org.apache.cocoon.environment.SourceResolver;
28:
29: /**
30: * This action can be used to set information in either the object model,
31: * the request or the session.
32: * All parameters set for this action are set in the according location
33: * whereas the parameter name is the key and the value of the parameter
34: * will be set as a string value for this key.
35: *
36: * @version SVN $Id: SetterAction.java 433543 2006-08-22 06:22:54Z crossley $
37: */
38: public class SetterAction extends AbstractAction implements
39: Parameterizable, ThreadSafe {
40:
41: public static final int MODE_OBJECT_MODEL = 1;
42: public static final int MODE_REQUEST_ATTR = 2;
43: public static final int MODE_SESSION_ATTR = 3;
44:
45: public static final String MODEDEF_OBJECT_MODEL = "object-model";
46: public static final String MODEDEF_REQUEST_ATTR = "request-attribute";
47: public static final String MODEDEF_SESSION_ATTR = "session-attribute";
48:
49: protected int mode = MODE_OBJECT_MODEL;
50:
51: /**
52: * @see Parameterizable#parameterize(org.apache.avalon.framework.parameters.Parameters)
53: * @throws ParameterException
54: */
55: public void parameterize(Parameters params)
56: throws ParameterException {
57: String modeDef = params.getParameter("mode", null);
58: if (modeDef != null) {
59: if (MODEDEF_OBJECT_MODEL.equals(modeDef)) {
60: this .mode = MODE_OBJECT_MODEL;
61: } else if (MODEDEF_REQUEST_ATTR.equals(modeDef)) {
62: this .mode = MODE_REQUEST_ATTR;
63: } else if (MODEDEF_SESSION_ATTR.equals(modeDef)) {
64: this .mode = MODE_SESSION_ATTR;
65: } else {
66: throw new ParameterException("Unknown mode: "
67: + this .mode);
68: }
69: }
70: }
71:
72: /**
73: * @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)
74: */
75: public Map act(Redirector redirector, SourceResolver resolver,
76: Map objectModel, String source, Parameters parameters)
77: throws Exception {
78: final String[] names = parameters.getNames();
79: for (int i = 0; i < names.length; i++) {
80: final String name = names[i];
81: if (this.mode == MODE_OBJECT_MODEL) {
82: objectModel.put(name, parameters.getParameter(name));
83: } else if (this.mode == MODE_REQUEST_ATTR) {
84: ObjectModelHelper.getRequest(objectModel).setAttribute(
85: name, parameters.getParameter(name));
86: } else if (this.mode == MODE_SESSION_ATTR) {
87: ObjectModelHelper.getRequest(objectModel).getSession()
88: .setAttribute(name,
89: parameters.getParameter(name));
90: }
91: }
92: return EMPTY_MAP;
93: }
94: }
|