001: package com.meterware.httpunit;
002:
003: /********************************************************************************************************************
004: * $Id: WebConversation.java,v 1.38 2004/09/22 02:02:09 russgold Exp $
005: *
006: * Copyright (c) 2000-2004, 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.io.IOException;
024:
025: import java.net.HttpURLConnection;
026: import java.net.MalformedURLException;
027: import java.net.URL;
028: import java.net.URLConnection;
029:
030: import java.util.Dictionary;
031: import java.util.Enumeration;
032: import java.util.Properties;
033:
034: /**
035: * The context for a series of HTTP requests. This class manages cookies used to maintain
036: * session context, computes relative URLs, and generally emulates the browser behavior
037: * needed to build an automated test of a web site.
038: *
039: * @author Russell Gold
040: **/
041: public class WebConversation extends WebClient {
042:
043: private String _proxyHost;
044: private int _proxyPort;
045:
046: /**
047: * Creates a new web conversation.
048: **/
049: public WebConversation() {
050: }
051:
052: //---------------------------------- protected members --------------------------------
053:
054: /**
055: * Creates a web response object which represents the response to the specified web request.
056: **/
057: protected WebResponse newResponse(WebRequest request,
058: FrameSelector targetFrame) throws MalformedURLException,
059: IOException {
060: Properties savedProperties = (Properties) System
061: .getProperties().clone();
062: try {
063: if (_proxyHost != null) {
064: System.setProperty("proxyHost", _proxyHost);
065: System.setProperty("proxyPort", Integer
066: .toString(_proxyPort));
067: }
068: URLConnection connection = openConnection(getRequestURL(request));
069: if (HttpUnitOptions.isLoggingHttpHeaders()) {
070: String urlString = request.getURLString();
071: System.out.println("\nConnecting to "
072: + request.getURL().getHost());
073: System.out.println("Sending:: " + request.getMethod()
074: + " " + urlString);
075: }
076: sendHeaders(connection, getHeaderFields(request.getURL()));
077: sendHeaders(connection, request.getHeaderDictionary());
078: request.completeRequest(connection);
079: return new HttpWebResponse(this , targetFrame, request,
080: connection, getExceptionsThrownOnErrorStatus());
081: } finally {
082: System.setProperties(savedProperties);
083: }
084: }
085:
086: public void clearProxyServer() {
087: _proxyHost = null;
088: }
089:
090: public void setProxyServer(String proxyHost, int proxyPort) {
091: _proxyHost = proxyHost;
092: _proxyPort = proxyPort;
093: }
094:
095: private URL getRequestURL(WebRequest request)
096: throws MalformedURLException {
097: DNSListener dnsListener = getClientProperties()
098: .getDnsListener();
099: if (dnsListener == null)
100: return request.getURL();
101:
102: String hostName = request.getURL().getHost();
103: String portPortion = request.getURL().getPort() == -1 ? ""
104: : (":" + request.getURL().getPort());
105: setHeaderField("Host", hostName + portPortion);
106: String actualHost = dnsListener.getIpAddress(hostName);
107: if (HttpUnitOptions.isLoggingHttpHeaders())
108: System.out.println("Rerouting request to :: " + actualHost);
109: return new URL(request.getURL().getProtocol(), actualHost,
110: request.getURL().getPort(), request.getURL().getFile());
111: }
112:
113: //---------------------------------- private members --------------------------------
114:
115: private URLConnection openConnection(URL url)
116: throws MalformedURLException, IOException {
117: URLConnection connection = url.openConnection();
118: if (connection instanceof HttpURLConnection)
119: ((HttpURLConnection) connection)
120: .setInstanceFollowRedirects(false);
121: connection.setUseCaches(false);
122: return connection;
123: }
124:
125: private void sendHeaders(URLConnection connection,
126: Dictionary headers) {
127: for (Enumeration e = headers.keys(); e.hasMoreElements();) {
128: String key = (String) e.nextElement();
129: connection.setRequestProperty(key, (String) headers
130: .get(key));
131: if (HttpUnitOptions.isLoggingHttpHeaders()) {
132: if (key.equalsIgnoreCase("authorization")
133: || key.equalsIgnoreCase("proxy-authorization")) {
134: System.out.println("Sending:: " + key + ": "
135: + headers.get(key));
136: } else {
137: System.out.println("Sending:: " + key + ": "
138: + connection.getRequestProperty(key));
139: }
140: }
141: }
142: }
143: }
|