Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/ResponseCache/Test.java
38811 views
/*1* Copyright (c) 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/* @test24* @summary Fixed a potential NullPointerException when setting a ResponseCache that returns a null CacheRequest25* @bug 483726726* @author Michael McMahon27*/2829import com.sun.net.httpserver.*;30import java.net.*;31import java.io.*;32import java.util.*;3334public class Test35{3637static class MyHandler implements HttpHandler {38public void handle(HttpExchange t) throws IOException {39byte[] b = new byte[1024];40int r = 0;41InputStream is = t.getRequestBody();42while (is.read(b) != -1) ;43String response = "This is the response";44t.sendResponseHeaders(200, response.length());45OutputStream os = t.getResponseBody();46os.write(response.getBytes());47os.close();48}49}5051public static void main(String args[]) throws Exception {52HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);53server.createContext("/", new MyHandler());54server.start();55ResponseCache bak = ResponseCache.getDefault();5657try {58ResponseCache.setDefault (new ResponseCache() {59public CacheResponse get (URI uri, String rqstMethod, Map<String,List<String>> rqstHeaders)60throws IOException {61return null;62}63public CacheRequest put(URI uri, URLConnection conn) throws IOException64{65return null;66}67});6869URL url = new URL ("http://localhost:" + server.getAddress().getPort() + "/");70URLConnection urlc = url.openConnection ();71InputStream is = urlc.getInputStream();72while (is.read() != -1) ;73is.close();74} finally {75ResponseCache.setDefault(bak);76server.stop(0);77}78}79}808182