Browse Source

Merge branch '5.3.x' into main

pull/27927/head
rstoyanchev 4 years ago
parent
commit
6643efe0b4
  1. 14
      spring-web/src/main/java/org/springframework/http/client/reactive/AbstractClientHttpRequest.java
  2. 15
      spring-web/src/main/java/org/springframework/http/client/reactive/HttpComponentsClientHttpRequest.java
  3. 36
      spring-web/src/main/java/org/springframework/http/client/reactive/HttpComponentsHeadersAdapter.java
  4. 9
      spring-web/src/main/java/org/springframework/http/client/reactive/JettyClientHttpRequest.java
  5. 8
      spring-web/src/main/java/org/springframework/http/client/reactive/ReactorClientHttpRequest.java

14
spring-web/src/main/java/org/springframework/http/client/reactive/AbstractClientHttpRequest.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2020 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -81,7 +81,7 @@ public abstract class AbstractClientHttpRequest implements ClientHttpRequest {
return this.readOnlyHeaders; return this.readOnlyHeaders;
} }
else if (State.COMMITTED.equals(this.state.get())) { else if (State.COMMITTED.equals(this.state.get())) {
this.readOnlyHeaders = HttpHeaders.readOnlyHttpHeaders(this.headers); this.readOnlyHeaders = initReadOnlyHeaders();
return this.readOnlyHeaders; return this.readOnlyHeaders;
} }
else { else {
@ -89,6 +89,16 @@ public abstract class AbstractClientHttpRequest implements ClientHttpRequest {
} }
} }
/**
* Initialize the read-only headers after the request is committed.
* <p>By default, this method simply applies a read-only wrapper.
* Subclasses can do the same for headers from the native request.
* @since 5.3.15
*/
protected HttpHeaders initReadOnlyHeaders() {
return HttpHeaders.readOnlyHttpHeaders(this.headers);
}
@Override @Override
public MultiValueMap<String, HttpCookie> getCookies() { public MultiValueMap<String, HttpCookie> getCookies() {
if (State.COMMITTED.equals(this.state.get())) { if (State.COMMITTED.equals(this.state.get())) {

15
spring-web/src/main/java/org/springframework/http/client/reactive/HttpComponentsClientHttpRequest.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2021 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -61,6 +61,8 @@ class HttpComponentsClientHttpRequest extends AbstractClientHttpRequest {
@Nullable @Nullable
private Flux<ByteBuffer> byteBufferFlux; private Flux<ByteBuffer> byteBufferFlux;
private transient long contentLength = -1;
public HttpComponentsClientHttpRequest(HttpMethod method, URI uri, HttpClientContext context, public HttpComponentsClientHttpRequest(HttpMethod method, URI uri, HttpClientContext context,
DataBufferFactory dataBufferFactory) { DataBufferFactory dataBufferFactory) {
@ -127,6 +129,8 @@ class HttpComponentsClientHttpRequest extends AbstractClientHttpRequest {
if (!this.httpRequest.containsHeader(HttpHeaders.ACCEPT)) { if (!this.httpRequest.containsHeader(HttpHeaders.ACCEPT)) {
this.httpRequest.addHeader(HttpHeaders.ACCEPT, MediaType.ALL_VALUE); this.httpRequest.addHeader(HttpHeaders.ACCEPT, MediaType.ALL_VALUE);
} }
this.contentLength = headers.getContentLength();
} }
@Override @Override
@ -148,6 +152,11 @@ class HttpComponentsClientHttpRequest extends AbstractClientHttpRequest {
}); });
} }
@Override
protected HttpHeaders initReadOnlyHeaders() {
return HttpHeaders.readOnlyHttpHeaders(new HttpComponentsHeadersAdapter(this.httpRequest));
}
public AsyncRequestProducer toRequestProducer() { public AsyncRequestProducer toRequestProducer() {
ReactiveEntityProducer reactiveEntityProducer = null; ReactiveEntityProducer reactiveEntityProducer = null;
@ -157,8 +166,8 @@ class HttpComponentsClientHttpRequest extends AbstractClientHttpRequest {
if (getHeaders().getContentType() != null) { if (getHeaders().getContentType() != null) {
contentType = ContentType.parse(getHeaders().getContentType().toString()); contentType = ContentType.parse(getHeaders().getContentType().toString());
} }
reactiveEntityProducer = new ReactiveEntityProducer(this.byteBufferFlux, getHeaders().getContentLength(), reactiveEntityProducer = new ReactiveEntityProducer(
contentType, contentEncoding); this.byteBufferFlux, this.contentLength, contentType, contentEncoding);
} }
return new BasicRequestProducer(this.httpRequest, reactiveEntityProducer); return new BasicRequestProducer(this.httpRequest, reactiveEntityProducer);

36
spring-web/src/main/java/org/springframework/http/client/reactive/HttpComponentsHeadersAdapter.java

@ -28,7 +28,7 @@ import java.util.Map;
import java.util.Set; import java.util.Set;
import org.apache.hc.core5.http.Header; import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpResponse; import org.apache.hc.core5.http.HttpMessage;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
@ -44,23 +44,23 @@ import org.springframework.util.MultiValueMap;
*/ */
class HttpComponentsHeadersAdapter implements MultiValueMap<String, String> { class HttpComponentsHeadersAdapter implements MultiValueMap<String, String> {
private final HttpResponse response; private final HttpMessage message;
HttpComponentsHeadersAdapter(HttpResponse response) { HttpComponentsHeadersAdapter(HttpMessage message) {
this.response = response; this.message = message;
} }
@Override @Override
public String getFirst(String key) { public String getFirst(String key) {
Header header = this.response.getFirstHeader(key); Header header = this.message.getFirstHeader(key);
return (header != null ? header.getValue() : null); return (header != null ? header.getValue() : null);
} }
@Override @Override
public void add(String key, @Nullable String value) { public void add(String key, @Nullable String value) {
this.response.addHeader(key, value); this.message.addHeader(key, value);
} }
@Override @Override
@ -75,7 +75,7 @@ class HttpComponentsHeadersAdapter implements MultiValueMap<String, String> {
@Override @Override
public void set(String key, @Nullable String value) { public void set(String key, @Nullable String value) {
this.response.setHeader(key, value); this.message.setHeader(key, value);
} }
@Override @Override
@ -86,29 +86,29 @@ class HttpComponentsHeadersAdapter implements MultiValueMap<String, String> {
@Override @Override
public Map<String, String> toSingleValueMap() { public Map<String, String> toSingleValueMap() {
Map<String, String> map = CollectionUtils.newLinkedHashMap(size()); Map<String, String> map = CollectionUtils.newLinkedHashMap(size());
this.response.headerIterator().forEachRemaining(h -> map.putIfAbsent(h.getName(), h.getValue())); this.message.headerIterator().forEachRemaining(h -> map.putIfAbsent(h.getName(), h.getValue()));
return map; return map;
} }
@Override @Override
public int size() { public int size() {
return this.response.getHeaders().length; return this.message.getHeaders().length;
} }
@Override @Override
public boolean isEmpty() { public boolean isEmpty() {
return (this.response.getHeaders().length == 0); return (this.message.getHeaders().length == 0);
} }
@Override @Override
public boolean containsKey(Object key) { public boolean containsKey(Object key) {
return (key instanceof String headerName && this.response.containsHeader(headerName)); return (key instanceof String headerName && this.message.containsHeader(headerName));
} }
@Override @Override
public boolean containsValue(Object value) { public boolean containsValue(Object value) {
return (value instanceof String && return (value instanceof String &&
Arrays.stream(this.response.getHeaders()).anyMatch(h -> h.getValue().equals(value))); Arrays.stream(this.message.getHeaders()).anyMatch(h -> h.getValue().equals(value)));
} }
@Nullable @Nullable
@ -116,7 +116,7 @@ class HttpComponentsHeadersAdapter implements MultiValueMap<String, String> {
public List<String> get(Object key) { public List<String> get(Object key) {
List<String> values = null; List<String> values = null;
if (containsKey(key)) { if (containsKey(key)) {
Header[] headers = this.response.getHeaders((String) key); Header[] headers = this.message.getHeaders((String) key);
values = new ArrayList<>(headers.length); values = new ArrayList<>(headers.length);
for (Header header : headers) { for (Header header : headers) {
values.add(header.getValue()); values.add(header.getValue());
@ -138,7 +138,7 @@ class HttpComponentsHeadersAdapter implements MultiValueMap<String, String> {
public List<String> remove(Object key) { public List<String> remove(Object key) {
if (key instanceof String headerName) { if (key instanceof String headerName) {
List<String> oldValues = get(key); List<String> oldValues = get(key);
this.response.removeHeaders(headerName); this.message.removeHeaders(headerName);
return oldValues; return oldValues;
} }
return null; return null;
@ -151,13 +151,13 @@ class HttpComponentsHeadersAdapter implements MultiValueMap<String, String> {
@Override @Override
public void clear() { public void clear() {
this.response.setHeaders(); this.message.setHeaders();
} }
@Override @Override
public Set<String> keySet() { public Set<String> keySet() {
Set<String> keys = new LinkedHashSet<>(size()); Set<String> keys = new LinkedHashSet<>(size());
for (Header header : this.response.getHeaders()) { for (Header header : this.message.getHeaders()) {
keys.add(header.getName()); keys.add(header.getName());
} }
return keys; return keys;
@ -166,7 +166,7 @@ class HttpComponentsHeadersAdapter implements MultiValueMap<String, String> {
@Override @Override
public Collection<List<String>> values() { public Collection<List<String>> values() {
Collection<List<String>> values = new ArrayList<>(size()); Collection<List<String>> values = new ArrayList<>(size());
for (Header header : this.response.getHeaders()) { for (Header header : this.message.getHeaders()) {
values.add(get(header.getName())); values.add(get(header.getName()));
} }
return values; return values;
@ -196,7 +196,7 @@ class HttpComponentsHeadersAdapter implements MultiValueMap<String, String> {
private class EntryIterator implements Iterator<Entry<String, List<String>>> { private class EntryIterator implements Iterator<Entry<String, List<String>>> {
private final Iterator<Header> iterator = response.headerIterator(); private final Iterator<Header> iterator = message.headerIterator();
@Override @Override
public boolean hasNext() { public boolean hasNext() {

9
spring-web/src/main/java/org/springframework/http/client/reactive/JettyClientHttpRequest.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2021 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -125,7 +125,6 @@ class JettyClientHttpRequest extends AbstractClientHttpRequest {
}); });
} }
@Override @Override
protected void applyCookies() { protected void applyCookies() {
getCookies().values().stream().flatMap(Collection::stream) getCookies().values().stream().flatMap(Collection::stream)
@ -144,9 +143,13 @@ class JettyClientHttpRequest extends AbstractClientHttpRequest {
}); });
} }
@Override
protected HttpHeaders initReadOnlyHeaders() {
return HttpHeaders.readOnlyHttpHeaders(new JettyHeadersAdapter(this.jettyRequest.getHeaders()));
}
public ReactiveRequest toReactiveRequest() { public ReactiveRequest toReactiveRequest() {
return this.builder.build(); return this.builder.build();
} }
} }

8
spring-web/src/main/java/org/springframework/http/client/reactive/ReactorClientHttpRequest.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2020 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -31,6 +31,7 @@ import reactor.netty.http.client.HttpClientRequest;
import org.springframework.core.io.buffer.DataBuffer; import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory; import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.core.io.buffer.NettyDataBufferFactory; import org.springframework.core.io.buffer.NettyDataBufferFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
import org.springframework.http.ZeroCopyHttpOutputMessage; import org.springframework.http.ZeroCopyHttpOutputMessage;
@ -133,4 +134,9 @@ class ReactorClientHttpRequest extends AbstractClientHttpRequest implements Zero
.forEach(this.request::addCookie); .forEach(this.request::addCookie);
} }
@Override
protected HttpHeaders initReadOnlyHeaders() {
return HttpHeaders.readOnlyHttpHeaders(new NettyHeadersAdapter(this.request.requestHeaders()));
}
} }

Loading…
Cancel
Save