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