1/*
2 * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test
26 * @bug 4696512
27 * @summary HTTP client: Improve proxy server configuration and selection
28 * @modules java.base/sun.net.www
29 * @library ../../../sun/net/www/httptest/
30 * @build ClosedChannelList TestHttpServer HttpTransaction HttpCallback
31 * @compile ProxyTest.java
32 * @run main/othervm -Dhttp.proxyHost=inexistant -Dhttp.proxyPort=8080 ProxyTest
33 */
34
35import java.net.*;
36import java.io.*;
37import java.util.ArrayList;
38
39public class ProxyTest implements HttpCallback {
40    static TestHttpServer server;
41
42    public ProxyTest() {
43    }
44
45    public void request (HttpTransaction req) {
46        req.setResponseEntityBody ("Hello .");
47        try {
48            req.sendResponse (200, "Ok");
49            req.orderlyClose();
50        } catch (IOException e) {
51        }
52    }
53
54    static public class MyProxySelector extends ProxySelector {
55        private ProxySelector def = null;
56        private ArrayList<Proxy> noProxy;
57
58        public MyProxySelector() {
59            noProxy = new ArrayList<Proxy>(1);
60            noProxy.add(Proxy.NO_PROXY);
61        }
62
63        public java.util.List<Proxy> select(URI uri) {
64            return noProxy;
65        }
66
67        public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
68        }
69    }
70
71
72    public static void main(String[] args) {
73        ProxySelector defSelector = ProxySelector.getDefault();
74        if (defSelector == null)
75            throw new RuntimeException("Default ProxySelector is null");
76        ProxySelector.setDefault(new MyProxySelector());
77        try {
78            server = new TestHttpServer (new ProxyTest(), 1, 10, 0);
79            URL url = new URL("http://localhost:"+server.getLocalPort());
80            System.out.println ("client opening connection to: " + url);
81            HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
82            InputStream is = urlc.getInputStream ();
83            is.close();
84        } catch (Exception e) {
85                throw new RuntimeException(e);
86        } finally {
87            if (server != null) {
88                server.terminate();
89            }
90        }
91    }
92}
93