jdk/test/sun/net/www/AuthHeaderTest.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 13788 3f38e525f30a
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4804309
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @library ../../../sun/net/www/httptest/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @build HttpCallback HttpServer ClosedChannelList HttpTransaction
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @run main AuthHeaderTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @summary AuthHeaderTest bug
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
public class AuthHeaderTest implements HttpCallback {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    static int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    static String authstring;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    void errorReply (HttpTransaction req, String reply) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        req.addResponseHeader ("Connection", "close");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        req.addResponseHeader ("Www-authenticate", reply);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        req.sendResponse (401, "Unauthorized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        req.orderlyClose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    void okReply (HttpTransaction req) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        req.setResponseEntityBody ("Hello .");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        req.sendResponse (200, "Ok");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        req.orderlyClose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    public void request (HttpTransaction req) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            authstring = req.getRequestHeader ("Authorization");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            System.out.println (authstring);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            switch (count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                errorReply (req, "Basic realm=\"wallyworld\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                /* client stores a username/pw for wallyworld
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                okReply (req);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            count ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    static void read (InputStream is) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        int c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        System.out.println ("reading");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        while ((c=is.read()) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            System.out.write (c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        System.out.println ("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        System.out.println ("finished reading");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    static void client (String u) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        URL url = new URL (u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        System.out.println ("client opening connection to: " + u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        URLConnection urlc = url.openConnection ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        InputStream is = urlc.getInputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        read (is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    static HttpServer server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    public static void main (String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        MyAuthenticator auth = new MyAuthenticator ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        Authenticator.setDefault (auth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            server = new HttpServer (new AuthHeaderTest(), 1, 10, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            System.out.println ("Server: listening on port: " + server.getLocalPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            client ("http://localhost:"+server.getLocalPort()+"/d1/foo.html");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            if (server != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                server.terminate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        int f = auth.getCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        if (f != 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            except ("Authenticator was called "+f+" times. Should be 1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        server.terminate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    public static void except (String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        server.terminate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        throw new RuntimeException (s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    static class MyAuthenticator extends Authenticator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        MyAuthenticator () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            super ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        public PasswordAuthentication getPasswordAuthentication () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            PasswordAuthentication pw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            pw = new PasswordAuthentication ("user", "pass2".toCharArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            count ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            return pw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        public int getCount () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            return (count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
}