blob: 41cfcde10ef6a4dfffbc66a9c5239bb5c1c9cb4d [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2005 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/*
25 * @test
26 * @bug 6262486
27 * @library ../../httptest/
28 * @build HttpCallback HttpServer ClosedChannelList HttpTransaction
29 * @run main/othervm -Dhttp.keepAlive=false ResponseCacheStream
30 * @summary COMPATIBILITY: jagex_com - Monkey Puzzle applet fails to load
31 */
32
33import java.net.*;
34import java.io.*;
35import java.util.*;
36
37public class ResponseCacheStream implements HttpCallback {
38
39 void okReply (HttpTransaction req) throws IOException {
40 req.setResponseEntityBody ("Hello, This is the response body. Let's make it as long as possible since we need to test the cache mechanism.");
41 req.sendResponse (200, "Ok");
42 System.out.println ("Server: sent response");
43 req.orderlyClose();
44 }
45
46 public void request (HttpTransaction req) {
47 try {
48 okReply (req);
49 } catch (IOException e) {
50 e.printStackTrace();
51 }
52 }
53
54 static class MyCacheRequest extends CacheRequest {
55 private OutputStream buf = null;
56
57 public MyCacheRequest(OutputStream out) {
58 buf = out;
59 }
60
61 public OutputStream getBody() throws IOException {
62 return buf;
63 }
64
65 /**
66 * Aborts the attempt to cache the response. If an IOException is
67 * encountered while reading the response or writing to the cache,
68 * the current cache store operation will be abandoned.
69 */
70 public void abort() {
71 }
72
73 }
74
75 static class MyResponseCache extends ResponseCache {
76 private ByteArrayOutputStream buf = new ByteArrayOutputStream(1024);
77
78 public MyResponseCache() {
79 }
80
81 public CacheRequest put(URI uri, URLConnection conn) throws IOException {
82 return new MyCacheRequest(buf);
83 }
84
85 public CacheResponse get(URI uri, String rqstMethod, Map<String, List<String>> rqstHeaders) throws IOException {
86 return null;
87 }
88
89 public byte[] getBuffer() {
90 return buf.toByteArray();
91 }
92 }
93
94 static HttpServer server;
95
96 public static void main(String[] args) throws Exception {
97 MyResponseCache cache = new MyResponseCache();
98 try {
99 ResponseCache.setDefault(cache);
100 server = new HttpServer (new ResponseCacheStream());
101 System.out.println ("Server: listening on port: " + server.getLocalPort());
102 URL url = new URL ("http://127.0.0.1:"+server.getLocalPort()+"/");
103 System.out.println ("Client: connecting to " + url);
104 HttpURLConnection urlc = (HttpURLConnection)url.openConnection();
105 InputStream is = urlc.getInputStream();
106 System.out.println("is is " + is.getClass() + ". And markSupported: " + is.markSupported());
107 if (is.markSupported()) {
108 byte[] b = new byte[1024];
109 byte[] b2 = new byte[32];
110 int len;
111 int count;
112 is.mark(10);
113 len = is.read(b, 0, 10);
114 is.reset();
115 len = 0;
116 count = 0;
117 do {
118 len = is.read(b, count, 40 - count);
119 if (len > 0)
120 count += len;
121 } while (len > 0);
122 is.mark(20);
123 len = is.read(b2, 0, 20);
124 is.reset();
125 len = is.read(b, count, 10);
126 count += len;
127 is.mark(20);
128 len = is.read(b2, 0, 20);
129 is.reset();
130 do {
131 len = is.read(b, count, 1024 - count);
132 if (len > 0)
133 count += len;
134 } while (len > 0);
135 is.close();
136 String s1 = new String(b, 0 , count);
137 String s2 = new String(cache.getBuffer(), 0 , count);
138 if (! s1.equals(s2))
139 throw new RuntimeException("cache got corrupted!");
140 }
141 } catch (Exception e) {
142 if (server != null) {
143 server.terminate();
144 }
145 throw e;
146 }
147 server.terminate();
148 }
149}