Search in sources :

Example 26 with BaseRepositoryContentLayout

use of org.apache.archiva.repository.content.BaseRepositoryContentLayout in project archiva by apache.

the class SnapshotTransferTest method testTimestampDrivenSnapshotNotPresentAlreadyExpiredCacheFailure.

@Test
public void testTimestampDrivenSnapshotNotPresentAlreadyExpiredCacheFailure() throws Exception {
    String path = "org/apache/maven/test/get-timestamped-snapshot/1.0-SNAPSHOT/get-timestamped-snapshot-1.0-SNAPSHOT.jar";
    setupTestableManagedRepository(path);
    Path expectedFile = managedDefaultDir.resolve(path);
    BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout(BaseRepositoryContentLayout.class);
    Artifact artifact = layout.getArtifact(path);
    Files.deleteIfExists(expectedFile);
    assertFalse(Files.exists(expectedFile));
    // Configure Connector (usually done within archiva.xml configuration)
    saveConnector(ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.IGNORE, ReleasesPolicy.ALWAYS, SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.YES, false);
    saveConnector(ID_DEFAULT_MANAGED, ID_PROXIED2, ChecksumPolicy.IGNORE, ReleasesPolicy.ALWAYS, SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.YES, false);
    StorageAsset downloadedFile = proxyHandler.fetchFromProxies(managedDefaultRepository.getRepository(), artifact);
    Path proxiedFile = Paths.get(REPOPATH_PROXIED1, path);
    assertFileEquals(expectedFile, downloadedFile.getFilePath(), proxiedFile);
    assertNoTempFiles(expectedFile);
}
Also used : Path(java.nio.file.Path) BaseRepositoryContentLayout(org.apache.archiva.repository.content.BaseRepositoryContentLayout) StorageAsset(org.apache.archiva.repository.storage.StorageAsset) Artifact(org.apache.archiva.repository.content.Artifact) Test(org.junit.Test)

Example 27 with BaseRepositoryContentLayout

use of org.apache.archiva.repository.content.BaseRepositoryContentLayout in project archiva by apache.

the class SnapshotTransferTest method testTimestampDrivenSnapshotNotExpired.

/**
 * TODO: Has problems with wagon implementation not preserving timestamp.
 */
/*
    public void testNewerTimestampDrivenSnapshotOnSecondRepoThanFirstNotPresentAlready()
        throws Exception
    {
        String path = "org/apache/maven/test/get-timestamped-snapshot-in-both/1.0-SNAPSHOT/get-timestamped-snapshot-in-both-1.0-SNAPSHOT.jar";
        setupTestableManagedRepository( path );
        
        Path expectedFile = managedDefaultDir.resolve(path);
        ArtifactReference artifact = createArtifactReference( "default", path );

        Files.delete(expectedFile);
        assertFalse( Files.exists(expectedFile) );

        // Create customized proxy / target repository
        File targetProxyDir = saveTargetedRepositoryConfig( ID_PROXIED1_TARGET, REPOPATH_PROXIED1,
                                                            REPOPATH_PROXIED1_TARGET, "default" );

        new File( targetProxyDir, path ).setLastModified( getPastDate().getTime() );

        // Configure Connector (usually done within archiva.xml configuration)
        saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1_TARGET, ChecksumPolicy.IGNORED, ReleasesPolicy.IGNORED,
                       SnapshotsPolicy.IGNORED, CachedFailuresPolicy.IGNORED );
        saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, ChecksumPolicy.IGNORED, ReleasesPolicy.IGNORED,
                       SnapshotsPolicy.IGNORED, CachedFailuresPolicy.IGNORED );

        File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );

        // Should have downloaded the content from proxy2, as proxy1 has an old (by file.lastModified check) version.
        Path proxiedFile = Paths.get(REPOPATH_PROXIED2, path);
        assertFileEquals( expectedFile, downloadedFile, proxiedFile );
        assertNoTempFiles( expectedFile );
    } 

    public void testOlderTimestampDrivenSnapshotOnSecondRepoThanFirstNotPresentAlready()
        throws Exception
    {
        String path = "org/apache/maven/test/get-timestamped-snapshot-in-both/1.0-SNAPSHOT/get-timestamped-snapshot-in-both-1.0-SNAPSHOT.jar";
        setupTestableManagedRepository( path );
        
        Path expectedFile = managedDefaultDir.resolve(path);
        ArtifactReference artifact = createArtifactReference( "default", path );

        Files.delete(expectedFile);
        assertFalse( Files.exists(expectedFile) );

        // Create customized proxy / target repository
        File targetProxyDir = saveTargetedRepositoryConfig( ID_PROXIED2_TARGET, REPOPATH_PROXIED2,
                                                            REPOPATH_PROXIED2_TARGET, "default" );

        new File( targetProxyDir, path ).setLastModified( getPastDate().getTime() );

        // Configure Connector (usually done within archiva.xml configuration)
        saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.IGNORED, ReleasesPolicy.IGNORED,
                       SnapshotsPolicy.IGNORED, CachedFailuresPolicy.IGNORED );
        saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2_TARGET, ChecksumPolicy.IGNORED, ReleasesPolicy.IGNORED,
                       SnapshotsPolicy.IGNORED, CachedFailuresPolicy.IGNORED );

        File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );

        File proxiedFile = new File( REPOPATH_PROXIED1_TARGET, path );
        assertFileEquals( expectedFile, downloadedFile, proxiedFile );
        assertNoTempFiles( expectedFile );
    } */
