src/java.net.http/share/classes/jdk/internal/net/http/HttpResponseImpl.java
author dfuchs
Tue, 15 Jan 2019 11:34:20 +0000
changeset 53300 54aa3ea04fe8
parent 53271 14d078fd74cb
permissions -rw-r--r--
8216974: HttpConnection not returned to the pool after 204 response Summary: MultiExchange now call nullBody() on Exchange after receiving 204 Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     1
/*
53271
14d078fd74cb 8216478: Cleanup HttpResponseImpl back reference to HttpConnection
dfuchs
parents: 49765
diff changeset
     2
 * Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     4
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    10
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    15
 * accompanied this code).
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    16
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    20
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    23
 * questions.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    24
 */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    25
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    26
package jdk.internal.net.http;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    27
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    28
import java.io.IOException;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    29
import java.net.URI;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    30
import java.nio.ByteBuffer;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    31
import java.util.Optional;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    32
import java.util.concurrent.CompletableFuture;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    33
import java.util.function.Supplier;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    34
import javax.net.ssl.SSLSession;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    35
import java.net.http.HttpClient;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    36
import java.net.http.HttpHeaders;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    37
import java.net.http.HttpRequest;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    38
import java.net.http.HttpResponse;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    39
import jdk.internal.net.http.websocket.RawChannel;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    40
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    41
/**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    42
 * The implementation class for HttpResponse
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    43
 */
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    44
class HttpResponseImpl<T> implements HttpResponse<T>, RawChannel.Provider {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    45
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    46
    final int responseCode;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    47
    final HttpRequest initialRequest;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    48
    final Optional<HttpResponse<T>> previousResponse;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    49
    final HttpHeaders headers;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    50
    final Optional<SSLSession> sslSession;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    51
    final URI uri;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    52
    final HttpClient.Version version;
53271
14d078fd74cb 8216478: Cleanup HttpResponseImpl back reference to HttpConnection
dfuchs
parents: 49765
diff changeset
    53
    final RawChannelProvider rawChannelProvider;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    54
    final T body;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    55
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    56
    public HttpResponseImpl(HttpRequest initialRequest,
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    57
                            Response response,
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    58
                            HttpResponse<T> previousResponse,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    59
                            T body,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    60
                            Exchange<T> exch) {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    61
        this.responseCode = response.statusCode();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    62
        this.initialRequest = initialRequest;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    63
        this.previousResponse = Optional.ofNullable(previousResponse);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    64
        this.headers = response.headers();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    65
        //this.trailers = trailers;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    66
        this.sslSession = Optional.ofNullable(response.getSSLSession());
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    67
        this.uri = response.request().uri();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    68
        this.version = response.version();
53271
14d078fd74cb 8216478: Cleanup HttpResponseImpl back reference to HttpConnection
dfuchs
parents: 49765
diff changeset
    69
        this.rawChannelProvider = RawChannelProvider.create(response, exch);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    70
        this.body = body;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    71
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    72
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    73
    @Override
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    74
    public int statusCode() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    75
        return responseCode;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    76
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    77
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    78
    @Override
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    79
    public HttpRequest request() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    80
        return initialRequest;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    81
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    82
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    83
    @Override
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    84
    public Optional<HttpResponse<T>> previousResponse() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    85
        return previousResponse;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    86
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    87
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    88
    @Override
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    89
    public HttpHeaders headers() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    90
        return headers;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    91
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    92
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    93
    @Override
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    94
    public T body() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    95
        return body;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    96
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    97
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    98
    @Override
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    99
    public Optional<SSLSession> sslSession() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   100
        return sslSession;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   101
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   102
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   103
    @Override
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   104
    public URI uri() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   105
        return uri;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   106
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   107
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   108
    @Override
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   109
    public HttpClient.Version version() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   110
        return version;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   111
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   112
    // keepalive flag determines whether connection is closed or kept alive
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   113
    // by reading/skipping data
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   114
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   115
    /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   116
     * Returns a RawChannel that may be used for WebSocket protocol.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   117
     * @implNote This implementation does not support RawChannel over
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   118
     *           HTTP/2 connections.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   119
     * @return a RawChannel that may be used for WebSocket protocol.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   120
     * @throws UnsupportedOperationException if getting a RawChannel over
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   121
     *         this connection is not supported.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   122
     * @throws IOException if an I/O exception occurs while retrieving
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   123
     *         the channel.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   124
     */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   125
    @Override
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   126
    public synchronized RawChannel rawChannel() throws IOException {
53271
14d078fd74cb 8216478: Cleanup HttpResponseImpl back reference to HttpConnection
dfuchs
parents: 49765
diff changeset
   127
        if (rawChannelProvider == null) {
14d078fd74cb 8216478: Cleanup HttpResponseImpl back reference to HttpConnection
dfuchs
parents: 49765
diff changeset
   128
            throw new UnsupportedOperationException(
14d078fd74cb 8216478: Cleanup HttpResponseImpl back reference to HttpConnection
dfuchs
parents: 49765
diff changeset
   129
                    "RawChannel is only supported for WebSocket creation");
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   130
        }
53271
14d078fd74cb 8216478: Cleanup HttpResponseImpl back reference to HttpConnection
dfuchs
parents: 49765
diff changeset
   131
        return rawChannelProvider.rawChannel();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   132
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   133
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   134
    @Override
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   135
    public String toString() {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   136
        StringBuilder sb = new StringBuilder();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   137
        String method = request().method();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   138
        URI uri = request().uri();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   139
        String uristring = uri == null ? "" : uri.toString();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   140
        sb.append('(')
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   141
          .append(method)
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   142
          .append(" ")
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   143
          .append(uristring)
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   144
          .append(") ")
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   145
          .append(statusCode());
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   146
        return sb.toString();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   147
    }
53271
14d078fd74cb 8216478: Cleanup HttpResponseImpl back reference to HttpConnection
dfuchs
parents: 49765
diff changeset
   148
14d078fd74cb 8216478: Cleanup HttpResponseImpl back reference to HttpConnection
dfuchs
parents: 49765
diff changeset
   149
    /**
14d078fd74cb 8216478: Cleanup HttpResponseImpl back reference to HttpConnection
dfuchs
parents: 49765
diff changeset
   150
     * An auxiliary class used for RawChannel creation when creating a WebSocket.
14d078fd74cb 8216478: Cleanup HttpResponseImpl back reference to HttpConnection
dfuchs
parents: 49765
diff changeset
   151
     * This avoids keeping around references to connection/exchange in the
14d078fd74cb 8216478: Cleanup HttpResponseImpl back reference to HttpConnection
dfuchs
parents: 49765
diff changeset
   152
     * regular HttpResponse case. Only those responses corresponding to an
14d078fd74cb 8216478: Cleanup HttpResponseImpl back reference to HttpConnection
dfuchs
parents: 49765
diff changeset
   153
     * initial WebSocket request have a RawChannelProvider.
14d078fd74cb 8216478: Cleanup HttpResponseImpl back reference to HttpConnection
dfuchs
parents: 49765
diff changeset
   154
     */
14d078fd74cb 8216478: Cleanup HttpResponseImpl back reference to HttpConnection
dfuchs
parents: 49765
diff changeset
   155
    private static final class RawChannelProvider implements RawChannel.Provider {
14d078fd74cb 8216478: Cleanup HttpResponseImpl back reference to HttpConnection
dfuchs
parents: 49765
diff changeset
   156
        private final HttpConnection connection;
14d078fd74cb 8216478: Cleanup HttpResponseImpl back reference to HttpConnection
dfuchs
parents: 49765
diff changeset
   157
        private final Exchange<?> exchange;
14d078fd74cb 8216478: Cleanup HttpResponseImpl back reference to HttpConnection
dfuchs
parents: 49765
diff changeset
   158
        private RawChannel rawchan;
14d078fd74cb 8216478: Cleanup HttpResponseImpl back reference to HttpConnection
dfuchs
parents: 49765
diff changeset
   159
        RawChannelProvider(HttpConnection conn, Exchange<?> exch) {
14d078fd74cb 8216478: Cleanup HttpResponseImpl back reference to HttpConnection
dfuchs
parents: 49765
diff changeset
   160
            connection = conn;
14d078fd74cb 8216478: Cleanup HttpResponseImpl back reference to HttpConnection
dfuchs
parents: 49765
diff changeset
   161
            exchange = exch;
14d078fd74cb 8216478: Cleanup HttpResponseImpl back reference to HttpConnection
dfuchs
parents: 49765
diff changeset
   162
        }
14d078fd74cb 8216478: Cleanup HttpResponseImpl back reference to HttpConnection
dfuchs
parents: 49765
diff changeset
   163
14d078fd74cb 8216478: Cleanup HttpResponseImpl back reference to HttpConnection
dfuchs
parents: 49765
diff changeset
   164
        static RawChannelProvider create(Response resp, Exchange<?> exch) {
14d078fd74cb 8216478: Cleanup HttpResponseImpl back reference to HttpConnection
dfuchs
parents: 49765
diff changeset
   165
            if (resp.request().isWebSocket()) {
14d078fd74cb 8216478: Cleanup HttpResponseImpl back reference to HttpConnection
dfuchs
parents: 49765
diff changeset
   166
                return new RawChannelProvider(connection(resp, exch), exch);
14d078fd74cb 8216478: Cleanup HttpResponseImpl back reference to HttpConnection
dfuchs
parents: 49765
diff changeset
   167
            }
14d078fd74cb 8216478: Cleanup HttpResponseImpl back reference to HttpConnection
dfuchs
parents: 49765
diff changeset
   168
            return null;
14d078fd74cb 8216478: Cleanup HttpResponseImpl back reference to HttpConnection
dfuchs
parents: 49765
diff changeset
   169
        }
14d078fd74cb 8216478: Cleanup HttpResponseImpl back reference to HttpConnection
dfuchs
parents: 49765
diff changeset
   170
14d078fd74cb 8216478: Cleanup HttpResponseImpl back reference to HttpConnection
dfuchs
parents: 49765
diff changeset
   171
        @Override
14d078fd74cb 8216478: Cleanup HttpResponseImpl back reference to HttpConnection
dfuchs
parents: 49765
diff changeset
   172
        public synchronized RawChannel rawChannel() {
14d078fd74cb 8216478: Cleanup HttpResponseImpl back reference to HttpConnection
dfuchs
parents: 49765
diff changeset
   173
            if (rawchan == null) {
14d078fd74cb 8216478: Cleanup HttpResponseImpl back reference to HttpConnection
dfuchs
parents: 49765
diff changeset
   174
                ExchangeImpl<?> exchImpl = exchangeImpl();
14d078fd74cb 8216478: Cleanup HttpResponseImpl back reference to HttpConnection
dfuchs
parents: 49765
diff changeset
   175
                if (!(exchImpl instanceof Http1Exchange)) {
14d078fd74cb 8216478: Cleanup HttpResponseImpl back reference to HttpConnection
dfuchs
parents: 49765
diff changeset
   176
                    // RawChannel is only used for WebSocket - and WebSocket
14d078fd74cb 8216478: Cleanup HttpResponseImpl back reference to HttpConnection
dfuchs
parents: 49765
diff changeset
   177
                    // is not supported over HTTP/2 yet, so we should not come
14d078fd74cb 8216478: Cleanup HttpResponseImpl back reference to HttpConnection
dfuchs
parents: 49765
diff changeset
   178
                    // here. Getting a RawChannel over HTTP/2 might be supported
14d078fd74cb 8216478: Cleanup HttpResponseImpl back reference to HttpConnection
dfuchs
parents: 49765
diff changeset
   179
                    // in the future, but it would entail retrieving any left over
14d078fd74cb 8216478: Cleanup HttpResponseImpl back reference to HttpConnection
dfuchs
parents: 49765
diff changeset
   180
                    // bytes that might have been read but not consumed by the
14d078fd74cb 8216478: Cleanup HttpResponseImpl back reference to HttpConnection
dfuchs
parents: 49765
diff changeset
   181
                    // HTTP/2 connection.
14d078fd74cb 8216478: Cleanup HttpResponseImpl back reference to HttpConnection
dfuchs
parents: 49765
diff changeset
   182
                    throw new UnsupportedOperationException("RawChannel is not supported over HTTP/2");
14d078fd74cb 8216478: Cleanup HttpResponseImpl back reference to HttpConnection
dfuchs
parents: 49765
diff changeset
   183
                }
14d078fd74cb 8216478: Cleanup HttpResponseImpl back reference to HttpConnection
dfuchs
parents: 49765
diff changeset
   184
                // Http1Exchange may have some remaining bytes in its
14d078fd74cb 8216478: Cleanup HttpResponseImpl back reference to HttpConnection
dfuchs
parents: 49765
diff changeset
   185
                // internal buffer.
14d078fd74cb 8216478: Cleanup HttpResponseImpl back reference to HttpConnection
dfuchs
parents: 49765
diff changeset
   186
                Supplier<ByteBuffer> initial = ((Http1Exchange<?>) exchImpl)::drainLeftOverBytes;
14d078fd74cb 8216478: Cleanup HttpResponseImpl back reference to HttpConnection
dfuchs
parents: 49765
diff changeset
   187
                rawchan = new RawChannelTube(connection, initial);
14d078fd74cb 8216478: Cleanup HttpResponseImpl back reference to HttpConnection
dfuchs
parents: 49765
diff changeset
   188
            }
14d078fd74cb 8216478: Cleanup HttpResponseImpl back reference to HttpConnection
dfuchs
parents: 49765
diff changeset
   189
            return rawchan;
14d078fd74cb 8216478: Cleanup HttpResponseImpl back reference to HttpConnection
dfuchs
parents: 49765
diff changeset
   190
        }
14d078fd74cb 8216478: Cleanup HttpResponseImpl back reference to HttpConnection
dfuchs
parents: 49765
diff changeset
   191
14d078fd74cb 8216478: Cleanup HttpResponseImpl back reference to HttpConnection
dfuchs
parents: 49765
diff changeset
   192
        private static HttpConnection connection(Response resp, Exchange<?> exch) {
14d078fd74cb 8216478: Cleanup HttpResponseImpl back reference to HttpConnection
dfuchs
parents: 49765
diff changeset
   193
            if (exch == null || exch.exchImpl == null) {
14d078fd74cb 8216478: Cleanup HttpResponseImpl back reference to HttpConnection
dfuchs
parents: 49765
diff changeset
   194
                assert resp.statusCode == 407;
14d078fd74cb 8216478: Cleanup HttpResponseImpl back reference to HttpConnection
dfuchs
parents: 49765
diff changeset
   195
                return null; // case of Proxy 407
14d078fd74cb 8216478: Cleanup HttpResponseImpl back reference to HttpConnection
dfuchs
parents: 49765
diff changeset
   196
            }
14d078fd74cb 8216478: Cleanup HttpResponseImpl back reference to HttpConnection
dfuchs
parents: 49765
diff changeset
   197
            return exch.exchImpl.connection();
14d078fd74cb 8216478: Cleanup HttpResponseImpl back reference to HttpConnection
dfuchs
parents: 49765
diff changeset
   198
        }
14d078fd74cb 8216478: Cleanup HttpResponseImpl back reference to HttpConnection
dfuchs
parents: 49765
diff changeset
   199
14d078fd74cb 8216478: Cleanup HttpResponseImpl back reference to HttpConnection
dfuchs
parents: 49765
diff changeset
   200
        private ExchangeImpl<?> exchangeImpl() {
14d078fd74cb 8216478: Cleanup HttpResponseImpl back reference to HttpConnection
dfuchs
parents: 49765
diff changeset
   201
            return exchange != null ? exchange.exchImpl : null;
14d078fd74cb 8216478: Cleanup HttpResponseImpl back reference to HttpConnection
dfuchs
parents: 49765
diff changeset
   202
        }
14d078fd74cb 8216478: Cleanup HttpResponseImpl back reference to HttpConnection
dfuchs
parents: 49765
diff changeset
   203
14d078fd74cb 8216478: Cleanup HttpResponseImpl back reference to HttpConnection
dfuchs
parents: 49765
diff changeset
   204
    }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   205
}