KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.IOException JavaDoc;
41 import java.net.URL JavaDoc;
42 import java.util.List JavaDoc;
43 import java.util.Map JavaDoc;
44
45 import org.org.apache.commons.httpclient.HttpState;
46
47 /**
48  * An object that handles the actual communication portion of page
49  * retrieval/submission <p />
50  *
51  * THIS CLASS IS FOR INTERNAL USE ONLY
52  *
53  * @version $Revision: 100 $
54  * @author <a HREF="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
55  */

56 public abstract class WebConnection {
57     private final String JavaDoc proxyHost_;
58     private final int proxyPort_;
59     private final WebClient webClient_;
60
61
62     /**
63      * Create an instance that will not use a proxy server
64      *
65      * @param webClient The WebClient that is using this connection
66      */

67     public WebConnection( final WebClient webClient ) {
68         webClient_ = webClient;
69         proxyHost_ = null;
70         proxyPort_ = 0;
71     }
72
73
74     /**
75      * Create an instance that will use the specified proxy server
76      *
77      * @param proxyHost The server that will act as proxy
78      * @param proxyPort The port to use on the proxy server
79      * @param webClient The web client that is using this connection
80      */

81     public WebConnection( final WebClient webClient, final String JavaDoc proxyHost, final int proxyPort ) {
82         webClient_ = webClient;
83         proxyHost_ = proxyHost;
84         proxyPort_ = proxyPort;
85     }
86
87     /**
88      * Submit a request and retrieve a response
89      *
90      * @param webRequestSettings Settings to make the request with
91      * @return See above
92      * @exception IOException If an IO error occurs
93      */

94     public abstract WebResponse getResponse(final WebRequestSettings webRequestSettings)
95         throws IOException JavaDoc;
96
97     /**
98      * Submit a request and retrieve a response
99      *
100      * @param parameters Any parameters
101      * @param url The url of the server
102      * @param submitMethod The submit method. Ie SubmitMethod.GET
103      * @param requestHeaders Any headers that need to be put into the request.
104      * @return See above
105      * @exception IOException If an IO error occurs
106      * @deprecated Use {@link #getResponse(WebRequestSettings)}
107      */

108     public WebResponse getResponse(
109             final URL JavaDoc url,
110             final SubmitMethod submitMethod,
111             final List JavaDoc parameters,
112             final Map JavaDoc requestHeaders )
113         throws
114             IOException JavaDoc {
115         final WebRequestSettings wrs = new WebRequestSettings(url);
116         wrs.setSubmitMethod(submitMethod);
117         wrs.setRequestParameters(parameters);
118         wrs.setAdditionalHeaders(requestHeaders);
119         return getResponse(wrs);
120     }
121
122     /**
123      * Submit a request and retrieve a response
124      *
125      * @param parameters Any parameters
126      * @param url The url of the server
127      * @param encType Encoding type of the form when done as a POST
128      * @param submitMethod The submit method. Ie SubmitMethod.GET
129      * @param requestHeaders Any headers that need to be put into the request.
130      * @return See above
131      * @exception IOException If an IO error occurs
132      * @deprecated Use {@link #getResponse(WebRequestSettings)}
133      */

134     public WebResponse getResponse(
135             final URL JavaDoc url,
136             final FormEncodingType encType,
137             final SubmitMethod submitMethod,
138             final List JavaDoc parameters,
139             final Map JavaDoc requestHeaders )
140         throws
141             IOException JavaDoc {
142         final WebRequestSettings wrs = new WebRequestSettings(url);
143         wrs.setEncodingType(encType);
144         wrs.setSubmitMethod(submitMethod);
145         wrs.setRequestParameters(parameters);
146         wrs.setAdditionalHeaders(requestHeaders);
147         return getResponse(wrs);
148     }
149
150     /**
151      * Return the web client
152      * @return The web client.
153      */

154     public final WebClient getWebClient() {
155         return webClient_;
156     }
157
158
159     /**
160      * Return the proxy host
161      * @return The proxy host.
162      */

163     public final String JavaDoc getProxyHost() {
164         return proxyHost_;
165     }
166
167
168     /**
169      * Return the proxy port.
170      * @return The proxy port.
171      */

172     public final int getProxyPort() {
173         return proxyPort_;
174     }
175
176
177     /**
178      * Return the {@link HttpState} that is being used for a given domain
179      * @param url The url from which the domain will be determined
180      * @return The state or null if no state can be found for this domain.
181      */

182     public abstract HttpState getStateForUrl( final URL JavaDoc url );
183 }
184
185
Popular Tags