001: package com.meterware.httpunit;
002:
003: /********************************************************************************************************************
004: * $Id: FixedURLWebRequestSource.java,v 1.5 2004/07/23 01:31:04 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.util.*;
024: import java.net.URL;
025: import java.io.IOException;
026:
027: import org.w3c.dom.Node;
028:
029: /**
030: * An implementation of web request source whose URL does not change under user action.
031: *
032: * @author <a href="mailto:russgold@acm.org">Russell Gold</a>
033: **/
034: abstract class FixedURLWebRequestSource extends WebRequestSource {
035:
036: private static final String[] NO_VALUES = new String[0];
037: private Map _presetParameterMap;
038: private ArrayList _presetParameterList;
039: private String _characterSet;
040:
041: public FixedURLWebRequestSource(WebResponse response, Node node,
042: URL baseURL, String destination, FrameSelector frame,
043: String defaultTarget, String characterSet) {
044: super (response, node, baseURL, destination, frame,
045: defaultTarget);
046: _characterSet = characterSet;
047: }
048:
049: //------------------------------------------- WebRequestSource methods -------------------------------------------------
050:
051: /**
052: * Creates and returns a web request which will simulate clicking on this link.
053: **/
054: public WebRequest getRequest() {
055: return new GetMethodWebRequest(this );
056: }
057:
058: /**
059: * Returns an array containing the names of any parameters defined as part of this link's URL.
060: **/
061: public String[] getParameterNames() {
062: ArrayList parameterNames = new ArrayList(
063: getPresetParameterMap().keySet());
064: return (String[]) parameterNames
065: .toArray(new String[parameterNames.size()]);
066: }
067:
068: /**
069: * Returns the multiple default values of the named parameter.
070: **/
071: public String[] getParameterValues(String name) {
072: final String[] values = (String[]) getPresetParameterMap().get(
073: name);
074: return values == null ? NO_VALUES : values;
075: }
076:
077: protected void addPresetParameter(String name, String value) {
078: _presetParameterMap.put(name, HttpUnitUtils.withNewValue(
079: (String[]) _presetParameterMap.get(name), value));
080: _presetParameterList.add(new PresetParameter(name, value));
081: }
082:
083: protected String getEmptyParameterValue() {
084: return "";
085: }
086:
087: protected void setDestination(String destination) {
088: super .setDestination(destination);
089: _presetParameterList = null;
090: _presetParameterMap = null;
091: }
092:
093: //------------------------------------------- ParameterHolder methods --------------------------------------------------
094:
095: /**
096: * Specifies the position at which an image button (if any) was clicked.
097: **/
098: void selectImageButtonPosition(SubmitButton imageButton, int x,
099: int y) {
100: throw new IllegalNonFormParametersRequest();
101: }
102:
103: /**
104: * Iterates through the fixed, predefined parameters in this holder, recording them in the supplied parameter processor.\
105: * These parameters always go on the URL, no matter what encoding method is used.
106: **/
107:
108: void recordPredefinedParameters(ParameterProcessor processor)
109: throws IOException {
110: }
111:
112: /**
113: * Iterates through the parameters in this holder, recording them in the supplied parameter processor.
114: **/
115: void recordParameters(ParameterProcessor processor)
116: throws IOException {
117: Iterator i = getPresetParameterList().iterator();
118: while (i.hasNext()) {
119: PresetParameter o = (PresetParameter) i.next();
120: processor.addParameter(o.getName(), o.getValue(),
121: getCharacterSet());
122: }
123: }
124:
125: /**
126: * Removes a parameter name from this collection.
127: **/
128: void removeParameter(String name) {
129: throw new IllegalNonFormParametersRequest();
130: }
131:
132: /**
133: * Sets the value of a parameter in a web request.
134: **/
135: void setParameter(String name, String value) {
136: setParameter(name, new String[] { value });
137: }
138:
139: /**
140: * Sets the multiple values of a parameter in a web request.
141: **/
142: void setParameter(String name, String[] values) {
143: if (values == null) {
144: throw new IllegalArgumentException(
145: "May not supply a null argument array to setParameter()");
146: } else if (!getPresetParameterMap().containsKey(name)) {
147: throw new IllegalNonFormParametersRequest();
148: } else if (!equals(getParameterValues(name), values)) {
149: throw new IllegalNonFormParametersRequest();
150: }
151: }
152:
153: String getCharacterSet() {
154: return _characterSet;
155: }
156:
157: private boolean equals(String[] left, String[] right) {
158: if (left.length != right.length)
159: return false;
160: List rightValues = Arrays.asList(right);
161: for (int i = 0; i < left.length; i++) {
162: if (!rightValues.contains(left[i]))
163: return false;
164: }
165: return true;
166: }
167:
168: /**
169: * Sets the multiple values of a file upload parameter in a web request.
170: **/
171: void setParameter(String name, UploadFileSpec[] files) {
172: throw new IllegalNonFormParametersRequest();
173: }
174:
175: /**
176: * Returns true if the specified parameter is a file field.
177: **/
178: boolean isFileParameter(String name) {
179: return false;
180: }
181:
182: boolean isSubmitAsMime() {
183: return false;
184: }
185:
186: void setSubmitAsMime(boolean mimeEncoded) {
187: throw new IllegalStateException(
188: "May not change the encoding for a validated request created from a link");
189: }
190:
191: private Map getPresetParameterMap() {
192: if (_presetParameterMap == null)
193: loadPresetParameters();
194: return _presetParameterMap;
195: }
196:
197: private ArrayList getPresetParameterList() {
198: if (_presetParameterList == null)
199: loadPresetParameters();
200: return _presetParameterList;
201: }
202:
203: private void loadPresetParameters() {
204: _presetParameterMap = new HashMap();
205: _presetParameterList = new ArrayList();
206: loadDestinationParameters();
207: }
208:
209: }
210:
211: class PresetParameter {
212: private String _name;
213: private String _value;
214:
215: public PresetParameter(String name, String value) {
216: _name = name;
217: _value = value;
218: }
219:
220: public String getName() {
221: return _name;
222: }
223:
224: public String getValue() {
225: return _value;
226: }
227: }
228:
229: class IllegalNonFormParametersRequest extends
230: IllegalRequestParameterException {
231:
232: public IllegalNonFormParametersRequest() {
233: }
234:
235: public String getMessage() {
236: return "May not modify parameters for a request not derived from a form with parameter checking enabled.";
237: }
238:
239: }
|