001: package com.meterware.httpunit;
002:
003: /********************************************************************************************************************
004: * $Id: UncheckedParameterHolder.java,v 1.9 2003/08/20 12:06:15 russgold Exp $
005: *
006: * Copyright (c) 2002, Russell Gold
007: *
008: * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
009: * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
010: * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
011: * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included in all copies or substantial portions
014: * of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
017: * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
018: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
019: * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
020: * DEALINGS IN THE SOFTWARE.
021: *
022: *******************************************************************************************************************/
023: import java.io.IOException;
024:
025: import java.util.Hashtable;
026: import java.util.Enumeration;
027:
028: /**
029: *
030: * @author <a href="mailto:russgold@httpunit.org">Russell Gold</a>
031: **/
032: final class UncheckedParameterHolder extends ParameterHolder implements
033: ParameterProcessor {
034:
035: private static final String[] NO_VALUES = new String[0];
036: private final String _characterSet;
037:
038: private Hashtable _parameters = new Hashtable();
039: private boolean _submitAsMime;
040:
041: UncheckedParameterHolder() {
042: _characterSet = HttpUnitOptions.getDefaultCharacterSet();
043: }
044:
045: UncheckedParameterHolder(WebRequestSource source) {
046: _characterSet = source.getCharacterSet();
047: _submitAsMime = source.isSubmitAsMime();
048:
049: try {
050: source.recordPredefinedParameters(this );
051: source.recordParameters(this );
052: } catch (IOException e) {
053: throw new RuntimeException("This should never happen");
054: }
055: }
056:
057: //----------------------------------- ParameterProcessor methods -------------------------------------------------------
058:
059: public void addParameter(String name, String value,
060: String characterSet) throws IOException {
061: Object[] values = (Object[]) _parameters.get(name);
062: _parameters
063: .put(name, HttpUnitUtils.withNewValue(values, value));
064: }
065:
066: public void addFile(String parameterName, UploadFileSpec fileSpec)
067: throws IOException {
068: Object[] values = (Object[]) _parameters.get(parameterName);
069: _parameters.put(parameterName, HttpUnitUtils.withNewValue(
070: values, fileSpec));
071: }
072:
073: //----------------------------------- ParameterHolder methods ----------------------------------------------------------
074:
075: /**
076: * Specifies the position at which an image button (if any) was clicked.
077: **/
078: void selectImageButtonPosition(SubmitButton imageButton, int x,
079: int y) {
080: setParameter(imageButton.getName() + ".x", Integer.toString(x));
081: setParameter(imageButton.getName() + ".y", Integer.toString(y));
082: }
083:
084: /**
085: * Does nothing, since unchecked requests treat all parameters the same.
086: **/
087: void recordPredefinedParameters(ParameterProcessor processor)
088: throws IOException {
089: }
090:
091: /**
092: * Iterates through the parameters in this holder, recording them in the supplied parameter processor.
093: **/
094: void recordParameters(ParameterProcessor processor)
095: throws IOException {
096: Enumeration e = _parameters.keys();
097:
098: while (e.hasMoreElements()) {
099: String name = (String) e.nextElement();
100: Object[] values = (Object[]) _parameters.get(name);
101: for (int i = 0; i < values.length; i++) {
102: if (values[i] instanceof String) {
103: processor.addParameter(name, (String) values[i],
104: _characterSet);
105: } else if (values[i] instanceof UploadFileSpec) {
106: processor.addFile(name, (UploadFileSpec) values[i]);
107: }
108: }
109: }
110: }
111:
112: String[] getParameterNames() {
113: return (String[]) _parameters.keySet().toArray(
114: new String[_parameters.size()]);
115: }
116:
117: String getParameterValue(String name) {
118: String[] values = getParameterValues(name);
119: return values.length == 0 ? null : values[0];
120: }
121:
122: String[] getParameterValues(String name) {
123: Object[] values = (Object[]) _parameters.get(name);
124: if (values == null)
125: return NO_VALUES;
126:
127: String[] result = new String[values.length];
128: for (int i = 0; i < result.length; i++) {
129: result[i] = values[i] instanceof UploadFileSpec ? ((UploadFileSpec) values[i])
130: .getFileName()
131: : values[i].toString();
132: }
133: return result;
134: }
135:
136: void removeParameter(String name) {
137: _parameters.remove(name);
138: }
139:
140: void setParameter(String name, String value) {
141: _parameters.put(name, new Object[] { value });
142: }
143:
144: void setParameter(String name, String[] values) {
145: _parameters.put(name, values);
146: }
147:
148: void setParameter(String name, UploadFileSpec[] files) {
149: _parameters.put(name, files);
150: }
151:
152: boolean isFileParameter(String name) {
153: return true;
154: }
155:
156: String getCharacterSet() {
157: return _characterSet;
158: }
159:
160: boolean isSubmitAsMime() {
161: return _submitAsMime;
162: }
163:
164: void setSubmitAsMime(boolean mimeEncoded) {
165: _submitAsMime = mimeEncoded;
166: }
167: }
|