Search in sources :

Example 21 with Proxy

use of org.apache.maven.settings.Proxy in project maven-plugins by apache.

the class SettingsStub method getActiveProxy.

/** {@inheritDoc} */
public synchronized Proxy getActiveProxy() {
    Proxy proxy = new Proxy();
    proxy.setActive(true);
    proxy.setHost("http://localhost");
    proxy.setPort(80);
    proxy.setUsername("toto");
    proxy.setPassword("toto");
    proxy.setNonProxyHosts("www.google.com|*.somewhere.com");
    return proxy;
}
Also used : Proxy(org.apache.maven.settings.Proxy)

Example 22 with Proxy

use of org.apache.maven.settings.Proxy in project maven-plugins by apache.

the class AbstractSiteDeployWebDavTest method davDeployThruProxyWithoutAuthzInProxy.

@Test
public void davDeployThruProxyWithoutAuthzInProxy() throws Exception {
    FileUtils.cleanDirectory(siteTargetPath);
    SimpleDavServerHandler simpleDavServerHandler = new SimpleDavServerHandler(siteTargetPath);
    try {
        File pluginXmlFile = getTestFile("src/test/resources/unit/deploy-dav/pom.xml");
        AbstractMojo mojo = getMojo(pluginXmlFile);
        assertNotNull(mojo);
        SiteMavenProjectStub siteMavenProjectStub = new SiteMavenProjectStub("deploy-dav");
        // olamy, Note : toto is something like foo or bar for french folks :-)
        String siteUrl = "dav:http://toto.com/site/";
        siteMavenProjectStub.getDistributionManagement().getSite().setUrl(siteUrl);
        setVariableValueToObject(mojo, "project", siteMavenProjectStub);
        Settings settings = new Settings();
        Proxy proxy = new Proxy();
        //dummy proxy
        proxy.setActive(true);
        proxy.setHost("localhost");
        proxy.setPort(simpleDavServerHandler.getPort());
        proxy.setProtocol("http");
        proxy.setNonProxyHosts("www.google.com|*.somewhere.com");
        settings.addProxy(proxy);
        setVariableValueToObject(mojo, "settings", settings);
        MavenExecutionRequest request = new DefaultMavenExecutionRequest();
        request.setProxies(Arrays.asList(proxy));
        MavenSession mavenSession = new MavenSession(getContainer(), null, request, null);
        setVariableValueToObject(mojo, "mavenSession", mavenSession);
        File inputDirectory = new File("src/test/resources/unit/deploy-dav/target/site");
        setVariableValueToObject(mojo, "inputDirectory", inputDirectory);
        mojo.execute();
        assertContentInFiles();
        assertTrue(requestsContainsProxyUse(simpleDavServerHandler.httpRequests));
        for (HttpRequest rq : simpleDavServerHandler.httpRequests) {
            log.info(rq.toString());
        }
    } finally {
        simpleDavServerHandler.stop();
    }
}
Also used : HttpRequest(org.apache.maven.plugins.site.deploy.SimpleDavServerHandler.HttpRequest) SiteMavenProjectStub(org.apache.maven.plugins.site.stubs.SiteMavenProjectStub) MavenSession(org.apache.maven.execution.MavenSession) Proxy(org.apache.maven.settings.Proxy) MavenExecutionRequest(org.apache.maven.execution.MavenExecutionRequest) DefaultMavenExecutionRequest(org.apache.maven.execution.DefaultMavenExecutionRequest) DefaultMavenExecutionRequest(org.apache.maven.execution.DefaultMavenExecutionRequest) AbstractMojo(org.apache.maven.plugin.AbstractMojo) File(java.io.File) Settings(org.apache.maven.settings.Settings) Test(org.junit.Test)

Example 23 with Proxy

use of org.apache.maven.settings.Proxy in project maven-plugins by apache.

the class ProjectInfoReportUtils method getContent.

