-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #274 from jenkinsci/install_with_proxy
AST-38150 Install cx version with the proxy
- Loading branch information
Showing
4 changed files
with
114 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/main/java/com/checkmarx/jenkins/tools/ProxyHttpClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.checkmarx.jenkins.tools; | ||
|
||
|
||
import java.io.IOException; | ||
import java.net.InetSocketAddress; | ||
import java.net.Proxy; | ||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import okhttp3.*; | ||
import org.apache.commons.lang.StringUtils; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
|
||
public class ProxyHttpClient { | ||
|
||
public OkHttpClient getHttpClient(String proxyString, int connectionTimeoutMillis, int readTimeoutMillis) throws URISyntaxException { | ||
OkHttpClient.Builder okClientBuilder = new OkHttpClient.Builder() | ||
.connectTimeout(connectionTimeoutMillis, TimeUnit.MILLISECONDS) | ||
.readTimeout(readTimeoutMillis, TimeUnit.MILLISECONDS); | ||
if (proxyString != null) { | ||
URI proxy = new URI(proxyString); | ||
if (isValidProxy(proxy.getHost(), proxy.getPort())) { | ||
Proxy _httpProxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxy.getHost(), proxy.getPort())); | ||
if (StringUtils.isNotEmpty(proxy.getUserInfo())) { | ||
String[] userPass = proxy.getUserInfo().split(":"); | ||
Authenticator _httpProxyAuth = new Authenticator() { | ||
@Nullable | ||
@Override | ||
public Request authenticate(Route route, Response response) throws IOException { | ||
String credential = Credentials.basic(userPass[0], userPass[1]); | ||
return response.request().newBuilder() | ||
.header("Proxy-Authorization", credential).build(); | ||
} | ||
} ; | ||
return okClientBuilder.proxyAuthenticator(_httpProxyAuth).proxy(_httpProxy).build(); | ||
} else { | ||
return okClientBuilder.proxy(_httpProxy).build(); | ||
} | ||
} | ||
} | ||
return okClientBuilder.build(); | ||
} | ||
|
||
private static boolean isValidProxy(String proxyHost, int proxyPort) { | ||
return (!proxyHost.isEmpty()) && (proxyPort >= 10) && (proxyPort <= 65535); | ||
} | ||
} |