5511N/A/*
5511N/A * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
5511N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5511N/A *
5511N/A * This code is free software; you can redistribute it and/or modify it
5511N/A * under the terms of the GNU General Public License version 2 only, as
5511N/A * published by the Free Software Foundation.
5511N/A *
5511N/A * This code is distributed in the hope that it will be useful, but WITHOUT
5511N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5511N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
5511N/A * version 2 for more details (a copy is included in the LICENSE file that
5511N/A * accompanied this code).
5511N/A *
5511N/A * You should have received a copy of the GNU General Public License version
5511N/A * 2 along with this work; if not, write to the Free Software Foundation,
5511N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5511N/A *
5511N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5511N/A * or visit www.oracle.com if you need additional information or have any
5511N/A * questions.
5511N/A */
5511N/A
5511N/A/* @test
5511N/A * @summary Fixed a potential NullPointerException when setting a ResponseCache that returns a null CacheRequest
5511N/A * @bug 4837267
5511N/A * @author Michael McMahon
5511N/A */
5511N/A
5511N/Aimport com.sun.net.httpserver.*;
5511N/Aimport java.net.*;
5511N/Aimport java.io.*;
5511N/Aimport java.util.*;
5511N/A
5511N/Apublic class Test
5511N/A{
5511N/A
5511N/A static class MyHandler implements HttpHandler {
5511N/A public void handle(HttpExchange t) throws IOException {
5511N/A byte[] b = new byte[1024];
5511N/A int r = 0;
5511N/A InputStream is = t.getRequestBody();
5511N/A while (is.read(b) != -1) ;
5511N/A String response = "This is the response";
5511N/A t.sendResponseHeaders(200, response.length());
5511N/A OutputStream os = t.getResponseBody();
5511N/A os.write(response.getBytes());
5511N/A os.close();
5511N/A }
5511N/A }
5511N/A
5511N/A public static void main(String args[]) throws Exception {
5511N/A HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);
5511N/A server.createContext("/", new MyHandler());
5511N/A server.start();
5511N/A ResponseCache bak = ResponseCache.getDefault();
5511N/A
5511N/A try {
5511N/A ResponseCache.setDefault (new ResponseCache() {
5511N/A public CacheResponse get (URI uri, String rqstMethod, Map<String,List<String>> rqstHeaders)
5511N/A throws IOException {
5511N/A return null;
5511N/A }
5511N/A public CacheRequest put(URI uri, URLConnection conn) throws IOException
5511N/A {
5511N/A return null;
5511N/A }
5511N/A });
5511N/A
5511N/A URL url = new URL ("http://localhost:" + server.getAddress().getPort() + "/");
5511N/A URLConnection urlc = url.openConnection ();
5511N/A InputStream is = urlc.getInputStream();
5511N/A while (is.read() != -1) ;
5511N/A is.close();
5511N/A } finally {
5511N/A ResponseCache.setDefault(bak);
5511N/A server.stop(0);
5511N/A }
5511N/A }
5511N/A}