@Test
public void testTimestampDrivenSnapshotNotExpired() throws Exception {
    String path = "org/apache/maven/test/get-present-timestamped-snapshot/1.0-SNAPSHOT/get-present-timestamped-snapshot-1.0-SNAPSHOT.jar";
    setupTestableManagedRepository(path);
    Path expectedFile = managedDefaultDir.resolve(path);
    BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout(BaseRepositoryContentLayout.class);
    Artifact artifact = layout.getArtifact(path);
    assertTrue(Files.exists(expectedFile));
    Path proxiedFile = Paths.get(REPOPATH_PROXIED1, path);
    Files.setLastModifiedTime(proxiedFile, FileTime.from(getFutureDate().getTime(), TimeUnit.MILLISECONDS));
    // Configure Connector (usually done within archiva.xml configuration)
    saveConnector(ID_DEFAULT_MANAGED, ID_PROXIED1, false);
    StorageAsset downloadedFile = proxyHandler.fetchFromProxies(managedDefaultRepository.getRepository(), artifact);
    assertFileEquals(expectedFile, downloadedFile.getFilePath(), proxiedFile);
    assertNoTempFiles(expectedFile);
}
Also used : Path(java.nio.file.Path) BaseRepositoryContentLayout(org.apache.archiva.repository.content.BaseRepositoryContentLayout) StorageAsset(org.apache.archiva.repository.storage.StorageAsset) Artifact(org.apache.archiva.repository.content.Artifact) Test(org.junit.Test)

Example 28 with BaseRepositoryContentLayout

use of org.apache.archiva.repository.content.BaseRepositoryContentLayout in project archiva by apache.

the class ErrorHandlingTest method performDownload.

private StorageAsset performDownload(String path) throws ProxyDownloadException, LayoutException {
    // Attempt the proxy fetch.
    BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout(BaseRepositoryContentLayout.class);
    StorageAsset downloadedFile = proxyHandler.fetchFromProxies(managedDefaultRepository.getRepository(), layout.getArtifact(path));
    return downloadedFile;
}
Also used : BaseRepositoryContentLayout(org.apache.archiva.repository.content.BaseRepositoryContentLayout) StorageAsset(org.apache.archiva.repository.storage.StorageAsset)

Example 29 with BaseRepositoryContentLayout

use of org.apache.archiva.repository.content.BaseRepositoryContentLayout in project archiva by apache.

the class DefaultRepositoriesService method copyArtifact.

