KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > gargoylesoftware > htmlunit > WebRequestSettings


1 /*
2  * Copyright (c) 2002, 2005 Gargoyle Software Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright notice,
10  * this list of conditions and the following disclaimer in the documentation
11  * and/or other materials provided with the distribution.
12  * 3. The end-user documentation included with the redistribution, if any, must
13  * include the following acknowledgment:
14  *
15  * "This product includes software developed by Gargoyle Software Inc.
16  * (http://www.GargoyleSoftware.com/)."
17  *
18  * Alternately, this acknowledgment may appear in the software itself, if
19  * and wherever such third-party acknowledgments normally appear.
20  * 4. The name "Gargoyle Software" must not be used to endorse or promote
21  * products derived from this software without prior written permission.
22  * For written permission, please contact info@GargoyleSoftware.com.
23  * 5. Products derived from this software may not be called "HtmlUnit", nor may
24  * "HtmlUnit" appear in their name, without prior written permission of
25  * Gargoyle Software Inc.
26  *
27  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
28  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
29  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
30  * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
33  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
36  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37  */

38 package com.gargoylesoftware.htmlunit;
39
40 import java.net.URL JavaDoc;
41 import java.util.Collections JavaDoc;
42 import java.util.HashMap JavaDoc;
43 import java.util.List JavaDoc;
44 import java.util.Map JavaDoc;
45
46 import org.org.apache.commons.httpclient.auth.CredentialsProvider;
47 import org.apache.commons.lang.ClassUtils;
48
49 /**
50  * Parameter object for making web requests
51  *
52  * @version $Revision: 100 $
53  * @author Brad Clarke
54  * @author Hans Donner
55  */

56 public class WebRequestSettings {
57     private URL JavaDoc url_;
58     private SubmitMethod submitMethod_ = SubmitMethod.GET;
59     private FormEncodingType encodingType_ = FormEncodingType.URL_ENCODED;
60     private Map JavaDoc additionalHeaders_ = new HashMap JavaDoc();
61     private CredentialsProvider credentialsProvider_ = null;
62
63     /* These two are mutually exclusive; additionally, requestBody_ should only be set for POST requests. */
64     private List JavaDoc requestParameters_ = Collections.EMPTY_LIST;
65     private String JavaDoc requestBody_ = null;
66
67     /**
68      * @param target The URL for this request
69      */

70     public WebRequestSettings(final URL JavaDoc target) {
71         setURL(target);
72     }
73
74     /**
75      * @param target The URL for this request
76      * @param submitMethod The submitMethod to set.
77      */

78     public WebRequestSettings(final URL JavaDoc target, final SubmitMethod submitMethod) {
79         this(target);
80         setSubmitMethod(submitMethod);
81     }
82
83     /**
84      * @return the URL
85      */

86     public URL JavaDoc getURL() {
87         return url_;
88     }
89
90     /**
91      * @param url The new URL
92      */

93     public void setURL(final URL JavaDoc url) {
94         url_ = url;
95     }
96
97     /**
98      * @return Returns the encodingType.
99      */

100     public FormEncodingType getEncodingType() {
101         return encodingType_;
102     }
103
104     /**
105      * @param encodingType The encodingType to set.
106      */

107     public void setEncodingType(final FormEncodingType encodingType) {
108         encodingType_ = encodingType;
109     }
110
111     /**
112      * @return Returns the requestParameters.
113      */

114     public List JavaDoc getRequestParameters() {
115         return requestParameters_;
116     }
117
118     /**
119      * @param requestParameters The requestParameters to set.
120      * @throws RuntimeException If the request body has already been set.
121      */

122     public void setRequestParameters(final List JavaDoc requestParameters) throws RuntimeException JavaDoc {
123         if( requestBody_ != null ) {
124             final String JavaDoc msg = "Trying to set the request parameters, but the request body has already been specified;"
125                              + "the two are mutually exclusive!";
126             throw new RuntimeException JavaDoc( msg );
127         }
128         requestParameters_ = requestParameters;
129     }
130
131     /**
132      * Returns the body content to be submitted if this is a <tt>POST</tt> request. Ignored for
133      * all other request types. Should not be used in combination with parameters.
134      * @return The body content to be submitted if this is a <tt>POST</tt> request.
135      */

136     public String JavaDoc getRequestBody() {
137         return requestBody_;
138     }
139
140     /**
141      * @param requestBody The body content to be submitted if this is a <tt>POST</tt> request.
142      * @throws RuntimeException If the request parameters have already been set or this is not a <tt>POST</tt> request.
143      */

144     public void setRequestBody(final String JavaDoc requestBody) throws RuntimeException JavaDoc {
145         if( requestParameters_ != null && requestParameters_.size() > 0 ) {
146             String JavaDoc msg = "Trying to set the request body, but the request parameters have already been specified;"
147                        + "the two are mutually exclusive!";
148             throw new RuntimeException JavaDoc( msg );
149         }
150         if( submitMethod_ != SubmitMethod.POST ) {
151             String JavaDoc msg = "The request body may only be set for POST requests!";
152             throw new RuntimeException JavaDoc( msg );
153         }
154         requestBody_ = requestBody;
155     }
156
157     /**
158      * @return Returns the submitMethod.
159      */

160     public SubmitMethod getSubmitMethod() {
161         return submitMethod_;
162     }
163
164     /**
165      * @param submitMethod The submitMethod to set.
166      */

167     public void setSubmitMethod(final SubmitMethod submitMethod) {
168         submitMethod_ = submitMethod;
169     }
170     /**
171      * @return Returns the additionalHeaders.
172      */

173     public Map JavaDoc getAdditionalHeaders() {
174         return additionalHeaders_;
175     }
176
177     /**
178      * @param additionalHeaders The additionalHeaders to set.
179      */

180     public void setAdditionalHeaders(final Map JavaDoc additionalHeaders) {
181         additionalHeaders_ = additionalHeaders;
182     }
183
184     /**
185      * Adds the specified name/value pair to the additional headers.
186      * @param name The name of the additional header.
187      * @param value The value of the additional header.
188      */

189     public void addAdditionalHeader(final String JavaDoc name, final String JavaDoc value) {
190         additionalHeaders_.put( name, value );
191     }
192
193     /**
194      * @return Returns the credentialsProvider.
195      */

196     public CredentialsProvider getCredentialsProvider() {
197         return credentialsProvider_;
198     }
199     /**
200      * @param credentialsProvider The credentialProvider to set.
201      */

202     public void setCredentialsProvider(final CredentialsProvider credentialsProvider) {
203         credentialsProvider_ = credentialsProvider;
204     }
205
206     /**
207      * Return a string representation of this object
208      * @return See above
209      */

210     public String JavaDoc toString() {
211         final StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
212
213         buffer.append(ClassUtils.getShortClassName(getClass()));
214
215         buffer.append("[<");
216         buffer.append("url=\"" + url_.toExternalForm() + "\"");
217         buffer.append(", " + submitMethod_);
218         buffer.append(", " + encodingType_);
219         buffer.append(", " + requestParameters_);
220         buffer.append(", " + additionalHeaders_);
221         buffer.append(", " + credentialsProvider_);
222         buffer.append(">]");
223
224         return buffer.toString();
225     }
226
227 }
228
Popular Tags