Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/meterware/httpunit/WebConversation.java


1   package com.meterware.httpunit;
2   /********************************************************************************************************************
3   * $Id: WebConversation.java,v 1.38 2004/09/22 02:02:09 russgold Exp $
4   *
5   * Copyright (c) 2000-2004, 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.io.IOException;
23  
24  import java.net.HttpURLConnection;
25  import java.net.MalformedURLException;
26  import java.net.URL;
27  import java.net.URLConnection;
28  
29  import java.util.Dictionary;
30  import java.util.Enumeration;
31  import java.util.Properties;
32  
33  
34  /**
35   * The context for a series of HTTP requests. This class manages cookies used to maintain
36   * session context, computes relative URLs, and generally emulates the browser behavior
37   * needed to build an automated test of a web site.
38   *
39   * @author Russell Gold
40   **/
41  public class WebConversation extends WebClient {
42  
43      private String _proxyHost;
44      private int _proxyPort;
45  
46  
47      /**
48       * Creates a new web conversation.
49       **/
50      public WebConversation() {
51      }
52  
53  
54  //---------------------------------- protected members --------------------------------
55  
56  
57      /**
58       * Creates a web response object which represents the response to the specified web request.
59       **/
60      protected WebResponse newResponse( WebRequest request, FrameSelector targetFrame ) throws MalformedURLException, IOException {
61          Properties savedProperties = (Properties) System.getProperties().clone();
62          try {
63              if (_proxyHost != null) {
64                  System.setProperty( "proxyHost", _proxyHost );
65                  System.setProperty( "proxyPort", Integer.toString( _proxyPort ) );
66              }
67              URLConnection connection = openConnection( getRequestURL( request ) );
68              if (HttpUnitOptions.isLoggingHttpHeaders()) {
69                  String urlString = request.getURLString();
70                  System.out.println( "\nConnecting to " + request.getURL().getHost() );
71                  System.out.println( "Sending:: " + request.getMethod() + " " + urlString );
72              }
73              sendHeaders( connection, getHeaderFields( request.getURL() ) );
74              sendHeaders( connection, request.getHeaderDictionary() );
75              request.completeRequest( connection );
76              return new HttpWebResponse( this, targetFrame, request, connection, getExceptionsThrownOnErrorStatus() );
77          } finally {
78              System.setProperties( savedProperties );
79          }
80      }
81  
82  
83      public void clearProxyServer() {
84          _proxyHost = null;
85      }
86  
87  
88      public void setProxyServer( String proxyHost, int proxyPort ) {
89          _proxyHost = proxyHost;
90          _proxyPort = proxyPort;
91      }
92  
93  
94      private URL getRequestURL( WebRequest request ) throws MalformedURLException {
95          DNSListener dnsListener = getClientProperties().getDnsListener();
96          if (dnsListener == null) return request.getURL();
97  
98          String hostName = request.getURL().getHost();
99          String portPortion = request.getURL().getPort() == -1 ? "" : (":" + request.getURL().getPort());
100         setHeaderField( "Host", hostName + portPortion );
101         String actualHost = dnsListener.getIpAddress( hostName );
102         if (HttpUnitOptions.isLoggingHttpHeaders()) System.out.println( "Rerouting request to :: " + actualHost );
103         return new URL( request.getURL().getProtocol(), actualHost, request.getURL().getPort(), request.getURL().getFile() );
104     }
105 
106 
107 //---------------------------------- private members --------------------------------
108 
109 
110     private URLConnection openConnection( URL url ) throws MalformedURLException, IOException {
111         URLConnection connection = url.openConnection();
112         if (connection instanceof HttpURLConnection) ((HttpURLConnection) connection).setInstanceFollowRedirects( false );
113         connection.setUseCaches( false );
114         return connection;
115     }
116 
117 
118     private void sendHeaders( URLConnection connection, Dictionary headers ) {
119         for (Enumeration e = headers.keys(); e.hasMoreElements();) {
120             String key = (String) e.nextElement();
121             connection.setRequestProperty( key, (String) headers.get( key ) );
122             if (HttpUnitOptions.isLoggingHttpHeaders()) {
123                 if (key.equalsIgnoreCase( "authorization" ) || key.equalsIgnoreCase( "proxy-authorization") ) {
124                     System.out.println( "Sending:: " + key + ": " + headers.get( key ) );
125                 } else {
126                     System.out.println( "Sending:: " + key + ": " + connection.getRequestProperty( key ) );
127                 }
128             }
129         }
130     }
131 }