Search in sources :

Example 1 with StorageAsset

use of org.apache.archiva.repository.storage.StorageAsset in project archiva by apache.

the class PropagateErrorsDownloadPolicyTest method testPolicyIgnore.

@Test
public void testPolicyIgnore() throws Exception {
    DownloadErrorPolicy policy = lookupPolicy();
    StorageAsset localFile = getFile();
    Properties request = createRequest();
    Exception ex = new RuntimeException();
    Map<String, Exception> exMap = new HashMap<>();
    assertFalse(policy.applyPolicy(PropagateErrorsDownloadPolicy.IGNORE, request, localFile, ex, exMap));
}
Also used : StorageAsset(org.apache.archiva.repository.storage.StorageAsset) IOException(java.io.IOException) Test(org.junit.Test)

Example 2 with StorageAsset

use of org.apache.archiva.repository.storage.StorageAsset in project archiva by apache.

the class SnapshotsPolicyTest method assertSnapshotPolicy.

private void assertSnapshotPolicy(PolicyOption setting, String path, boolean createLocalFile) throws Exception {
    PreDownloadPolicy policy = lookupPolicy();
    Properties request = new Properties();
    request.setProperty("filetype", path.endsWith("/maven-metadata.xml") ? "metadata" : "artifact");
    if (path.contains("1.0-SNAPSHOT")) {
        request.setProperty("version", "1.0-SNAPSHOT");
    }
    if (path.contains("2.0")) {
        request.setProperty("version", "2.0");
    }
    StorageAsset targetDir = ChecksumPolicyTest.getTestFile("target/test-policy/");
    StorageAsset localFile = targetDir.resolve(path);
    Files.deleteIfExists(localFile.getFilePath());
    if (createLocalFile) {
        Files.createDirectories(localFile.getParent().getFilePath());
        org.apache.archiva.common.utils.FileUtils.writeStringToFile(localFile.getFilePath(), FILE_ENCODING, "random-junk");
        Files.setLastModifiedTime(localFile.getFilePath(), FileTime.fromMillis(Files.getLastModifiedTime(localFile.getFilePath()).toMillis() - generatedLocalFileUpdateDelta));
    }
    policy.applyPolicy(setting, request, localFile);
}
Also used : StorageAsset(org.apache.archiva.repository.storage.StorageAsset) Properties(java.util.Properties)

Example 3 with StorageAsset

use of org.apache.archiva.repository.storage.StorageAsset in project archiva by apache.

the class DefaultRepositoryProxyHandler method fetchMetadataFromProxies.

@Override
public ProxyFetchResult fetchMetadataFromProxies(ManagedRepository repository, String rawLogicalPath) {
    String logicalPath;
    if (rawLogicalPath.startsWith("/")) {
        logicalPath = rawLogicalPath.substring(1);
    } else {
        logicalPath = rawLogicalPath;
    }
    StorageAsset localFile = repository.getAsset(logicalPath);
    Properties requestProperties = new Properties();
    requestProperties.setProperty("filetype", "metadata");
    boolean metadataNeedsUpdating = false;
    long originalTimestamp = getLastModified(localFile);
    List<ProxyConnector> connectors = new ArrayList<>(getProxyConnectors(repository));
    for (ProxyConnector connector : connectors) {
        if (!connector.isEnabled()) {
            continue;
        }
        RemoteRepository targetRepository = connector.getTargetRepository();
        StorageAsset localRepoFile = toLocalRepoFile(repository, targetRepository.getContent(), logicalPath);
        long originalMetadataTimestamp = getLastModified(localRepoFile);
        try {
            transferFile(connector, targetRepository, logicalPath, repository, localRepoFile, requestProperties, true);
            if (hasBeenUpdated(localRepoFile, originalMetadataTimestamp)) {
                metadataNeedsUpdating = true;
            }
        } catch (NotFoundException e) {
            log.debug("Metadata {} not found on remote repository '{}'.", logicalPath, targetRepository.getId(), e);
        } catch (NotModifiedException e) {
            log.debug("Metadata {} not updated on remote repository '{}'.", logicalPath, targetRepository.getId(), e);
        } catch (ProxyException e) {
            log.warn("Transfer error from repository {} for versioned Metadata {}, continuing to next repository. Error message: {}", targetRepository.getId(), logicalPath, e.getMessage());
            log.debug("Full stack trace", e);
        }
    }
    if (hasBeenUpdated(localFile, originalTimestamp)) {
        metadataNeedsUpdating = true;
    }
    if (metadataNeedsUpdating || !localFile.exists()) {
        try {
            metadataTools.updateMetadata(repository.getContent(), logicalPath);
        } catch (RepositoryMetadataException e) {
            log.warn("Unable to update metadata {}:{}", localFile.getPath(), e.getMessage(), e);
        }
    }
    if (fileExists(localFile)) {
        return new ProxyFetchResult(localFile, metadataNeedsUpdating);
    }
    return new ProxyFetchResult(null, false);
}
Also used : ProxyFetchResult(org.apache.archiva.proxy.model.ProxyFetchResult) ArrayList(java.util.ArrayList) RemoteRepository(org.apache.archiva.repository.RemoteRepository) Properties(java.util.Properties) RepositoryMetadataException(org.apache.archiva.repository.metadata.RepositoryMetadataException) StorageAsset(org.apache.archiva.repository.storage.StorageAsset) ProxyConnector(org.apache.archiva.proxy.model.ProxyConnector)

