package com.cloudbees.sdk.boot2;
import com.cloudbees.sdk.boot.Launcher;
import com.cloudbees.sdk.maven.RemoteRepositoryDecorator;
import org.sonatype.aether.repository.Authentication;
import org.sonatype.aether.repository.Proxy;
import org.sonatype.aether.repository.RemoteRepository;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Honors the proxy setting in ~/.bees/bees.config
*
* <p>
* This duplicates the code in bees-driver somewhat, but it's necessary to avoid bringing in
* larger dependencies.
*
* @author Kohsuke Kawaguchi
*/
class RemoteRepositoryDecoratorImpl extends RemoteRepositoryDecorator {
private Properties properties;
private String proxyHost;
private String proxyPort;
private String proxyUser;
private String proxyPass;
private void loadBeesConfig() {
if (properties!=null) return; // already loaded
properties = new Properties();
File config = new File(Launcher.getLocalRepository(), "bees.config");
if (config.exists()) {
try {
InputStream fis = new FileInputStream(config);
try {
properties.load(fis);
} finally {
fis.close();
}
proxyHost = properties.getProperty("proxy.host");
proxyPort = properties.getProperty("proxy.port");
proxyUser = properties.getProperty("proxy.user");
proxyPass = properties.getProperty("proxy.password");
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "Failed to load configurations from "+config);
}
} else {
// get http proxy system properties
if (System.getProperty("http.proxyHost") != null && System.getProperty("http.proxyPort") != null) {
proxyHost = System.getProperty("http.proxyHost");
proxyPort = System.getProperty("http.proxyPort");
if (System.getProperty("http.proxyUser") != null) {
proxyUser = System.getProperty("http.proxyUser");
}
if (System.getProperty("http.proxyPassword") != null) {
proxyPass = System.getProperty("http.proxyPassword");
}
}
}
}
@Override
public RemoteRepository decorate(RemoteRepository repo) {
loadBeesConfig();
if (proxyHost != null && proxyPort!=null) {
String proxyType = Proxy.TYPE_HTTP;
if (repo.getUrl().startsWith("https"))
proxyType = Proxy.TYPE_HTTPS;
Proxy proxy = new Proxy(proxyType, proxyHost, Integer.parseInt(proxyPort), null);
if (proxyUser != null) {
Authentication authentication = new Authentication(proxyUser, proxyPass);
proxy.setAuthentication(authentication);
}
repo.setProxy(proxy);
}
return repo;
}
private static final Logger LOGGER = Logger.getLogger(RemoteRepositoryDecoratorImpl.class.getName());
}