-
Notifications
You must be signed in to change notification settings - Fork 214
/
DefaultHttpClientFacade.java
87 lines (72 loc) · 3.08 KB
/
DefaultHttpClientFacade.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
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.internal.utils.http;
import java.util.concurrent.CompletionStage;
import javax.annotation.Nullable;
import org.eclipse.ditto.base.service.config.http.HttpProxyConfig;
import org.apache.pekko.actor.ActorSystem;
import org.apache.pekko.http.javadsl.Http;
import org.apache.pekko.http.javadsl.model.HttpRequest;
import org.apache.pekko.http.javadsl.model.HttpResponse;
import org.apache.pekko.http.javadsl.settings.ConnectionPoolSettings;
/**
* Default implementation of {@link HttpClientFacade}.
*/
public final class DefaultHttpClientFacade implements HttpClientFacade {
@Nullable private static DefaultHttpClientFacade instance;
private final ActorSystem actorSystem;
private final ConnectionPoolSettings connectionPoolSettings;
private DefaultHttpClientFacade(final ActorSystem actorSystem,
final ConnectionPoolSettings connectionPoolSettings) {
this.actorSystem = actorSystem;
this.connectionPoolSettings = connectionPoolSettings;
}
/**
* Returns the {@code HttpClientProvider} instance.
*
* @param actorSystem the {@code ActorSystem} in which to create the HttpClientProvider.
* @param httpProxyConfig the config of the HTTP proxy.
* @return the instance.
*/
public static DefaultHttpClientFacade getInstance(final ActorSystem actorSystem,
final HttpProxyConfig httpProxyConfig) {
// the HttpClientProvider is only configured at the very first invocation of getInstance(Config) as we can
// assume that the config does not change during runtime
DefaultHttpClientFacade result = instance;
if (null == result) {
result = createInstance(actorSystem, httpProxyConfig);
instance = result;
}
return result;
}
private static DefaultHttpClientFacade createInstance(final ActorSystem actorSystem,
final HttpProxyConfig proxyConfig) {
ConnectionPoolSettings connectionPoolSettings = ConnectionPoolSettings.create(actorSystem);
if (proxyConfig.isEnabled()) {
connectionPoolSettings = connectionPoolSettings.withTransport(proxyConfig.toClientTransport());
}
return new DefaultHttpClientFacade(actorSystem, connectionPoolSettings);
}
@Override
public CompletionStage<HttpResponse> createSingleHttpRequest(final HttpRequest request) {
return Http.get(actorSystem)
.singleRequest(request, Http.get(actorSystem).defaultClientHttpsContext(),
connectionPoolSettings,
actorSystem.log()
);
}
@Override
public ActorSystem getActorSystem() {
return actorSystem;
}
}