001: /*
002: * Copyright 2003 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package velosurf.web;
018:
019: import java.util.*;
020:
021: import org.apache.velocity.tools.view.context.ViewContext;
022: import org.apache.velocity.tools.view.tools.ParameterParser;
023:
024: import velosurf.util.Logger;
025:
026: /** This class extends the tool org.apache.velocity.tools.view.tools.ParameterParser,
027: * adding a generic setter. Values that are set manually hide any previous values that
028: * are present in the query under the same key.
029: *
030: * It is meant for the query scope.
031: *
032: * @author <a href=mailto:claude.brisson@gmail.com>Claude Brisson</a>
033: *
034: **/
035: public class HttpQueryTool extends ParameterParser implements Map {
036: /** extra values map. */
037: private Map<String, Object> extraValues = new HashMap<String, Object>();
038:
039: /**
040: * Constructor.
041: */
042: public HttpQueryTool() {
043: }
044:
045: /**
046: * Initialize this tool.
047: * @param viewContext view context
048: */
049: public void init(Object viewContext) {
050:
051: super .init(viewContext);
052:
053: /* review all parameter keys to interpret "dots" inside keynames (only one level for now - FIXME: implement a recursive behaviour) */
054: for (Map.Entry<String, Object> entry : (Set<Map.Entry<String, Object>>) getSource()
055: .entrySet()) {
056: String key = entry.getKey();
057: int dot = key.indexOf('.');
058: if (dot > 0 && dot < key.length() - 1) {
059: String parentKey = key.substring(0, dot);
060: String subKey = key.substring(dot + 1);
061: Object value = entry.getValue();
062: if (value.getClass().isArray()
063: && ((String[]) value).length == 1) {
064: value = ((String[]) value)[0];
065: }
066: Map map = (Map) extraValues.get(parentKey);
067: if (map == null) {
068: map = new HashMap();
069: extraValues.put(parentKey, map);
070: }
071: map.put(subKey, value);
072: }
073: }
074: }
075:
076: /**
077: * Generic getter.
078: * @param key key
079: * @return value or null
080: */
081: public Object get(Object key) {
082: Object ret = extraValues.get(key);
083: if (ret == null) {
084: return super .get(key.toString());
085: } else {
086: return ret;
087: }
088: }
089:
090: /**
091: * Generic getter with String argument (necessary to properly overload super getter).
092: * @param key key
093: * @return value or null
094: */
095: public Object get(String key) {
096: return get((Object) key);
097: }
098:
099: /**
100: * Generic setter.
101: * @param key key
102: * @param value value
103: * @return previous value
104: */
105: public Object put(Object key, Object value) {
106: return extraValues.put((String) key, value);
107: }
108:
109: /**
110: * Get the number of parameters.
111: * @return number of parameters
112: */
113: public int size() {
114: return getSource().size() + extraValues.size();
115: }
116:
117: /**
118: * Check for the presence of parameters.
119: * @return true if empty
120: */
121: public boolean isEmpty() {
122: return getSource().isEmpty() && extraValues.isEmpty();
123: }
124:
125: /**
126: * Check for the presence of a parameter.
127: * @param key parameter name
128: * @return true if present
129: */
130: public boolean containsKey(Object key) {
131: return getSource().containsKey(key)
132: || extraValues.containsKey(key);
133: }
134:
135: /**
136: * Check for the presence of a value.
137: * @param value value to find
138: * @return true if present
139: */
140: public boolean containsValue(Object value) {
141: String[] array = new String[1];
142: array[0] = (String) value;
143: return getSource().containsValue(array)
144: || extraValues.containsValue(value);
145: }
146:
147: /**
148: * Remove a parameter (from extra values).
149: * @param key parameter name
150: * @return value or null
151: */
152: public Object remove(Object key) {
153: return extraValues.remove(key);
154: }
155:
156: /**
157: * Put all key/values from a map.
158: * @param map source map
159: */
160: public void putAll(Map map) {
161: extraValues.putAll(map);
162: }
163:
164: /**
165: * Clear extra parameters.
166: */
167: public void clear() {
168: extraValues.clear();
169: }
170:
171: /**
172: * Get the set of parameter names.
173: * @return set of names
174: */
175: public Set keySet() {
176: Set ret = new HashSet();
177: ret.addAll(getSource().keySet());
178: ret.addAll(extraValues.keySet());
179: return ret;
180: }
181:
182: /**
183: * Get the collection of values
184: * @return collection of values
185: */
186: public Collection values() {
187: String[] array;
188: Collection ret = new HashSet();
189: Collection coll = getSource().values();
190: for (Object value : coll) {
191: if (value.getClass().isArray()) {
192: array = (String[]) value;
193: ret.add(array.length == 1 ? (Object) array[0] : array);
194: } else {
195: ret.add(value);
196: }
197: }
198: ret.addAll(extraValues.values());
199: return ret;
200: }
201:
202: public Set<Entry> entrySet() {
203: Map map = new HashMap();
204: Set<Entry> coll = getSource().entrySet();
205: for (Entry entry : coll) {
206: Object value = entry.getValue();
207: if (value.getClass().isArray()
208: && ((String[]) value).length == 1) {
209: value = ((String[]) value)[0];
210: }
211: map.put(entry.getKey(), value);
212: }
213: Set<Entry> ret = new HashSet(map.entrySet());
214: ret.addAll(extraValues.entrySet());
215: return ret;
216: }
217:
218: public String toString() {
219: StringBuilder ret = new StringBuilder("{ ");
220: for (Entry entry : entrySet()) {
221: ret.append((String) entry.getKey());
222: ret.append('=');
223: Object value = ret.append(entry.getValue().toString());
224: ret.append(' ');
225: }
226: ret.append('}');
227: return ret.toString();
228: }
229:
230: public Set<String> getExtraKeys() {
231: return extraValues.keySet();
232: }
233:
234: /**
235: * Debugging method: returns a query string corresponding to query parameters
236: * Warning: it also includes POST parameters (so strictly speaking
237: * it's not the real query string)
238: * @return reconstitued query string
239: */
240: public String getQueryString() {
241: StringBuffer result = new StringBuffer();
242: for (Map.Entry entry : entrySet()) {
243: if (result.length() > 0) {
244: result.append('&');
245: }
246: result.append(String.valueOf(entry.getKey()));
247: result.append('=');
248: result.append(String.valueOf(entry.getValue()));
249: }
250: return result.toString();
251: }
252:
253: public Integer getInteger(String key) {
254: Integer ret = super .getInteger(key);
255: /* try in extraValues */
256: if (ret == null) {
257: Object v = extraValues.get(key);
258: if (v != null) {
259: try {
260: ret = Integer.parseInt(String.valueOf(v));
261: } catch (NumberFormatException nfe) {
262: }
263: }
264: }
265: return ret;
266: }
267:
268: /* TODO subclass other getXXX() methods */
269: }
|