Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/Authenticator/B4933582.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*/2223import java.io.*;24import java.net.*;25import java.util.*;26import sun.net.www.protocol.http.*;2728public class B4933582 implements HttpCallback {2930static int count = 0;31static String authstring;3233void errorReply (HttpTransaction req, String reply) throws IOException {34req.addResponseHeader ("Connection", "close");35req.addResponseHeader ("WWW-Authenticate", reply);36req.sendResponse (401, "Unauthorized");37req.orderlyClose();38}3940void okReply (HttpTransaction req) throws IOException {41req.setResponseEntityBody ("Hello .");42req.sendResponse (200, "Ok");43req.orderlyClose();44}4546static boolean firstTime = true;4748public void request (HttpTransaction req) {49try {50authstring = req.getRequestHeader ("Authorization");51if (firstTime) {52switch (count) {53case 0:54errorReply (req, "Basic realm=\"wallyworld\"");55break;56case 1:57/* client stores a username/pw for wallyworld58*/59save (authstring);60okReply (req);61break;62}63} else {64/* check the auth string is premptively set from last time */65String savedauth = retrieve();66if (savedauth.equals (authstring)) {67okReply (req);68} else {69System.out.println ("savedauth = " + savedauth);70System.out.println ("authstring = " + authstring);71errorReply (req, "Basic realm=\"wallyworld\"");72}73}74count ++;75} catch (IOException e) {76e.printStackTrace();77}78}7980void save (String s) {81try {82FileOutputStream f = new FileOutputStream ("auth.save");83ObjectOutputStream os = new ObjectOutputStream (f);84os.writeObject (s);85} catch (IOException e) {86assert false;87}88}8990String retrieve () {91String s = null;92try {93FileInputStream f = new FileInputStream ("auth.save");94ObjectInputStream is = new ObjectInputStream (f);95s = (String) is.readObject();96} catch (Exception e) {97assert false;98}99return s;100}101102static void read (InputStream is) throws IOException {103int c;104System.out.println ("reading");105while ((c=is.read()) != -1) {106System.out.write (c);107}108System.out.println ("");109System.out.println ("finished reading");110}111112static void client (String u) throws Exception {113URL url = new URL (u);114System.out.println ("client opening connection to: " + u);115URLConnection urlc = url.openConnection ();116InputStream is = urlc.getInputStream ();117read (is);118is.close();119}120121static TestHttpServer server;122123public static void main (String[] args) throws Exception {124firstTime = args[0].equals ("first");125MyAuthenticator auth = new MyAuthenticator ();126Authenticator.setDefault (auth);127CacheImpl cache;128try {129if (firstTime) {130server = new TestHttpServer (new B4933582(), 1, 10, 0);131cache = new CacheImpl (server.getLocalPort());132} else {133cache = new CacheImpl ();134server = new TestHttpServer(new B4933582(), 1, 10, cache.getPort());135}136AuthCacheValue.setAuthCache (cache);137System.out.println ("Server: listening on port: " + server.getLocalPort());138client ("http://localhost:"+server.getLocalPort()+"/d1/foo.html");139} catch (Exception e) {140if (server != null) {141server.terminate();142}143throw e;144}145int f = auth.getCount();146if (firstTime && f != 1) {147except ("Authenticator was called "+f+" times. Should be 1");148}149if (!firstTime && f != 0) {150except ("Authenticator was called "+f+" times. Should be 0");151}152server.terminate();153}154155public static void except (String s) {156server.terminate();157throw new RuntimeException (s);158}159160static class MyAuthenticator extends Authenticator {161MyAuthenticator () {162super ();163}164165int count = 0;166167public PasswordAuthentication getPasswordAuthentication () {168PasswordAuthentication pw;169pw = new PasswordAuthentication ("user", "pass1".toCharArray());170count ++;171return pw;172}173174public int getCount () {175return (count);176}177}178179static class CacheImpl extends AuthCacheImpl {180HashMap map;181int port; // need to store the port number the server is using182183CacheImpl () throws IOException {184this (-1);185}186187CacheImpl (int port) throws IOException {188super();189this.port = port;190File src = new File ("cache.ser");191if (src.exists()) {192ObjectInputStream is = new ObjectInputStream (193new FileInputStream (src)194);195try {196map = (HashMap)is.readObject ();197this.port = (Integer)is.readObject ();198System.out.println ("read port from file " + port);199} catch (ClassNotFoundException e) {200assert false;201}202is.close();203System.out.println ("setMap from cache.ser");204} else {205map = new HashMap();206}207setMap (map);208}209210int getPort () {211return port;212}213214private void writeMap () {215try {216File dst = new File ("cache.ser");217dst.delete();218if (!dst.createNewFile()) {219return;220}221ObjectOutputStream os = new ObjectOutputStream (222new FileOutputStream (dst)223);224os.writeObject(map);225os.writeObject(port);226System.out.println ("wrote port " + port);227os.close();228} catch (IOException e) {}229}230231public void put (String pkey, AuthCacheValue value) {232System.out.println ("put: " + pkey + " " + value);233super.put (pkey, value);234writeMap();235}236237public AuthCacheValue get (String pkey, String skey) {238System.out.println ("get: " + pkey + " " + skey);239AuthCacheValue i = super.get (pkey, skey);240System.out.println ("---> " + i);241return i;242}243244public void remove (String pkey, AuthCacheValue value) {245System.out.println ("remove: " + pkey + " " + value);246super.remove (pkey, value);247writeMap();248}249}250}251252253