blob: 1ea04ae08d37c17e2f3fceeec5a2a0d2aca88e17 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2003 Sun Microsystems, Inc. All Rights Reserved.
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 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24import java.io.*;
25import java.net.*;
26import java.util.*;
27import sun.net.www.protocol.http.*;
28
29public class B4933582 implements HttpCallback {
30
31 static int count = 0;
32 static String authstring;
33
34 void errorReply (HttpTransaction req, String reply) throws IOException {
35 req.addResponseHeader ("Connection", "close");
36 req.addResponseHeader ("WWW-Authenticate", reply);
37 req.sendResponse (401, "Unauthorized");
38 req.orderlyClose();
39 }
40
41 void okReply (HttpTransaction req) throws IOException {
42 req.setResponseEntityBody ("Hello .");
43 req.sendResponse (200, "Ok");
44 req.orderlyClose();
45 }
46
47 static boolean firstTime = true;
48
49 public void request (HttpTransaction req) {
50 try {
51 authstring = req.getRequestHeader ("Authorization");
52 if (firstTime) {
53 switch (count) {
54 case 0:
55 errorReply (req, "Basic realm=\"wallyworld\"");
56 break;
57 case 1:
58 /* client stores a username/pw for wallyworld
59 */
60 save (authstring);
61 okReply (req);
62 break;
63 }
64 } else {
65 /* check the auth string is premptively set from last time */
66 String savedauth = retrieve();
67 if (savedauth.equals (authstring)) {
68 okReply (req);
69 } else {
70 System.out.println ("savedauth = " + savedauth);
71 System.out.println ("authstring = " + authstring);
72 errorReply (req, "Basic realm=\"wallyworld\"");
73 }
74 }
75 count ++;
76 } catch (IOException e) {
77 e.printStackTrace();
78 }
79 }
80
81 void save (String s) {
82 try {
83 FileOutputStream f = new FileOutputStream ("auth.save");
84 ObjectOutputStream os = new ObjectOutputStream (f);
85 os.writeObject (s);
86 } catch (IOException e) {
87 assert false;
88 }
89 }
90
91 String retrieve () {
92 String s = null;
93 try {
94 FileInputStream f = new FileInputStream ("auth.save");
95 ObjectInputStream is = new ObjectInputStream (f);
96 s = (String) is.readObject();
97 } catch (Exception e) {
98 assert false;
99 }
100 return s;
101 }
102
103 static void read (InputStream is) throws IOException {
104 int c;
105 System.out.println ("reading");
106 while ((c=is.read()) != -1) {
107 System.out.write (c);
108 }
109 System.out.println ("");
110 System.out.println ("finished reading");
111 }
112
113 static void client (String u) throws Exception {
114 URL url = new URL (u);
115 System.out.println ("client opening connection to: " + u);
116 URLConnection urlc = url.openConnection ();
117 InputStream is = urlc.getInputStream ();
118 read (is);
119 is.close();
120 }
121
122 static HttpServer server;
123
124 public static void main (String[] args) throws Exception {
125 firstTime = args[0].equals ("first");
126 MyAuthenticator auth = new MyAuthenticator ();
127 Authenticator.setDefault (auth);
128 AuthCacheValue.setAuthCache (new CacheImpl());
129 try {
130 server = new HttpServer (new B4933582(), 1, 10, 5009);
131 System.out.println ("Server: listening on port: " + server.getLocalPort());
132 client ("http://localhost:"+server.getLocalPort()+"/d1/foo.html");
133 } catch (Exception e) {
134 if (server != null) {
135 server.terminate();
136 }
137 throw e;
138 }
139 int f = auth.getCount();
140 if (firstTime && f != 1) {
141 except ("Authenticator was called "+f+" times. Should be 1");
142 }
143 if (!firstTime && f != 0) {
144 except ("Authenticator was called "+f+" times. Should be 0");
145 }
146 server.terminate();
147 }
148
149 public static void except (String s) {
150 server.terminate();
151 throw new RuntimeException (s);
152 }
153
154 static class MyAuthenticator extends Authenticator {
155 MyAuthenticator () {
156 super ();
157 }
158
159 int count = 0;
160
161 public PasswordAuthentication getPasswordAuthentication () {
162 PasswordAuthentication pw;
163 pw = new PasswordAuthentication ("user", "pass1".toCharArray());
164 count ++;
165 return pw;
166 }
167
168 public int getCount () {
169 return (count);
170 }
171 }
172
173 static class CacheImpl extends AuthCacheImpl {
174 HashMap map;
175 CacheImpl () throws IOException {
176 super();
177 File src = new File ("cache.ser");
178 if (src.exists()) {
179 ObjectInputStream is = new ObjectInputStream (
180 new FileInputStream (src)
181 );
182 try {
183 map = (HashMap)is.readObject ();
184 } catch (ClassNotFoundException e) {
185 assert false;
186 }
187 is.close();
188 System.out.println ("setMap from cache.ser");
189 } else {
190 map = new HashMap();
191 }
192 setMap (map);
193 }
194
195 private void writeMap () {
196 try {
197 File dst = new File ("cache.ser");
198 dst.delete();
199 if (!dst.createNewFile()) {
200 return;
201 }
202 ObjectOutputStream os = new ObjectOutputStream (
203 new FileOutputStream (dst)
204 );
205 os.writeObject(map);
206 os.close();
207 } catch (IOException e) {}
208 }
209
210 public void put (String pkey, AuthCacheValue value) {
211 System.out.println ("put: " + pkey + " " + value);
212 super.put (pkey, value);
213 writeMap();
214 }
215
216 public AuthCacheValue get (String pkey, String skey) {
217 System.out.println ("get: " + pkey + " " + skey);
218 AuthCacheValue i = super.get (pkey, skey);
219 System.out.println ("---> " + i);
220 return i;
221 }
222
223 public void remove (String pkey, AuthCacheValue value) {
224 System.out.println ("remove: " + pkey + " " + value);
225 super.remove (pkey, value);
226 writeMap();
227 }
228 }
229}