I am writing my first IntelliJ plugin in Java. The plugin function needs to reuse IntelliJ system settings HTTP Proxy setup. Basically, I need to know if “No proxy” is selected beforehand, if manual proxy configuration is selected, I want to know the host name and port number.
Since HttpConfigurable was deprecated and to be removed. How should I get above values programmatically with the new API ?
If my memory is correct, the IntelliJ Platform provides an http client that automatically follows the IDE proxy settings.
If anyone remembers the name of this client…
While HTTP connections created using the IDE’s factory should automatically use the IDE’s proxy configuration, there are situations where you may need to ensure that some external system/call uses those settings properly. If so, you can do the following:
ProxyConfiguration proxyConfiguration = ProxySettings.getInstance().getProxyConfiguration();
if (proxyConfiguration instanceof ProxyConfiguration.StaticProxyConfiguration staticProxyConfiguration) {
String host = staticProxyConfiguration.getHost();
int port = staticProxyConfiguration.getPort();
if (StringUtil.isNotEmpty(host)) {
// NOTE: This is to configure Apache CXF to use the proxy settings; you will likely have to do something different
httpClientPolicy.setProxyServer(host);
httpClientPolicy.setProxyServerPort(port);
httpClientPolicy.setProxyServerType(staticProxyConfiguration.getProtocol() == ProxyConfiguration.ProxyProtocol.SOCKS ? ProxyServerType.SOCKS : ProxyServerType.HTTP);
httpClientPolicy.setNonProxyHosts(staticProxyConfiguration.getExceptions());
Credentials credentials = ProxyCredentialStore.getInstance().getCredentials(host, port);
String username = credentials != null ? credentials.getUserName() : null;
String password = credentials != null ? credentials.getPasswordAsString() : null;
if (StringUtil.isNotEmpty(username) && StringUtil.isNotEmpty(password)) {
ProxyAuthorizationPolicy proxyAuthorizationPolicy = httpConduit.getProxyAuthorization();
proxyAuthorizationPolicy.setUserName(username);
proxyAuthorizationPolicy.setPassword(password);
}
}
}
Note that the code above only works in 2024.2+, though. If your plugin needs to support older versions, the following will work in all recent versions but should be updated to the code above once your plugin no longer needs to support 2024.1 and earlier:
HttpConfigurable httpConfigurable = HttpConfigurable.getInstance();
if (httpConfigurable != null) {
if (httpConfigurable.USE_HTTP_PROXY && StringUtil.isNotEmpty(httpConfigurable.PROXY_HOST)) {
// NOTE: This is to configure Apache CXF to use the proxy settings; you will likely have to do something different
httpClientPolicy.setProxyServer(httpConfigurable.PROXY_HOST);
httpClientPolicy.setProxyServerPort(httpConfigurable.PROXY_PORT);
httpClientPolicy.setProxyServerType(httpConfigurable.PROXY_TYPE_IS_SOCKS ? ProxyServerType.SOCKS : ProxyServerType.HTTP);
httpClientPolicy.setNonProxyHosts(httpConfigurable.PROXY_EXCEPTIONS);
String proxyLogin = ProxySettings.getProxyLogin();
if (httpConfigurable.PROXY_AUTHENTICATION && StringUtil.isNotEmpty(proxyLogin)) {
ProxyAuthorizationPolicy proxyAuthorizationPolicy = httpConduit.getProxyAuthorization();
proxyAuthorizationPolicy.setUserName(proxyLogin);
proxyAuthorizationPolicy.setPassword(httpConfigurable.getPlainProxyPassword());
}
}
}