/
DefaultHttpClientFacade.java
91 lines (76 loc) · 3.29 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
88
89
90
91
/*
* Copyright (c) 2017 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.services.gateway.security.utils;
import java.util.concurrent.CompletionStage;
import javax.annotation.Nullable;
import org.eclipse.ditto.services.base.config.http.HttpProxyConfig;
import akka.actor.ActorSystem;
import akka.http.javadsl.Http;
import akka.http.javadsl.model.HttpRequest;
import akka.http.javadsl.model.HttpResponse;
import akka.http.javadsl.settings.ConnectionPoolSettings;
import akka.stream.ActorMaterializer;
/**
* 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 final ActorMaterializer actorMaterializer;
private DefaultHttpClientFacade(final ActorSystem actorSystem, final ActorMaterializer actorMaterializer,
final ConnectionPoolSettings connectionPoolSettings) {
this.actorSystem = actorSystem;
this.actorMaterializer = actorMaterializer;
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, ActorMaterializer.create(actorSystem), connectionPoolSettings);
}
@Override
public CompletionStage<HttpResponse> createSingleHttpRequest(final HttpRequest request) {
return Http.get(actorSystem).singleRequest(request,
Http.get(actorSystem).defaultClientHttpsContext(),
connectionPoolSettings,
actorSystem.log(),
actorMaterializer
);
}
@Override
public ActorMaterializer getActorMaterializer() {
return actorMaterializer;
}
}