Source code: com/meterware/httpunit/FixedURLWebRequestSource.java
1 package com.meterware.httpunit;
2 /********************************************************************************************************************
3 * $Id: FixedURLWebRequestSource.java,v 1.5 2004/07/23 01:31:04 russgold Exp $
4 *
5 * Copyright (c) 2002, Russell Gold
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
8 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
10 * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in all copies or substantial portions
13 * of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
16 * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
18 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19 * DEALINGS IN THE SOFTWARE.
20 *
21 *******************************************************************************************************************/
22 import java.util.*;
23 import java.net.URL;
24 import java.io.IOException;
25
26 import org.w3c.dom.Node;
27
28
29 /**
30 * An implementation of web request source whose URL does not change under user action.
31 *
32 * @author <a href="mailto:russgold@acm.org">Russell Gold</a>
33 **/
34 abstract class FixedURLWebRequestSource extends WebRequestSource {
35
36 private static final String[] NO_VALUES = new String[0];
37 private Map _presetParameterMap;
38 private ArrayList _presetParameterList;
39 private String _characterSet;
40
41
42 public FixedURLWebRequestSource( WebResponse response, Node node, URL baseURL, String destination, FrameSelector frame, String defaultTarget, String characterSet ) {
43 super( response, node, baseURL, destination, frame, defaultTarget );
44 _characterSet = characterSet;
45 }
46
47
48 //------------------------------------------- WebRequestSource methods -------------------------------------------------
49
50
51 /**
52 * Creates and returns a web request which will simulate clicking on this link.
53 **/
54 public WebRequest getRequest() {
55 return new GetMethodWebRequest( this );
56 }
57
58
59 /**
60 * Returns an array containing the names of any parameters defined as part of this link's URL.
61 **/
62 public String[] getParameterNames() {
63 ArrayList parameterNames = new ArrayList( getPresetParameterMap().keySet() );
64 return (String[]) parameterNames.toArray( new String[ parameterNames.size() ] );
65 }
66
67
68 /**
69 * Returns the multiple default values of the named parameter.
70 **/
71 public String[] getParameterValues( String name ) {
72 final String[] values = (String[]) getPresetParameterMap().get( name );
73 return values == null ? NO_VALUES : values;
74 }
75
76
77 protected void addPresetParameter( String name, String value ) {
78 _presetParameterMap.put( name, HttpUnitUtils.withNewValue( (String[]) _presetParameterMap.get( name ), value ) );
79 _presetParameterList.add( new PresetParameter( name, value ) );
80 }
81
82
83 protected String getEmptyParameterValue() {
84 return "";
85 }
86
87
88 protected void setDestination( String destination ) {
89 super.setDestination( destination );
90 _presetParameterList = null;
91 _presetParameterMap = null;
92 }
93
94
95 //------------------------------------------- ParameterHolder methods --------------------------------------------------
96
97
98 /**
99 * Specifies the position at which an image button (if any) was clicked.
100 **/
101 void selectImageButtonPosition( SubmitButton imageButton, int x, int y ) {
102 throw new IllegalNonFormParametersRequest();
103 }
104
105
106 /**
107 * Iterates through the fixed, predefined parameters in this holder, recording them in the supplied parameter processor.\
108 * These parameters always go on the URL, no matter what encoding method is used.
109 **/
110
111 void recordPredefinedParameters( ParameterProcessor processor ) throws IOException {
112 }
113
114
115 /**
116 * Iterates through the parameters in this holder, recording them in the supplied parameter processor.
117 **/
118 void recordParameters( ParameterProcessor processor ) throws IOException {
119 Iterator i = getPresetParameterList().iterator();
120 while (i.hasNext()) {
121 PresetParameter o = (PresetParameter) i.next();
122 processor.addParameter( o.getName(), o.getValue(), getCharacterSet() );
123 }
124 }
125
126
127 /**
128 * Removes a parameter name from this collection.
129 **/
130 void removeParameter( String name ) {
131 throw new IllegalNonFormParametersRequest();
132 }
133
134
135 /**
136 * Sets the value of a parameter in a web request.
137 **/
138 void setParameter( String name, String value ) {
139 setParameter( name, new String[] { value } );
140 }
141
142
143 /**
144 * Sets the multiple values of a parameter in a web request.
145 **/
146 void setParameter( String name, String[] values ) {
147 if (values == null) {
148 throw new IllegalArgumentException( "May not supply a null argument array to setParameter()" );
149 } else if (!getPresetParameterMap().containsKey( name )) {
150 throw new IllegalNonFormParametersRequest();
151 } else if (!equals( getParameterValues( name ), values )) {
152 throw new IllegalNonFormParametersRequest();
153 }
154 }
155
156
157 String getCharacterSet() {
158 return _characterSet;
159 }
160
161
162 private boolean equals( String[] left, String[] right ) {
163 if (left.length != right.length) return false;
164 List rightValues = Arrays.asList( right );
165 for (int i = 0; i < left.length; i++) {
166 if (!rightValues.contains( left[i] )) return false;
167 }
168 return true;
169 }
170
171
172 /**
173 * Sets the multiple values of a file upload parameter in a web request.
174 **/
175 void setParameter( String name, UploadFileSpec[] files ) {
176 throw new IllegalNonFormParametersRequest();
177 }
178
179
180 /**
181 * Returns true if the specified parameter is a file field.
182 **/
183 boolean isFileParameter( String name ) {
184 return false;
185 }
186
187
188 boolean isSubmitAsMime() {
189 return false;
190 }
191
192
193 void setSubmitAsMime( boolean mimeEncoded ) {
194 throw new IllegalStateException( "May not change the encoding for a validated request created from a link" );
195 }
196
197
198 private Map getPresetParameterMap() {
199 if (_presetParameterMap == null) loadPresetParameters();
200 return _presetParameterMap;
201 }
202
203
204 private ArrayList getPresetParameterList() {
205 if (_presetParameterList == null) loadPresetParameters();
206 return _presetParameterList;
207 }
208
209
210 private void loadPresetParameters() {
211 _presetParameterMap = new HashMap();
212 _presetParameterList = new ArrayList();
213 loadDestinationParameters();
214 }
215
216
217 }
218
219
220
221
222 class PresetParameter {
223 private String _name;
224 private String _value;
225
226
227 public PresetParameter( String name, String value ) {
228 _name = name;
229 _value = value;
230 }
231
232
233 public String getName() {
234 return _name;
235 }
236
237
238 public String getValue() {
239 return _value;
240 }
241 }
242
243
244 class IllegalNonFormParametersRequest extends IllegalRequestParameterException {
245
246 public IllegalNonFormParametersRequest() {
247 }
248
249 public String getMessage() {
250 return "May not modify parameters for a request not derived from a form with parameter checking enabled.";
251 }
252
253
254 }