001: /*
002: * Copyright (c) 2002-2008 Gargoyle Software Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * 1. Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: * 2. Redistributions in binary form must reproduce the above copyright notice,
010: * this list of conditions and the following disclaimer in the documentation
011: * and/or other materials provided with the distribution.
012: * 3. The end-user documentation included with the redistribution, if any, must
013: * include the following acknowledgment:
014: *
015: * "This product includes software developed by Gargoyle Software Inc.
016: * (http://www.GargoyleSoftware.com/)."
017: *
018: * Alternately, this acknowledgment may appear in the software itself, if
019: * and wherever such third-party acknowledgments normally appear.
020: * 4. The name "Gargoyle Software" must not be used to endorse or promote
021: * products derived from this software without prior written permission.
022: * For written permission, please contact info@GargoyleSoftware.com.
023: * 5. Products derived from this software may not be called "HtmlUnit", nor may
024: * "HtmlUnit" appear in their name, without prior written permission of
025: * Gargoyle Software Inc.
026: *
027: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
028: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
029: * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
030: * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
031: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
032: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
033: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
034: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
035: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
036: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
037: */
038: package com.gargoylesoftware.htmlunit;
039:
040: import java.net.URL;
041: import java.util.Collections;
042: import java.util.HashMap;
043: import java.util.List;
044: import java.util.Map;
045:
046: import org.apache.commons.httpclient.auth.CredentialsProvider;
047: import org.apache.commons.lang.ClassUtils;
048:
049: /**
050: * Parameter object for making web requests.
051: *
052: * @version $Revision: 2132 $
053: * @author Brad Clarke
054: * @author Hans Donner
055: * @author Ahmed Ashour
056: */
057: public class WebRequestSettings {
058:
059: private URL url_;
060: private String proxyHost_;
061: private int proxyPort_;
062: private SubmitMethod submitMethod_ = SubmitMethod.GET;
063: private FormEncodingType encodingType_ = FormEncodingType.URL_ENCODED;
064: private Map additionalHeaders_ = new HashMap();
065: private CredentialsProvider credentialsProvider_;
066: private String charset_ = TextUtil.DEFAULT_CHARSET;
067: private String cookiePolicy_;
068:
069: /* These two are mutually exclusive; additionally, requestBody_ should only be set for POST requests. */
070: private List requestParameters_ = Collections.EMPTY_LIST;
071: private String requestBody_;
072:
073: /**
074: * @param target The URL for this request
075: */
076: public WebRequestSettings(final URL target) {
077: setURL(target);
078: }
079:
080: /**
081: * Instantiate a {@link WebRequestSettings} for the given url using the proxy configuration from the original
082: * request
083: * @param originalRequest the original request
084: * @param target The URL for this request
085: */
086: public WebRequestSettings(final WebRequestSettings originalRequest,
087: final URL target) {
088: this (target);
089: setProxyHost(originalRequest.getProxyHost());
090: setProxyPort(originalRequest.getProxyPort());
091: }
092:
093: /**
094: * @param target The URL for this request
095: * @param submitMethod The submitMethod to set.
096: */
097: public WebRequestSettings(final URL target,
098: final SubmitMethod submitMethod) {
099: this (target);
100: setSubmitMethod(submitMethod);
101: }
102:
103: /**
104: * @return the URL
105: */
106: public URL getURL() {
107: return url_;
108: }
109:
110: /**
111: * @param url The new URL
112: */
113: public void setURL(final URL url) {
114: url_ = url;
115: }
116:
117: /**
118: * @return The proxy host.
119: */
120: public String getProxyHost() {
121: return proxyHost_;
122: }
123:
124: /**
125: * @param proxyHost The new proxy host.
126: */
127: public void setProxyHost(final String proxyHost) {
128: proxyHost_ = proxyHost;
129: }
130:
131: /**
132: * @return The proxy port.
133: */
134: public int getProxyPort() {
135: return proxyPort_;
136: }
137:
138: /**
139: * @param proxyPort The new proxy port.
140: */
141: public void setProxyPort(final int proxyPort) {
142: proxyPort_ = proxyPort;
143: }
144:
145: /**
146: * @return Returns the encodingType.
147: */
148: public FormEncodingType getEncodingType() {
149: return encodingType_;
150: }
151:
152: /**
153: * @param encodingType The encodingType to set.
154: */
155: public void setEncodingType(final FormEncodingType encodingType) {
156: encodingType_ = encodingType;
157: }
158:
159: /**
160: * @return Returns the requestParameters.
161: */
162: public List getRequestParameters() {
163: return requestParameters_;
164: }
165:
166: /**
167: * @param requestParameters The requestParameters to set.
168: * @throws RuntimeException If the request body has already been set.
169: */
170: public void setRequestParameters(final List requestParameters)
171: throws RuntimeException {
172: if (requestBody_ != null) {
173: final String msg = "Trying to set the request parameters, but the request body has already been specified;"
174: + "the two are mutually exclusive!";
175: throw new RuntimeException(msg);
176: }
177: requestParameters_ = requestParameters;
178: }
179:
180: /**
181: * Returns the body content to be submitted if this is a <tt>POST</tt> request. Ignored for
182: * all other request types. Should not be used in combination with parameters.
183: * @return The body content to be submitted if this is a <tt>POST</tt> request.
184: */
185: public String getRequestBody() {
186: return requestBody_;
187: }
188:
189: /**
190: * @param requestBody The body content to be submitted if this is a <tt>POST</tt> request.
191: * @throws RuntimeException If the request parameters have already been set or this is not a <tt>POST</tt> request.
192: */
193: public void setRequestBody(final String requestBody)
194: throws RuntimeException {
195: if (requestParameters_ != null && requestParameters_.size() > 0) {
196: final String msg = "Trying to set the request body, but the request parameters have already been specified;"
197: + "the two are mutually exclusive!";
198: throw new RuntimeException(msg);
199: }
200: if (submitMethod_ != SubmitMethod.POST) {
201: final String msg = "The request body may only be set for POST requests!";
202: throw new RuntimeException(msg);
203: }
204: requestBody_ = requestBody;
205: }
206:
207: /**
208: * @return Returns the submitMethod.
209: */
210: public SubmitMethod getSubmitMethod() {
211: return submitMethod_;
212: }
213:
214: /**
215: * @param submitMethod The submitMethod to set.
216: */
217: public void setSubmitMethod(final SubmitMethod submitMethod) {
218: submitMethod_ = submitMethod;
219: }
220:
221: /**
222: * @return Returns the additionalHeaders.
223: */
224: public Map getAdditionalHeaders() {
225: return additionalHeaders_;
226: }
227:
228: /**
229: * @param additionalHeaders The additionalHeaders to set.
230: */
231: public void setAdditionalHeaders(final Map additionalHeaders) {
232: additionalHeaders_ = additionalHeaders;
233: }
234:
235: /**
236: * Adds the specified name/value pair to the additional headers.
237: * @param name The name of the additional header.
238: * @param value The value of the additional header.
239: */
240: public void addAdditionalHeader(final String name,
241: final String value) {
242: additionalHeaders_.put(name, value);
243: }
244:
245: /**
246: * @return Returns the credentialsProvider.
247: */
248: public CredentialsProvider getCredentialsProvider() {
249: return credentialsProvider_;
250: }
251:
252: /**
253: * @param credentialsProvider The credentialProvider to set.
254: */
255: public void setCredentialsProvider(
256: final CredentialsProvider credentialsProvider) {
257: credentialsProvider_ = credentialsProvider;
258: }
259:
260: /**
261: * Return a string representation of this object
262: * @return See above
263: */
264: public String toString() {
265: final StringBuffer buffer = new StringBuffer();
266:
267: buffer.append(ClassUtils.getShortClassName(getClass()));
268:
269: buffer.append("[<");
270: buffer.append("url=\"" + url_.toExternalForm() + "\"");
271: buffer.append(", " + submitMethod_);
272: buffer.append(", " + encodingType_);
273: buffer.append(", " + requestParameters_);
274: buffer.append(", " + additionalHeaders_);
275: buffer.append(", " + credentialsProvider_);
276: buffer.append(">]");
277:
278: return buffer.toString();
279: }
280:
281: /**
282: * Gets the charset to use to perform the request
283: * @return the charset.
284: */
285: public String getCharset() {
286: return charset_;
287: }
288:
289: /**
290: * Sets the charset. Default value is {@link TextUtil#DEFAULT_CHARSET}
291: * @param charset the new charset
292: */
293: public void setCharset(final String charset) {
294: charset_ = charset;
295: }
296:
297: /**
298: * Gets the cookie policy to use to perform the request.
299: * @return the cookie policy.
300: */
301: public String getCookiePolicy() {
302: return cookiePolicy_;
303: }
304:
305: /**
306: * Sets the cookie policy.
307: * @param cookiePolicy the new cookie policy.
308: * @deprecated
309: * @see WebClient#setCookiesEnabled(boolean)
310: */
311: public void setCookiePolicy(final String cookiePolicy) {
312: cookiePolicy_ = cookiePolicy;
313: }
314: }
|