|
| 1 | +/* |
| 2 | + * Copyright 2002-2019 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.http.client.reactive; |
| 18 | + |
| 19 | +import java.net.URI; |
| 20 | +import java.net.http.HttpClient; |
| 21 | +import java.net.http.HttpRequest; |
| 22 | +import java.net.http.HttpResponse; |
| 23 | +import java.nio.ByteBuffer; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Map; |
| 26 | +import java.util.Set; |
| 27 | +import java.util.concurrent.Flow; |
| 28 | +import java.util.function.Function; |
| 29 | +import java.util.stream.Collectors; |
| 30 | + |
| 31 | +import org.reactivestreams.Publisher; |
| 32 | +import reactor.adapter.JdkFlowAdapter; |
| 33 | +import reactor.core.publisher.Flux; |
| 34 | +import reactor.core.publisher.Mono; |
| 35 | + |
| 36 | +import org.springframework.core.io.buffer.DataBuffer; |
| 37 | +import org.springframework.core.io.buffer.DataBufferFactory; |
| 38 | +import org.springframework.http.HttpHeaders; |
| 39 | +import org.springframework.http.HttpMethod; |
| 40 | +import org.springframework.util.Assert; |
| 41 | + |
| 42 | +/** |
| 43 | + * {@link ClientHttpRequest} implementation for the Java 11 HTTP client. |
| 44 | + * |
| 45 | + * @author Julien Eyraud |
| 46 | + * @since 5.2 |
| 47 | + * @see <a href="https://docs.oracle.com/en/java/javase/11/docs/api/java.net.http/java/net/http/HttpClient.html">Java HttpClient</a> |
| 48 | + */ |
| 49 | +class JdkClientHttpRequest extends AbstractClientHttpRequest { |
| 50 | + |
| 51 | + private static final Set<String> DISALLOWED_HEADERS = Set.of("connection", "content-length", "date", "expect", "from", "host", "upgrade", "via", "warning"); |
| 52 | + |
| 53 | + private final HttpClient httpClient; |
| 54 | + |
| 55 | + private final HttpMethod method; |
| 56 | + |
| 57 | + private final URI uri; |
| 58 | + |
| 59 | + private final HttpRequest.Builder builder; |
| 60 | + |
| 61 | + private final DataBufferFactory bufferFactory; |
| 62 | + |
| 63 | + private Mono<ClientHttpResponse> response; |
| 64 | + |
| 65 | + |
| 66 | + public JdkClientHttpRequest(final HttpClient httpClient, final HttpMethod httpMethod, final URI uri, final DataBufferFactory bufferFactory) { |
| 67 | + Assert.notNull(httpClient, "HttpClient should not be null"); |
| 68 | + Assert.notNull(httpMethod, "HttpMethod should not be null"); |
| 69 | + Assert.notNull(uri, "URI should not be null"); |
| 70 | + Assert.notNull(bufferFactory, "DataBufferFactory should not be null"); |
| 71 | + this.httpClient = httpClient; |
| 72 | + this.method = httpMethod; |
| 73 | + this.uri = uri; |
| 74 | + this.builder = HttpRequest.newBuilder(uri); |
| 75 | + this.bufferFactory = bufferFactory; |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + protected void applyHeaders() { |
| 80 | + HttpHeaders headers = getHeaders(); |
| 81 | + for (Map.Entry<String, List<String>> header : getHeaders().entrySet()) { |
| 82 | + if (!DISALLOWED_HEADERS.contains(header.getKey().toLowerCase())) { |
| 83 | + for (String value : header.getValue()) { |
| 84 | + this.builder.header(header.getKey(), value); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + if (!headers.containsKey(HttpHeaders.ACCEPT)) { |
| 89 | + this.builder.header(HttpHeaders.ACCEPT, "*/*"); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + protected void applyCookies() { |
| 95 | + final String cookies = getCookies().values().stream().flatMap(List::stream).map(c -> c.getName() + "=" + c.getValue()).collect(Collectors.joining("; ")); |
| 96 | + this.builder.header(HttpHeaders.COOKIE, cookies); |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public HttpMethod getMethod() { |
| 101 | + return this.method; |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public URI getURI() { |
| 106 | + return this.uri; |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public DataBufferFactory bufferFactory() { |
| 111 | + return this.bufferFactory; |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + @SuppressWarnings("unchecked") |
| 116 | + public <T> T getNativeRequest() { |
| 117 | + return (T) this.builder.build(); |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public Mono<Void> writeWith(final Publisher<? extends DataBuffer> body) { |
| 122 | + return doCommit(() -> { |
| 123 | + final Flow.Publisher<ByteBuffer> flowAdapter = JdkFlowAdapter.publisherToFlowPublisher(Flux.from(body).map(DataBuffer::asByteBuffer)); |
| 124 | + final long contentLength = getHeaders().getContentLength(); |
| 125 | + final HttpRequest.BodyPublisher bodyPublisher = contentLength >= 0 ? HttpRequest.BodyPublishers.fromPublisher(flowAdapter, contentLength) |
| 126 | + : HttpRequest.BodyPublishers.fromPublisher(flowAdapter); |
| 127 | + this.response = Mono |
| 128 | + .fromCompletionStage(() -> this.httpClient.sendAsync(this.builder.method(this.method.name(), bodyPublisher).build(), HttpResponse.BodyHandlers.ofPublisher())) |
| 129 | + .map(r -> new JdkClientHttpResponse(r, this.bufferFactory)); |
| 130 | + return Mono.empty(); |
| 131 | + }); |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public Mono<Void> writeAndFlushWith(final Publisher<? extends Publisher<? extends DataBuffer>> body) { |
| 136 | + return writeWith(Flux.from(body).flatMap(Function.identity())); |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public Mono<Void> setComplete() { |
| 141 | + if (isCommitted()) { |
| 142 | + return Mono.empty(); |
| 143 | + } |
| 144 | + else { |
| 145 | + return doCommit(() -> { |
| 146 | + this.response = Mono |
| 147 | + .fromCompletionStage(() -> this.httpClient.sendAsync(this.builder.method(this.method.name(), HttpRequest.BodyPublishers.noBody()).build(), HttpResponse.BodyHandlers.ofPublisher())) |
| 148 | + .map(r -> new JdkClientHttpResponse(r, this.bufferFactory)); |
| 149 | + return Mono.empty(); |
| 150 | + }); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + public Mono<ClientHttpResponse> getResponse() { |
| 155 | + return this.response; |
| 156 | + } |
| 157 | +} |
0 commit comments