48083
|
1 |
/*
|
49765
|
2 |
* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
|
48083
|
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;
|
|
26 |
import java.io.OutputStream;
|
|
27 |
import java.util.function.Supplier;
|
50681
|
28 |
import jdk.internal.net.http.common.HttpHeadersBuilder;
|
48083
|
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 {
|
|
40 |
examineExchange(t);
|
|
41 |
try (InputStream is = t.getRequestBody()) {
|
|
42 |
is.readAllBytes();
|
|
43 |
String location = supplier.get();
|
|
44 |
System.err.printf("RedirectHandler request to %s from %s\n",
|
|
45 |
t.getRequestURI().toString(), t.getRemoteAddress().toString());
|
|
46 |
System.err.println("Redirecting to: " + location);
|
50681
|
47 |
HttpHeadersBuilder headersBuilder = t.getResponseHeaders();
|
|
48 |
headersBuilder.addHeader("Location", location);
|
48083
|
49 |
t.sendResponseHeaders(301, 1024);
|
|
50 |
byte[] bb = new byte[1024];
|
|
51 |
OutputStream os = t.getResponseBody();
|
|
52 |
os.write(bb);
|
|
53 |
os.close();
|
|
54 |
t.close();
|
|
55 |
}
|
|
56 |
}
|
|
57 |
|
|
58 |
// override in sub-class to examine the exchange, but don't
|
|
59 |
// alter transaction state by reading the request body etc.
|
|
60 |
protected void examineExchange(Http2TestExchange t) {
|
|
61 |
}
|
|
62 |
}
|