use of org.apache.archiva.repository.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).get();
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.PasswordCredentials in project archiva by apache.
the class RepositoryProviderMock method updateRemoteInstance.
@Override
public void updateRemoteInstance(EditableRemoteRepository remoteRepository, RemoteRepositoryConfiguration configuration) throws RepositoryException {
try {
remoteRepository.setName(remoteRepository.getPrimaryLocale(), configuration.getName());
remoteRepository.setBaseUri(new URI(""));
remoteRepository.setDescription(remoteRepository.getPrimaryLocale(), configuration.getDescription());
remoteRepository.setLayout(configuration.getLayout());
remoteRepository.setSchedulingDefinition(configuration.getRefreshCronExpression());
remoteRepository.setCheckPath(configuration.getCheckPath());
remoteRepository.setExtraHeaders(configuration.getExtraHeaders());
remoteRepository.setExtraParameters(configuration.getExtraParameters());
remoteRepository.setTimeout(Duration.ofSeconds(configuration.getTimeout()));
char[] pwd = configuration.getPassword() == null ? "".toCharArray() : configuration.getPassword().toCharArray();
remoteRepository.setCredentials(new PasswordCredentials(configuration.getUsername(), pwd));
remoteRepository.setLocation(new URI(configuration.getUrl() == null ? "" : configuration.getUrl()));
RemoteIndexFeature rif = remoteRepository.getFeature(RemoteIndexFeature.class).get();
rif.setDownloadRemoteIndexOnStartup(configuration.isDownloadRemoteIndexOnStartup());
rif.setDownloadRemoteIndex(configuration.isDownloadRemoteIndex());
rif.setIndexUri(new URI(configuration.getIndexDir()));
rif.setDownloadTimeout(Duration.ofSeconds(configuration.getRemoteDownloadTimeout()));
rif.setProxyId(configuration.getRemoteDownloadNetworkProxyId());
} catch (Exception e) {
throw new RepositoryException("Error", e);
}
}
use of org.apache.archiva.repository.PasswordCredentials 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.archiva.repository.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).get();
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).get();
adminRepo.setIndexDirectory(PathUtil.getPathFromUri(icf.getIndexPath()).toString());
}
adminRepo.setDescription(repo.getDescription());
return adminRepo;
}
use of org.apache.archiva.repository.PasswordCredentials in project archiva by apache.
the class DownloadRemoteIndexTask method run.
@Override
public void run() {
// so short lock : not sure we need it
synchronized (this.runningRemoteDownloadIds) {
if (this.runningRemoteDownloadIds.contains(this.remoteRepository.getId())) {
// skip it as it's running
log.info("skip download index remote for repo {} it's already running", this.remoteRepository.getId());
return;
}
this.runningRemoteDownloadIds.add(this.remoteRepository.getId());
}
Path tempIndexDirectory = null;
StopWatch stopWatch = new StopWatch();
stopWatch.start();
try {
log.info("start download remote index for remote repository {}", this.remoteRepository.getId());
if (this.remoteRepository.getIndexingContext() == null) {
throw new IndexNotFoundException("No index context set for repository " + remoteRepository.getId());
}
if (this.remoteRepository.getType() != RepositoryType.MAVEN) {
throw new RepositoryException("Bad repository type");
}
if (!this.remoteRepository.supportsFeature(RemoteIndexFeature.class)) {
throw new RepositoryException("Repository does not support RemotIndexFeature " + remoteRepository.getId());
}
RemoteIndexFeature rif = this.remoteRepository.getFeature(RemoteIndexFeature.class).get();
IndexingContext indexingContext = this.remoteRepository.getIndexingContext().getBaseContext(IndexingContext.class);
// create a temp directory to download files
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 = this.remoteRepository.getLocation().getScheme();
final StreamWagon wagon = (StreamWagon) wagonFactory.getWagon(new WagonFactoryRequest(wagonProtocol, this.remoteRepository.getExtraHeaders()).networkProxy(this.networkProxy));
// FIXME olamy having 2 config values
wagon.setReadTimeout((int) rif.getDownloadTimeout().toMillis());
wagon.setTimeout((int) remoteRepository.getTimeout().toMillis());
if (wagon instanceof AbstractHttpClientWagon) {
HttpConfiguration httpConfiguration = new HttpConfiguration();
HttpMethodConfiguration httpMethodConfiguration = new HttpMethodConfiguration();
httpMethodConfiguration.setUsePreemptive(true);
httpMethodConfiguration.setReadTimeout((int) rif.getDownloadTimeout().toMillis());
httpConfiguration.setGet(httpMethodConfiguration);
AbstractHttpClientWagon.class.cast(wagon).setHttpConfiguration(httpConfiguration);
}
wagon.addTransferListener(new DownloadListener());
ProxyInfo proxyInfo = null;
if (this.networkProxy != null) {
proxyInfo = new ProxyInfo();
proxyInfo.setType(this.networkProxy.getProtocol());
proxyInfo.setHost(this.networkProxy.getHost());
proxyInfo.setPort(this.networkProxy.getPort());
proxyInfo.setUserName(this.networkProxy.getUsername());
proxyInfo.setPassword(this.networkProxy.getPassword());
}
AuthenticationInfo authenticationInfo = null;
if (this.remoteRepository.getLoginCredentials() != null && this.remoteRepository.getLoginCredentials() instanceof PasswordCredentials) {
PasswordCredentials creds = (PasswordCredentials) this.remoteRepository.getLoginCredentials();
authenticationInfo = new AuthenticationInfo();
authenticationInfo.setUserName(creds.getUsername());
authenticationInfo.setPassword(new String(creds.getPassword()));
}
log.debug("Connection to {}, authInfo={}", this.remoteRepository.getId(), authenticationInfo);
wagon.connect(new Repository(this.remoteRepository.getId(), baseIndexUrl), authenticationInfo, proxyInfo);
Path indexDirectory = indexingContext.getIndexDirectoryFile().toPath();
if (!Files.exists(indexDirectory)) {
Files.createDirectories(indexDirectory);
}
log.debug("Downloading index file to {}", indexDirectory);
log.debug("Index cache dir {}", indexCacheDirectory);
ResourceFetcher resourceFetcher = new WagonResourceFetcher(log, tempIndexDirectory, wagon, remoteRepository);
IndexUpdateRequest request = new IndexUpdateRequest(indexingContext, resourceFetcher);
request.setForceFullUpdate(this.fullDownload);
request.setLocalIndexCacheDir(indexCacheDirectory.toFile());
IndexUpdateResult result = this.indexUpdater.fetchAndUpdateIndex(request);
log.debug("Update result success: {}", result.isSuccessful());
stopWatch.stop();
log.info("time update index from remote for repository {}: {}ms", this.remoteRepository.getId(), (stopWatch.getTime()));
// index packing optionnal ??
// IndexPackingRequest indexPackingRequest =
// new IndexPackingRequest( indexingContext, indexingContext.getIndexDirectoryFile() );
// indexPacker.packIndex( indexPackingRequest );
indexingContext.updateTimestamp(true);
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new RuntimeException(e.getMessage(), e);
} finally {
deleteDirectoryQuiet(tempIndexDirectory);
this.runningRemoteDownloadIds.remove(this.remoteRepository.getId());
}
log.info("end download remote index for remote repository {}", this.remoteRepository.getId());
}
Aggregations