/**
     * Get the input stream from a URL.
     *
     * @param url not null
     * @param project could be null
     * @param settings not null to handle proxy settings
     * @param encoding the wanted encoding for the URL input stream. If null, UTF-8 will be used.
     * @return the input stream decoded with the wanted encoding as string
     * @throws IOException if any
     * @since 2.3
     */
public static String getContent(URL url, MavenProject project, Settings settings, String encoding) throws IOException {
    String scheme = url.getProtocol();
    if (StringUtils.isEmpty(encoding)) {
        encoding = DEFAULT_ENCODING;
    }
    if ("file".equals(scheme)) {
        InputStream in = null;
        try {
            URLConnection conn = url.openConnection();
            in = conn.getInputStream();
            final String content = IOUtil.toString(in, encoding);
            in.close();
            in = null;
            return content;
        } finally {
            IOUtil.close(in);
        }
    }
    Proxy proxy = settings.getActiveProxy();
    if (proxy != null) {
        if ("http".equals(scheme) || "https".equals(scheme) || "ftp".equals(scheme)) {
            scheme += ".";
        } else {
            scheme = "";
        }
        String host = proxy.getHost();
        if (!StringUtils.isEmpty(host)) {
            Properties p = System.getProperties();
            p.setProperty(scheme + "proxySet", "true");
            p.setProperty(scheme + "proxyHost", host);
            p.setProperty(scheme + "proxyPort", String.valueOf(proxy.getPort()));
            if (!StringUtils.isEmpty(proxy.getNonProxyHosts())) {
                p.setProperty(scheme + "nonProxyHosts", proxy.getNonProxyHosts());
            }
            final String userName = proxy.getUsername();
            if (!StringUtils.isEmpty(userName)) {
                final String pwd = StringUtils.isEmpty(proxy.getPassword()) ? "" : proxy.getPassword();
                Authenticator.setDefault(new Authenticator() {

                    /** {@inheritDoc} */
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(userName, pwd.toCharArray());
                    }
                });
            }
        }
    }
    InputStream in = null;
    try {
        URLConnection conn = getURLConnection(url, project, settings);
        in = conn.getInputStream();
        final String string = IOUtil.toString(in, encoding);
        in.close();
        in = null;
        return string;
    } finally {
        IOUtil.close(in);
    }
}
Also used : Proxy(org.apache.maven.settings.Proxy) InputStream(java.io.InputStream) Properties(java.util.Properties) URLConnection(java.net.URLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) Authenticator(java.net.Authenticator) PasswordAuthentication(java.net.PasswordAuthentication)

Example 24 with Proxy

use of org.apache.maven.settings.Proxy in project cxf by apache.

the class AbstractCodegenMoho method configureProxyServerSettings.

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_PORT, authPassword);
        }
    }
}
Also used : Proxy(org.apache.maven.settings.Proxy) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Authenticator(java.net.Authenticator) PasswordAuthentication(java.net.PasswordAuthentication)

Aggregations

Proxy (org.apache.maven.settings.Proxy)24 Settings (org.apache.maven.settings.Settings)6 ProxyInfo (org.apache.maven.wagon.proxy.ProxyInfo)5 File (java.io.File)4 URL (java.net.URL)3 HashMap (java.util.HashMap)3 MavenExecutionRequest (org.apache.maven.execution.MavenExecutionRequest)3 MavenSession (org.apache.maven.execution.MavenSession)3 FileNotFoundException (java.io.FileNotFoundException)2 InputStream (java.io.InputStream)2 Authenticator (java.net.Authenticator)2 MalformedURLException (java.net.MalformedURLException)2 PasswordAuthentication (java.net.PasswordAuthentication)2 SocketTimeoutException (java.net.SocketTimeoutException)2 ArrayList (java.util.ArrayList)2 Credentials (org.apache.commons.httpclient.Credentials)2 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)2 DefaultMavenExecutionRequest (org.apache.maven.execution.DefaultMavenExecutionRequest)2 AbstractMojo (org.apache.maven.plugin.AbstractMojo)2 AuthAsyncProxyServlet (org.apache.maven.plugin.javadoc.ProxyServer.AuthAsyncProxyServlet)2