|
| 1 | +/* |
| 2 | + * Copyright 2002-2023 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.web.service.invoker; |
| 18 | + |
| 19 | +import java.time.Duration; |
| 20 | + |
| 21 | +import reactor.core.publisher.Flux; |
| 22 | +import reactor.core.publisher.Mono; |
| 23 | + |
| 24 | +import org.springframework.core.ParameterizedTypeReference; |
| 25 | +import org.springframework.core.ReactiveAdapterRegistry; |
| 26 | +import org.springframework.http.HttpHeaders; |
| 27 | +import org.springframework.http.ResponseEntity; |
| 28 | +import org.springframework.lang.Nullable; |
| 29 | +import org.springframework.util.Assert; |
| 30 | + |
| 31 | +/** |
| 32 | + * |
| 33 | + * @author Rossen Stoyanchev |
| 34 | + * @since 6.1 |
| 35 | + */ |
| 36 | +@SuppressWarnings("removal") |
| 37 | +public abstract class AbstractReactorHttpExchangeAdapter |
| 38 | + implements ReactorHttpExchangeAdapter, org.springframework.web.service.invoker.HttpClientAdapter { |
| 39 | + |
| 40 | + private ReactiveAdapterRegistry reactiveAdapterRegistry = ReactiveAdapterRegistry.getSharedInstance(); |
| 41 | + |
| 42 | + @Nullable |
| 43 | + private Duration blockTimeout; |
| 44 | + |
| 45 | + |
| 46 | + /** |
| 47 | + * Protected constructor, for subclasses. |
| 48 | + */ |
| 49 | + protected AbstractReactorHttpExchangeAdapter() { |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | + /** |
| 54 | + * |
| 55 | + * @param reactiveAdapterRegistry |
| 56 | + */ |
| 57 | + public void setReactiveAdapterRegistry(ReactiveAdapterRegistry reactiveAdapterRegistry) { |
| 58 | + this.reactiveAdapterRegistry = reactiveAdapterRegistry; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * |
| 63 | + * @return |
| 64 | + */ |
| 65 | + @Override |
| 66 | + public ReactiveAdapterRegistry getReactiveAdapterRegistry() { |
| 67 | + return this.reactiveAdapterRegistry; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * |
| 72 | + * @param blockTimeout |
| 73 | + */ |
| 74 | + public void setBlockTimeout(@Nullable Duration blockTimeout) { |
| 75 | + this.blockTimeout = blockTimeout; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * |
| 80 | + * @return |
| 81 | + */ |
| 82 | + @Override |
| 83 | + @Nullable |
| 84 | + public Duration getBlockTimeout() { |
| 85 | + return this.blockTimeout; |
| 86 | + } |
| 87 | + |
| 88 | + |
| 89 | + @Override |
| 90 | + public void exchange(HttpRequestValues requestValues) { |
| 91 | + if (this.blockTimeout != null) { |
| 92 | + exchangeForMono(requestValues).block(this.blockTimeout); |
| 93 | + } |
| 94 | + else { |
| 95 | + exchangeForMono(requestValues).block(); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public HttpHeaders exchangeForHeaders(HttpRequestValues requestValues) { |
| 101 | + HttpHeaders headers = (this.blockTimeout != null ? |
| 102 | + exchangeForHeadersMono(requestValues).block(this.blockTimeout) : |
| 103 | + exchangeForHeadersMono(requestValues).block()); |
| 104 | + Assert.state(headers != null, "Expected HttpHeaders"); |
| 105 | + return headers; |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public <T> T exchangeForBody(HttpRequestValues requestValues, ParameterizedTypeReference<T> bodyType) { |
| 110 | + return (this.blockTimeout != null ? |
| 111 | + exchangeForBodyMono(requestValues, bodyType).block(this.blockTimeout) : |
| 112 | + exchangeForBodyMono(requestValues, bodyType).block()); |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public ResponseEntity<Void> exchangeForBodilessEntity(HttpRequestValues requestValues) { |
| 117 | + ResponseEntity<Void> entity = (this.blockTimeout != null ? |
| 118 | + exchangeForBodilessEntityMono(requestValues).block(this.blockTimeout) : |
| 119 | + exchangeForBodilessEntityMono(requestValues).block()); |
| 120 | + Assert.state(entity != null, "Expected ResponseEntity"); |
| 121 | + return entity; |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public <T> ResponseEntity<T> exchangeForEntity(HttpRequestValues requestValues, ParameterizedTypeReference<T> bodyType) { |
| 126 | + ResponseEntity<T> entity = (this.blockTimeout != null ? |
| 127 | + exchangeForEntityMono(requestValues, bodyType).block(this.blockTimeout) : |
| 128 | + exchangeForEntityMono(requestValues, bodyType).block()); |
| 129 | + Assert.state(entity != null, "Expected ResponseEntity"); |
| 130 | + return entity; |
| 131 | + } |
| 132 | + |
| 133 | + |
| 134 | + // HttpClientAdapter implementation |
| 135 | + |
| 136 | + @Override |
| 137 | + public Mono<Void> requestToVoid(HttpRequestValues requestValues) { |
| 138 | + return exchangeForMono(requestValues); |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + public Mono<HttpHeaders> requestToHeaders(HttpRequestValues requestValues) { |
| 143 | + return exchangeForHeadersMono(requestValues); |
| 144 | + } |
| 145 | + |
| 146 | + @Override |
| 147 | + public <T> Mono<T> requestToBody(HttpRequestValues requestValues, ParameterizedTypeReference<T> bodyType) { |
| 148 | + return exchangeForBodyMono(requestValues, bodyType); |
| 149 | + } |
| 150 | + |
| 151 | + @Override |
| 152 | + public <T> Flux<T> requestToBodyFlux(HttpRequestValues requestValues, ParameterizedTypeReference<T> bodyType) { |
| 153 | + return exchangeForBodyFlux(requestValues, bodyType); |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + public Mono<ResponseEntity<Void>> requestToBodilessEntity(HttpRequestValues requestValues) { |
| 158 | + return exchangeForBodilessEntityMono(requestValues); |
| 159 | + } |
| 160 | + |
| 161 | + @Override |
| 162 | + public <T> Mono<ResponseEntity<T>> requestToEntity( |
| 163 | + HttpRequestValues requestValues, ParameterizedTypeReference<T> bodyType) { |
| 164 | + |
| 165 | + return exchangeForEntityMono(requestValues, bodyType); |
| 166 | + } |
| 167 | + |
| 168 | + @Override |
| 169 | + public <T> Mono<ResponseEntity<Flux<T>>> requestToEntityFlux( |
| 170 | + HttpRequestValues requestValues, ParameterizedTypeReference<T> bodyType) { |
| 171 | + |
| 172 | + return exchangeForEntityFlux(requestValues, bodyType); |
| 173 | + } |
| 174 | + |
| 175 | +} |
0 commit comments