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:
18: package org.apache.cocoon.components.modules.input;
19:
20: import org.apache.avalon.framework.configuration.Configuration;
21: import org.apache.avalon.framework.configuration.ConfigurationException;
22: import org.apache.avalon.framework.thread.ThreadSafe;
23: import org.apache.cocoon.environment.ObjectModelHelper;
24:
25: import java.util.Iterator;
26: import java.util.Map;
27: import java.util.Vector;
28:
29: /**
30: * RawRequestParameterModule accesses request parameters without
31: * decoding to the specified <code>form-encoding</code> or casting. It uses the
32: * {@link org.apache.cocoon.environment.Request#get} method instead of the
33: * {@link org.apache.cocoon.environment.Request#getParameter} method of the
34: * {@link org.apache.cocoon.environment.Request Request} This is useful for example
35: * in conjunction with uploads.
36: *
37: * <p>If <code>get()</code> returns a Vector, <code>getAttribute()</code> will return
38: * the first element, otherwise it will return the same as <code>get()</code>.
39: * <code>getAttributeValues()</code> will either convert the Vector to an array,
40: * place the result in a new array, or return the array as is.</p>
41: *
42: * @author <a href="mailto:haul@apache.org">Christian Haul</a>
43: * @version $Id: RawRequestParameterModule.java 433543 2006-08-22 06:22:54Z crossley $
44: */
45: public class RawRequestParameterModule extends AbstractInputModule
46: implements ThreadSafe {
47:
48: public Object getAttribute(String name, Configuration modeConf,
49: Map objectModel) throws ConfigurationException {
50:
51: String pname = (String) this .settings.get("parameter", name);
52: if (modeConf != null) {
53: pname = modeConf.getAttribute("parameter", pname);
54: // preferred
55: pname = modeConf.getChild("parameter").getValue(pname);
56: }
57: Object obj = ObjectModelHelper.getRequest(objectModel).get(
58: pname);
59: if (obj instanceof Vector) {
60: return ((Vector) obj).firstElement();
61: } else {
62: return obj;
63: }
64:
65: }
66:
67: public Iterator getAttributeNames(Configuration modeConf,
68: Map objectModel) throws ConfigurationException {
69:
70: return new IteratorHelper(ObjectModelHelper.getRequest(
71: objectModel).getParameterNames());
72: }
73:
74: public Object[] getAttributeValues(String name,
75: Configuration modeConf, Map objectModel)
76: throws ConfigurationException {
77:
78: Object obj = getAttribute(name, modeConf, objectModel);
79: if (obj instanceof Vector) {
80: return ((Vector) obj).toArray();
81: } else if (obj.getClass().isArray()) {
82: return (Object[]) obj;
83: } else {
84: Object[] tmp = new Object[1];
85: tmp[0] = obj;
86: return tmp;
87: }
88: }
89:
90: }
|