use of org.apache.maven.wagon.proxy.ProxyInfo in project felix by apache.
the class RemoteFileManager method getProxyInfo.
/**
* Convenience method to map a Proxy object from the user system settings to a ProxyInfo object.
* @param settings project settings given by maven
* @return a proxyInfo object instancied or null if no active proxy is define in the settings.xml
*/
public static ProxyInfo getProxyInfo(Settings settings) {
ProxyInfo proxyInfo = null;
if (settings != null && settings.getActiveProxy() != null) {
Proxy settingsProxy = settings.getActiveProxy();
proxyInfo = new ProxyInfo();
proxyInfo.setHost(settingsProxy.getHost());
proxyInfo.setType(settingsProxy.getProtocol());
proxyInfo.setPort(settingsProxy.getPort());
proxyInfo.setNonProxyHosts(settingsProxy.getNonProxyHosts());
proxyInfo.setUserName(settingsProxy.getUsername());
proxyInfo.setPassword(settingsProxy.getPassword());
}
return proxyInfo;
}
use of org.apache.maven.wagon.proxy.ProxyInfo in project fabric8 by jboss-fuse.
the class ConfigurableHttpWagon method execute.
@Override
protected CloseableHttpResponse execute(HttpUriRequest httpMethod) throws HttpException, IOException {
setHeaders(httpMethod);
String userAgent = getUserAgent(httpMethod);
if (userAgent != null) {
httpMethod.setHeader(HTTP.USER_AGENT, userAgent);
}
RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();
// WAGON-273: default the cookie-policy to browser compatible
requestConfigBuilder.setCookieSpec(CookieSpecs.BROWSER_COMPATIBILITY);
Repository repo = getRepository();
ProxyInfo proxyInfo = getProxyInfo(repo.getProtocol(), repo.getHost());
if (proxyInfo != null) {
HttpHost proxy = new HttpHost(proxyInfo.getHost(), proxyInfo.getPort());
requestConfigBuilder.setProxy(proxy);
}
HttpMethodConfiguration config = getHttpConfiguration() == null ? null : getHttpConfiguration().getMethodConfiguration(httpMethod);
if (config != null) {
copyConfig(config, requestConfigBuilder);
} else {
requestConfigBuilder.setSocketTimeout(getReadTimeout());
requestConfigBuilder.setConnectTimeout(getTimeout());
}
getLocalContext().setRequestConfig(requestConfigBuilder.build());
if (config != null && config.isUsePreemptive()) {
HttpHost targetHost = new HttpHost(repo.getHost(), repo.getPort(), repo.getProtocol());
AuthScope targetScope = getBasicAuthScope().getScope(targetHost);
if (getCredentialsProvider().getCredentials(targetScope) != null) {
BasicScheme targetAuth = new BasicScheme();
targetAuth.processChallenge(new BasicHeader(AUTH.WWW_AUTH, "BASIC preemptive"));
getAuthCache().put(targetHost, targetAuth);
}
}
if (proxyInfo != null) {
if (proxyInfo.getHost() != null) {
HttpHost proxyHost = new HttpHost(proxyInfo.getHost(), proxyInfo.getPort());
AuthScope proxyScope = getProxyBasicAuthScope().getScope(proxyHost);
String proxyUsername = proxyInfo.getUserName();
String proxyPassword = proxyInfo.getPassword();
String proxyNtlmHost = proxyInfo.getNtlmHost();
String proxyNtlmDomain = proxyInfo.getNtlmDomain();
if (proxyUsername != null && proxyPassword != null) {
Credentials creds;
if (proxyNtlmHost != null || proxyNtlmDomain != null) {
creds = new NTCredentials(proxyUsername, proxyPassword, proxyNtlmHost, proxyNtlmDomain);
} else {
creds = new UsernamePasswordCredentials(proxyUsername, proxyPassword);
}
getCredentialsProvider().setCredentials(proxyScope, creds);
BasicScheme proxyAuth = new BasicScheme();
proxyAuth.processChallenge(new BasicHeader(AUTH.PROXY_AUTH, "BASIC preemptive"));
getAuthCache().put(proxyHost, proxyAuth);
}
}
}
return client.execute(httpMethod, getLocalContext());
}
use of org.apache.maven.wagon.proxy.ProxyInfo in project archiva by apache.
the class RepositoryModelResolver method connectToRepository.
/**
* Using wagon, connect to the remote repository.
*
* @param wagon the wagon instance to establish the connection on.
* @return true if the connection was successful. false if not connected.
*/
private boolean connectToRepository(Wagon wagon, RemoteRepository remoteRepository) {
boolean connected;
final NetworkProxy proxyConnector = this.networkProxyMap.get(remoteRepository.getId());
ProxyInfo networkProxy = null;
if (proxyConnector != null) {
networkProxy = new ProxyInfo();
networkProxy.setType(proxyConnector.getProtocol());
networkProxy.setHost(proxyConnector.getHost());
networkProxy.setPort(proxyConnector.getPort());
networkProxy.setUserName(proxyConnector.getUsername());
networkProxy.setPassword(proxyConnector.getPassword());
String msg = "Using network proxy " + networkProxy.getHost() + ":" + networkProxy.getPort() + " to connect to remote repository " + remoteRepository.getLocation();
if (networkProxy.getNonProxyHosts() != null) {
msg += "; excluding hosts: " + networkProxy.getNonProxyHosts();
}
if (StringUtils.isNotBlank(networkProxy.getUserName())) {
msg += "; as user: " + networkProxy.getUserName();
}
log.debug(msg);
}
AuthenticationInfo authInfo = null;
RepositoryCredentials creds = remoteRepository.getLoginCredentials();
String username = "";
String password = "";
if (creds instanceof UsernamePasswordCredentials) {
UsernamePasswordCredentials c = (UsernamePasswordCredentials) creds;
username = c.getUserName();
password = c.getPassword();
}
if (StringUtils.isNotBlank(username) && StringUtils.isNotBlank(password)) {
log.debug("Using username {} to connect to remote repository {}", username, remoteRepository.getLocation());
authInfo = new AuthenticationInfo();
authInfo.setUserName(username);
authInfo.setPassword(password);
}
int timeoutInMilliseconds = ((int) remoteRepository.getTimeout().getSeconds()) * 1000;
// FIXME olamy having 2 config values
// Set timeout
wagon.setReadTimeout(timeoutInMilliseconds);
wagon.setTimeout(timeoutInMilliseconds);
try {
org.apache.maven.wagon.repository.Repository wagonRepository = new org.apache.maven.wagon.repository.Repository(remoteRepository.getId(), remoteRepository.getLocation().toString());
if (networkProxy != null) {
wagon.connect(wagonRepository, authInfo, networkProxy);
} else {
wagon.connect(wagonRepository, authInfo);
}
connected = true;
} catch (ConnectionException | AuthenticationException e) {
log.error("Could not connect to {}:{} ", remoteRepository.getName(), e.getMessage());
connected = false;
}
return connected;
}
use of org.apache.maven.wagon.proxy.ProxyInfo in project archiva by apache.
the class DefaultRepositoryProxyConnectors method connectToRepository.
/**
* Using wagon, connect to the remote repository.
*
* @param connector the connector configuration to utilize (for obtaining network proxy configuration from)
* @param wagon the wagon instance to establish the connection on.
* @param remoteRepository the remote repository to connect to.
* @return true if the connection was successful. false if not connected.
*/
private boolean connectToRepository(ProxyConnector connector, Wagon wagon, RemoteRepositoryContent remoteRepository) {
boolean connected = false;
final ProxyInfo networkProxy = connector.getProxyId() == null ? null : this.networkProxyMap.get(connector.getProxyId());
if (log.isDebugEnabled()) {
if (networkProxy != null) {
// TODO: move to proxyInfo.toString()
String msg = "Using network proxy " + networkProxy.getHost() + ":" + networkProxy.getPort() + " to connect to remote repository " + remoteRepository.getURL();
if (networkProxy.getNonProxyHosts() != null) {
msg += "; excluding hosts: " + networkProxy.getNonProxyHosts();
}
if (StringUtils.isNotBlank(networkProxy.getUserName())) {
msg += "; as user: " + networkProxy.getUserName();
}
log.debug(msg);
}
}
AuthenticationInfo authInfo = null;
String username = "";
String password = "";
RepositoryCredentials repCred = remoteRepository.getRepository().getLoginCredentials();
if (repCred != null && repCred instanceof PasswordCredentials) {
PasswordCredentials pwdCred = (PasswordCredentials) repCred;
username = pwdCred.getUsername();
password = pwdCred.getPassword() == null ? "" : new String(pwdCred.getPassword());
}
if (StringUtils.isNotBlank(username) && StringUtils.isNotBlank(password)) {
log.debug("Using username {} to connect to remote repository {}", username, remoteRepository.getURL());
authInfo = new AuthenticationInfo();
authInfo.setUserName(username);
authInfo.setPassword(password);
}
// Convert seconds to milliseconds
long timeoutInMilliseconds = remoteRepository.getRepository().getTimeout().toMillis();
// Set timeout read and connect
// FIXME olamy having 2 config values
wagon.setReadTimeout((int) timeoutInMilliseconds);
wagon.setTimeout((int) timeoutInMilliseconds);
try {
Repository wagonRepository = new Repository(remoteRepository.getId(), remoteRepository.getURL().toString());
wagon.connect(wagonRepository, authInfo, networkProxy);
connected = true;
} catch (ConnectionException | AuthenticationException e) {
log.warn("Could not connect to {}: {}", remoteRepository.getRepository().getName(), e.getMessage());
connected = false;
}
return connected;
}
use of org.apache.maven.wagon.proxy.ProxyInfo in project archiva by apache.
the class DefaultRemoteRepositoriesService method checkRemoteConnectivity.
@Override
public Boolean checkRemoteConnectivity(String repositoryId) throws ArchivaRestServiceException {
try {
RemoteRepository remoteRepository = remoteRepositoryAdmin.getRemoteRepository(repositoryId);
if (remoteRepository == null) {
log.warn("ignore scheduleDownloadRemote for repo with id {} as not exists", repositoryId);
return Boolean.FALSE;
}
NetworkProxy networkProxy = null;
if (StringUtils.isNotBlank(remoteRepository.getRemoteDownloadNetworkProxyId())) {
networkProxy = networkProxyAdmin.getNetworkProxy(remoteRepository.getRemoteDownloadNetworkProxyId());
if (networkProxy == null) {
log.warn("your remote repository is configured to download remote index trought a proxy we cannot find id:{}", remoteRepository.getRemoteDownloadNetworkProxyId());
}
}
String wagonProtocol = new URL(remoteRepository.getUrl()).getProtocol();
final Wagon wagon = wagonFactory.getWagon(//
new WagonFactoryRequest(wagonProtocol, remoteRepository.getExtraHeaders()).networkProxy(networkProxy));
// hardcoded value as it's a check of the remote repo connectivity
wagon.setReadTimeout(checkReadTimeout);
wagon.setTimeout(checkTimeout);
if (wagon instanceof AbstractHttpClientWagon) {
HttpMethodConfiguration httpMethodConfiguration = //
new HttpMethodConfiguration().setUsePreemptive(//
true).setReadTimeout(checkReadTimeout);
HttpConfiguration httpConfiguration = new HttpConfiguration().setGet(httpMethodConfiguration);
AbstractHttpClientWagon.class.cast(wagon).setHttpConfiguration(httpConfiguration);
}
ProxyInfo proxyInfo = null;
if (networkProxy != null) {
proxyInfo = new ProxyInfo();
proxyInfo.setType(networkProxy.getProtocol());
proxyInfo.setHost(networkProxy.getHost());
proxyInfo.setPort(networkProxy.getPort());
proxyInfo.setUserName(networkProxy.getUsername());
proxyInfo.setPassword(networkProxy.getPassword());
}
String url = StringUtils.stripEnd(remoteRepository.getUrl(), "/");
wagon.connect(new Repository(remoteRepository.getId(), url), proxyInfo);
// MRM-1933, there are certain servers that do not allow browsing
if (!(StringUtils.isEmpty(remoteRepository.getCheckPath()) || "/".equals(remoteRepository.getCheckPath()))) {
return wagon.resourceExists(remoteRepository.getCheckPath());
} else {
// we only check connectivity as remote repo can be empty
// MRM-1909: Wagon implementation appends a slash already
wagon.getFileList("");
}
return Boolean.TRUE;
} catch (TransferFailedException e) {
log.info("TransferFailedException :{}", e.getMessage());
return Boolean.FALSE;
} catch (Exception e) {
// This service returns either true or false, Exception cannot be handled by the clients
log.debug("Exception occured on connectivity test.", e);
log.info("Connection exception: {}", e.getMessage());
return Boolean.FALSE;
}
}
Aggregations