use of org.apache.archiva.repository.base.PasswordCredentials in project archiva by apache.
the class ArchivaIndexManagerMock method update.
@Override
public void update(final ArchivaIndexingContext context, final boolean fullUpdate) throws IndexUpdateFailedException {
log.info("start download remote index for remote repository {}", context.getRepository().getId());
URI remoteUpdateUri;
if (!(context.getRepository() instanceof RemoteRepository) || !(context.getRepository().supportsFeature(RemoteIndexFeature.class))) {
throw new IndexUpdateFailedException("The context is not associated to a remote repository with remote index " + context.getId());
} else {
RemoteIndexFeature rif = context.getRepository().getFeature(RemoteIndexFeature.class);
remoteUpdateUri = context.getRepository().getLocation().resolve(rif.getIndexUri());
}
final RemoteRepository remoteRepository = (RemoteRepository) context.getRepository();
executeUpdateFunction(context, indexingContext -> {
try {
// create a temp directory to download files
Path tempIndexDirectory = Paths.get(indexingContext.getIndexDirectoryFile().getParent(), ".tmpIndex");
Path indexCacheDirectory = Paths.get(indexingContext.getIndexDirectoryFile().getParent(), ".indexCache");
Files.createDirectories(indexCacheDirectory);
if (Files.exists(tempIndexDirectory)) {
org.apache.archiva.common.utils.FileUtils.deleteDirectory(tempIndexDirectory);
}
Files.createDirectories(tempIndexDirectory);
tempIndexDirectory.toFile().deleteOnExit();
String baseIndexUrl = indexingContext.getIndexUpdateUrl();
String wagonProtocol = remoteUpdateUri.toURL().getProtocol();
NetworkProxy networkProxy = null;
if (remoteRepository.supportsFeature(RemoteIndexFeature.class)) {
RemoteIndexFeature rif = remoteRepository.getFeature(RemoteIndexFeature.class);
if (StringUtils.isNotBlank(rif.getProxyId())) {
networkProxy = proxyRegistry.getNetworkProxy(rif.getProxyId());
if (networkProxy == null) {
log.warn("your remote repository is configured to download remote index trought a proxy we cannot find id:{}", rif.getProxyId());
}
}
final StreamWagon wagon = (StreamWagon) wagonFactory.getWagon(new WagonFactoryRequest(wagonProtocol, remoteRepository.getExtraHeaders()).networkProxy(networkProxy));
int readTimeout = (int) rif.getDownloadTimeout().toMillis() * 1000;
wagon.setReadTimeout(readTimeout);
wagon.setTimeout((int) remoteRepository.getTimeout().toMillis() * 1000);
if (wagon instanceof AbstractHttpClientWagon) {
HttpConfiguration httpConfiguration = new HttpConfiguration();
HttpMethodConfiguration httpMethodConfiguration = new HttpMethodConfiguration();
httpMethodConfiguration.setUsePreemptive(true);
httpMethodConfiguration.setReadTimeout(readTimeout);
httpConfiguration.setGet(httpMethodConfiguration);
AbstractHttpClientWagon.class.cast(wagon).setHttpConfiguration(httpConfiguration);
}
wagon.addTransferListener(new DownloadListener());
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(new String(networkProxy.getPassword()));
}
AuthenticationInfo authenticationInfo = null;
if (remoteRepository.getLoginCredentials() != null && (remoteRepository.getLoginCredentials() instanceof PasswordCredentials)) {
PasswordCredentials creds = (PasswordCredentials) remoteRepository.getLoginCredentials();
authenticationInfo = new AuthenticationInfo();
authenticationInfo.setUserName(creds.getUsername());
authenticationInfo.setPassword(new String(creds.getPassword()));
}
wagon.connect(new org.apache.maven.wagon.repository.Repository(remoteRepository.getId(), baseIndexUrl), authenticationInfo, proxyInfo);
Path indexDirectory = indexingContext.getIndexDirectoryFile().toPath();
if (!Files.exists(indexDirectory)) {
Files.createDirectories(indexDirectory);
}
ResourceFetcher resourceFetcher = new WagonResourceFetcher(log, tempIndexDirectory, wagon, remoteRepository);
IndexUpdateRequest request = new IndexUpdateRequest(indexingContext, resourceFetcher);
request.setForceFullUpdate(fullUpdate);
request.setLocalIndexCacheDir(indexCacheDirectory.toFile());
// indexUpdater.fetchAndUpdateIndex( request );
indexingContext.updateTimestamp(true);
}
} catch (AuthenticationException e) {
log.error("Could not login to the remote proxy for updating index of {}", remoteRepository.getId(), e);
throw new IndexUpdateFailedException("Login in to proxy failed while updating remote repository " + remoteRepository.getId(), e);
} catch (ConnectionException e) {
log.error("Connection error during index update for remote repository {}", remoteRepository.getId(), e);
throw new IndexUpdateFailedException("Connection error during index update for remote repository " + remoteRepository.getId(), e);
} catch (MalformedURLException e) {
log.error("URL for remote index update of remote repository {} is not correct {}", remoteRepository.getId(), remoteUpdateUri, e);
throw new IndexUpdateFailedException("URL for remote index update of repository is not correct " + remoteUpdateUri, e);
} catch (IOException e) {
log.error("IOException during index update of remote repository {}: {}", remoteRepository.getId(), e.getMessage(), e);
throw new IndexUpdateFailedException("IOException during index update of remote repository " + remoteRepository.getId() + (StringUtils.isNotEmpty(e.getMessage()) ? ": " + e.getMessage() : ""), e);
} catch (WagonFactoryException e) {
log.error("Wagon for remote index download of {} could not be created: {}", remoteRepository.getId(), e.getMessage(), e);
throw new IndexUpdateFailedException("Error while updating the remote index of " + remoteRepository.getId(), e);
}
});
}
use of org.apache.archiva.repository.base.PasswordCredentials in project archiva by apache.
the class DefaultRemoteRepositoryAdmin method convertRepo.
/*
* Conversion between the repository from the registry and the serialized DTO for the admin API
*/
private org.apache.archiva.admin.model.beans.RemoteRepository convertRepo(RemoteRepository repo) {
if (repo == null) {
return null;
}
org.apache.archiva.admin.model.beans.RemoteRepository adminRepo = new org.apache.archiva.admin.model.beans.RemoteRepository(getArchivaConfiguration().getDefaultLocale());
setBaseRepoAttributes(adminRepo, repo);
adminRepo.setUrl(convertUriToString(repo.getLocation()));
adminRepo.setCronExpression(repo.getSchedulingDefinition());
adminRepo.setCheckPath(repo.getCheckPath());
adminRepo.setExtraHeaders(repo.getExtraHeaders());
adminRepo.setExtraParameters(repo.getExtraParameters());
adminRepo.setTimeout((int) repo.getTimeout().getSeconds());
RepositoryCredentials creds = repo.getLoginCredentials();
if (creds != null && creds instanceof PasswordCredentials) {
PasswordCredentials pCreds = (PasswordCredentials) creds;
adminRepo.setUserName(pCreds.getUsername());
adminRepo.setPassword(new String(pCreds.getPassword() != null ? pCreds.getPassword() : new char[0]));
}
if (repo.supportsFeature(RemoteIndexFeature.class)) {
RemoteIndexFeature rif = repo.getFeature(RemoteIndexFeature.class);
adminRepo.setRemoteIndexUrl(convertUriToString(rif.getIndexUri()));
adminRepo.setDownloadRemoteIndex(rif.isDownloadRemoteIndex());
adminRepo.setRemoteDownloadNetworkProxyId(rif.getProxyId());
adminRepo.setDownloadRemoteIndexOnStartup(rif.isDownloadRemoteIndexOnStartup());
adminRepo.setRemoteDownloadTimeout((int) rif.getDownloadTimeout().getSeconds());
}
if (repo.supportsFeature(IndexCreationFeature.class)) {
IndexCreationFeature icf = repo.getFeature(IndexCreationFeature.class);
adminRepo.setIndexDirectory(PathUtil.getPathFromUri(icf.getIndexPath()).toString());
}
adminRepo.setDescription(repo.getDescription());
return adminRepo;
}
use of org.apache.archiva.repository.base.PasswordCredentials in project archiva by apache.
the class MavenIndexManager method update.
@Override
public void update(final ArchivaIndexingContext context, final boolean fullUpdate) throws IndexUpdateFailedException {
log.info("start download remote index for remote repository {}", context.getRepository().getId());
URI remoteUpdateUri;
if (!(context.getRepository() instanceof RemoteRepository) || !(context.getRepository().supportsFeature(RemoteIndexFeature.class))) {
throw new IndexUpdateFailedException("The context is not associated to a remote repository with remote index " + context.getId());
} else {
RemoteIndexFeature rif = context.getRepository().getFeature(RemoteIndexFeature.class);
remoteUpdateUri = context.getRepository().getLocation().resolve(rif.getIndexUri());
}
final RemoteRepository remoteRepository = (RemoteRepository) context.getRepository();
executeUpdateFunction(context, indexingContext -> {
try {
// create a temp directory to download files
Path tempIndexDirectory = Paths.get(indexingContext.getIndexDirectoryFile().getParent(), ".tmpIndex");
Path indexCacheDirectory = Paths.get(indexingContext.getIndexDirectoryFile().getParent(), ".indexCache");
Files.createDirectories(indexCacheDirectory);
if (Files.exists(tempIndexDirectory)) {
org.apache.archiva.common.utils.FileUtils.deleteDirectory(tempIndexDirectory);
}
Files.createDirectories(tempIndexDirectory);
tempIndexDirectory.toFile().deleteOnExit();
String baseIndexUrl = indexingContext.getIndexUpdateUrl();
String wagonProtocol = remoteUpdateUri.toURL().getProtocol();
NetworkProxy networkProxy = null;
if (remoteRepository.supportsFeature(RemoteIndexFeature.class)) {
RemoteIndexFeature rif = remoteRepository.getFeature(RemoteIndexFeature.class);
if (StringUtils.isNotBlank(rif.getProxyId())) {
networkProxy = proxyRegistry.getNetworkProxy(rif.getProxyId());
if (networkProxy == null) {
log.warn("your remote repository is configured to download remote index trought a proxy we cannot find id:{}", rif.getProxyId());
}
}
final StreamWagon wagon = (StreamWagon) wagonFactory.getWagon(new WagonFactoryRequest(wagonProtocol, remoteRepository.getExtraHeaders()).networkProxy(networkProxy));
int readTimeout = (int) rif.getDownloadTimeout().toMillis() * 1000;
wagon.setReadTimeout(readTimeout);
wagon.setTimeout((int) remoteRepository.getTimeout().toMillis() * 1000);
if (wagon instanceof AbstractHttpClientWagon) {
HttpConfiguration httpConfiguration = new HttpConfiguration();
HttpMethodConfiguration httpMethodConfiguration = new HttpMethodConfiguration();
httpMethodConfiguration.setUsePreemptive(true);
httpMethodConfiguration.setReadTimeout(readTimeout);
httpConfiguration.setGet(httpMethodConfiguration);
AbstractHttpClientWagon.class.cast(wagon).setHttpConfiguration(httpConfiguration);
}
wagon.addTransferListener(new DownloadListener());
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(new String(networkProxy.getPassword()));
}
AuthenticationInfo authenticationInfo = null;
if (remoteRepository.getLoginCredentials() != null && (remoteRepository.getLoginCredentials() instanceof PasswordCredentials)) {
PasswordCredentials creds = (PasswordCredentials) remoteRepository.getLoginCredentials();
authenticationInfo = new AuthenticationInfo();
authenticationInfo.setUserName(creds.getUsername());
authenticationInfo.setPassword(new String(creds.getPassword()));
}
wagon.connect(new org.apache.maven.wagon.repository.Repository(remoteRepository.getId(), baseIndexUrl), authenticationInfo, proxyInfo);
Path indexDirectory = indexingContext.getIndexDirectoryFile().toPath();
if (!Files.exists(indexDirectory)) {
Files.createDirectories(indexDirectory);
}
ResourceFetcher resourceFetcher = new WagonResourceFetcher(log, tempIndexDirectory, wagon, remoteRepository);
IndexUpdateRequest request = new IndexUpdateRequest(indexingContext, resourceFetcher);
request.setForceFullUpdate(fullUpdate);
request.setLocalIndexCacheDir(indexCacheDirectory.toFile());
indexUpdater.fetchAndUpdateIndex(request);
indexingContext.updateTimestamp(true);
}
} catch (AuthenticationException e) {
log.error("Could not login to the remote proxy for updating index of {}", remoteRepository.getId(), e);
throw new IndexUpdateFailedException("Login in to proxy failed while updating remote repository " + remoteRepository.getId(), e);
} catch (ConnectionException e) {
log.error("Connection error during index update for remote repository {}", remoteRepository.getId(), e);
throw new IndexUpdateFailedException("Connection error during index update for remote repository " + remoteRepository.getId(), e);
} catch (MalformedURLException e) {
log.error("URL for remote index update of remote repository {} is not correct {}", remoteRepository.getId(), remoteUpdateUri, e);
throw new IndexUpdateFailedException("URL for remote index update of repository is not correct " + remoteUpdateUri, e);
} catch (IOException e) {
log.error("IOException during index update of remote repository {}: {}", remoteRepository.getId(), e.getMessage(), e);
throw new IndexUpdateFailedException("IOException during index update of remote repository " + remoteRepository.getId() + (StringUtils.isNotEmpty(e.getMessage()) ? ": " + e.getMessage() : ""), e);
} catch (WagonFactoryException e) {
log.error("Wagon for remote index download of {} could not be created: {}", remoteRepository.getId(), e.getMessage(), e);
throw new IndexUpdateFailedException("Error while updating the remote index of " + remoteRepository.getId(), e);
}
});
}
use of org.apache.archiva.repository.base.PasswordCredentials in project archiva by apache.
the class RepositoryProviderMock method getRemoteConfiguration.
@Override
public RemoteRepositoryConfiguration getRemoteConfiguration(RemoteRepository remoteRepository) throws RepositoryException {
RemoteRepositoryConfiguration configuration = new RemoteRepositoryConfiguration();
configuration.setId(remoteRepository.getId());
configuration.setName(remoteRepository.getName());
configuration.setDescription(remoteRepository.getDescription());
configuration.setLayout(remoteRepository.getLayout());
configuration.setRefreshCronExpression(remoteRepository.getSchedulingDefinition());
configuration.setCheckPath(remoteRepository.getCheckPath());
configuration.setExtraHeaders(remoteRepository.getExtraHeaders());
configuration.setExtraParameters(remoteRepository.getExtraParameters());
configuration.setTimeout((int) remoteRepository.getTimeout().getSeconds());
RepositoryCredentials creds = remoteRepository.getLoginCredentials();
if (creds != null) {
PasswordCredentials pwdCreds = (PasswordCredentials) creds;
configuration.setUsername(pwdCreds.getUsername());
configuration.setPassword(new String(pwdCreds.getPassword()));
}
configuration.setUrl(remoteRepository.getLocation() == null ? "" : remoteRepository.getLocation().toString());
RemoteIndexFeature rif = remoteRepository.getFeature(RemoteIndexFeature.class);
configuration.setDownloadRemoteIndex(rif.isDownloadRemoteIndex());
configuration.setDownloadRemoteIndexOnStartup(rif.isDownloadRemoteIndexOnStartup());
configuration.setIndexDir(rif.getIndexUri() == null ? "" : rif.getIndexUri().toString());
configuration.setRemoteDownloadNetworkProxyId(rif.getProxyId());
return configuration;
}
use of org.apache.archiva.repository.base.PasswordCredentials in project archiva by apache.
the class MavenRepositoryProxyHandler 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.
*/
protected boolean connectToRepository(ProxyConnector connector, Wagon wagon, RemoteRepository remoteRepository) {
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.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;
String username = "";
String password = "";
RepositoryCredentials repCred = remoteRepository.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.getLocation());
authInfo = new AuthenticationInfo();
authInfo.setUserName(username);
authInfo.setPassword(password);
}
// Convert seconds to milliseconds
long timeoutInMilliseconds = remoteRepository.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.getLocation().toString());
wagon.connect(wagonRepository, authInfo, networkProxy);
return true;
} catch (ConnectionException | AuthenticationException e) {
log.warn("Could not connect to {}: {}", remoteRepository.getId(), e.getMessage());
return false;
}
}
Aggregations