B6431193.java revision 2362:00cd9dc3c2b5
176259Sgreen/*
276259Sgreen * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
376259Sgreen * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
476259Sgreen *
576259Sgreen * This code is free software; you can redistribute it and/or modify it
676259Sgreen * under the terms of the GNU General Public License version 2 only, as
776259Sgreen * published by the Free Software Foundation.
876259Sgreen *
976259Sgreen * This code is distributed in the hope that it will be useful, but WITHOUT
1076259Sgreen * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1176259Sgreen * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1276259Sgreen * version 2 for more details (a copy is included in the LICENSE file that
1376259Sgreen * accompanied this code).
1476259Sgreen *
1576259Sgreen * You should have received a copy of the GNU General Public License version
1676259Sgreen * 2 along with this work; if not, write to the Free Software Foundation,
1776259Sgreen * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1876259Sgreen *
1976259Sgreen * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2076259Sgreen * or visit www.oracle.com if you need additional information or have any
2176259Sgreen * questions.
2276259Sgreen */
2376259Sgreen
2476259Sgreen/**
2576259Sgreen * @test
26149749Sdes * @bug 6431193
2776259Sgreen * @summary  The new HTTP server exits immediately
2876259Sgreen */
2976259Sgreen
3076259Sgreenimport java.io.IOException;
3176259Sgreenimport java.io.InputStream;
3276259Sgreenimport java.io.OutputStream;
3376259Sgreenimport java.net.*;
3476259Sgreen
3576259Sgreenimport com.sun.net.httpserver.*;
3676259Sgreen
3776259Sgreenpublic class B6431193 {
3876259Sgreen
3998675Sdes    static boolean error = false;
4076259Sgreen
4176259Sgreen    public static void read (InputStream i) throws IOException {
4276259Sgreen        while (i.read() != -1);
4376259Sgreen        i.close();
4476259Sgreen    }
4576259Sgreen
4676259Sgreen    /**
4776259Sgreen         * @param args
4876259Sgreen         */
4976259Sgreen    public static void main(String[] args) {
5076259Sgreen        class MyHandler implements HttpHandler {
5176259Sgreen            public void handle(HttpExchange t) throws IOException {
5276259Sgreen                InputStream is = t.getRequestBody();
5376259Sgreen                read(is);
54149749Sdes                // .. read the request body
55149749Sdes                    String response = "This is the response";
5676259Sgreen                t.sendResponseHeaders(200, response.length());
5776259Sgreen                OutputStream os = t.getResponseBody();
5876259Sgreen                os.write(response.getBytes());
5976259Sgreen                os.close();
60149749Sdes                error = Thread.currentThread().isDaemon();
61149749Sdes            }
62149749Sdes        }
6376259Sgreen
6476259Sgreen
6576259Sgreen        HttpServer server;
6676259Sgreen        try {
6776259Sgreen            server = HttpServer.create(new InetSocketAddress(0), 10);
6876259Sgreen
6976259Sgreen            server.createContext("/apps", new MyHandler());
7076259Sgreen            server.setExecutor(null);
7176259Sgreen            // creates a default executor
7276259Sgreen                server.start();
7376259Sgreen            int port = server.getAddress().getPort();
7476259Sgreen            String s = "http://localhost:"+port+"/apps/foo";
7576259Sgreen            URL url = new URL (s);
7676259Sgreen            InputStream is = url.openStream();
7776259Sgreen            read (is);
7876259Sgreen            server.stop (1);
7976259Sgreen            if (error) {
8076259Sgreen                throw new RuntimeException ("error in test");
8176259Sgreen            }
8276259Sgreen
83149749Sdes        }
8476259Sgreen        catch (IOException e) {
8576259Sgreen            // TODO Auto-generated catch block
8676259Sgreen                e.printStackTrace();
8776259Sgreen        }
8876259Sgreen    }
8976259Sgreen}
9076259Sgreen