Search in sources :

Example 1 with ProxySettings

use of com.helger.network.proxy.settings.ProxySettings in project ph-web by phax.

the class AuthenticatorProxySettingsManagerTest method testProxyMultipleMixed.

@Test
public void testProxyMultipleMixed() {
    ProxySettingsManager.registerProvider((sProtocol, sHostName, nPort) -> new CommonsArrayList<>(new ProxySettings(Proxy.Type.HTTP, "http://proxysrv", 8080), new ProxySettings(Proxy.Type.HTTP, "http://proxysrv2", 8080, "user", "pw")));
    final PasswordAuthentication aPA = Authenticator.requestPasswordAuthentication("orf.at", null, 80, "http", "bla", null, null, RequestorType.PROXY);
    assertNotNull(aPA);
    assertEquals("user", aPA.getUserName());
    assertEquals("pw", new String(aPA.getPassword()));
}
Also used : ProxySettings(com.helger.network.proxy.settings.ProxySettings) PasswordAuthentication(java.net.PasswordAuthentication) Test(org.junit.Test)

Example 2 with ProxySettings

use of com.helger.network.proxy.settings.ProxySettings in project ph-web by phax.

the class ProxySelectorProxySettingsManagerTest method testDifferentHost.

@Test
public void testDifferentHost() {
    ProxySettingsManager.registerProvider((sProtocol, sHostName, nPort) -> new CommonsArrayList<>(sHostName.contains(".orf.at") ? ProxySettings.createNoProxySettings() : new ProxySettings(Proxy.Type.HTTP, "http://proxysrv", 8080)));
    // Will choose the http proxy
    List<Proxy> aProxies = ProxySelector.getDefault().select(URLHelper.getAsURI("http://www.helger.com/blafoo"));
    assertNotNull(aProxies);
    assertEquals(1, aProxies.size());
    assertNotNull(aProxies.get(0));
    assertEquals(Proxy.Type.HTTP, aProxies.get(0).type());
    assertEquals("http://proxysrv", ((InetSocketAddress) aProxies.get(0).address()).getHostString());
    assertEquals(8080, ((InetSocketAddress) aProxies.get(0).address()).getPort());
    // Will choose the no proxy
    aProxies = ProxySelector.getDefault().select(URLHelper.getAsURI("http://anyserver.orf.at/blafoo"));
    assertNotNull(aProxies);
    assertEquals(1, aProxies.size());
    assertNotNull(aProxies.get(0));
    assertEquals(Proxy.Type.DIRECT, aProxies.get(0).type());
}
Also used : Proxy(java.net.Proxy) ProxySettings(com.helger.network.proxy.settings.ProxySettings) Test(org.junit.Test)

Example 3 with ProxySettings

use of com.helger.network.proxy.settings.ProxySettings in project phoss-smp by phax.

the class SMPServerConfiguration method getAsHttpsProxySettings.

/**
 * @return A single object for all https (but not http) proxy settings. May be
 *         <code>null</code>.
 * @see #getAsHttpProxySettings()
 * @since 5.0.7
 */
@Nullable
public static IProxySettings getAsHttpsProxySettings() {
    final String sHostname = getHttpsProxyHost();
    final int nPort = getHttpsProxyPort();
    if (sHostname != null && nPort > 0)
        return new ProxySettings(Proxy.Type.HTTP, sHostname, nPort, getProxyUsername(), getProxyPassword());
    return null;
}
Also used : IProxySettings(com.helger.network.proxy.settings.IProxySettings) ProxySettings(com.helger.network.proxy.settings.ProxySettings) Nullable(javax.annotation.Nullable)

Example 4 with ProxySettings

use of com.helger.network.proxy.settings.ProxySettings in project phoss-smp by phax.

the class SMPServerConfiguration method getAsHttpProxySettings.

/**
 * @return A single object for all http (but not https) proxy settings. May be
 *         <code>null</code>.
 * @see #getAsHttpsProxySettings()
 * @since 5.0.7
 */
@Nullable
public static IProxySettings getAsHttpProxySettings() {
    final String sHostname = getHttpProxyHost();
    final int nPort = getHttpProxyPort();
    if (sHostname != null && nPort > 0)
        return new ProxySettings(Proxy.Type.HTTP, sHostname, nPort, getProxyUsername(), getProxyPassword());
    return null;
}
Also used : IProxySettings(com.helger.network.proxy.settings.IProxySettings) ProxySettings(com.helger.network.proxy.settings.ProxySettings) Nullable(javax.annotation.Nullable)

Example 5 with ProxySettings

use of com.helger.network.proxy.settings.ProxySettings in project ph-web by phax.

the class ProxyAutoConfigHelper method getProxyListForURL.

@Nonnull
public ICommonsList<IProxySettings> getProxyListForURL(@Nonnull final String sURL, @Nonnull final String sHost) throws ScriptException {
    final ICommonsList<IProxySettings> ret = new CommonsArrayList<>();
    String sProxyCode = findProxyForURL(sURL, sHost);
    if (sProxyCode != null) {
        // parse result of PAC call
        sProxyCode = sProxyCode.trim();
        if (sProxyCode.length() > 0) {
            for (String sDirective : StringHelper.getExploded(';', sProxyCode)) {
                boolean bError = true;
                sDirective = sDirective.trim();
                if (sDirective.equals("DIRECT")) {
                    ret.add(ProxySettings.createNoProxySettings());
                    bError = false;
                } else if (sDirective.startsWith("PROXY")) {
                    String[] aParts = StringHelper.getExplodedArray(' ', sDirective, 2);
                    if (aParts.length == 2) {
                        aParts = StringHelper.getExplodedArray(':', aParts[1], 2);
                        if (aParts.length == 2) {
                            final String sProxyHost = aParts[0];
                            final String sProxyPort = aParts[1];
                            final int nProxyPort = StringParser.parseInt(sProxyPort, -1);
                            ret.add(new ProxySettings(Proxy.Type.HTTP, sProxyHost, nProxyPort));
                            bError = false;
                        }
                    }
                } else if (sDirective.startsWith("SOCKS")) {
                    String[] aParts = StringHelper.getExplodedArray(' ', sDirective, 2);
                    if (aParts.length == 2) {
                        aParts = StringHelper.getExplodedArray(':', aParts[1], 2);
                        if (aParts.length == 2) {
                            final String sProxyHost = aParts[0];
                            final String sProxyPort = aParts[1];
                            final int nProxyPort = StringParser.parseInt(sProxyPort, SocksProxyConfig.DEFAULT_SOCKS_PROXY_PORT);
                            ret.add(new ProxySettings(Proxy.Type.SOCKS, sProxyHost, nProxyPort));
                            bError = false;
                        }
                    }
                }
                if (bError)
                    if (LOGGER.isWarnEnabled())
                        LOGGER.warn("Found unknown proxy directive '" + sDirective + "'");
            }
        }
    }
    return ret;
}
Also used : IProxySettings(com.helger.network.proxy.settings.IProxySettings) IProxySettings(com.helger.network.proxy.settings.IProxySettings) ProxySettings(com.helger.network.proxy.settings.ProxySettings) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) Nonnull(javax.annotation.Nonnull)

Aggregations

ProxySettings (com.helger.network.proxy.settings.ProxySettings)11 Test (org.junit.Test)8 PasswordAuthentication (java.net.PasswordAuthentication)6 IProxySettings (com.helger.network.proxy.settings.IProxySettings)4 Nullable (javax.annotation.Nullable)2 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)1 Proxy (java.net.Proxy)1 Nonnull (javax.annotation.Nonnull)1