Code Monkey home page Code Monkey logo

Comments (4)

Stephan972 avatar Stephan972 commented on August 24, 2024 1

I would see the proxy setting feature as a nice enhancement to the PostgresStarter class.

from postgresql-embedded.

smecsia avatar smecsia commented on August 24, 2024

@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();

from postgresql-embedded.

stritti avatar stritti commented on August 24, 2024

@smecsia could you provide a running example? Would be cool if the environment variables could be used.

from postgresql-embedded.

Persi avatar Persi commented on August 24, 2024

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}");
		}
	}
}

from postgresql-embedded.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.