|
4 | 4 | import java.net.URL;
|
5 | 5 | import java.util.Arrays;
|
6 | 6 | import java.util.HashMap;
|
7 |
| -import java.util.HashSet; |
8 | 7 | import java.util.Map;
|
9 |
| -import java.util.Set; |
10 |
| -import java.util.concurrent.Executors; |
11 | 8 | import java.util.concurrent.TimeUnit;
|
12 | 9 |
|
13 |
| -import org.apache.cxf.transport.common.gzip.GZIPFeature; |
14 |
| - |
15 |
| -import com.microsoft.bingads.internal.CxfUtils; |
16 | 10 | import com.microsoft.bingads.internal.ServiceFactoryImpl;
|
17 | 11 | import com.microsoft.bingads.internal.ServiceInfo;
|
18 | 12 | import com.microsoft.bingads.internal.ServiceUtils;
|
| 13 | +import com.microsoft.bingads.internal.functionalinterfaces.Function; |
19 | 14 |
|
20 | 15 | import jakarta.ws.rs.client.Client;
|
21 | 16 | import jakarta.ws.rs.client.ClientBuilder;
|
22 | 17 | import jakarta.ws.rs.client.WebTarget;
|
23 | 18 |
|
24 | 19 | public class HttpClientProvider {
|
25 |
| - private Map<String, WebTarget> webTargetByService; |
26 |
| - |
27 |
| - public void initialize() { |
28 |
| - if (webTargetByService != null) { |
29 |
| - return; |
30 |
| - } |
| 20 | + private final Client client; |
| 21 | + private final Map<String, WebTarget> webTargetByService; |
31 | 22 |
|
32 |
| - ClientBuilder clientBuilder = configureClientBuilder(ClientBuilder.newBuilder()); |
33 |
| - |
34 |
| - Client client = clientBuilder.build(); |
| 23 | + HttpClientProvider(ClientBuilder clientBuilder) { |
| 24 | + this(clientBuilder, HttpClientProvider::initializeWebTargets); |
| 25 | + } |
35 | 26 |
|
36 |
| - webTargetByService = createWebTargets(client); |
| 27 | + HttpClientProvider(ClientBuilder clientBuilder, Function<Client, Map<String, WebTarget>> webTargetByServiceFunction) { |
| 28 | + client = clientBuilder.connectTimeout(1, TimeUnit.MINUTES) |
| 29 | + .readTimeout(10, TimeUnit.MINUTES) |
| 30 | + .build(); |
| 31 | + webTargetByService = webTargetByServiceFunction.apply(client); |
37 | 32 | }
|
38 | 33 |
|
39 | 34 | public WebTarget get(Class<?> serviceInterface, ApiEnvironment environment) {
|
40 | 35 | String key = getKey(serviceInterface, environment);
|
41 |
| - |
42 | 36 | return webTargetByService.get(key);
|
43 | 37 | }
|
44 | 38 |
|
45 |
| - private static String getKey(Class<?> serviceInterface, ApiEnvironment environment) { |
46 |
| - return serviceInterface.getName() + "_" + environment.toString(); |
| 39 | + protected void close() { |
| 40 | + client.close(); |
47 | 41 | }
|
48 | 42 |
|
49 |
| - protected ClientBuilder configureClientBuilder(ClientBuilder clientBuilder) { |
50 |
| - String clientClassName = clientBuilder.getClass().getName(); |
51 |
| - |
52 |
| - if (clientClassName.contains("org.apache.cxf")) { |
53 |
| - clientBuilder = clientBuilder.register(new GZIPFeature()); |
54 |
| - } |
55 |
| - |
56 |
| - if (clientClassName.contains("org.glassfish.jersey")) { |
57 |
| - clientBuilder = clientBuilder.property("jersey.config.client.suppressHttpComplianceValidation", true); // allow DELETE requests with body |
58 |
| - } |
59 |
| - |
60 |
| - return clientBuilder |
61 |
| - .connectTimeout(1, TimeUnit.MINUTES) |
62 |
| - .readTimeout(10, TimeUnit.MINUTES); |
63 |
| - } |
64 |
| - |
65 |
| - protected Map<String, WebTarget> createWebTargets(Client client) { |
66 |
| - boolean isCxf = client.getClass().getName().contains("org.apache.cxf"); |
67 |
| - |
68 |
| - if (isCxf) { |
69 |
| - return CxfUtils.runOnNewBus( |
70 |
| - () -> initializeWebTargets(client), |
71 |
| - (logging) -> { |
72 |
| - Set<String> headerNames = new HashSet<>(); |
73 |
| - |
74 |
| - headerNames.add("Authorization"); |
75 |
| - headerNames.add("Password"); |
76 |
| - |
77 |
| - logging.setSensitiveProtocolHeaderNames(headerNames); |
78 |
| - } |
79 |
| - ); |
80 |
| - } |
81 |
| - |
82 |
| - return initializeWebTargets(client); |
| 43 | + private static String getKey(Class<?> serviceInterface, ApiEnvironment environment) { |
| 44 | + return serviceInterface.getName() + "_" + environment.toString(); |
83 | 45 | }
|
84 | 46 |
|
85 | 47 | protected static Map<String, WebTarget> initializeWebTargets(Client client) {
|
|
0 commit comments