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

The following examples show how to use org.apache.maven.settings.Proxy#getHost() . 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: Repository.java    From webdriverextensions-maven-plugin with Apache License 2.0 6 votes vote down vote up
private static String downloadAsString(URL url, Proxy proxySettings) throws IOException {
    URLConnection connection;
    if (proxySettings != null) {
        java.net.Proxy proxy = new java.net.Proxy(java.net.Proxy.Type.HTTP,
                                                  new InetSocketAddress(proxySettings.getHost(),
                                                                        proxySettings.getPort()));
        if (proxySettings.getUsername() != null) {
            ProxyUtils.setProxyAuthenticator(proxySettings);
        }
        connection = url.openConnection(proxy);
    } else {
        connection = url.openConnection();
    }

    try (InputStream inputStream = connection.getInputStream()) {
        return IOUtils.toString(inputStream, UTF_8);
    }
}
 
Example 2
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 3
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 4
Source File: AbstractSwarmMojo.java    From wildfly-swarm with Apache License 2.0 5 votes vote down vote up
public static org.eclipse.aether.repository.Proxy convertFromMavenProxy(Proxy proxy) {
    if (proxy != null) {
        return  new org.eclipse.aether.repository.Proxy(proxy.getProtocol(),
                                                        proxy.getHost(),
                                                        proxy.getPort(),
                                                        new AuthenticationBuilder()
                                                                .addUsername(proxy.getUsername())
                                                                .addPassword(proxy.getPassword())
                                                                .build());
    }

    return null;
}
 
Example 5
Source File: RawXJC2Mojo.java    From maven-jaxb2-plugin with BSD 2-Clause "Simplified" License 5 votes vote down vote up
protected String getActiveProxyAsHttpproxy() {
	if (getSettings() == null) {
		return null;
	}

	final Settings settings = getSettings();

	final Proxy activeProxy = settings.getActiveProxy();
	if (activeProxy == null || activeProxy.getHost() == null) {
		return null;
	}

	return createXJCProxyArgument(activeProxy.getHost(), activeProxy.getPort(), activeProxy.getUsername(),
			activeProxy.getPassword());
}
 
Example 6
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 7
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;
}
 
Example 8
Source File: GatewayAbstractMojo.java    From apigee-deploy-maven-plugin with Apache License 2.0 4 votes vote down vote up
/**
	 * Create a server profile from the mojo configuration provided.
	 * <p>
	 * TODO this is a smell, why do I have to fiddle with auth fields when I just want to build/configure a build artifact.
	 *
	 * @param validateAuthentication A flag to indicate if the server profile must include authentication details
	 *
	 * @return a valid server profile (with or without auth information - yuck)
	 */
	public ServerProfile createProfile(boolean validateAuthentication) throws MojoExecutionException {
		ServerProfile profile = new ServerProfile();

		// basic stuff
		if (isBlank(orgName)) {
			throw new MojoExecutionException("Parameter apigee.org must be provided");
		} else {
			profile.setOrg(orgName);
		}

		profile.setApi_version(apiVersion);

		profile.setHostUrl(hostURL);
		profile.setEnvironment(deploymentEnv);
		profile.setProfileId(id);

		// setup legacy build options only if no options have been provided
		if (isBlank(options) && isNotBlank(buildOption)) {
			if ("deploy-inactive".equals(buildOption)) {
				getLog().warn("Note: -Dbuild.option=deploy-inactive is Deprecated, use -Dapigee.options=inactive instead");
				profile.addAction(ActionFlags.INACTIVE, ActionFlags.VALIDATE);
			} else if ("undeploy".equals(buildOption)) {
				// FIXME The original code only undeploys the module but does not delete it, there is no equivalent in the current option set.
				// old code did this: client.deactivateBundle(bundle);
				getLog().warn("Note: -Dbuild.option=undeploy is Deprecated, use -Dapigee.options=clean instead");
				profile.addAction(ActionFlags.CLEAN);
			} else if ("delete".equals(buildOption)) {
				profile.addAction(ActionFlags.CLEAN);
			}
		} else {
			profile.setActions(parseOptions(options));
		}

		profile.setDelay(delay);
		profile.setDelayOverride(overrideDelay);

		// process auth settings
		if (validateAuthentication) {
			profile.setTokenUrl(tokenURL);
			profile.setMFAToken(mfaToken);
			profile.setAuthType(authType);
			profile.setBearerToken(bearer);
			profile.setRefreshToken(refresh);
			profile.setCredential_user(username);
			profile.setCredential_pwd(password);
			profile.setClientId(clientid);
			profile.setClientSecret(clientsecret);
			// TODO check auth type in [oauth|basic]
			if ("basic".equalsIgnoreCase(profile.getAuthType()) &&
					// basic auth requires both username and password
					(isBlank(profile.getCredential_user()) || isBlank(profile.getCredential_pwd()))) {
				throw new MojoExecutionException("Username and password must be provided for basic authentication.");
			} //else if ("oauth".equalsIgnoreCase(profile.getAuthType()) &&
					// either one is good enough
				//	(isBlank(profile.getBearerToken()) && isBlank(profile.getRefreshToken()))) {
				//throw new MojoExecutionException("A bearer or refresh token must be provided for token based authentication.");
			//}

			// process proxy for management api endpoint
			Proxy mavenProxy = getProxy(settings, hostURL);
			if (mavenProxy != null) {
				getLog().info("set proxy to " + mavenProxy.getHost() + ":" + mavenProxy.getPort());
				DefaultHttpClient httpClient = new DefaultHttpClient();
				HttpHost proxy = new HttpHost(mavenProxy.getHost(), mavenProxy.getPort());
				httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);

				if (isNotBlank(mavenProxy.getNonProxyHosts())) {
//				System.setProperty("http.nonProxyHosts", mavenProxy.getNonProxyHosts().replaceAll("[,;]", "|"));
					// TODO selector based proxy
				}

				if (isNotBlank(mavenProxy.getUsername()) && isNotBlank(mavenProxy.getPassword())) {
					getLog().debug("set proxy credentials");
					httpClient.setProxyAuthenticationHandler(new DefaultProxyAuthenticationHandler());
					httpClient.getCredentialsProvider().setCredentials(
							new AuthScope(mavenProxy.getHost(), mavenProxy.getPort()),
							new UsernamePasswordCredentials(mavenProxy.getUsername(), mavenProxy.getPassword())
					);
				}
				profile.setApacheHttpClient(httpClient);
				
				//Set Proxy configurations
				profile.setHasProxy(true);
				profile.setProxyProtocol(mavenProxy.getProtocol());
				profile.setProxyServer(mavenProxy.getHost());
				profile.setProxyPort(mavenProxy.getPort());
			}

		}

		return profile;
	}
 
Example 9
Source File: ProxyUtils.java    From webdriverextensions-maven-plugin with Apache License 2.0 4 votes vote down vote up
public static HttpHost createProxyFromSettings(Proxy proxySettings) throws MojoExecutionException {
    if (proxySettings == null) {
        return null;
    }
    return new HttpHost(proxySettings.getHost(), proxySettings.getPort());
}