Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import okhttp3.logging.HttpLoggingInterceptor;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.concurrent.TimeUnit;

public class OkHttpFactory implements RestApiFactory {
Expand All @@ -18,7 +20,7 @@ public class OkHttpFactory implements RestApiFactory {
public OkHttpFactory(CastleConfiguration configuration, CastleGsonModel modelInstance) {
this.configuration = configuration;
this.modelInstance = modelInstance;
client = createOkHttpClient();
this.client = createOkHttpClient();
}

private OkHttpClient createOkHttpClient() {
Expand All @@ -41,6 +43,14 @@ private OkHttpClient createOkHttpClient() {
builder = builder.addInterceptor(logging);
}

String httpProxyAddress = configuration.getHttpProxyAddress();
if (httpProxyAddress != null && !httpProxyAddress.isEmpty()) {
String[] proxyParts = httpProxyAddress.split(":");
String proxyHost = proxyParts[0];
int proxyPort = Integer.parseInt(proxyParts[1]);
builder = builder.proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)));
}

OkHttpClient client = builder
.addInterceptor(new Interceptor() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,12 @@ public class CastleConfiguration {
*/
private final int maxRequests;

public CastleConfiguration(String apiBaseUrl, int timeout, AuthenticateFailoverStrategy authenticateFailoverStrategy, List<String> allowListHeaders, List<String> denyListHeaders, String apiSecret, String castleAppId, CastleBackendProvider backendProvider, boolean logHttpRequests, List<String> ipHeaders, Integer maxRequests) {
/**
* Address to proxy the HTTP requests through.
*/
private final String httpProxyAddress;

public CastleConfiguration(String apiBaseUrl, int timeout, AuthenticateFailoverStrategy authenticateFailoverStrategy, List<String> allowListHeaders, List<String> denyListHeaders, String apiSecret, String castleAppId, CastleBackendProvider backendProvider, boolean logHttpRequests, List<String> ipHeaders, Integer maxRequests, String httpProxyAddress) {
this.apiBaseUrl = apiBaseUrl;
this.timeout = timeout;
this.authenticateFailoverStrategy = authenticateFailoverStrategy;
Expand All @@ -78,6 +83,7 @@ public CastleConfiguration(String apiBaseUrl, int timeout, AuthenticateFailoverS
this.logHttpRequests = logHttpRequests;
this.ipHeaders = ipHeaders;
this.maxRequests = maxRequests;
this.httpProxyAddress = httpProxyAddress;
}

public String getApiBaseUrl() {
Expand Down Expand Up @@ -133,4 +139,8 @@ public List<String> getIpHeaders() {
public int getMaxRequests() {
return maxRequests;
}

public String getHttpProxyAddress() {
return httpProxyAddress;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ public class CastleConfigurationBuilder {
*/
private int maxRequests = 5;

/**
* Address to proxy the HTTP requests through.
*/
private String httpProxyAddress;

private CastleConfigurationBuilder() {
}

Expand Down Expand Up @@ -336,7 +341,8 @@ public CastleConfiguration build() throws CastleSdkConfigurationException {
backendProvider,
logHttpRequests,
ipHeaders,
maxRequests);
maxRequests,
httpProxyAddress);
}

/**
Expand Down Expand Up @@ -407,4 +413,20 @@ public CastleConfigurationBuilder withIPHeaders(List<String> ipHeaders) {
public CastleConfigurationBuilder ipHeaders(List<String> ipHeaders) {
return withIPHeaders(ipHeaders);
}

/**
* Address to proxy the HTTP requests through.
*
* @param httpProxyAddress address (host:port) to proxy the HTTP requests through.
* @return a castleConfigurationBuilder with HTTP proxy address set
*/
public CastleConfigurationBuilder withHttpProxyAddress(String httpProxyAddress) {
this.httpProxyAddress = httpProxyAddress;
return this;
}

/* alias for withHttpProxyAddress */
public CastleConfigurationBuilder httpProxyAddress(String httpProxyAddress) {
return withHttpProxyAddress(httpProxyAddress);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ public CastleConfigurationBuilder loadConfigurationBuilder() {
"ip_headers",
"CASTLE_SDK_IP_HEADERS"
);
String httpProxyAddress = loadConfigurationValue(
castleConfigurationProperties,
"http_proxy_address",
"CASTLE_SDK_HTTP_PROXY_ADDRESS"
);
CastleConfigurationBuilder builder = CastleConfigurationBuilder
.defaultConfigBuilder()
.withApiSecret(envApiSecret)
Expand Down Expand Up @@ -194,6 +199,9 @@ public CastleConfigurationBuilder loadConfigurationBuilder() {
if (ipHeadersValue != null) {
builder.withIPHeaders(Splitter.on(",").splitToList(ipHeadersValue));
}
if (httpProxyAddress != null) {
builder.withHttpProxyAddress(httpProxyAddress);
}

return builder;
}
Expand Down