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