0N/A/*
2362N/A * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/**
0N/A * @test
0N/A * @bug 6421581
0N/A * @summary NPE while closing HttpExchange.getResonseBody()
0N/A */
0N/A
0N/Aimport com.sun.net.httpserver.*;
0N/Aimport java.net.*;
0N/Aimport java.io.*;
0N/Aimport java.util.concurrent.*;
0N/A
0N/Apublic class B6421581 {
0N/A
0N/A static boolean error = false;
0N/A static int iter = 0;
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A once();
0N/A }
0N/A
0N/A public static void once() throws Exception {
0N/A InetSocketAddress inetAddress = new InetSocketAddress(
0N/A "localhost", 0);
0N/A HttpServer server = HttpServer.create(inetAddress, 5);
0N/A int port = server.getAddress().getPort();
0N/A ExecutorService e = (Executors.newFixedThreadPool(5));
0N/A server.setExecutor(e);
0N/A HttpContext context = server.createContext("/hello");
0N/A server.start();
0N/A context.setHandler(new HttpHandler() {
0N/A public void handle(HttpExchange msg) {
0N/A iter ++;
0N/A System.out.println("Got request");
0N/A switch (iter) {
0N/A case 1:
0N/A /* close output stream without opening inpustream */
0N/A /* chunked encoding */
0N/A try {
0N/A msg.sendResponseHeaders(200, 0);
0N/A OutputStream out = msg.getResponseBody();
0N/A out.write("hello".getBytes());
0N/A out.close();
0N/A } catch(Exception e) {
0N/A error = true;
0N/A } finally {
0N/A msg.close();
0N/A }
0N/A break;
0N/A case 2:
0N/A /* close output stream without opening inpustream */
0N/A /* fixed encoding */
0N/A try {
0N/A msg.sendResponseHeaders(200, 5);
0N/A OutputStream out = msg.getResponseBody();
0N/A out.write("hello".getBytes());
0N/A out.close();
0N/A } catch(Exception e) {
0N/A error = true;
0N/A } finally {
0N/A msg.close();
0N/A }
0N/A break;
0N/A case 3:
0N/A /* close exchange without opening any stream */
0N/A try {
0N/A msg.sendResponseHeaders(200, -1);
0N/A msg.close();
0N/A } catch(Exception e) {
0N/A error = true;
0N/A }
0N/A break;
0N/A }
0N/A }
0N/A });
0N/A
0N/A URL url = new URL("http://localhost:"+port+"/hello/url.text");
0N/A doURL(url);
0N/A doURL(url);
0N/A doURL(url);
0N/A e.shutdown();
0N/A e.awaitTermination(4, TimeUnit.SECONDS);
0N/A server.stop(0);
0N/A if (error) {
0N/A throw new RuntimeException ("test failed");
0N/A }
0N/A }
0N/A
0N/A static void doURL (URL url) throws Exception {
0N/A InputStream is = url.openStream();
0N/A while (is.read() != -1) ;
0N/A is.close();
0N/A }
0N/A}