jdk/test/sun/net/www/protocol/https/HttpsClient/ProxyAuthTest.java
author chegar
Mon, 18 Jul 2016 08:28:48 +0100
changeset 41579 c0fe2e6364d9
parent 30820 0d4717a011d3
child 41595 f1213215e135
permissions -rw-r--r--
8160838: Better HTTP service Reviewed-by: ahgross, alanb, michaelm
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
     2
 * Copyright (c) 2001, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
41579
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 30820
diff changeset
    26
 * @bug 4323990 4413069 8160838
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary HttpsURLConnection doesn't send Proxy-Authorization on CONNECT
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    28
 *     Incorrect checking of proxy server response
30820
0d4717a011d3 8081347: Add @modules to jdk_core tests
mchung
parents: 23052
diff changeset
    29
 * @modules java.base/sun.net.www
41579
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 30820
diff changeset
    30
 * @run main/othervm ProxyAuthTest fail
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 30820
diff changeset
    31
 * @run main/othervm -Djdk.http.auth.tunneling.disabledSchemes=Basic ProxyAuthTest fail
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 30820
diff changeset
    32
 * @run main/othervm -Djdk.http.auth.tunneling.disabledSchemes=Basic, ProxyAuthTest fail
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 30820
diff changeset
    33
 * @run main/othervm -Djdk.http.auth.tunneling.disabledSchemes=BAsIc ProxyAuthTest fail
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 30820
diff changeset
    34
 * @run main/othervm -Djdk.http.auth.tunneling.disabledSchemes=Basic,Digest ProxyAuthTest fail
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 30820
diff changeset
    35
 * @run main/othervm -Djdk.http.auth.tunneling.disabledSchemes=Unknown,bAsIc ProxyAuthTest fail
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 30820
diff changeset
    36
 * @run main/othervm -Djdk.http.auth.tunneling.disabledSchemes= ProxyAuthTest succeed
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 30820
diff changeset
    37
 * @run main/othervm -Djdk.http.auth.tunneling.disabledSchemes=Digest,NTLM,Negotiate ProxyAuthTest succeed
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 30820
diff changeset
    38
 * @run main/othervm -Djdk.http.auth.tunneling.disabledSchemes=UNKNOWN,notKnown ProxyAuthTest succeed
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
41579
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 30820
diff changeset
    41
// No way to reserve and restore java.lang.Authenticator, as well as read-once
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 30820
diff changeset
    42
// system properties, so this tests needs to run in othervm mode.
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 30820
diff changeset
    43
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.security.KeyStore;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import javax.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.security.cert.*;
41579
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 30820
diff changeset
    50
