Polishing contribution

Closes gh-29958
This commit is contained in:
rstoyanchev
2024-03-12 10:40:08 +00:00
parent 8af1d8e842
commit 6767f7010c
10 changed files with 62 additions and 50 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -48,6 +48,12 @@ public interface ClientHttpRequest extends ReactiveHttpOutputMessage {
*/
MultiValueMap<String, HttpCookie> getCookies();
/**
* Return a mutable map of the request attributes.
* @since 6.2
*/
Map<String, Object> getAttributes();
/**
* Return the request from the underlying HTTP library.
* @param <T> the expected type of the request to cast to
@@ -55,9 +61,4 @@ public interface ClientHttpRequest extends ReactiveHttpOutputMessage {
*/
<T> T getNativeRequest();
/**
* Return a mutable map of the request attributes.
*/
Map<String, Object> getAttributes();
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -76,6 +76,11 @@ public class ClientHttpRequestDecorator implements ClientHttpRequest {
return this.delegate.getCookies();
}
@Override
public Map<String, Object> getAttributes() {
return this.delegate.getAttributes();
}
@Override
public DataBufferFactory bufferFactory() {
return this.delegate.bufferFactory();
@@ -86,11 +91,6 @@ public class ClientHttpRequestDecorator implements ClientHttpRequest {
return this.delegate.getNativeRequest();
}
@Override
public Map<String, Object> getAttributes() {
return this.delegate.getAttributes();
}
@Override
public void beforeCommit(Supplier<? extends Mono<Void>> action) {
this.delegate.beforeCommit(action);

View File

@@ -163,7 +163,7 @@ class HttpComponentsClientHttpRequest extends AbstractClientHttpRequest {
@Override
protected void applyAttributes() {
getAttributes().forEach((key, value) -> {
if(this.context.getAttribute(key) == null) {
if (this.context.getAttribute(key) == null) {
this.context.setAttribute(key, value);
}
});

View File

@@ -138,14 +138,6 @@ class JettyClientHttpRequest extends AbstractClientHttpRequest {
.forEach(this.jettyRequest::cookie);
}
/**
* Applies the attributes to {@link Request#getAttributes()}.
*/
@Override
protected void applyAttributes() {
getAttributes().forEach(this.jettyRequest::attribute);
}
@Override
protected void applyHeaders() {
HttpHeaders headers = getHeaders();
@@ -162,6 +154,15 @@ class JettyClientHttpRequest extends AbstractClientHttpRequest {
return HttpHeaders.readOnlyHttpHeaders(new JettyHeadersAdapter(this.jettyRequest.getHeaders()));
}
@Override
protected void applyAttributes() {
getAttributes().forEach((key, value) -> {
if (this.jettyRequest.getAttributes().get(key) == null) {
this.jettyRequest.attribute(key, value);
}
});
}
public ReactiveRequest toReactiveRequest() {
return this.builder.build();
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,9 +17,11 @@
package org.springframework.http.client.reactive;
import java.net.URI;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
import io.netty.util.AttributeKey;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import reactor.core.publisher.Mono;
@@ -49,6 +51,13 @@ import org.springframework.util.Assert;
*/
public class ReactorClientHttpConnector implements ClientHttpConnector, SmartLifecycle {
/**
* Channel attribute key under which {@code WebClient} request attributes are stored as a Map.
* @since 6.2
*/
public static final AttributeKey<Map<String, Object>> ATTRIBUTES_KEY =
AttributeKey.valueOf(ReactorClientHttpRequest.class.getName() + ".ATTRIBUTES");
private static final Log logger = LogFactory.getLog(ReactorClientHttpConnector.class);
private static final Function<HttpClient, HttpClient> defaultInitializer = client -> client.compress(true);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,11 +18,9 @@ package org.springframework.http.client.reactive;
import java.net.URI;
import java.nio.file.Path;
import java.util.Map;
import io.netty.buffer.ByteBuf;
import io.netty.handler.codec.http.cookie.DefaultCookie;
import io.netty.util.AttributeKey;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
@@ -48,8 +46,6 @@ import org.springframework.http.support.Netty4HeadersAdapter;
*/
class ReactorClientHttpRequest extends AbstractClientHttpRequest implements ZeroCopyHttpOutputMessage {
public static final String ATTRIBUTES_CHANNEL_KEY = "attributes";
private final HttpMethod httpMethod;
private final URI uri;
@@ -141,14 +137,16 @@ class ReactorClientHttpRequest extends AbstractClientHttpRequest implements Zero
}
/**
* Applies the request attributes to the {@link reactor.netty.http.client.HttpClientRequest} by setting
* a single {@link Map} into the {@link reactor.netty.channel.ChannelOperations#channel()},
* with {@link io.netty5.util.AttributeKey#name()} equal to {@link #ATTRIBUTES_CHANNEL_KEY}.
* Saves the {@link #getAttributes() request attributes} to the
* {@link reactor.netty.channel.ChannelOperations#channel() channel} as a single map
* attribute under the key {@link ReactorClientHttpConnector#ATTRIBUTES_KEY}.
*/
@Override
protected void applyAttributes() {
((ChannelOperations<?, ?>) this.request)
.channel().attr(AttributeKey.valueOf(ATTRIBUTES_CHANNEL_KEY)).set(getAttributes());
if (!getAttributes().isEmpty()) {
((ChannelOperations<?, ?>) this.request).channel()
.attr(ReactorClientHttpConnector.ATTRIBUTES_KEY).set(getAttributes());
}
}
@Override

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,9 +17,11 @@
package org.springframework.http.client.reactive;
import java.net.URI;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
import io.netty5.util.AttributeKey;
import reactor.core.publisher.Mono;
import reactor.netty5.NettyOutbound;
import reactor.netty5.http.client.HttpClient;
@@ -41,6 +43,13 @@ import org.springframework.util.Assert;
*/
public class ReactorNetty2ClientHttpConnector implements ClientHttpConnector {
/**
* Channel attribute key under which {@code WebClient} request attributes are stored as a Map.
* @since 6.2
*/
public static final AttributeKey<Map<String, Object>> ATTRIBUTES_KEY =
AttributeKey.valueOf(ReactorNetty2ClientHttpRequest.class.getName() + ".ATTRIBUTES");
private static final Function<HttpClient, HttpClient> defaultInitializer = client -> client.compress(true);

View File

@@ -18,11 +18,9 @@ package org.springframework.http.client.reactive;
import java.net.URI;
import java.nio.file.Path;
import java.util.Map;
import io.netty5.buffer.Buffer;
import io.netty5.handler.codec.http.headers.DefaultHttpCookiePair;
import io.netty5.util.AttributeKey;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
@@ -49,8 +47,6 @@ import org.springframework.http.support.Netty5HeadersAdapter;
*/
class ReactorNetty2ClientHttpRequest extends AbstractClientHttpRequest implements ZeroCopyHttpOutputMessage {
public static final String ATTRIBUTES_CHANNEL_KEY = "attributes";
private final HttpMethod httpMethod;
private final URI uri;
@@ -144,14 +140,16 @@ class ReactorNetty2ClientHttpRequest extends AbstractClientHttpRequest implement
}
/**
* Applies the request attributes to the {@link reactor.netty.http.client.HttpClientRequest} by setting
* a single {@link Map} into the {@link reactor.netty.channel.ChannelOperations#channel()},
* with {@link AttributeKey#name()} equal to {@link #ATTRIBUTES_CHANNEL_KEY}.
* Saves the {@link #getAttributes() request attributes} to the
* {@link reactor.netty.channel.ChannelOperations#channel() channel} as a single map
* attribute under the key {@link ReactorNetty2ClientHttpConnector#ATTRIBUTES_KEY}.
*/
@Override
protected void applyAttributes() {
((ChannelOperations<?, ?>) this.request)
.channel().attr(AttributeKey.valueOf(ATTRIBUTES_CHANNEL_KEY)).set(getAttributes());
if (!getAttributes().isEmpty()) {
((ChannelOperations<?, ?>) this.request).channel()
.attr(ReactorNetty2ClientHttpConnector.ATTRIBUTES_KEY).set(getAttributes());
}
}
@Override