src/java.net.http/share/classes/java/net/http/internal/ResponseBodyHandlers.java
author chegar
Wed, 07 Feb 2018 14:17:24 +0000
branchhttp-client-branch
changeset 56089 42208b2f224e
parent 56080 src/jdk.incubator.httpclient/share/classes/jdk/incubator/http/internal/ResponseBodyHandlers.java@64846522c0d5
permissions -rw-r--r--
http-client-branch: move to standard package and module name
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
56079
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
     1
/*
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
     4
 *
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    10
 *
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    15
 * accompanied this code).
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    16
 *
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    20
 *
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    23
 * questions.
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    24
 */
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    25
56089
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56080
diff changeset
    26
package java.net.http.internal;
56079
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    27
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    28
import java.io.IOException;
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    29
import java.net.URI;
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    30
import java.nio.file.OpenOption;
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    31
import java.nio.file.Path;
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    32
import java.nio.file.Paths;
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    33
import java.security.AccessControlContext;
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    34
import java.util.concurrent.CompletableFuture;
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    35
import java.util.concurrent.ConcurrentMap;
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    36
import java.util.function.Function;
56089
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56080
diff changeset
    37
import java.net.http.HttpHeaders;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56080
diff changeset
    38
import java.net.http.HttpRequest;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56080
diff changeset
    39
import java.net.http.HttpResponse;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56080
diff changeset
    40
import java.net.http.HttpResponse.BodyHandler;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56080
diff changeset
    41
import java.net.http.HttpResponse.BodySubscriber;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56080
diff changeset
    42
import java.net.http.internal.ResponseSubscribers.PathSubscriber;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56080
diff changeset
    43
import static java.net.http.internal.common.Utils.unchecked;
56079
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    44
56080
64846522c0d5 http-client-branch: add several private <init> methods
chegar
parents: 56079
diff changeset
    45
