test/jdk/com/sun/net/httpserver/bugs/HttpExchange/AutoCloseableHttpExchange.java
author michaelm
Mon, 02 Sep 2019 15:58:44 +0100
changeset 57981 c4ec55644b4b
permissions -rw-r--r--
8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable Reviewed-by: dfuchs, michaelm Contributed-by: patrick.concannon@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57981
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
     1
/*
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
     4
 *
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
     8
 *
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    13
 * accompanied this code).
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    14
 *
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    18
 *
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    21
 * questions.
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    22
 */
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    23
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    24
import com.sun.net.httpserver.*;
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    25
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    26
import java.io.IOException;
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    27
import java.io.InputStream;
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    28
import java.net.*;
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    29
import java.util.concurrent.CountDownLatch;
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    30
import java.util.concurrent.ExecutorService;
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    31
import java.util.concurrent.Executors;
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    32
import java.util.concurrent.atomic.AtomicBoolean;
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    33
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    34
import jdk.test.lib.net.URIBuilder;
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    35
import sun.net.httpserver.HttpExchangeAccess;
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    36
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    37
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    38
/**
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    39
 * @test
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    40
 * @bug 8203036
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    41
 * @library /test/lib
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    42
 * @modules jdk.httpserver/sun.net.httpserver
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    43
 * @build jdk.httpserver/sun.net.httpserver.HttpExchangeAccess AutoCloseableHttpExchange
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    44
 * @run main/othervm AutoCloseableHttpExchange
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    45
 * @summary Ensure that HttpExchange closes correctly when utilising the
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    46
 * AutoCloseable interface e.g. both request InputStream and response OutputStream
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    47
 * are closed, if not already.
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    48
 */
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    49
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    50
public class AutoCloseableHttpExchange {
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    51
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    52
    static HttpServer testHttpServer;
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    53
    static AtomicBoolean exchangeCloseFail = new AtomicBoolean(false);
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    54
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    55
    static class Handler implements HttpHandler {
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    56
        private CountDownLatch latch;
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    57
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    58
        Handler(CountDownLatch latch) {
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    59
            this.latch = latch;
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    60
        }
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    61
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    62
        public void handle(HttpExchange t) throws IOException {
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    63
            InputStream is = t.getRequestBody();
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    64
            try (HttpExchange e = t) {
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    65
                while (is.read() != -1) ;
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    66
                t.sendResponseHeaders(200, -1);
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    67
            }
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    68
            if (!HttpExchangeAccess.isClosed(t)) {
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    69
                exchangeCloseFail.set(true);
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    70
            }
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    71
            latch.countDown();
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    72
        }
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    73
    }
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    74
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    75
    static void connectAndCheck(String realm) throws Exception {
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    76
        URL url = URIBuilder.newBuilder()
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    77
                .scheme("http")
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    78
                .loopback()
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    79
                .port(testHttpServer.getAddress().getPort())
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    80
                .path(realm)
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    81
                .toURL();
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    82
        HttpURLConnection testConnection = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    83
        InputStream is = testConnection.getInputStream();
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    84
        while (is.read() != -1) ;
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    85
        is.close();
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    86
    }
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    87
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    88
    public static void main(String[] args) throws Exception {
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    89
        int CONNECTION_COUNT = 5;
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    90
        CountDownLatch latch = new CountDownLatch(CONNECTION_COUNT);
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    91
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    92
        InetSocketAddress addr = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    93
        testHttpServer = HttpServer.create(addr, 0);
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    94
        testHttpServer.createContext("/test", new Handler(latch));
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    95
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    96
        ExecutorService executor = Executors.newFixedThreadPool(CONNECTION_COUNT);
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    97
        testHttpServer.setExecutor(executor);
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    98
        testHttpServer.start();
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
    99
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
   100
        while (CONNECTION_COUNT-- != 0) {
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
   101
            connectAndCheck("/test");
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
   102
        }
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
   103
        latch.await();
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
   104
        testHttpServer.stop(2);
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
   105
        executor.shutdown();
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
   106
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
   107
        if (exchangeCloseFail.get())
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
   108
            throw new RuntimeException("The exchange was not closed properly");
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
   109
    }
c4ec55644b4b 8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
michaelm
parents:
diff changeset
   110
}