1/*
2 * Copyright (c) 2006, 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 6421581
27 * @summary NPE while closing HttpExchange.getResonseBody()
28 */
29
30import com.sun.net.httpserver.*;
31import java.net.*;
32import java.io.*;
33import java.util.concurrent.*;
34
35public class B6421581 {
36
37    static boolean error = false;
38    static int iter = 0;
39
40    public static void main(String[] args) throws Exception {
41                once();
42    }
43
44    public static void once() throws Exception {
45        InetSocketAddress inetAddress = new InetSocketAddress(
46            "localhost", 0);
47        HttpServer server = HttpServer.create(inetAddress, 5);
48        int port = server.getAddress().getPort();
49        ExecutorService e = (Executors.newFixedThreadPool(5));
50        server.setExecutor(e);
51        HttpContext context = server.createContext("/hello");
52        server.start();
53        context.setHandler(new HttpHandler() {
54            public void handle(HttpExchange msg) {
55                iter ++;
56                System.out.println("Got request");
57                switch (iter) {
58                case 1:
59                    /* close output stream without opening inpustream */
60                    /* chunked encoding */
61                    try {
62                        msg.sendResponseHeaders(200, 0);
63                        OutputStream out = msg.getResponseBody();
64                        out.write("hello".getBytes());
65                        out.close();
66                    } catch(Exception e) {
67                        error = true;
68                    } finally {
69                        msg.close();
70                    }
71                    break;
72                case 2:
73                    /* close output stream without opening inpustream */
74                    /* fixed encoding */
75                    try {
76                        msg.sendResponseHeaders(200, 5);
77                        OutputStream out = msg.getResponseBody();
78                        out.write("hello".getBytes());
79                        out.close();
80                    } catch(Exception e) {
81                        error = true;
82                    } finally {
83                        msg.close();
84                    }
85                    break;
86                case 3:
87                    /* close exchange without opening any stream */
88                    try {
89                        msg.sendResponseHeaders(200, -1);
90                        msg.close();
91                    } catch(Exception e) {
92                        error = true;
93                    }
94                    break;
95                }
96            }
97        });
98
99        URL url = new URL("http://localhost:"+port+"/hello/url.text");
100        doURL(url);
101        doURL(url);
102        doURL(url);
103        e.shutdown();
104        e.awaitTermination(4, TimeUnit.SECONDS);
105        server.stop(0);
106        if (error) {
107            throw new RuntimeException ("test failed");
108        }
109    }
110
111    static void doURL (URL url) throws Exception {
112        InputStream is = url.openStream();
113        while (is.read() != -1) ;
114        is.close();
115    }
116}
117