@Override
public ActionStatus copyArtifact(ArtifactTransferRequest artifactTransferRequest) throws ArchivaRestServiceException {
    // check parameters
    String userName = getAuditInformation().getUser().getUsername();
    if (StringUtils.isBlank(userName)) {
        throw new ArchivaRestServiceException("copyArtifact call: userName not found", null);
    }
    if (StringUtils.isBlank(artifactTransferRequest.getRepositoryId())) {
        throw new ArchivaRestServiceException("copyArtifact call: sourceRepositoryId cannot be null", null);
    }
    if (StringUtils.isBlank(artifactTransferRequest.getTargetRepositoryId())) {
        throw new ArchivaRestServiceException("copyArtifact call: targetRepositoryId cannot be null", null);
    }
    ManagedRepository source = null;
    source = repositoryRegistry.getManagedRepository(artifactTransferRequest.getRepositoryId());
    if (source == null) {
        throw new ArchivaRestServiceException("cannot find repository with id " + artifactTransferRequest.getRepositoryId(), null);
    }
    ManagedRepository target = null;
    target = repositoryRegistry.getManagedRepository(artifactTransferRequest.getTargetRepositoryId());
    if (target == null) {
        throw new ArchivaRestServiceException("cannot find repository with id " + artifactTransferRequest.getTargetRepositoryId(), null);
    }
    if (StringUtils.isBlank(artifactTransferRequest.getGroupId())) {
        throw new ArchivaRestServiceException("groupId is mandatory", null);
    }
    if (StringUtils.isBlank(artifactTransferRequest.getArtifactId())) {
        throw new ArchivaRestServiceException("artifactId is mandatory", null);
    }
    if (StringUtils.isBlank(artifactTransferRequest.getVersion())) {
        throw new ArchivaRestServiceException("version is mandatory", null);
    }
    if (VersionUtil.isSnapshot(artifactTransferRequest.getVersion())) {
        throw new ArchivaRestServiceException("copy of SNAPSHOT not supported", null);
    }
    // end check parameters
    User user = null;
    try {
        user = securitySystem.getUserManager().findUser(userName);
    } catch (UserNotFoundException e) {
        throw new ArchivaRestServiceException("user " + userName + " not found", e);
    } catch (UserManagerException e) {
        throw new ArchivaRestServiceException("ArchivaRestServiceException:" + e.getMessage(), e);
    }
    // check karma on source : read
    AuthenticationResult authn = new AuthenticationResult(true, userName, null);
    SecuritySession securitySession = new DefaultSecuritySession(authn, user);
    try {
        boolean authz = securitySystem.isAuthorized(securitySession, ArchivaRoleConstants.OPERATION_READ_REPOSITORY, artifactTransferRequest.getRepositoryId());
        if (!authz) {
            throw new ArchivaRestServiceException("not authorized to access repo:" + artifactTransferRequest.getRepositoryId(), null);
        }
    } catch (AuthorizationException e) {
        log.error("error reading permission: {}", e.getMessage(), e);
        throw new ArchivaRestServiceException(e.getMessage(), e);
    }
    // check karma on target: write
    try {
        boolean authz = securitySystem.isAuthorized(securitySession, ArchivaRoleConstants.OPERATION_ADD_ARTIFACT, artifactTransferRequest.getTargetRepositoryId());
        if (!authz) {
            throw new ArchivaRestServiceException("not authorized to write to repo:" + artifactTransferRequest.getTargetRepositoryId(), null);
        }
    } catch (AuthorizationException e) {
        log.error("error reading permission: {}", e.getMessage(), e);
        throw new ArchivaRestServiceException(e.getMessage(), e);
    }
    // sounds good we can continue !
    String packaging = StringUtils.trim(artifactTransferRequest.getPackaging());
    ItemSelector selector = ArchivaItemSelector.builder().withProjectId(artifactTransferRequest.getArtifactId()).withArtifactId(artifactTransferRequest.getArtifactId()).withNamespace(artifactTransferRequest.getGroupId()).withArtifactVersion(artifactTransferRequest.getVersion()).withClassifier(artifactTransferRequest.getClassifier()).withExtension(StringUtils.isEmpty(packaging) ? "jar" : packaging).build();
    try {
        ManagedRepositoryContent sourceRepository = getManagedRepositoryContent(artifactTransferRequest.getRepositoryId());
        BaseRepositoryContentLayout layout = sourceRepository.getLayout(BaseRepositoryContentLayout.class);
        // String artifactSourcePath = sourceRepository.toPath( selector );
        org.apache.archiva.repository.content.Artifact sourceArtifact = layout.getArtifact(selector);
        if (!sourceArtifact.exists()) {
            log.error("cannot find artifact {}", artifactTransferRequest);
            throw new ArchivaRestServiceException("cannot find artifact " + artifactTransferRequest.toString(), null);
        }
        StorageAsset artifactFile = sourceArtifact.getAsset();
        ManagedRepositoryContent targetRepository = getManagedRepositoryContent(artifactTransferRequest.getTargetRepositoryId());
        String artifactPath = artifactFile.getPath();
        int lastIndex = artifactPath.lastIndexOf('/');
        String path = artifactPath.substring(0, lastIndex);
        StorageAsset targetDir = target.getAsset(path);
        Date lastUpdatedTimestamp = Calendar.getInstance().getTime();
        int newBuildNumber = 1;
        String timestamp = null;
        StorageAsset versionMetadataFile = target.getAsset(path + "/" + MetadataTools.MAVEN_METADATA);
        /* unused */
        getMetadata(targetRepository.getRepository().getType(), versionMetadataFile);
        if (!targetDir.exists()) {
            targetDir = target.addAsset(targetDir.getPath(), true);
            targetDir.create();
        }
        String filename = artifactPath.substring(lastIndex + 1);
        boolean fixChecksums = !(archivaAdministration.getKnownContentConsumers().contains("create-missing-checksums"));
        StorageAsset targetFile = target.getAsset(targetDir.getPath() + "/" + filename);
        if (targetFile.exists() && target.blocksRedeployments()) {
            throw new ArchivaRestServiceException("artifact already exists in target repo: " + artifactTransferRequest.getTargetRepositoryId() + " and redeployment blocked", null);
        } else {
            copyFile(artifactFile, targetFile, fixChecksums);
            queueRepositoryTask(target.getId(), targetFile);
        }
        // copy source pom to target repo
        String pomFilename = filename;
        if (StringUtils.isNotBlank(artifactTransferRequest.getClassifier())) {
            pomFilename = StringUtils.remove(pomFilename, "-" + artifactTransferRequest.getClassifier());
        }
        pomFilename = FilenameUtils.removeExtension(pomFilename) + ".pom";
        StorageAsset pomFile = source.getAsset(artifactPath.substring(0, artifactPath.lastIndexOf('/')) + "/" + pomFilename);
        if (pomFile != null && pomFile.exists()) {
            StorageAsset targetPomFile = target.getAsset(targetDir.getPath() + "/" + pomFilename);
            copyFile(pomFile, targetPomFile, fixChecksums);
            queueRepositoryTask(target.getId(), targetPomFile);
        }
        // explicitly update only if metadata-updater consumer is not enabled!
        if (!archivaAdministration.getKnownContentConsumers().contains("metadata-updater")) {
            updateProjectMetadata(target.getType(), target, targetDir, lastUpdatedTimestamp, timestamp, newBuildNumber, fixChecksums, artifactTransferRequest);
        }
        String msg = "Artifact \'" + artifactTransferRequest.getGroupId() + ":" + artifactTransferRequest.getArtifactId() + ":" + artifactTransferRequest.getVersion() + "\' was successfully deployed to repository \'" + artifactTransferRequest.getTargetRepositoryId() + "\'";
        log.debug("copyArtifact {}", msg);
    } catch (RepositoryException | LayoutException e) {
        log.error("RepositoryException: {}", e.getMessage(), e);
        throw new ArchivaRestServiceException(e.getMessage(), e);
    } catch (RepositoryAdminException e) {
        log.error("RepositoryAdminException: {}", e.getMessage(), e);
        throw new ArchivaRestServiceException(e.getMessage(), e);
    } catch (IOException e) {
        log.error("IOException: {}", e.getMessage(), e);
        throw new ArchivaRestServiceException(e.getMessage(), e);
    }
    return ActionStatus.SUCCESS;
}
Also used : UserNotFoundException(org.apache.archiva.redback.users.UserNotFoundException) ManagedRepository(org.apache.archiva.repository.ManagedRepository) User(org.apache.archiva.redback.users.User) AuthorizationException(org.apache.archiva.redback.authorization.AuthorizationException) AuthenticationResult(org.apache.archiva.redback.authentication.AuthenticationResult) LayoutException(org.apache.archiva.repository.content.LayoutException) DefaultSecuritySession(org.apache.archiva.redback.system.DefaultSecuritySession) SecuritySession(org.apache.archiva.redback.system.SecuritySession) DefaultSecuritySession(org.apache.archiva.redback.system.DefaultSecuritySession) RepositoryException(org.apache.archiva.repository.RepositoryException) MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) IOException(java.io.IOException) RepositoryAdminException(org.apache.archiva.admin.model.RepositoryAdminException) Date(java.util.Date) ItemSelector(org.apache.archiva.repository.content.ItemSelector) ArchivaItemSelector(org.apache.archiva.repository.content.base.ArchivaItemSelector) UserManagerException(org.apache.archiva.redback.users.UserManagerException) BaseRepositoryContentLayout(org.apache.archiva.repository.content.BaseRepositoryContentLayout) ArchivaRestServiceException(org.apache.archiva.rest.api.services.ArchivaRestServiceException) StorageAsset(org.apache.archiva.repository.storage.StorageAsset) ManagedRepositoryContent(org.apache.archiva.repository.ManagedRepositoryContent)

