Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/Authenticator/B4921848.java
38812 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 492184826* @library ../../../sun/net/www/httptest/27* @build HttpCallback TestHttpServer ClosedChannelList HttpTransaction28* @run main/othervm -Dhttp.auth.preference=basic B492184829* @summary Allow user control over authentication schemes30*/3132import java.io.*;33import java.net.*;3435public class B4921848 implements HttpCallback {3637static int count = 0;3839public void request (HttpTransaction req) {40try {41if (count == 0 ) {42req.addResponseHeader ("Connection", "close");43req.addResponseHeader ("WWW-Authenticate", "Basic realm=\"foo\"");44req.addResponseHeader ("WWW-Authenticate", "Digest realm=\"bar\" domain=/biz nonce=\"hereisanonce\"");45req.sendResponse (401, "Unauthorized");46req.orderlyClose();47} else {48String authheader = req.getRequestHeader ("Authorization");49if (authheader.startsWith ("Basic")) {50req.setResponseEntityBody ("Hello .");51req.sendResponse (200, "Ok");52req.orderlyClose();53} else {54req.sendResponse (400, "Bad Request");55req.orderlyClose();56}57}58count ++;59} catch (IOException e) {60e.printStackTrace();61}62}6364static void read (InputStream is) throws IOException {65int c;66System.out.println ("reading");67while ((c=is.read()) != -1) {68System.out.write (c);69}70System.out.println ("");71System.out.println ("finished reading");72}737475static void client (String u) throws Exception {76URL url = new URL (u);77System.out.println ("client opening connection to: " + u);78URLConnection urlc = url.openConnection ();79InputStream is = urlc.getInputStream ();80read (is);81is.close();82}8384static TestHttpServer server;8586public static void main (String[] args) throws Exception {87MyAuthenticator auth = new MyAuthenticator ();88Authenticator.setDefault (auth);89try {90server = new TestHttpServer (new B4921848(), 1, 10, 0);91System.out.println ("Server started: listening on port: " + server.getLocalPort());92client ("http://localhost:"+server.getLocalPort()+"/d1/d2/d3/foo.html");93} catch (Exception e) {94if (server != null) {95server.terminate();96}97throw e;98}99server.terminate();100}101102public static void except (String s) {103server.terminate();104throw new RuntimeException (s);105}106107static class MyAuthenticator extends Authenticator {108MyAuthenticator () {109super ();110}111112public PasswordAuthentication getPasswordAuthentication () {113return (new PasswordAuthentication ("user", "passwordNotCheckedAnyway".toCharArray()));114}115116}117118}119120121