Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/Authenticator/B4962064.java
38812 views
/*1* Copyright (c) 2004, 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 496206426* @library ../../../sun/net/www/httptest/27* @build HttpCallback TestHttpServer ClosedChannelList HttpTransaction28* @run main/othervm B496206429* @summary Extend Authenticator to provide access to request URI and server/proxy30*/3132import java.io.*;33import java.net.*;3435public class B4962064 implements HttpCallback {3637static int count = 0;3839public void request (HttpTransaction req) {40try {41switch (count) {42case 0:43req.addResponseHeader ("Connection", "close");44req.addResponseHeader ("WWW-Authenticate", "Basic realm=\"foo\"");45req.sendResponse (401, "Unauthorized");46req.orderlyClose();47break;48case 1:49case 3:50req.setResponseEntityBody ("Hello .");51req.sendResponse (200, "Ok");52req.orderlyClose();53break;54case 2:55req.addResponseHeader ("Connection", "close");56req.addResponseHeader ("Proxy-Authenticate", "Basic realm=\"foo\"");57req.sendResponse (407, "Proxy Authentication Required");58req.orderlyClose();59break;60}61count ++;62} catch (IOException e) {63e.printStackTrace();64}65}6667static void read (InputStream is) throws IOException {68int c;69System.out.println ("reading");70while ((c=is.read()) != -1) {71System.out.write (c);72}73System.out.println ("");74System.out.println ("finished reading");75}767778static void client (String u) throws Exception {79URL url = new URL (u);80System.out.println ("client opening connection to: " + u);81URLConnection urlc = url.openConnection ();82InputStream is = urlc.getInputStream ();83read (is);84is.close();85}8687static TestHttpServer server;88static URL urlsave;8990public static void main (String[] args) throws Exception {91try {92server = new TestHttpServer (new B4962064(), 1, 10, 0);93int port = server.getLocalPort();94System.setProperty ("http.proxyHost", "localhost");95System.setProperty ("http.proxyPort", Integer.toString (port));96MyAuthenticator auth = new MyAuthenticator ();97Authenticator.setDefault (auth);98System.out.println ("Server started: listening on port: " + port);99//String s = new String ("http://localhost:"+port+"/d1/d2/d3/foo.html");100String s = new String ("http://foo.com/d1/d2/d3/foo.html");101urlsave = new URL (s);102client (s);103//s = new String ("http://localhost:"+port+"/dr/d3/foo.html");104s = new String ("http://bar.com/dr/d3/foo.html");105urlsave = new URL (s);106client (s);107} catch (Exception e) {108if (server != null) {109server.terminate();110}111throw e;112}113server.terminate();114}115116public static void except (String s) {117server.terminate();118throw new RuntimeException (s);119}120121static class MyAuthenticator extends Authenticator {122int count = 0;123MyAuthenticator () {124super ();125}126127public PasswordAuthentication getPasswordAuthentication () {128URL url = getRequestingURL ();129if (!url.equals (urlsave)) {130except ("urls not equal");131}132Authenticator.RequestorType expected;133if (count == 0) {134expected = Authenticator.RequestorType.SERVER;135} else {136expected = Authenticator.RequestorType.PROXY;137}138if (getRequestorType() != expected) {139except ("wrong authtype");140}141count ++;142return (new PasswordAuthentication ("user", "passwordNotCheckedAnyway".toCharArray()));143}144145}146147}148149150