postgresql-embedded
postgresql-embedded copied to clipboard
Proxy support
I'm currently trying to get postgresql-embedded to work, but unfortunately our corporate proxy is blocking the download. Would be nice if something like mvn clean install -DargLine="-Dhttp.proxyHost=www.proxy.com -Dhttp.proxyPort=8000" would be supported. Is there a way to set the proxy within the code as a work around for now?
@TheNitek thanks for suggestion. This project is based on embed.process library, which supports such ability. You should specify your own proxyConfig within the DownloadConfigBuilder, which is a propery of ArtifactStoreBuilder. If you specify your own manually, you can set up the proxy configuration. Something like:
IRuntimeConfig runtimeConfig = new RuntimeConfigBuilder()
.defaults(Command.Postgres)
.artifactStore(new ArtifactStoreBuilder()
.defaults(Command.Postgres)
.download(new DownloadConfigBuilder()
.defaultsForCommand(Command.Postgres)
.proxyFactory(new HttpProxyFactory("www.proxy.com", 8000))
.build()
)
).build();
I would see the proxy setting feature as a nice enhancement to the PostgresStarter class.
@smecsia could you provide a running example? Would be cool if the environment variables could be used.
Hi guys,
I've created a small Proxyfactory which is working nicely with our corporate proxy server.
It is evaluating the system environment variables -Dhttp.proxyHost, -Dhttp.proxyPort, -Dhttps.proxyHost, -Dhttps.proxyPort but is not supporting proxyservers which needs authentication. If proxysettings for https and http are defined, https settings are preferred.
Maybe it's an inspiration for a default implementation.
Looks like the following:
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.UnknownHostException;
import java.util.Objects;
import de.flapdoodle.embed.process.config.store.IProxyFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class HttpProxyFactory implements IProxyFactory {
private static final Logger LOGGER = LoggerFactory.getLogger(HttpProxyFactory.class);
@Override
public Proxy createProxy() {
ProxyConfiguration httpConfig = createProxyConfig("http");
ProxyConfiguration httpsConfig = createProxyConfig("https");
if (!httpConfig.isValid() && !httpsConfig.isValid()) {
return Proxy.NO_PROXY;
}
if (httpsConfig.isValid()) {
return configureProxy(httpsConfig);
}
return configureProxy(httpConfig);
}
private ProxyConfiguration createProxyConfig(String protocol) {
return new ProxyConfiguration(System.getProperty(protocol + ".proxyHost"),
System.getProperty(protocol + ".proxyPort"));
}
private Proxy configureProxy(ProxyConfiguration config) {
try {
Proxy proxy = new Proxy(Proxy.Type.HTTP,
new InetSocketAddress(InetAddress.getByName(config.getHost()), config.getPort()));
LOGGER.info("Using proxy " + config.getHost() + ":" + config.getPort());
return proxy;
} catch (UnknownHostException e) {
LOGGER.error("Creating proxy failed, falling back to no proxy", e);
return Proxy.NO_PROXY;
}
}
private static class ProxyConfiguration {
private final String host;
private final String port;
private ProxyConfiguration(String host, String port) {
this.host = host;
this.port = port;
}
private String getHost() {
return host;
}
private Integer getPort() {
return Integer.valueOf(port);
}
private boolean isValid() {
return Objects.nonNull(host) && Objects.nonNull(port) && port.matches("[0-9]{4,5}");
}
}
}