2
|
1 |
/*
|
|
2 |
* Copyright 2003-2004 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 |
|
|
24 |
/* @test
|
|
25 |
* @summary Unit test for java.net.ResponseCache
|
|
26 |
* @bug 4837267
|
|
27 |
* @author Yingxian Wang
|
|
28 |
*/
|
|
29 |
|
|
30 |
import java.net.*;
|
|
31 |
import java.util.*;
|
|
32 |
import java.io.*;
|
|
33 |
import java.nio.*;
|
|
34 |
import sun.net.www.ParseUtil;
|
|
35 |
import javax.net.ssl.*;
|
|
36 |
|
|
37 |
/**
|
|
38 |
* Request should get serviced by the cache handler. Response get
|
|
39 |
* saved through the cache handler.
|
|
40 |
*/
|
|
41 |
public class ResponseCacheTest implements Runnable {
|
|
42 |
ServerSocket ss;
|
|
43 |
static URL url1;
|
|
44 |
static URL url2;
|
|
45 |
static String FNPrefix, OutFNPrefix;
|
|
46 |
/*
|
|
47 |
* Our "http" server to return a 404 */
|
|
48 |
public void run() {
|
|
49 |
try {
|
|
50 |
Socket s = ss.accept();
|
|
51 |
|
|
52 |
InputStream is = s.getInputStream ();
|
|
53 |
BufferedReader r = new BufferedReader(new InputStreamReader(is));
|
|
54 |
String x;
|
|
55 |
while ((x=r.readLine()) != null) {
|
|
56 |
if (x.length() ==0) {
|
|
57 |
break;
|
|
58 |
}
|
|
59 |
}
|
|
60 |
PrintStream out = new PrintStream(
|
|
61 |
new BufferedOutputStream(
|
|
62 |
s.getOutputStream() ));
|
|
63 |
|
|
64 |
/* send file2.1 */
|
|
65 |
File file2 = new File(FNPrefix+"file2.1");
|
|
66 |
out.print("HTTP/1.1 200 OK\r\n");
|
|
67 |
out.print("Content-Type: text/html; charset=iso-8859-1\r\n");
|
|
68 |
out.print("Content-Length: "+file2.length()+"\r\n");
|
|
69 |
out.print("Connection: close\r\n");
|
|
70 |
out.print("\r\n");
|
|
71 |
FileInputStream fis = new FileInputStream(file2);
|
|
72 |
byte[] buf = new byte[(int)file2.length()];
|
|
73 |
int len;
|
|
74 |
while ((len = fis.read(buf)) != -1) {
|
|
75 |
out.print(new String(buf));
|
|
76 |
}
|
|
77 |
|
|
78 |
out.flush();
|
|
79 |
|
|
80 |
s.close();
|
|
81 |
ss.close();
|
|
82 |
} catch (Exception e) {
|
|
83 |
e.printStackTrace();
|
|
84 |
}
|
|
85 |
}
|
|
86 |
static class NameVerifier implements HostnameVerifier {
|
|
87 |
public boolean verify(String hostname, SSLSession session) {
|
|
88 |
return true;
|
|
89 |
}
|
|
90 |
}
|
|
91 |
ResponseCacheTest() throws Exception {
|
|
92 |
/* start the server */
|
|
93 |
ss = new ServerSocket(0);
|
|
94 |
(new Thread(this)).start();
|
|
95 |
/* establish http connection to server */
|
|
96 |
url1 = new URL("http://localhost/file1.cache");
|
|
97 |
HttpURLConnection http = (HttpURLConnection)url1.openConnection();
|
|
98 |
InputStream is = null;
|
|
99 |
System.out.println("request headers: "+http.getRequestProperties());
|
|
100 |
System.out.println("responsecode is :"+http.getResponseCode());
|
|
101 |
Map<String,List<String>> headers1 = http.getHeaderFields();
|
|
102 |
try {
|
|
103 |
is = http.getInputStream();
|
|
104 |
} catch (IOException ioex) {
|
|
105 |
throw new RuntimeException(ioex.getMessage());
|
|
106 |
}
|
|
107 |
BufferedReader r = new BufferedReader(new InputStreamReader(is));
|
|
108 |
String x;
|
|
109 |
File fileout = new File(OutFNPrefix+"file1");
|
|
110 |
PrintStream out = new PrintStream(
|
|
111 |
new BufferedOutputStream(
|
|
112 |
new FileOutputStream(fileout)));
|
|
113 |
while ((x=r.readLine()) != null) {
|
|
114 |
out.print(x+"\n");
|
|
115 |
}
|
|
116 |
out.flush();
|
|
117 |
out.close();
|
|
118 |
|
|
119 |
http.disconnect();
|
|
120 |
|
|
121 |
// testing ResponseCacheHandler.put()
|
|
122 |
url2 = new URL("http://localhost:" +
|
|
123 |
Integer.toString(ss.getLocalPort())+"/file2.1");
|
|
124 |
http = (HttpURLConnection)url2.openConnection();
|
|
125 |
System.out.println("responsecode2 is :"+http.getResponseCode());
|
|
126 |
Map<String,List<String>> headers2 = http.getHeaderFields();
|
|
127 |
|
|
128 |
try {
|
|
129 |
is = http.getInputStream();
|
|
130 |
} catch (IOException ioex) {
|
|
131 |
throw new RuntimeException(ioex.getMessage());
|
|
132 |
}
|
|
133 |
r = new BufferedReader(new InputStreamReader(is));
|
|
134 |
fileout = new File(OutFNPrefix+"file2.2");
|
|
135 |
out = new PrintStream(
|
|
136 |
new BufferedOutputStream(
|
|
137 |
new FileOutputStream(fileout)));
|
|
138 |
while ((x=r.readLine()) != null) {
|
|
139 |
out.print(x+"\n");
|
|
140 |
}
|
|
141 |
out.flush();
|
|
142 |
out.close();
|
|
143 |
|
|
144 |
// assert (headers1 == headers2 && file1 == file2.2)
|
|
145 |
File file1 = new File(OutFNPrefix+"file1");
|
|
146 |
File file2 = new File(OutFNPrefix+"file2.2");
|
|
147 |
System.out.println("headers1"+headers1+"\nheaders2="+headers2);
|
|
148 |
if (!headers1.equals(headers2) || file1.length() != file2.length()) {
|
|
149 |
throw new RuntimeException("test failed");
|
|
150 |
}
|
|
151 |
}
|
|
152 |
public static void main(String args[]) throws Exception {
|
|
153 |
ResponseCache.setDefault(new MyResponseCache());
|
|
154 |
FNPrefix = System.getProperty("test.src", ".")+"/";
|
|
155 |
OutFNPrefix = System.getProperty("test.scratch", ".")+"/";
|
|
156 |
new ResponseCacheTest();
|
|
157 |
}
|
|
158 |
|
|
159 |
static class MyResponseCache extends ResponseCache {
|
|
160 |
public CacheResponse
|
|
161 |
get(URI uri, String rqstMethod, Map<String,List<String>> rqstHeaders)
|
|
162 |
throws IOException {
|
|
163 |
if (uri.equals(ParseUtil.toURI(url1))) {
|
|
164 |
return new MyCacheResponse(FNPrefix+"file1.cache");
|
|
165 |
}
|
|
166 |
return null;
|
|
167 |
}
|
|
168 |
|
|
169 |
public CacheRequest put(URI uri, URLConnection conn) throws IOException {
|
|
170 |
// save cache to file2.cache
|
|
171 |
// 1. serialize headers into file2.cache
|
|
172 |
// 2. write data to file2.cache
|
|
173 |
return new MyCacheRequest(OutFNPrefix+"file2.cache", conn.getHeaderFields());
|
|
174 |
}
|
|
175 |
}
|
|
176 |
|
|
177 |
static class MyCacheResponse extends CacheResponse {
|
|
178 |
FileInputStream fis;
|
|
179 |
Map<String,List<String>> headers;
|
|
180 |
public MyCacheResponse(String filename) {
|
|
181 |
try {
|
|
182 |
fis = new FileInputStream(new File(filename));
|
|
183 |
ObjectInputStream ois = new ObjectInputStream(fis);
|
|
184 |
headers = (Map<String,List<String>>)ois.readObject();
|
|
185 |
} catch (Exception ex) {
|
|
186 |
// throw new RuntimeException(ex.getMessage());
|
|
187 |
}
|
|
188 |
}
|
|
189 |
|
|
190 |
public InputStream getBody() throws IOException {
|
|
191 |
return fis;
|
|
192 |
}
|
|
193 |
|
|
194 |
public Map<String,List<String>> getHeaders() throws IOException {
|
|
195 |
return headers;
|
|
196 |
}
|
|
197 |
}
|
|
198 |
|
|
199 |
static class MyCacheRequest extends CacheRequest {
|
|
200 |
FileOutputStream fos;
|
|
201 |
public MyCacheRequest(String filename, Map<String,List<String>> rspHeaders) {
|
|
202 |
try {
|
|
203 |
File file = new File(filename);
|
|
204 |
fos = new FileOutputStream(file);
|
|
205 |
ObjectOutputStream oos = new ObjectOutputStream(fos);
|
|
206 |
oos.writeObject(rspHeaders);
|
|
207 |
} catch (Exception ex) {
|
|
208 |
throw new RuntimeException(ex.getMessage());
|
|
209 |
}
|
|
210 |
}
|
|
211 |
public OutputStream getBody() throws IOException {
|
|
212 |
return fos;
|
|
213 |
}
|
|
214 |
|
|
215 |
public void abort() {
|
|
216 |
// no op
|
|
217 |
}
|
|
218 |
}
|
|
219 |
|
|
220 |
|
|
221 |
}
|