Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/ProxySelector/ProxyTest.java
38811 views
/*1* Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 469651226* @summary HTTP client: Improve proxy server configuration and selection27* @library ../../../sun/net/www/httptest/28* @build ClosedChannelList TestHttpServer HttpTransaction HttpCallback29* @compile ProxyTest.java30* @run main/othervm -Dhttp.proxyHost=inexistant -Dhttp.proxyPort=8080 ProxyTest31*/3233import java.net.*;34import java.io.*;35import java.util.ArrayList;3637public class ProxyTest implements HttpCallback {38static TestHttpServer server;3940public ProxyTest() {41}4243public void request (HttpTransaction req) {44req.setResponseEntityBody ("Hello .");45try {46req.sendResponse (200, "Ok");47req.orderlyClose();48} catch (IOException e) {49}50}5152static public class MyProxySelector extends ProxySelector {53private ProxySelector def = null;54private ArrayList<Proxy> noProxy;5556public MyProxySelector() {57noProxy = new ArrayList<Proxy>(1);58noProxy.add(Proxy.NO_PROXY);59}6061public java.util.List<Proxy> select(URI uri) {62return noProxy;63}6465public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {66}67}686970public static void main(String[] args) {71ProxySelector defSelector = ProxySelector.getDefault();72if (defSelector == null)73throw new RuntimeException("Default ProxySelector is null");74ProxySelector.setDefault(new MyProxySelector());75try {76server = new TestHttpServer (new ProxyTest(), 1, 10, 0);77URL url = new URL("http://localhost:"+server.getLocalPort());78System.out.println ("client opening connection to: " + url);79HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();80InputStream is = urlc.getInputStream ();81is.close();82} catch (Exception e) {83throw new RuntimeException(e);84} finally {85if (server != null) {86server.terminate();87}88}89}90}919293