Skip to content

Commit ca28797

Browse files
committed
feat(http-client-configration): add proxy support
1 parent b924c59 commit ca28797

2 files changed

Lines changed: 189 additions & 4 deletions

File tree

src/main/java/io/apimatic/core/configurations/http/client/CoreHttpClientConfiguration.java

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.apimatic.core.configurations.http.client;
22

3+
import io.apimatic.coreinterfaces.http.proxy.ProxyConfiguration;
34
import java.util.Arrays;
45
import java.util.HashSet;
56
import java.util.Set;
@@ -122,6 +123,12 @@ public final class CoreHttpClientConfiguration implements ClientConfiguration {
122123
*/
123124
private final boolean skipSslCertVerification;
124125

126+
/**
127+
* The proxy configuration used to route network requests through a proxy server.
128+
* Contains details such as address, port, and optional authentication credentials.
129+
*/
130+
private final ProxyConfiguration proxyConfiguration;
131+
125132
/**
126133
* @param timeout
127134
* @param numberOfRetries
@@ -140,7 +147,7 @@ private CoreHttpClientConfiguration(final long timeout, final int numberOfRetrie
140147
final boolean skipSslCertVerification, final Set<Integer> httpStatusCodesToRetry,
141148
final Set<Method> httpMethodsToRetry, final long maximumRetryWaitTime,
142149
final boolean shouldRetryOnTimeout, final okhttp3.OkHttpClient httpClientInstance,
143-
final boolean overrideHttpClientConfigurations) {
150+
final boolean overrideHttpClientConfigurations, final ProxyConfiguration proxyConfiguration) {
144151
this.timeout = timeout;
145152
this.numberOfRetries = numberOfRetries;
146153
this.backOffFactor = backOffFactor;
@@ -152,6 +159,7 @@ private CoreHttpClientConfiguration(final long timeout, final int numberOfRetrie
152159
this.httpClientInstance = httpClientInstance;
153160
this.overrideHttpClientConfigurations = overrideHttpClientConfigurations;
154161
this.skipSslCertVerification = skipSslCertVerification;
162+
this.proxyConfiguration = proxyConfiguration;
155163
}
156164

157165
/**
@@ -243,6 +251,14 @@ public boolean skipSslCertVerification() {
243251
return skipSslCertVerification;
244252
}
245253

254+
/**
255+
* Returns the proxy configuration used to route requests through a proxy server.
256+
* This includes the proxy address, port, and any authentication credentials.
257+
*
258+
* @return the {@link ProxyConfiguration}, or {@code null} if no proxy is configured
259+
*/
260+
public ProxyConfiguration getProxyConfiguration() { return proxyConfiguration; }
261+
246262
/**
247263
* Converts this HttpClientConfiguration into string format.
248264
* @return String representation of this class.
@@ -255,7 +271,8 @@ public String toString() {
255271
+ ", httpMethodsToRetry=" + httpMethodsToRetry + ", maximumRetryWaitTime="
256272
+ maximumRetryWaitTime + ", shouldRetryOnTimeout=" + shouldRetryOnTimeout
257273
+ ", httpClientInstance=" + httpClientInstance
258-
+ ", overrideHttpClientConfigurations=" + overrideHttpClientConfigurations + "]";
274+
+ ", overrideHttpClientConfigurations=" + overrideHttpClientConfigurations
275+
+ ", proxy=" + proxyConfiguration + "]";
259276
}
260277

261278
/**
@@ -269,7 +286,8 @@ public Builder newBuilder() {
269286
.httpStatusCodesToRetry(httpStatusCodesToRetry)
270287
.httpMethodsToRetry(httpMethodsToRetry).maximumRetryWaitTime(maximumRetryWaitTime)
271288
.shouldRetryOnTimeout(shouldRetryOnTimeout)
272-
.httpClientInstance(httpClientInstance, overrideHttpClientConfigurations);
289+
.httpClientInstance(httpClientInstance, overrideHttpClientConfigurations)
290+
.proxyConfiguration(proxyConfiguration);
273291
}
274292

275293
/**
@@ -322,6 +340,11 @@ public static class Builder {
322340
* Skip Ssl certification.
323341
*/
324342
private boolean skipSslCertVerification;
343+
/**
344+
* The proxy configuration used to route network requests through a proxy server.
345+
* Contains details such as address, port, and optional authentication credentials.
346+
*/
347+
private ProxyConfiguration proxyConfiguration;
325348

326349
/**
327350
* Default Constructor to initiate builder with default properties.
@@ -463,6 +486,17 @@ public Builder skipSslCertVerification(boolean skipSslCertVerification) {
463486
return this;
464487
}
465488

489+
/**
490+
* Sets the proxy configuration to be used for routing requests through a proxy server.
491+
*
492+
* @param proxyConfiguration the {@link ProxyConfiguration} instance to use
493+
* @return the builder instance
494+
*/
495+
public Builder proxyConfiguration(ProxyConfiguration proxyConfiguration) {
496+
this.proxyConfiguration = proxyConfiguration;
497+
return this;
498+
}
499+
466500
/**
467501
* Builds a new HttpClientConfiguration object using the set fields.
468502
* @return {@link CoreHttpClientConfiguration}.
@@ -471,7 +505,7 @@ public CoreHttpClientConfiguration build() {
471505
return new CoreHttpClientConfiguration(timeout, numberOfRetries, backOffFactor,
472506
retryInterval, skipSslCertVerification, httpStatusCodesToRetry,
473507
httpMethodsToRetry, maximumRetryWaitTime, shouldRetryOnTimeout,
474-
httpClientInstance, overrideHttpClientConfigurations);
508+
httpClientInstance, overrideHttpClientConfigurations, proxyConfiguration);
475509
}
476510
}
477511
}
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
package io.apimatic.core.configurations.http.client;
2+
3+
import io.apimatic.coreinterfaces.http.proxy.ProxyConfiguration;
4+
5+
/**
6+
* Represents a proxy configuration with address, port, and optional authentication credentials.
7+
* This class is an implementation of the {@link ProxyConfiguration} interface.
8+
*/
9+
public class CoreProxyConfiguration implements ProxyConfiguration {
10+
11+
/**
12+
* The proxy server address (e.g., IP or domain name).
13+
*/
14+
private final String address;
15+
16+
/**
17+
* The port on which the proxy server is listening.
18+
*/
19+
private final int port;
20+
21+
/**
22+
* The username for proxy authentication, if required.
23+
*/
24+
private final String username;
25+
26+
/**
27+
* The password for proxy authentication, if required.
28+
*/
29+
private final String password;
30+
31+
/**
32+
* Creates a new instance of {@code CoreProxyConfiguration} with the specified address,
33+
* port, and optional authentication credentials.
34+
*
35+
* @param address The proxy server address.
36+
* @param port The proxy server port.
37+
* @param username The username for proxy authentication (can be {@code null}).
38+
* @param password The password for proxy authentication (can be {@code null}).
39+
*/
40+
private CoreProxyConfiguration(String address, int port, String username, String password) {
41+
this.address = address;
42+
this.port = port;
43+
this.username = username;
44+
this.password = password;
45+
}
46+
47+
@Override
48+
public String getAddress() {
49+
return address;
50+
}
51+
52+
@Override
53+
public int getPort() {
54+
return port;
55+
}
56+
57+
@Override
58+
public String getUsername() {
59+
return username;
60+
}
61+
62+
@Override
63+
public String getPassword() {
64+
return password;
65+
}
66+
67+
/**
68+
* Creates a new builder initialized with the current configuration's values.
69+
* @return a new {@link Builder} instance
70+
*/
71+
public Builder newBuilder() {
72+
return new Builder()
73+
.address(this.address)
74+
.port(this.port)
75+
.username(this.username)
76+
.password(this.password);
77+
}
78+
79+
/**
80+
* Returns a string representation of this proxy configuration.
81+
* @return string representation of this object
82+
*/
83+
@Override
84+
public String toString() {
85+
return "CoreProxyConfiguration [" +
86+
"address=" + address +
87+
", port=" + port +
88+
", username=" + (username != null ? username : "null") +
89+
", password=" + (password != null ? password : "null") +
90+
"]";
91+
}
92+
93+
/**
94+
* Builder class for constructing {@link CoreProxyConfiguration} instances.
95+
*/
96+
public static class Builder {
97+
98+
private String address;
99+
private int port;
100+
private String username;
101+
private String password;
102+
103+
/**
104+
* Sets the proxy server address.
105+
* @param address the address to set
106+
* @return the builder instance
107+
*/
108+
public Builder address(String address) {
109+
this.address = address;
110+
return this;
111+
}
112+
113+
/**
114+
* Sets the proxy server port.
115+
* @param port the port to set
116+
* @return the builder instance
117+
*/
118+
public Builder port(int port) {
119+
this.port = port;
120+
return this;
121+
}
122+
123+
/**
124+
* Sets the proxy username for authentication.
125+
* @param username the username to set
126+
* @return the builder instance
127+
*/
128+
public Builder username(String username) {
129+
this.username = username;
130+
return this;
131+
}
132+
133+
/**
134+
* Sets the proxy password for authentication.
135+
* @param password the password to set
136+
* @return the builder instance
137+
*/
138+
public Builder password(String password) {
139+
this.password = password;
140+
return this;
141+
}
142+
143+
/**
144+
* Builds a new {@link CoreProxyConfiguration} instance using the set fields.
145+
* @return the built {@link CoreProxyConfiguration}
146+
*/
147+
public CoreProxyConfiguration build() {
148+
return new CoreProxyConfiguration(address, port, username, password);
149+
}
150+
}
151+
}

0 commit comments

Comments
 (0)