import static java.nio.charset.StandardCharsets.US_ASCII;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * ProxyAuthTest.java -- includes a simple server that can serve
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * Http get request in both clear and secure channel, and a client
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * that makes https requests behind the firewall through an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * authentication proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
public class ProxyAuthTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * Where do we find the keystores?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     */
23052
241885315119 8032473: Restructure JSSE regression test hierarchy in jdk test
xuelei
parents: 10328
diff changeset
    63
    static String pathToStores = "../../../../../../javax/net/ssl/etc";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    static String keyStoreFile = "keystore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    static String trustStoreFile = "truststore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    static String passwd = "passphrase";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    volatile private static int serverPort = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * The TestServer implements a OriginServer that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * processes HTTP requests and responses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    static class TestServer extends OriginServer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        public TestServer(ServerSocket ss) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            super(ss);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
         * Returns an array of bytes containing the bytes for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
         * the data sent in the response.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
         * @return bytes for the data in the response
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        public byte[] getBytes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            return "Proxy authentication for tunneling succeeded ..".
41579
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 30820
diff changeset
    87
                        getBytes(US_ASCII);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * Main method to create the server and the client
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    94
    public static void main(String args[]) throws Exception {
41579
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 30820
diff changeset
    95
        boolean expectSuccess;
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 30820
diff changeset
    96
        if (args[0].equals("succeed")) {
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 30820
diff changeset
    97
            expectSuccess = true;
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 30820
diff changeset
    98
        } else {
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 30820
diff changeset
    99
            expectSuccess = false;
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 30820
diff changeset
   100
        }
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 30820
diff changeset
   101
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        String keyFilename =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            System.getProperty("test.src", "./") + "/" + pathToStores +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                "/" + keyStoreFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        String trustFilename =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            System.getProperty("test.src", "./") + "/" + pathToStores +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                "/" + trustStoreFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        System.setProperty("javax.net.ssl.keyStore", keyFilename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        System.setProperty("javax.net.ssl.keyStorePassword", passwd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        System.setProperty("javax.net.ssl.trustStore", trustFilename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        System.setProperty("javax.net.ssl.trustStorePassword", passwd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        boolean useSSL = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
         * setup the server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
         */
41579
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 30820
diff changeset
   118
        Closeable server;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            ServerSocketFactory ssf =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                ProxyAuthTest.getServerSocketFactory(useSSL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            ServerSocket ss = ssf.createServerSocket(serverPort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            serverPort = ss.getLocalPort();
41579
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 30820
diff changeset
   124
            server = new TestServer(ss);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            System.out.println("Server side failed:" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                                e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        // trigger the client
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            doClientSide();
41579
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 30820
diff changeset
   133
            if (!expectSuccess) {
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 30820
diff changeset
   134
                throw new RuntimeException(
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 30820
diff changeset
   135
                        "Expected exception/failure to connect, but succeeded.");
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 30820
diff changeset
   136
            }
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 30820
diff changeset
   137
        } catch (IOException e) {
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 30820
diff changeset
   138
            if (expectSuccess) {
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 30820
diff changeset
   139
                System.out.println("Client side failed: " + e.getMessage());
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 30820
diff changeset
   140
                throw e;
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 30820
diff changeset
   141
            }
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 30820
diff changeset
   142
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 30820
diff changeset
   143
            if (! (e.getMessage().contains("Unable to tunnel through proxy") &&
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 30820
diff changeset
   144
                   e.getMessage().contains("407")) ) {
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 30820
diff changeset
   145
                throw new RuntimeException(
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 30820
diff changeset
   146
                        "Expected exception about cannot tunnel, 407, etc, but got", e);
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 30820
diff changeset
   147
            } else {
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 30820
diff changeset
   148
                // Informative
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 30820
diff changeset
   149
                System.out.println("Caught expected exception: " + e.getMessage());
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 30820
diff changeset
   150
            }
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 30820
diff changeset
   151
        } finally {
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 30820
diff changeset
   152
            if (server != null)
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 30820
diff changeset
   153
                server.close();
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   154
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    private static ServerSocketFactory getServerSocketFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                   (boolean useSSL) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        if (useSSL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            SSLServerSocketFactory ssf = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            // set up key manager to do server authentication
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            SSLContext ctx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            KeyManagerFactory kmf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            KeyStore ks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            char[] passphrase = passwd.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            ctx = SSLContext.getInstance("TLS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            kmf = KeyManagerFactory.getInstance("SunX509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            ks = KeyStore.getInstance("JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            ks.load(new FileInputStream(System.getProperty(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                        "javax.net.ssl.keyStore")), passphrase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            kmf.init(ks, passphrase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            ctx.init(kmf.getKeyManagers(), null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            ssf = ctx.getServerSocketFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            return ssf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            return ServerSocketFactory.getDefault();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
41579
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 30820
diff changeset
   183
    static void doClientSide() throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
         * setup up a proxy with authentication information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
         */
41579
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 30820
diff changeset
   187
        ProxyTunnelServer ps = setupProxy();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
         * we want to avoid URLspoofCheck failures in cases where the cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
         * DN name does not match the hostname in the URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        HttpsURLConnection.setDefaultHostnameVerifier(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                                      new NameVerifier());
41579
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 30820
diff changeset
   195
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 30820
diff changeset
   196
        InetSocketAddress paddr = new InetSocketAddress("localhost", ps.getPort());
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 30820
diff changeset
   197
        Proxy proxy = new Proxy(Proxy.Type.HTTP, paddr);
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 30820
diff changeset
   198
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        URL url = new URL("https://" + "localhost:" + serverPort
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                                + "/index.html");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        BufferedReader in = null;
41579
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 30820
diff changeset
   202
        HttpsURLConnection uc = (HttpsURLConnection) url.openConnection(proxy);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        try {
41579
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 30820
diff changeset
   204
            in = new BufferedReader(new InputStreamReader(uc.getInputStream()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            String inputLine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            System.out.print("Client recieved from the server: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            while ((inputLine = in.readLine()) != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                System.out.println(inputLine);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            in.close();
41579
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 30820
diff changeset
   210
        } catch (IOException e) {
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 30820
diff changeset
   211
            // Assert that the error stream is not accessible from the failed
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 30820
diff changeset
   212
            // tunnel setup.
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 30820
diff changeset
   213
            if (uc.getErrorStream() != null) {
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 30820
diff changeset
   214
                throw new RuntimeException("Unexpected error stream.");
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 30820
diff changeset
   215
            }
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 30820
diff changeset
   216
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            if (in != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    static class NameVerifier implements HostnameVerifier {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        public boolean verify(String hostname, SSLSession session) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
41579
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 30820
diff changeset
   229
    static ProxyTunnelServer setupProxy() throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        ProxyTunnelServer pserver = new ProxyTunnelServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
         * register a system wide authenticator and setup the proxy for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
         * authentication
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        Authenticator.setDefault(new TestAuthenticator());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        // register with the username and password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        pserver.needUserAuth(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        pserver.setUserAuth("Test", "test123");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        pserver.start();
41579
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 30820
diff changeset
   242
        return pserver;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    public static class TestAuthenticator extends Authenticator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        public PasswordAuthentication getPasswordAuthentication() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            return new PasswordAuthentication("Test",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                                         "test123".toCharArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
}