author | alanb |
Fri, 02 Nov 2012 15:50:11 +0000 | |
changeset 14342 | 8435a30053c1 |
parent 13788 | 3f38e525f30a |
child 30820 | 0d4717a011d3 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
14342
8435a30053c1
7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents:
13788
diff
changeset
|
2 |
* Copyright (c) 2004, 2012, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
5506 | 19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
2 | 22 |
*/ |
23 |
||
24 |
/** |
|
25 |
* @test |
|
26 |
* @bug 4962064 |
|
27 |
* @library ../../../sun/net/www/httptest/ |
|
13788 | 28 |
* @build HttpCallback TestHttpServer ClosedChannelList HttpTransaction |
2 | 29 |
* @run main/othervm B4962064 |
30 |
* @summary Extend Authenticator to provide access to request URI and server/proxy |
|
31 |
*/ |
|
32 |
||
33 |
import java.io.*; |
|
34 |
import java.net.*; |
|
35 |
||
36 |
public class B4962064 implements HttpCallback { |
|
37 |
||
38 |
static int count = 0; |
|
39 |
||
40 |
public void request (HttpTransaction req) { |
|
41 |
try { |
|
42 |
switch (count) { |
|
43 |
case 0: |
|
44 |
req.addResponseHeader ("Connection", "close"); |
|
45 |
req.addResponseHeader ("WWW-Authenticate", "Basic realm=\"foo\""); |
|
46 |
req.sendResponse (401, "Unauthorized"); |
|
47 |
req.orderlyClose(); |
|
48 |
break; |
|
49 |
case 1: |
|
50 |
case 3: |
|
51 |
req.setResponseEntityBody ("Hello ."); |
|
52 |
req.sendResponse (200, "Ok"); |
|
53 |
req.orderlyClose(); |
|
54 |
break; |
|
55 |
case 2: |
|
56 |
req.addResponseHeader ("Connection", "close"); |
|
57 |
req.addResponseHeader ("Proxy-Authenticate", "Basic realm=\"foo\""); |
|
58 |
req.sendResponse (407, "Proxy Authentication Required"); |
|
59 |
req.orderlyClose(); |
|
60 |
break; |
|
61 |
} |
|
62 |
count ++; |
|
63 |
} catch (IOException e) { |
|
64 |
e.printStackTrace(); |
|
65 |
} |
|
66 |
} |
|
67 |
||
68 |
static void read (InputStream is) throws IOException { |
|
69 |
int c; |
|
70 |
System.out.println ("reading"); |
|
71 |
while ((c=is.read()) != -1) { |
|
72 |
System.out.write (c); |
|
73 |
} |
|
74 |
System.out.println (""); |
|
75 |
System.out.println ("finished reading"); |
|
76 |
} |
|
77 |
||
78 |
||
79 |
static void client (String u) throws Exception { |
|
80 |
URL url = new URL (u); |
|
81 |
System.out.println ("client opening connection to: " + u); |
|
82 |
URLConnection urlc = url.openConnection (); |
|
83 |
InputStream is = urlc.getInputStream (); |
|
84 |
read (is); |
|
85 |
is.close(); |
|
86 |
} |
|
87 |
||
13788 | 88 |
static TestHttpServer server; |
2 | 89 |
static URL urlsave; |
90 |
||
91 |
public static void main (String[] args) throws Exception { |
|
92 |
try { |
|
13788 | 93 |
server = new TestHttpServer (new B4962064(), 1, 10, 0); |
2 | 94 |
int port = server.getLocalPort(); |
95 |
System.setProperty ("http.proxyHost", "localhost"); |
|
96 |
System.setProperty ("http.proxyPort", Integer.toString (port)); |
|
97 |
MyAuthenticator auth = new MyAuthenticator (); |
|
98 |
Authenticator.setDefault (auth); |
|
99 |
System.out.println ("Server started: listening on port: " + port); |
|
100 |
//String s = new String ("http://localhost:"+port+"/d1/d2/d3/foo.html"); |
|
101 |
String s = new String ("http://foo.com/d1/d2/d3/foo.html"); |
|
102 |
urlsave = new URL (s); |
|
103 |
client (s); |
|
104 |
//s = new String ("http://localhost:"+port+"/dr/d3/foo.html"); |
|
105 |
s = new String ("http://bar.com/dr/d3/foo.html"); |
|
106 |
urlsave = new URL (s); |
|
107 |
client (s); |
|
108 |
} catch (Exception e) { |
|
109 |
if (server != null) { |
|
110 |
server.terminate(); |
|
111 |
} |
|
112 |
throw e; |
|
113 |
} |
|
114 |
server.terminate(); |
|
115 |
} |
|
116 |
||
117 |
public static void except (String s) { |
|
118 |
server.terminate(); |
|
119 |
throw new RuntimeException (s); |
|
120 |
} |
|
121 |
||
122 |
static class MyAuthenticator extends Authenticator { |
|
123 |
int count = 0; |
|
124 |
MyAuthenticator () { |
|
125 |
super (); |
|
126 |
} |
|
127 |
||
128 |
public PasswordAuthentication getPasswordAuthentication () { |
|
129 |
URL url = getRequestingURL (); |
|
130 |
if (!url.equals (urlsave)) { |
|
131 |
except ("urls not equal"); |
|
132 |
} |
|
133 |
Authenticator.RequestorType expected; |
|
134 |
if (count == 0) { |
|
135 |
expected = Authenticator.RequestorType.SERVER; |
|
136 |
} else { |
|
137 |
expected = Authenticator.RequestorType.PROXY; |
|
138 |
} |
|
139 |
if (getRequestorType() != expected) { |
|
140 |
except ("wrong authtype"); |
|
141 |
} |
|
142 |
count ++; |
|
143 |
return (new PasswordAuthentication ("user", "passwordNotCheckedAnyway".toCharArray())); |
|
144 |
} |
|
145 |
||
146 |
} |
|
147 |
||
148 |
} |