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;
}
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();
}
}
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);
}
}
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);
}
}
}
Aggregations