public final class ResponseBodyHandlers {
64846522c0d5 http-client-branch: add several private <init> methods
chegar
parents: 56079
diff changeset
    46
64846522c0d5 http-client-branch: add several private <init> methods
chegar
parents: 56079
diff changeset
    47
    private ResponseBodyHandlers() { }
56079
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    48
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    49
    /**
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    50
     * A Path body handler.
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    51
     *
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    52
     * Note: Exists mainly too allow setting of the senders ACC post creation of
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    53
     * the handler.
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    54
     */
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    55
    public static class PathBodyHandler implements UntrustedBodyHandler<Path> {
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    56
        private final Path file;
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    57
        private final OpenOption[]openOptions;
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    58
        private volatile AccessControlContext acc;
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    59
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    60
        public PathBodyHandler(Path file, OpenOption... openOptions) {
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    61
            this.file = file;
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    62
            this.openOptions = openOptions;
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    63
        }
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    64
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    65
        @Override
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    66
        public void setAccessControlContext(AccessControlContext acc) {
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    67
            this.acc = acc;
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    68
        }
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    69
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    70
        @Override
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    71
        public BodySubscriber<Path> apply(int statusCode, HttpHeaders headers) {
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    72
            PathSubscriber bs = (PathSubscriber) asFileImpl(file, openOptions);
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    73
            bs.setAccessControlContext(acc);
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    74
            return bs;
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    75
        }
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    76
    }
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    77
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    78
    /** With push promise Map implementation */
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    79
    public static class PushPromisesHandlerWithMap<T>
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    80
        implements HttpResponse.PushPromiseHandler<T>
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    81
    {
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    82
        private final ConcurrentMap<HttpRequest,CompletableFuture<HttpResponse<T>>> pushPromisesMap;
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    83
        private final Function<HttpRequest,BodyHandler<T>> pushPromiseHandler;
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    84
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    85
        public PushPromisesHandlerWithMap(Function<HttpRequest,BodyHandler<T>> pushPromiseHandler,
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    86
                                          ConcurrentMap<HttpRequest,CompletableFuture<HttpResponse<T>>> pushPromisesMap) {
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    87
            this.pushPromiseHandler = pushPromiseHandler;
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    88
            this.pushPromisesMap = pushPromisesMap;
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    89
        }
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    90
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    91
        @Override
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    92
        public void applyPushPromise(
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    93
                HttpRequest initiatingRequest, HttpRequest pushRequest,
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    94
                Function<BodyHandler<T>,CompletableFuture<HttpResponse<T>>> acceptor)
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    95
        {
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    96
            URI initiatingURI = initiatingRequest.uri();
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    97
            URI pushRequestURI = pushRequest.uri();
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    98
            if (!initiatingURI.getHost().equalsIgnoreCase(pushRequestURI.getHost()))
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    99
                return;
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   100
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   101
            int initiatingPort = initiatingURI.getPort();
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   102
            if (initiatingPort == -1 ) {
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   103
                if ("https".equalsIgnoreCase(initiatingURI.getScheme()))
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   104
                    initiatingPort = 443;
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   105
                else
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   106
                    initiatingPort = 80;
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   107
            }
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   108
            int pushPort = pushRequestURI.getPort();
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   109
            if (pushPort == -1 ) {
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   110
                if ("https".equalsIgnoreCase(pushRequestURI.getScheme()))
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   111
                    pushPort = 443;
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   112
                else
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   113
                    pushPort = 80;
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   114
            }
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   115
            if (initiatingPort != pushPort)
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   116
                return;
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   117
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   118
            CompletableFuture<HttpResponse<T>> cf =
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   119
                    acceptor.apply(pushPromiseHandler.apply(pushRequest));
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   120
            pushPromisesMap.put(pushRequest, cf);
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   121
        }
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   122
    }
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   123
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   124
    // Similar to Path body handler, but for file download. Supports setting ACC.
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   125
    public static class FileDownloadBodyHandler implements UntrustedBodyHandler<Path> {
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   126
        private final Path directory;
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   127
        private final OpenOption[]openOptions;
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   128
        private volatile AccessControlContext acc;
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   129
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   130
        public FileDownloadBodyHandler(Path directory, OpenOption... openOptions) {
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   131
            this.directory = directory;
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   132
            this.openOptions = openOptions;
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   133
        }
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   134
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   135
        @Override
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   136
        public void setAccessControlContext(AccessControlContext acc) {
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   137
            this.acc = acc;
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   138
        }
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   139
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   140
        @Override
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   141
        public BodySubscriber<Path> apply(int statusCode, HttpHeaders headers) {
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   142
            String dispoHeader = headers.firstValue("Content-Disposition")
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   143
                    .orElseThrow(() -> unchecked(new IOException("No Content-Disposition")));
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   144
            if (!dispoHeader.startsWith("attachment;")) {
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   145
                throw unchecked(new IOException("Unknown Content-Disposition type"));
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   146
            }
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   147
            int n = dispoHeader.indexOf("filename=");
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   148
            if (n == -1) {
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   149
                throw unchecked(new IOException("Bad Content-Disposition type"));
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   150
            }
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   151
            int lastsemi = dispoHeader.lastIndexOf(';');
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   152
            String disposition;
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   153
            if (lastsemi < n) {
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   154
                disposition = dispoHeader.substring(n + 9);
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   155
            } else {
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   156
                disposition = dispoHeader.substring(n + 9, lastsemi);
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   157
            }
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   158
            Path file = Paths.get(directory.toString(), disposition);
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   159
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   160
            PathSubscriber bs = (PathSubscriber)asFileImpl(file, openOptions);
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   161
            bs.setAccessControlContext(acc);
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   162
            return bs;
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   163
        }
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   164
    }
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   165
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   166
    // no security check
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   167
    private static BodySubscriber<Path> asFileImpl(Path file, OpenOption... openOptions) {
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   168
        return new ResponseSubscribers.PathSubscriber(file, openOptions);
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   169
    }
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   170
}