author | michaelm |
Wed, 22 Nov 2017 11:21:36 +0000 | |
branch | http-client-branch |
changeset 55852 | 32f6aefec11e |
parent 55763 | 634d8e14c172 |
child 55973 | 4d9b002587db |
permissions | -rw-r--r-- |
55763 | 1 |
/* |
2 |
* Copyright (c) 2016, 2017, 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. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
22 |
*/ |
|
23 |
||
24 |
import java.io.IOException; |
|
25 |
import java.io.InputStream; |
|
55852
32f6aefec11e
http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents:
55763
diff
changeset
|
26 |
import java.io.OutputStream; |
55763 | 27 |
import java.util.function.Supplier; |
28 |
import jdk.incubator.http.internal.common.HttpHeadersImpl; |
|
29 |
||
30 |
public class Http2RedirectHandler implements Http2Handler { |
|
31 |
||
32 |
final Supplier<String> supplier; |
|
33 |
||
34 |
public Http2RedirectHandler(Supplier<String> redirectSupplier) { |
|
35 |
supplier = redirectSupplier; |
|
36 |
} |
|
37 |
||
38 |
@Override |
|
39 |
public void handle(Http2TestExchange t) throws IOException { |
|
55852
32f6aefec11e
http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents:
55763
diff
changeset
|
40 |
examineExchange(t); |
55763 | 41 |
try (InputStream is = t.getRequestBody()) { |
42 |
is.readAllBytes(); |
|
43 |
String location = supplier.get(); |
|
55852
32f6aefec11e
http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents:
55763
diff
changeset
|
44 |
System.err.printf("RedirectHandler request to %s from %s\n", |
32f6aefec11e
http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents:
55763
diff
changeset
|
45 |
t.getRequestURI().toString(), t.getRemoteAddress().toString()); |
55763 | 46 |
System.err.println("Redirecting to: " + location); |
47 |
HttpHeadersImpl map1 = t.getResponseHeaders(); |
|
48 |
map1.addHeader("Location", location); |
|
55852
32f6aefec11e
http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents:
55763
diff
changeset
|
49 |
t.sendResponseHeaders(301, 1024); |
32f6aefec11e
http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents:
55763
diff
changeset
|
50 |
byte[] bb = new byte[1024]; |
32f6aefec11e
http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents:
55763
diff
changeset
|
51 |
OutputStream os = t.getResponseBody(); |
32f6aefec11e
http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents:
55763
diff
changeset
|
52 |
os.write(bb); |
32f6aefec11e
http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents:
55763
diff
changeset
|
53 |
os.close(); |
55763 | 54 |
t.close(); |
55 |
} |
|
56 |
} |
|
55852
32f6aefec11e
http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents:
55763
diff
changeset
|
57 |
|
32f6aefec11e
http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents:
55763
diff
changeset
|
58 |
// override in sub-class to examine the exchange, but don't |
32f6aefec11e
http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents:
55763
diff
changeset
|
59 |
// alter transaction state by reading the request body etc. |
32f6aefec11e
http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents:
55763
diff
changeset
|
60 |
protected void examineExchange(Http2TestExchange t) { |
32f6aefec11e
http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents:
55763
diff
changeset
|
61 |
} |
55763 | 62 |
} |