jdk/test/java/net/URL/PerConnectionProxy.java
author juh
Tue, 25 Jun 2013 14:41:46 -0700
changeset 18552 005e115dc6ee
parent 14342 8435a30053c1
child 30820 0d4717a011d3
permissions -rw-r--r--
8017326: Cleanup of the javadoc <code> tag in java.security.spec Summary: Convert javadoc <code> and <tt> tags to {@code ...} Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 13788
diff changeset
     2
 * Copyright (c) 2003, 2012, 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: 4347
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4347
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4347
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
 * @bug 4920526
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary Needs per connection proxy support for URLs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @library ../../../sun/net/www/httptest/
13788
3f38e525f30a 6354758: rename old test HttpServer classes
chegar
parents: 5506
diff changeset
    28
 * @build ClosedChannelList TestHttpServer HttpTransaction HttpCallback
4347
ab0a9f495844 6907177: Update jdk tests to remove unncessary -source and -target options
darcy
parents: 2
diff changeset
    29
 * @compile PerConnectionProxy.java
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @run main/othervm -Dhttp.proxyHost=inexistant -Dhttp.proxyPort=8080 PerConnectionProxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.net.www.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public class PerConnectionProxy implements HttpCallback {
13788
3f38e525f30a 6354758: rename old test HttpServer classes
chegar
parents: 5506
diff changeset
    38
    static TestHttpServer server;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    public void request (HttpTransaction req) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        req.setResponseEntityBody ("Hello .");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
            req.sendResponse (200, "Ok");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
            req.orderlyClose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        try {
13788
3f38e525f30a 6354758: rename old test HttpServer classes
chegar
parents: 5506
diff changeset
    51
            server = new TestHttpServer (new PerConnectionProxy(), 1, 10, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            ProxyServer pserver = new ProxyServer(InetAddress.getByName("localhost"), server.getLocalPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            // start proxy server
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            new Thread(pserver).start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            URL url = new URL("http://localhost:"+server.getLocalPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            // for non existing proxy expect an IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                InetSocketAddress isa = InetSocketAddress.createUnresolved("inexistent", 8080);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                Proxy proxy = new Proxy(Proxy.Type.HTTP, isa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                HttpURLConnection urlc = (HttpURLConnection)url.openConnection (proxy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                InputStream is = urlc.getInputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                throw new RuntimeException("non existing per connection proxy should lead to IOException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            } catch (IOException ioex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                // expected
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            // for NO_PROXY, expect direct connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                HttpURLConnection urlc = (HttpURLConnection)url.openConnection (Proxy.NO_PROXY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                int respCode = urlc.getResponseCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                urlc.disconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            } catch (IOException ioex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                throw new RuntimeException("direct connection should succeed :"+ioex.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            // for a normal proxy setting expect to see connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            // goes through that proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                InetSocketAddress isa = InetSocketAddress.createUnresolved("localhost", pserver.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                Proxy p = new Proxy(Proxy.Type.HTTP, isa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                HttpURLConnection urlc = (HttpURLConnection)url.openConnection (p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                int respCode = urlc.getResponseCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                urlc.disconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            } catch (IOException ioex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                throw new RuntimeException("connection through a local proxy should succeed :"+ioex.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            throw new RuntimeException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            if (server != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                server.terminate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    static class ProxyServer extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        private static ServerSocket ss = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        // client requesting for a tunnel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        private Socket clientSocket = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
         * Origin server's address and port that the client
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
         * wants to establish the tunnel for communication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        private InetAddress serverInetAddr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        private int     serverPort;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        public ProxyServer(InetAddress server, int port) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            serverInetAddr = server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            serverPort = port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            ss = new ServerSocket(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                clientSocket = ss.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                processRequests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                System.out.println("Proxy Failed: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    ss.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                catch (IOException excep) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                    System.out.println("ProxyServer close error: " + excep);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                    excep.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        private void processRequests() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            // connection set to the tunneling mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            Socket serverSocket = new Socket(serverInetAddr, serverPort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            ProxyTunnel clientToServer = new ProxyTunnel(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                                                         clientSocket, serverSocket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            ProxyTunnel serverToClient = new ProxyTunnel(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                                                         serverSocket, clientSocket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            clientToServer.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            serverToClient.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            System.out.println("Proxy: Started tunneling.......");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            clientToServer.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            serverToClient.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            System.out.println("Proxy: Finished tunneling........");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            clientToServer.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            serverToClient.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
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
*                       helper methods follow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
***************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        public int getPort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            return ss.getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
         * This inner class provides unidirectional data flow through the sockets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
         * by continuously copying bytes from the input socket onto the output
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
         * socket, until both sockets are open and EOF has not been received.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        static class ProxyTunnel extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            Socket sockIn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            Socket sockOut;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            InputStream input;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            OutputStream output;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            public ProxyTunnel(Socket sockIn, Socket sockOut)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                this.sockIn = sockIn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                this.sockOut = sockOut;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                input = sockIn.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                output = sockOut.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                int BUFFER_SIZE = 400;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                byte[] buf = new byte[BUFFER_SIZE];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                int bytesRead = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                int count = 0;  // keep track of the amount of data transfer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                    while ((bytesRead = input.read(buf)) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                        output.write(buf, 0, bytesRead);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                        output.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                        count += bytesRead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                     * The peer end has closed the connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                     * we will close the tunnel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                    close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            public void close() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                    if (!sockIn.isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                        sockIn.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                    if (!sockOut.isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                        sockOut.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                } catch (IOException ignored) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
}