-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebClientFactory.java
95 lines (78 loc) · 3.41 KB
/
WebClientFactory.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package org.springframework.http.client.reactive;
import java.lang.reflect.Field;
import java.net.InetSocketAddress;
import java.net.URI;
import java.util.function.Consumer;
import java.util.function.Function;
import org.springframework.http.HttpMethod;
import org.springframework.web.reactive.function.client.WebClient;
import io.netty.bootstrap.Bootstrap;
import reactor.core.publisher.Mono;
import reactor.ipc.netty.http.client.HttpClient;
import reactor.ipc.netty.http.client.HttpClientOptions;
import reactor.ipc.netty.http.client.HttpClientRequest;
import reactor.ipc.netty.http.client.HttpClientResponse;
import reactor.ipc.netty.options.NettyOptions;
/**
* Please do not look at this horrible code. It's a complicated piece
* of reflection shit in order to have netty binding the right local
* ports so that we can freeze the clients.
*
* Sorry about that.
* @author bbossola
*
*/
public class WebClientFactory implements ClientHttpConnector {
private static int count = 0;
private final HttpClient httpClient;
public WebClientFactory(Consumer<? super HttpClientOptions.Builder> clientOptions) {
this.httpClient = HttpClient.create(clientOptions);
fixLocalAddress(System.getProperty("server.address"));
}
private void fixLocalAddress(String localAddress) {
if (count++ == 0)
System.err.println("Forced local address: "+localAddress);
if (localAddress != null) {
HttpClientOptions options = (HttpClientOptions) getFieldValue(HttpClient.class, "options", this.httpClient);
Bootstrap bootstrap = (Bootstrap) getFieldValue(NettyOptions.class, "bootstrapTemplate", options);
bootstrap.localAddress(new InetSocketAddress(localAddress, 0));
}
}
private Object getFieldValue(Class<?> clazz, String name, Object instance) {
try {
Field fieldOptions = clazz.getDeclaredField(name);
fieldOptions.setAccessible(true);
return fieldOptions.get(instance);
}
catch (Exception ex) {
throw new RuntimeException("Cannot get field "+name+" value from class "+clazz.getName(), ex);
}
}
@Override
public Mono<ClientHttpResponse> connect(HttpMethod method, URI uri,
Function<? super ClientHttpRequest, Mono<Void>> requestCallback) {
if (!uri.isAbsolute()) {
return Mono.error(new IllegalArgumentException("URI is not absolute: " + uri));
}
return this.httpClient
.request(adaptHttpMethod(method),
uri.toString(),
request -> requestCallback.apply(adaptRequest(method, uri, request)))
.map(this::adaptResponse);
}
private io.netty.handler.codec.http.HttpMethod adaptHttpMethod(HttpMethod method) {
return io.netty.handler.codec.http.HttpMethod.valueOf(method.name());
}
private ReactorClientHttpRequest adaptRequest(HttpMethod method, URI uri, HttpClientRequest request) {
return new ReactorClientHttpRequest(method, uri, request);
}
private ClientHttpResponse adaptResponse(HttpClientResponse response) {
return new ReactorClientHttpResponse(response);
}
public static WebClient newWebClient() {
WebClientFactory connector = new WebClientFactory(opt -> {
opt.disablePool();
});
return WebClient.builder().clientConnector(connector).build();
}
}