Example 4 with StorageAsset

use of org.apache.archiva.repository.storage.StorageAsset in project archiva by apache.

the class DefaultRepositoryProxyHandler method fetchFromProxies.

@Override
public StorageAsset fetchFromProxies(ManagedRepository repository, String path) {
    StorageAsset localFile = repository.getAsset(path);
    // no update policies for these paths
    if (localFile.exists()) {
        return null;
    }
    Properties requestProperties = new Properties();
    requestProperties.setProperty("filetype", "resource");
    requestProperties.setProperty("managedRepositoryId", repository.getId());
    List<ProxyConnector> connectors = getProxyConnectors(repository);
    for (ProxyConnector connector : connectors) {
        if (!connector.isEnabled()) {
            continue;
        }
        RemoteRepository targetRepository = connector.getTargetRepository();
        requestProperties.setProperty("remoteRepositoryId", targetRepository.getId());
        String targetPath = path;
        try {
            StorageAsset downloadedFile = transferFile(connector, targetRepository, targetPath, repository, localFile, requestProperties, false);
            if (fileExists(downloadedFile)) {
                log.debug("Successfully transferred: {}", downloadedFile.getPath());
                return downloadedFile;
            }
        } catch (NotFoundException e) {
            log.debug("Resource {} not found on repository \"{}\".", path, targetRepository.getId());
        } catch (NotModifiedException e) {
            log.debug("Resource {} not updated on repository \"{}\".", path, targetRepository.getId());
        } catch (ProxyException e) {
            log.warn("Transfer error from repository {} for resource {}, continuing to next repository. Error message: {}", targetRepository.getId(), path, e.getMessage());
            log.debug(MarkerFactory.getDetachedMarker("transfer.error"), "Transfer error from repository \"{}" + "\" for resource {}, continuing to next repository. Error message: {}", targetRepository.getId(), path, e.getMessage(), e);
        }
    }
    log.debug("Exhausted all target repositories, resource {} not found.", path);
    return null;
}
Also used : StorageAsset(org.apache.archiva.repository.storage.StorageAsset) RemoteRepository(org.apache.archiva.repository.RemoteRepository) ProxyConnector(org.apache.archiva.proxy.model.ProxyConnector) Properties(java.util.Properties)

Example 5 with StorageAsset

use of org.apache.archiva.repository.storage.StorageAsset in project archiva by apache.

the class PropagateErrorsOnUpdateDownloadPolicyTest method testPolicyQueue.

@Test
public void testPolicyQueue() throws Exception {
    DownloadErrorPolicy policy = lookupPolicy();
    StorageAsset localFile = getFile();
    Properties request = createRequest();
    Exception ex = new RuntimeException();
    Map<String, Exception> exMap = new HashMap<>();
    if (localFile.exists()) {
        localFile.getStorage().removeAsset(localFile);
    }
    assertTrue(policy.applyPolicy(PropagateErrorsOnUpdateDownloadPolicy.NOT_PRESENT, request, localFile, ex, exMap));
    localFile.create();
    assertFalse(policy.applyPolicy(PropagateErrorsOnUpdateDownloadPolicy.NOT_PRESENT, request, localFile, ex, exMap));
}
Also used : StorageAsset(org.apache.archiva.repository.storage.StorageAsset) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

StorageAsset (org.apache.archiva.repository.storage.StorageAsset)191 Path (java.nio.file.Path)91 BaseRepositoryContentLayout (org.apache.archiva.repository.content.BaseRepositoryContentLayout)61 IOException (java.io.IOException)59 Test (org.junit.Test)59 Artifact (org.apache.archiva.repository.content.Artifact)54 ManagedRepository (org.apache.archiva.repository.ManagedRepository)27 ArchivaIndexingContext (org.apache.archiva.indexer.ArchivaIndexingContext)22 ArchivaRepositoryMetadata (org.apache.archiva.model.ArchivaRepositoryMetadata)22 List (java.util.List)20 Inject (javax.inject.Inject)20 RepositoryMetadataException (org.apache.archiva.repository.metadata.RepositoryMetadataException)20 Collectors (java.util.stream.Collectors)19 RemoteRepository (org.apache.archiva.repository.RemoteRepository)19 IndexingContext (org.apache.maven.index.context.IndexingContext)19 FilesystemStorage (org.apache.archiva.repository.storage.fs.FilesystemStorage)18 StringUtils (org.apache.commons.lang3.StringUtils)18 Logger (org.slf4j.Logger)18 LoggerFactory (org.slf4j.LoggerFactory)18 Map (java.util.Map)17