jdk/test/sun/security/ssl/sun/net/www/protocol/https/HttpsURLConnection/CookieHandlerTest.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 7990 57019dc81b66
child 10328 06c93c42bca0
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 7990
diff changeset
     2
 * Copyright (c) 2003, 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
/* @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @summary Unit test for java.net.CookieHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4696506 4942650
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @author Yingxian Wang
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
public class CookieHandlerTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    static Map<String,String> cookies;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    ServerSocket ss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
     * =============================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
     * Set the various variables needed for the tests, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
     * specify what tests to run on each side.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * Should we run the client or server in a separate thread?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * Both sides can throw exceptions, but do you have a preference
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * as to which side should be the main thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    static boolean separateServerThread = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * Where do we find the keystores?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    static String pathToStores = "../../../../../../etc";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    static String keyStoreFile = "keystore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    static String trustStoreFile = "truststore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    static String passwd = "passphrase";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * Is the server ready to serve?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    volatile static boolean serverReady = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * Turn on SSL debugging?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    static boolean debug = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * Define the server side of the test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * If the server prematurely exits, serverReady will be set to true
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * to avoid infinite hangs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    void doServerSide() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        SSLServerSocketFactory sslssf =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        SSLServerSocket sslServerSocket =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            (SSLServerSocket) sslssf.createServerSocket(serverPort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        serverPort = sslServerSocket.getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
         * Signal Client, we're ready for his connect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        serverReady = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        SSLSocket sslSocket = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            sslSocket = (SSLSocket) sslServerSocket.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            // check request contains "Cookie"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            InputStream is = sslSocket.getInputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            BufferedReader r = new BufferedReader(new InputStreamReader(is));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            boolean flag = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            String x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            while ((x=r.readLine()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                if (x.length() ==0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                String header = "Cookie: ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                if (x.startsWith(header)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                    if (x.equals("Cookie: "+((String)cookies.get("Cookie")))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                        flag = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            if (!flag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                throw new RuntimeException("server should see cookie in request");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            PrintStream out = new PrintStream(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                                 new BufferedOutputStream(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                                    sslSocket.getOutputStream() ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            /* send the header */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            out.print("HTTP/1.1 200 OK\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            out.print("Set-Cookie2: "+((String)cookies.get("Set-Cookie2")+"\r\n"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            out.print("Content-Type: text/html; charset=iso-8859-1\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            out.print("Connection: close\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            out.print("\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            out.print("<HTML>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            out.print("<HEAD><TITLE>Testing cookie</TITLE></HEAD>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            out.print("<BODY>OK.</BODY>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            out.print("</HTML>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            sslSocket.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            sslServerSocket.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * Define the client side of the test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * If the server prematurely exits, serverReady will be set to true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * to avoid infinite hangs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    void doClientSide() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
         * Wait for server to get started.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        while (!serverReady) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            Thread.sleep(50);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        HttpsURLConnection http = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        /* establish http connection to server */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        String uri = "https://localhost:" + +serverPort ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        URL url = new URL(uri);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        HttpsURLConnection.setDefaultHostnameVerifier(new NameVerifier());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        http = (HttpsURLConnection)url.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        int respCode = http.getResponseCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        http.disconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    static class NameVerifier implements HostnameVerifier {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        public boolean verify(String hostname, SSLSession session) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * =============================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * The remainder is just support stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    // use any free port by default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    volatile int serverPort = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    volatile Exception serverException = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    volatile Exception clientException = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        String keyFilename =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            System.getProperty("test.src", "./") + "/" + pathToStores +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                "/" + keyStoreFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        String trustFilename =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            System.getProperty("test.src", "./") + "/" + pathToStores +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                "/" + trustStoreFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        System.setProperty("javax.net.ssl.keyStore", keyFilename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        System.setProperty("javax.net.ssl.keyStorePassword", passwd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        System.setProperty("javax.net.ssl.trustStore", trustFilename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        System.setProperty("javax.net.ssl.trustStorePassword", passwd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        if (debug)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            System.setProperty("javax.net.debug", "all");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
         * Start the tests.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        cookies = new HashMap<String, String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        cookies.put("Cookie",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
              "$Version=\"1\"; Customer=\"WILE_E_COYOTE\"; $Path=\"/acme\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        cookies.put("Set-Cookie2",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
          "$Version=\"1\"; Part_Number=\"Riding_Rocket_0023\"; " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
          "$Path=\"/acme/ammo\"; Part_Number=\"Rocket_Launcher_0001\"; "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
          "$Path=\"/acme\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        CookieHandler.setDefault(new MyCookieHandler());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        new CookieHandlerTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    Thread clientThread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    Thread serverThread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * Primary constructor, used to drive remainder of the test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * Fork off the other side, then do your work.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    CookieHandlerTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        if (separateServerThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            startServer(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            startClient(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            startClient(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            startServer(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
         * Wait for other side to close down.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        if (separateServerThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            serverThread.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            clientThread.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
         * When we get here, the test is pretty much over.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
         * If the main thread excepted, that propagates back
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
         * immediately.  If the other thread threw an exception, we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
         * should report back.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        if (serverException != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            throw serverException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        if (clientException != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            throw clientException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        if (!getCalled || !putCalled) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            throw new RuntimeException ("Either get or put method is not called");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    void startServer(boolean newThread) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        if (newThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            serverThread = new Thread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                        doServerSide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                    } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                         * Our server thread just died.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                         * Release the client, if not active already...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                        System.err.println("Server died...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                        serverReady = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                        serverException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            serverThread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            doServerSide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    void startClient(boolean newThread) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        if (newThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            clientThread = new Thread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                        doClientSide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                    } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                         * Our client thread just died.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                        System.err.println("Client died...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                        clientException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            clientThread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            doClientSide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    static boolean getCalled = false, putCalled = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    static class MyCookieHandler extends CookieHandler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        public Map<String,List<String>>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            get(URI uri, Map<String,List<String>> requestHeaders)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            getCalled = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            // returns cookies[0]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            // they will be include in request
7990
57019dc81b66 7012003: diamond conversion for ssl
smarks
parents: 5506
diff changeset
   303
            Map<String,List<String>> map = new HashMap<>();
57019dc81b66 7012003: diamond conversion for ssl
smarks
parents: 5506
diff changeset
   304
            List<String> l = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            l.add(cookies.get("Cookie"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            map.put("Cookie",l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            return Collections.unmodifiableMap(map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        public void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            put(URI uri, Map<String,List<String>> responseHeaders)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            putCalled = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            // check response has cookies[1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            List<String> l = responseHeaders.get("Set-Cookie2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            String value = l.get(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            if (!value.equals((String)cookies.get("Set-Cookie2"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                throw new RuntimeException("cookie should be available for handle to put into cache");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
               }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
}