NoCache.java revision 14606:bc3775e25b52
1/* 2 * Copyright (c) 2012, 2016, 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/* 25 * @test 26 * @bug 7133367 27 * @modules jdk.httpserver 28 * @summary ResponseCache.put should not be called when setUseCaches(false) 29 */ 30 31import java.net.*; 32import java.io.IOException; 33import java.util.List; 34import java.util.Map; 35import com.sun.net.httpserver.HttpExchange; 36import com.sun.net.httpserver.HttpHandler; 37import com.sun.net.httpserver.HttpServer; 38 39public class NoCache 40{ 41 public static void main(String[] args) throws IOException { 42 ResponseCache.setDefault(new ThrowingCache()); 43 44 HttpServer server = startHttpServer(); 45 try { 46 URL url = new URL("http://" + InetAddress.getLocalHost().getHostAddress() 47 + ":" + server.getAddress().getPort() + "/NoCache/"); 48 URLConnection uc = url.openConnection(); 49 uc.setUseCaches(false); 50 uc.getInputStream().close(); 51 } finally { 52 server.stop(0); 53 // clear the system-wide cache handler, samevm/agentvm mode 54 ResponseCache.setDefault(null); 55 } 56 } 57 58 static class ThrowingCache extends ResponseCache { 59 @Override 60 public CacheResponse get(URI uri, String rqstMethod, 61 Map<String,List<String>> rqstHeaders) { 62 throw new RuntimeException("ResponseCache.get should not be called"); 63 } 64 65 @Override 66 public CacheRequest put(URI uri, URLConnection conn) { 67 throw new RuntimeException("ResponseCache.put should not be called"); 68 } 69 } 70 71 // HTTP Server 72 static HttpServer startHttpServer() throws IOException { 73 HttpServer httpServer = HttpServer.create(new InetSocketAddress(0), 0); 74 httpServer.createContext("/NoCache/", new SimpleHandler()); 75 httpServer.start(); 76 return httpServer; 77 } 78 79 static class SimpleHandler implements HttpHandler { 80 @Override 81 public void handle(HttpExchange t) throws IOException { 82 t.sendResponseHeaders(200, -1); 83 t.close(); 84 } 85 } 86} 87