Java Code Examples for org.apache.maven.settings.Proxy#getNonProxyHosts()

The following examples show how to use org.apache.maven.settings.Proxy#getNonProxyHosts() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MavenMvnSettings.java    From galleon with Apache License 2.0 5 votes vote down vote up
MavenMvnSettings(MavenConfig config, RepositorySystem repoSystem, RepositoryListener listener) throws ArtifactException {
    this.config = config;
    Settings settings = buildMavenSettings(config.getSettings());
    Proxy proxy = settings.getActiveProxy();
    if (proxy != null) {
        MavenProxySelector.Builder builder = new MavenProxySelector.Builder(proxy.getHost(), proxy.getPort(), proxy.getProtocol());
        builder.setPassword(proxy.getPassword());
        builder.setUserName(proxy.getUsername());
        if (proxy.getNonProxyHosts() != null) {
            String[] hosts = proxy.getNonProxyHosts().split("\\|");
            builder.addNonProxyHosts(Arrays.asList(hosts));
        }
        proxySelector = builder.build();
        Authentication auth = null;
        if (proxy.getPassword() != null && proxy.getUsername() != null) {
            auth = new AuthenticationBuilder().addUsername(proxy.getUsername()).addPassword(proxy.getPassword()).build();
        }
        this.proxy = new org.eclipse.aether.repository.Proxy(proxy.getProtocol(), proxy.getHost(), proxy.getPort(), auth);
    } else {
        this.proxy = null;
        proxySelector = null;
    }
    try {
        repositories = Collections.unmodifiableList(buildRemoteRepositories(settings));
    } catch (MalformedURLException ex) {
        throw new ArtifactException(ex.getMessage(), ex);
    }
    session = Util.newRepositorySession(repoSystem,
            settings.getLocalRepository() == null ? config.getLocalRepository() : Paths.get(settings.getLocalRepository()),
            listener, proxySelector, settings.isOffline());
}
 
Example 2
Source File: ProxySettings.java    From mvn-golang with Apache License 2.0 5 votes vote down vote up
public ProxySettings(@Nonnull final Proxy mavenProxy) {
    this.protocol = mavenProxy.getProtocol();
    this.host = mavenProxy.getHost();
    this.port = mavenProxy.getPort();
    this.username = mavenProxy.getUsername();
    this.password = mavenProxy.getPassword();
    this.nonProxyHosts = mavenProxy.getNonProxyHosts();
}
 
Example 3
Source File: GatewayAbstractMojo.java    From apigee-deploy-maven-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * Check hostname that matched nonProxy setting
 *
 * @param proxy    Maven Proxy. Must not null
 * @param hostname
 *
 * @return matching result. true: match nonProxy
 */
protected boolean matchNonProxy(final Proxy proxy, final String hostname) {

	// code from org.apache.maven.plugins.site.AbstractDeployMojo#getProxyInfo
	final String nonProxyHosts = proxy.getNonProxyHosts();
	if (null != nonProxyHosts) {
		final String[] nonProxies = nonProxyHosts.split("(,)|(;)|(\\|)");
		if (null != nonProxies) {
			for (final String nonProxyHost : nonProxies) {
				//if ( StringUtils.contains( nonProxyHost, "*" ) )
				if (null != nonProxyHost && nonProxyHost.contains("*")) {
					// Handle wildcard at the end, beginning or middle of the nonProxyHost
					final int pos = nonProxyHost.indexOf('*');
					String nonProxyHostPrefix = nonProxyHost.substring(0, pos);
					String nonProxyHostSuffix = nonProxyHost.substring(pos + 1);
					// prefix*
					if (!isBlank(nonProxyHostPrefix) && hostname.startsWith(nonProxyHostPrefix) && isBlank(nonProxyHostSuffix)) {
						return true;
					}
					// *suffix
					if (isBlank(nonProxyHostPrefix) && !isBlank(nonProxyHostSuffix) && hostname.endsWith(nonProxyHostSuffix)) {
						return true;
					}
					// prefix*suffix
					if (!isBlank(nonProxyHostPrefix) && hostname.startsWith(nonProxyHostPrefix)
							&& !isBlank(nonProxyHostSuffix) && hostname.endsWith(nonProxyHostSuffix)) {
						return true;
					}
				} else if (hostname.equals(nonProxyHost)) {
					return true;
				}
			}
		}
	}

	return false;
}
 
