Skip to content

Commit db41184

Browse files
committed
added builder + setter for custom SSLContext instance
1 parent ad73110 commit db41184

3 files changed

Lines changed: 91 additions & 1 deletion

File tree

gitlab4j-api/src/main/java/org/gitlab4j/api/GitLabApi.java

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import java.util.logging.Level;
1010
import java.util.logging.Logger;
1111

12+
import javax.net.ssl.SSLContext;
13+
1214
import jakarta.ws.rs.core.MediaType;
1315
import jakarta.ws.rs.core.Response;
1416

@@ -114,6 +116,78 @@ public static final Logger getLogger() {
114116
return (LOGGER);
115117
}
116118

119+
/**
120+
* Builder class that allows for a different way to initialize a GitLabApi object
121+
*/
122+
public static class Builder {
123+
private ApiVersion apiVersion = ApiVersion.V4;
124+
private String hostUrl = null;
125+
private TokenType tokenType = TokenType.PRIVATE;
126+
private String authToken = null;
127+
private String secretToken = null;
128+
private Map<String, Object> clientConfigProperties = null;
129+
private SSLContext sslContext = null;
130+
131+
public Builder withApiVersion(ApiVersion apiVersion) {
132+
this.apiVersion = apiVersion;
133+
return this;
134+
}
135+
136+
public Builder withHostUrl(String hostUrl) {
137+
this.hostUrl = hostUrl;
138+
return this;
139+
}
140+
141+
public Builder withTokenType(TokenType tokenType) {
142+
this.tokenType = tokenType;
143+
return this;
144+
}
145+
146+
public Builder withAuthToken(String authToken) {
147+
this.authToken = authToken;
148+
return this;
149+
}
150+
151+
public Builder withSecretToken(String secretToken) {
152+
this.secretToken = secretToken;
153+
return this;
154+
}
155+
156+
public Builder withClientConfigProperties(Map<String, Object> clientConfigProperties) {
157+
this.clientConfigProperties = clientConfigProperties;
158+
return this;
159+
}
160+
161+
public Builder withSslContext(SSLContext sslContext) {
162+
this.sslContext = sslContext;
163+
return this;
164+
}
165+
166+
public GitLabApi build() {
167+
if (hostUrl == null || hostUrl.trim().isEmpty()) {
168+
throw new IllegalStateException("hostUrl must be provided.");
169+
}
170+
return new GitLabApi(this);
171+
}
172+
}
173+
174+
public static Builder builder() {
175+
return new Builder();
176+
}
177+
178+
private GitLabApi(Builder builder) {
179+
this.apiClient = new GitLabApiClient(
180+
builder.apiVersion,
181+
builder.hostUrl,
182+
builder.tokenType,
183+
builder.authToken,
184+
builder.secretToken,
185+
builder.clientConfigProperties);
186+
if (builder.sslContext != null) {
187+
this.apiClient.setSslContext(builder.sslContext);
188+
}
189+
}
190+
117191
/**
118192
* Constructs a GitLabApi instance set up to interact with the GitLab server
119193
* using GitLab API version 4. This is the primary way to authenticate with

gitlab4j-api/src/main/java/org/gitlab4j/api/GitLabApiClient.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public class GitLabApiClient implements AutoCloseable {
6161
private String secretToken;
6262
private boolean ignoreCertificateErrors;
6363
private SSLContext openSslContext;
64+
private SSLContext customSslContext;
6465
private HostnameVerifier openHostnameVerifier;
6566
private Long sudoAsId;
6667
private String userAgentHeader;
@@ -288,6 +289,19 @@ void enableRequestResponseLogging(Logger logger, Level level, int maxEntityLengt
288289
}
289290
}
290291

292+
/**
293+
* Sets the SSLContext for the underlying Jersey client.
294+
* Does nothing if setIgnoreCertificateErrors has been set to true
295+
*
296+
* @param sslContext the SSLContext instance to use.
297+
*/
298+
void setSslContext(SSLContext sslContext) {
299+
if (getIgnoreCertificateErrors()) {
300+
return;
301+
}
302+
this.customSslContext = sslContext;
303+
}
304+
291305
/**
292306
* Sets the per request connect and read timeout.
293307
*
@@ -839,6 +853,8 @@ protected Client createApiClient() {
839853

840854
if (ignoreCertificateErrors) {
841855
clientBuilder.sslContext(openSslContext).hostnameVerifier(openHostnameVerifier);
856+
} else if (customSslContext != null) {
857+
clientBuilder.sslContext(customSslContext);
842858
}
843859

844860
apiClient = clientBuilder.build();

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version=6.3.1-SNAPSHOT
1+
version=6.3.2-SNAPSHOT
22
lastVersion=6.3.0
33

44
githubRepositoryOwner=gitlab4j

0 commit comments

Comments
 (0)