Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/protocol/http/ResponseCacheStream.java
38867 views
/*1* Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 626248626* @library ../../httptest/27* @build HttpCallback TestHttpServer ClosedChannelList HttpTransaction28* @run main/othervm -Dhttp.keepAlive=false ResponseCacheStream29* @summary COMPATIBILITY: jagex_com - Monkey Puzzle applet fails to load30*/3132import java.net.*;33import java.io.*;34import java.util.*;3536public class ResponseCacheStream implements HttpCallback {3738void okReply (HttpTransaction req) throws IOException {39req.setResponseEntityBody ("Hello, This is the response body. Let's make it as long as possible since we need to test the cache mechanism.");40req.sendResponse (200, "Ok");41System.out.println ("Server: sent response");42req.orderlyClose();43}4445public void request (HttpTransaction req) {46try {47okReply (req);48} catch (IOException e) {49e.printStackTrace();50}51}5253static class MyCacheRequest extends CacheRequest {54private OutputStream buf = null;5556public MyCacheRequest(OutputStream out) {57buf = out;58}5960public OutputStream getBody() throws IOException {61return buf;62}6364/**65* Aborts the attempt to cache the response. If an IOException is66* encountered while reading the response or writing to the cache,67* the current cache store operation will be abandoned.68*/69public void abort() {70}7172}7374static class MyResponseCache extends ResponseCache {75private ByteArrayOutputStream buf = new ByteArrayOutputStream(1024);7677public MyResponseCache() {78}7980public CacheRequest put(URI uri, URLConnection conn) throws IOException {81return new MyCacheRequest(buf);82}8384public CacheResponse get(URI uri, String rqstMethod, Map<String, List<String>> rqstHeaders) throws IOException {85return null;86}8788public byte[] getBuffer() {89return buf.toByteArray();90}91}9293static TestHttpServer server;9495public static void main(String[] args) throws Exception {96MyResponseCache cache = new MyResponseCache();97try {98ResponseCache.setDefault(cache);99server = new TestHttpServer (new ResponseCacheStream());100System.out.println ("Server: listening on port: " + server.getLocalPort());101URL url = new URL ("http://127.0.0.1:"+server.getLocalPort()+"/");102System.out.println ("Client: connecting to " + url);103HttpURLConnection urlc = (HttpURLConnection)url.openConnection();104InputStream is = urlc.getInputStream();105System.out.println("is is " + is.getClass() + ". And markSupported: " + is.markSupported());106if (is.markSupported()) {107byte[] b = new byte[1024];108byte[] b2 = new byte[32];109int len;110int count;111is.mark(10);112len = is.read(b, 0, 10);113is.reset();114len = 0;115count = 0;116do {117len = is.read(b, count, 40 - count);118if (len > 0)119count += len;120} while (len > 0);121is.mark(20);122len = is.read(b2, 0, 20);123is.reset();124len = is.read(b, count, 10);125count += len;126is.mark(20);127len = is.read(b2, 0, 20);128is.reset();129do {130len = is.read(b, count, 1024 - count);131if (len > 0)132count += len;133} while (len > 0);134is.close();135String s1 = new String(b, 0 , count);136String s2 = new String(cache.getBuffer(), 0 , count);137if (! s1.equals(s2))138throw new RuntimeException("cache got corrupted!");139}140} catch (Exception e) {141if (server != null) {142server.terminate();143}144throw e;145}146server.terminate();147}148}149150151