Skip to content

Commit 7af51b8

Browse files
committed
Mods to support using a proxy server (#141).
1 parent cff589a commit 7af51b8

File tree

4 files changed

+51
-5
lines changed

4 files changed

+51
-5
lines changed

README.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ To utilize the GitLab API for Java in your project, simply add the following dep
1111
```java
1212
dependencies {
1313
...
14-
compile group: 'org.gitlab4j', name: 'gitlab4j-api', version: '4.8.1'
14+
compile group: 'org.gitlab4j', name: 'gitlab4j-api', version: '4.8.2'
1515
}
1616
```
1717

@@ -20,7 +20,7 @@ dependencies {
2020
<dependency>
2121
<groupId>org.gitlab4j</groupId>
2222
<artifactId>gitlab4j-api</artifactId>
23-
<version>4.8.1</version>
23+
<version>4.8.2</version>
2424
</dependency>
2525
```
2626

@@ -68,7 +68,20 @@ gitLabApi.sudo("johndoe")
6868
// To turn off sudo mode
6969
gitLabApi.unsudo();
7070
```
71-
71+
---
72+
## Connecting Through a Proxy Server
73+
As of GitLab4J-API 4.8.2 support has been added for connecting to the GitLab server using an HTTP proxy server:
74+
```java
75+
// Log in to the GitLab server using a proxy server (with basic auth on proxy)
76+
Map<String, Object> proxyConfiguration = ProxyClientConfig.createProxyClientConfig("http://your-proxy-server", "proxy-username", "proxy-password");
77+
GitLabApi gitLabApi = new GitLabApi("http://your.gitlab.server.com", "YOUR_PRIVATE_TOKEN", null, proxyConfiguration);
78+
```
79+
```java
80+
// Log in to the GitLab server using a proxy server (no auth on proxy)
81+
Map<String, Object> proxyConfiguration = ProxyClientConfig.createProxyClientConfig("http://your-proxy-server");
82+
GitLabApi gitLabApi = new GitLabApi("http://your.gitlab.server.com", "YOUR_PRIVATE_TOKEN", null, proxyConfiguration);
83+
```
84+
*NOTE: See the Javadoc on the GitLabApi class for a complete list of methods accepting the proxy configuration (clientConfiguration parameter)*
7285
---
7386
## GitLab API V3 and V4 Support
7487
As of GitLab4J-API 4.2.0 support has been added for GitLab API V4. If your application requires GitLab API V3,

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,11 @@
222222
<artifactId>jersey-client</artifactId>
223223
<version>${jersey.version}</version>
224224
</dependency>
225+
<dependency>
226+
<groupId>org.glassfish.jersey.connectors</groupId>
227+
<artifactId>jersey-apache-connector</artifactId>
228+
<version>${jersey.version}</version>
229+
</dependency>
225230
<dependency>
226231
<groupId>javax.servlet</groupId>
227232
<artifactId>javax.servlet-api</artifactId>

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.gitlab4j.api.Constants.TokenType;
3030
import org.gitlab4j.api.GitLabApi.ApiVersion;
3131
import org.gitlab4j.api.utils.JacksonJson;
32+
import org.glassfish.jersey.apache.connector.ApacheConnectorProvider;
3233
import org.glassfish.jersey.client.ClientConfig;
3334
import org.glassfish.jersey.client.ClientProperties;
3435

@@ -211,6 +212,11 @@ public GitLabApiClient(ApiVersion apiVersion, String hostUrl, TokenType tokenTyp
211212

212213
clientConfig = new ClientConfig();
213214
if (clientConfigProperties != null) {
215+
216+
if (clientConfigProperties.containsKey(ClientProperties.PROXY_URI)) {
217+
clientConfig.connectorProvider(new ApacheConnectorProvider());
218+
}
219+
214220
for (Map.Entry<String, Object> propertyEntry : clientConfigProperties.entrySet()) {
215221
clientConfig.property(propertyEntry.getKey(), propertyEntry.getValue());
216222
}

src/test/java/org/gitlab4j/api/TestGitLabApi.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
import static org.junit.Assert.assertNotNull;
44
import static org.junit.Assume.assumeTrue;
55

6-
import org.gitlab4j.api.GitLabApi.ApiVersion;
6+
import java.util.Map;
7+
78
import org.gitlab4j.api.models.Version;
89
import org.junit.Before;
910
import org.junit.BeforeClass;
@@ -23,9 +24,15 @@ public class TestGitLabApi {
2324
// The following needs to be set to your test repository
2425
private static final String TEST_HOST_URL;
2526
private static final String TEST_PRIVATE_TOKEN;
27+
private static final String TEST_PROXY_URI;
28+
private static final String TEST_PROXY_USERNAME;
29+
private static final String TEST_PROXY_PASSWORD;
2630
static {
2731
TEST_HOST_URL = TestUtils.getProperty("TEST_HOST_URL");
2832
TEST_PRIVATE_TOKEN = TestUtils.getProperty("TEST_PRIVATE_TOKEN");
33+
TEST_PROXY_URI = TestUtils.getProperty("TEST_PROXY_URI");
34+
TEST_PROXY_USERNAME = TestUtils.getProperty("TEST_PROXY_USERNAME");
35+
TEST_PROXY_PASSWORD = TestUtils.getProperty("TEST_PROXY_PASSWORD");
2936
}
3037

3138
private static GitLabApi gitLabApi;
@@ -47,7 +54,7 @@ public static void setup() {
4754
}
4855

4956
if (problems.isEmpty()) {
50-
gitLabApi = new GitLabApi(ApiVersion.V4, TEST_HOST_URL, TEST_PRIVATE_TOKEN);
57+
gitLabApi = new GitLabApi(TEST_HOST_URL, TEST_PRIVATE_TOKEN);
5158
} else {
5259
System.err.print(problems);
5360
}
@@ -66,4 +73,19 @@ public void testGetVersion() throws GitLabApiException {
6673
assertNotNull(version.getVersion());
6774
assertNotNull(version.getRevision());
6875
}
76+
77+
@Test
78+
public void testProxyConnection() throws GitLabApiException {
79+
assumeTrue(TEST_PROXY_URI != null && TEST_PROXY_USERNAME != null && TEST_PROXY_PASSWORD != null);
80+
81+
// Setup a GitLabApi instance to use a proxy
82+
Map<String, Object> clientConfig = ProxyClientConfig.createProxyClientConfig(TEST_PROXY_URI, TEST_PROXY_USERNAME, TEST_PROXY_PASSWORD);
83+
GitLabApi gitLabApi = new GitLabApi(TEST_HOST_URL, TEST_PRIVATE_TOKEN, null, clientConfig);
84+
85+
Version version = gitLabApi.getVersion();
86+
assertNotNull(version);
87+
System.out.format("version=%s, revision=%s%n", version.getVersion(), version.getRevision());
88+
assertNotNull(version.getVersion());
89+
assertNotNull(version.getRevision());
90+
}
6991
}

0 commit comments

Comments
 (0)