Example 4
Source File: AbstractCodegenMojo.java    From cxf with Apache License 2.0 5 votes vote down vote up
protected void configureProxyServerSettings() throws MojoExecutionException {

        Proxy proxy = mavenSession.getSettings().getActiveProxy();

        if (proxy != null) {

            getLog().info("Using proxy server configured in maven.");

            if (proxy.getHost() == null) {
                throw new MojoExecutionException("Proxy in settings.xml has no host");
            }
            if (proxy.getHost() != null) {
                System.setProperty(HTTP_PROXY_HOST, proxy.getHost());
            }
            if (String.valueOf(proxy.getPort()) != null) {
                System.setProperty(HTTP_PROXY_PORT, String.valueOf(proxy.getPort()));
            }
            if (proxy.getNonProxyHosts() != null) {
                System.setProperty(HTTP_NON_PROXY_HOSTS, proxy.getNonProxyHosts());
            }
            if (!StringUtils.isEmpty(proxy.getUsername())
                && !StringUtils.isEmpty(proxy.getPassword())) {
                final String authUser = proxy.getUsername();
                final String authPassword = proxy.getPassword();
                Authenticator.setDefault(new Authenticator() {
                    public PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(authUser, authPassword.toCharArray());
                    }
                });

                System.setProperty(HTTP_PROXY_USER, authUser);
                System.setProperty(HTTP_PROXY_PASSWORD, authPassword);
            }

        }
    }
 
Example 5
Source File: AbstractXmlMojo.java    From xml-maven-plugin with Apache License 2.0 4 votes vote down vote up
/**
 * Called to install the plugins proxy settings.
 */
protected Object activateProxy()
{
    if ( settings == null )
    {
        return null;
    }
    final Proxy proxy = settings.getActiveProxy();
    if ( proxy == null )
    {
        return null;
    }

    final List<String> properties = new ArrayList<String>();
    final String protocol = proxy.getProtocol();
    final String prefix = isEmpty( protocol ) ? "" : ( protocol + "." );

    final String host = proxy.getHost();
    final String hostProperty = prefix + "proxyHost";
    final String hostValue = isEmpty( host ) ? null : host;
    setProperty( properties, hostProperty, hostValue );
    final int port = proxy.getPort();
    final String portProperty = prefix + "proxyPort";
    final String portValue = ( port == 0 || port == -1 ) ? null : String.valueOf( port );
    setProperty( properties, portProperty, portValue );
    final String username = proxy.getUsername();
    final String userProperty = prefix + "proxyUser";
    final String userValue = isEmpty( username ) ? null : username;
    setProperty( properties, userProperty, userValue );
    final String password = proxy.getPassword();
    final String passwordProperty = prefix + "proxyPassword";
    final String passwordValue = isEmpty( password ) ? null : password;
    setProperty( properties, passwordProperty, passwordValue );
    final String nonProxyHosts = proxy.getNonProxyHosts();
    final String nonProxyHostsProperty = prefix + "nonProxyHosts";
    final String nonProxyHostsValue = isEmpty( nonProxyHosts ) ? null : nonProxyHosts.replace( ',', '|' );
    setProperty( properties, nonProxyHostsProperty, nonProxyHostsValue );
    getLog().debug( "Proxy settings: " + hostProperty + "=" + hostValue + ", " + portProperty + "=" + portValue
        + ", " + userProperty + "=" + userValue + ", " + passwordProperty + "="
        + ( passwordValue == null ? "null" : "<PasswordNotLogged>" ) + ", " + nonProxyHostsProperty + "="
        + nonProxyHostsValue );
    return properties;
}