|
| 1 | +/** |
| 2 | + * Copyright 2012-2018 The Feign Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
| 5 | + * in compliance with the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License |
| 10 | + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 11 | + * or implied. See the License for the specific language governing permissions and limitations under |
| 12 | + * the License. |
| 13 | + */ |
| 14 | +package feign.httpclient; |
| 15 | + |
| 16 | +import java.io.ByteArrayInputStream; |
| 17 | +import java.io.IOException; |
| 18 | +import java.net.URI; |
| 19 | +import java.net.URISyntaxException; |
| 20 | +import java.net.http.HttpClient; |
| 21 | +import java.net.http.HttpClient.Redirect; |
| 22 | +import java.net.http.HttpRequest; |
| 23 | +import java.net.http.HttpRequest.BodyPublisher; |
| 24 | +import java.net.http.HttpRequest.BodyPublishers; |
| 25 | +import java.net.http.HttpResponse; |
| 26 | +import java.net.http.HttpResponse.BodyHandlers; |
| 27 | +import java.util.*; |
| 28 | +import java.util.function.Function; |
| 29 | +import java.util.stream.Collectors; |
| 30 | +import feign.Client; |
| 31 | +import feign.Request; |
| 32 | +import feign.Request.Options; |
| 33 | +import feign.Response; |
| 34 | + |
| 35 | +public class Http2Client implements Client { |
| 36 | + |
| 37 | + @Override |
| 38 | + public Response execute(Request request, Options options) throws IOException { |
| 39 | + final HttpClient client = HttpClient.newBuilder() |
| 40 | + .followRedirects(Redirect.ALWAYS) |
| 41 | + .build(); |
| 42 | + |
| 43 | + URI uri; |
| 44 | + try { |
| 45 | + uri = new URI(request.url()); |
| 46 | + } catch (final URISyntaxException e) { |
| 47 | + throw new IOException("Invalid uri " + request.url(), e); |
| 48 | + } |
| 49 | + |
| 50 | + final BodyPublisher body; |
| 51 | + if (request.body() == null) { |
| 52 | + body = BodyPublishers.noBody(); |
| 53 | + } else { |
| 54 | + body = BodyPublishers.ofByteArray(request.body()); |
| 55 | + } |
| 56 | + |
| 57 | + final HttpRequest httpRequest = HttpRequest.newBuilder() |
| 58 | + .uri(uri) |
| 59 | + .method(request.method(), body) |
| 60 | + .headers(asString(filterRestrictedHeaders(request.headers()))) |
| 61 | + .build(); |
| 62 | + |
| 63 | + HttpResponse<byte[]> httpResponse; |
| 64 | + try { |
| 65 | + httpResponse = client.send(httpRequest, BodyHandlers.ofByteArray()); |
| 66 | + } catch (final InterruptedException e) { |
| 67 | + throw new IOException("Invalid uri " + request.url(), e); |
| 68 | + } |
| 69 | + |
| 70 | + System.out.println(httpResponse.headers().map()); |
| 71 | + |
| 72 | + final OptionalLong length = httpResponse.headers().firstValueAsLong("Content-Length"); |
| 73 | + |
| 74 | + final Response response = Response.builder() |
| 75 | + .body(new ByteArrayInputStream(httpResponse.body()), |
| 76 | + length.isPresent() ? (int) length.getAsLong() : null) |
| 77 | + .reason(httpResponse.headers().firstValue("Reason-Phrase").orElse(null)) |
| 78 | + .request(request) |
| 79 | + .status(httpResponse.statusCode()) |
| 80 | + .headers(castMapCollectType(httpResponse.headers().map())) |
| 81 | + .build(); |
| 82 | + return response; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * There is a bunch o headers that the http2 client do not allow to be set. |
| 87 | + * |
| 88 | + * @see jdk.internal.net.http.common.Utils.DISALLOWED_HEADERS_SET |
| 89 | + */ |
| 90 | + private static final Set<String> DISALLOWED_HEADERS_SET; |
| 91 | + |
| 92 | + static { |
| 93 | + // A case insensitive TreeSet of strings. |
| 94 | + final TreeSet<String> treeSet = new TreeSet<>(String.CASE_INSENSITIVE_ORDER); |
| 95 | + treeSet.addAll(Set.of("connection", "content-length", "date", "expect", "from", "host", |
| 96 | + "origin", "referer", "upgrade", "via", "warning")); |
| 97 | + DISALLOWED_HEADERS_SET = Collections.unmodifiableSet(treeSet); |
| 98 | + } |
| 99 | + |
| 100 | + private Map<String, Collection<String>> filterRestrictedHeaders(Map<String, Collection<String>> headers) { |
| 101 | + return headers.keySet() |
| 102 | + .stream() |
| 103 | + .filter(headerName -> !DISALLOWED_HEADERS_SET.contains(headerName)) |
| 104 | + .collect(Collectors.toMap( |
| 105 | + Function.identity(), |
| 106 | + headers::get)); |
| 107 | + } |
| 108 | + |
| 109 | + private Map<String, Collection<String>> castMapCollectType(Map<String, List<String>> map) { |
| 110 | + final Map<String, Collection<String>> result = new HashMap<>(); |
| 111 | + map.forEach((key, value) -> result.put(key, new HashSet<>(value))); |
| 112 | + return result; |
| 113 | + } |
| 114 | + |
| 115 | + private String[] asString(Map<String, Collection<String>> headers) { |
| 116 | + return headers.entrySet().stream() |
| 117 | + .flatMap(entry -> entry.getValue() |
| 118 | + .stream() |
| 119 | + .map(value -> Arrays.asList(entry.getKey(), value)) |
| 120 | + .flatMap(List::stream)) |
| 121 | + .collect(Collectors.toList()) |
| 122 | + .toArray(new String[0]); |
| 123 | + } |
| 124 | + |
| 125 | +} |
0 commit comments