Example 30 with BaseRepositoryContentLayout

use of org.apache.archiva.repository.content.BaseRepositoryContentLayout in project archiva by apache.

the class CleanupReleasedSnapshotsRepositoryPurge method process.

@Override
public void process(String path) throws RepositoryPurgeException {
    try {
        StorageAsset artifactFile = repository.getRepository().getRoot().resolve(path);
        BaseRepositoryContentLayout layout = repository.getLayout(BaseRepositoryContentLayout.class);
        if (!artifactFile.exists()) {
            // Nothing to do here, file doesn't exist, skip it.
            return;
        }
        Artifact artifactRef = layout.getArtifact(path);
        if (!VersionUtil.isSnapshot(artifactRef.getVersion().getId())) {
            // Nothing to do here, not a snapshot, skip it.
            return;
        }
        ItemSelector projectSelector = ArchivaItemSelector.builder().withNamespace(artifactRef.getNamespace().getId()).withProjectId(artifactRef.getId()).build();
        // Gether the released versions
        List<String> releasedVersions = new ArrayList<>();
        Collection<org.apache.archiva.repository.ManagedRepository> repos = repositoryRegistry.getManagedRepositories();
        for (org.apache.archiva.repository.ManagedRepository repo : repos) {
            if (repo.getActiveReleaseSchemes().contains(ReleaseScheme.RELEASE)) {
                BaseRepositoryContentLayout repoContent = repo.getContent().getLayout(BaseRepositoryContentLayout.class);
                Project proj = repoContent.getProject(projectSelector);
                for (Version version : repoContent.getVersions(proj)) {
                    if (!VersionUtil.isSnapshot(version.getId())) {
                        releasedVersions.add(version.getId());
                    }
                }
            }
        }
        Collections.sort(releasedVersions, VersionComparator.getInstance());
        // Now clean out any version that is earlier than the highest released version.
        boolean needsMetadataUpdate = false;
        ArchivaItemSelector.Builder versionSelectorBuilder = ArchivaItemSelector.builder().withNamespace(artifactRef.getNamespace().getId()).withProjectId(artifactRef.getId()).withArtifactId(artifactRef.getId());
        MetadataRepository metadataRepository = repositorySession.getRepository();
        if (releasedVersions.contains(VersionUtil.getReleaseVersion(artifactRef.getVersion().getId()))) {
            ArchivaItemSelector selector = versionSelectorBuilder.withVersion(artifactRef.getVersion().getId()).build();
            Version version = layout.getVersion(selector);
            if (version.exists()) {
                repository.deleteItem(version);
            }
            for (RepositoryListener listener : listeners) {
                listener.deleteArtifact(metadataRepository, repository.getId(), artifactRef.getNamespace().getId(), artifactRef.getId(), artifactRef.getVersion().getId(), artifactFile.getName());
            }
            metadataRepository.removeProjectVersion(repositorySession, repository.getId(), artifactRef.getNamespace().getId(), artifactRef.getId(), artifactRef.getVersion().getId());
            needsMetadataUpdate = true;
        }
        if (needsMetadataUpdate) {
            updateMetadata(artifactRef);
        }
    } catch (LayoutException e) {
        log.debug("Not processing file that is not an artifact: {}", e.getMessage());
    } catch (MetadataRepositoryException e) {
        log.error("Could not remove metadata during cleanup of released snapshots of {}", path, e);
    } catch (ContentAccessException e) {
        e.printStackTrace();
    } catch (ItemNotFoundException e) {
        log.error("Could not find item to delete {}", e.getMessage(), e);
    }
}
Also used : ArchivaItemSelector(org.apache.archiva.repository.content.base.ArchivaItemSelector) ArrayList(java.util.ArrayList) MetadataRepository(org.apache.archiva.metadata.repository.MetadataRepository) Version(org.apache.archiva.repository.content.Version) LayoutException(org.apache.archiva.repository.content.LayoutException) ContentAccessException(org.apache.archiva.repository.content.ContentAccessException) RepositoryListener(org.apache.archiva.metadata.audit.RepositoryListener) MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) Artifact(org.apache.archiva.repository.content.Artifact) Project(org.apache.archiva.repository.content.Project) ArchivaItemSelector(org.apache.archiva.repository.content.base.ArchivaItemSelector) ItemSelector(org.apache.archiva.repository.content.ItemSelector) BaseRepositoryContentLayout(org.apache.archiva.repository.content.BaseRepositoryContentLayout) StorageAsset(org.apache.archiva.repository.storage.StorageAsset) ItemNotFoundException(org.apache.archiva.repository.content.ItemNotFoundException)

Aggregations

BaseRepositoryContentLayout (org.apache.archiva.repository.content.BaseRepositoryContentLayout)62 StorageAsset (org.apache.archiva.repository.storage.StorageAsset)55 Artifact (org.apache.archiva.repository.content.Artifact)50 Path (java.nio.file.Path)49 Test (org.junit.Test)45 LayoutException (org.apache.archiva.repository.content.LayoutException)10 ArchivaItemSelector (org.apache.archiva.repository.content.base.ArchivaItemSelector)9 ContentItem (org.apache.archiva.repository.content.ContentItem)7 ManagedRepositoryContent (org.apache.archiva.repository.ManagedRepositoryContent)5 Project (org.apache.archiva.repository.content.Project)5 Version (org.apache.archiva.repository.content.Version)5 MetadataRepositoryException (org.apache.archiva.metadata.repository.MetadataRepositoryException)4 ContentAccessException (org.apache.archiva.repository.content.ContentAccessException)4 ItemSelector (org.apache.archiva.repository.content.ItemSelector)4 ResourceDoesNotExistException (org.apache.maven.wagon.ResourceDoesNotExistException)4 IOException (java.io.IOException)3 MetadataRepository (org.apache.archiva.metadata.repository.MetadataRepository)3 MetadataResolutionException (org.apache.archiva.metadata.repository.MetadataResolutionException)3 ManagedRepository (org.apache.archiva.repository.ManagedRepository)3 RepositoryException (org.apache.archiva.repository.RepositoryException)3