Search in sources :

Example 11 with FilesystemAsset

use of org.apache.archiva.repository.storage.fs.FilesystemAsset in project archiva by apache.

the class Maven2RepositoryMerger method createFolderStructure.

private void createFolderStructure(String sourceRepoId, String targetRepoId, ArtifactMetadata artifactMetadata) throws IOException, RepositoryException {
    Configuration config = configuration.getConfiguration();
    ManagedRepositoryConfiguration targetRepoConfig = config.findManagedRepositoryById(targetRepoId);
    ManagedRepositoryConfiguration sourceRepoConfig = config.findManagedRepositoryById(sourceRepoId);
    Date lastUpdatedTimestamp = Calendar.getInstance().getTime();
    TimeZone timezone = TimeZone.getTimeZone("UTC");
    DateFormat fmt = new SimpleDateFormat("yyyyMMdd.HHmmss");
    fmt.setTimeZone(timezone);
    String timestamp = fmt.format(lastUpdatedTimestamp);
    String targetRepoPath = targetRepoConfig.getLocation();
    String sourceRepoPath = sourceRepoConfig.getLocation();
    String artifactPath = pathTranslator.toPath(artifactMetadata.getNamespace(), artifactMetadata.getProject(), artifactMetadata.getProjectVersion(), artifactMetadata.getId());
    Path sourceArtifactFile = Paths.get(sourceRepoPath, artifactPath);
    Path targetArtifactFile = Paths.get(targetRepoPath, artifactPath);
    log.debug("artifactPath {}", artifactPath);
    int lastIndex = artifactPath.lastIndexOf(RepositoryPathTranslator.PATH_SEPARATOR);
    Path targetFile = Paths.get(targetRepoPath, artifactPath.substring(0, lastIndex));
    if (!Files.exists(targetFile)) {
        // create the folder structure when it does not exist
        Files.createDirectories(targetFile);
    }
    // artifact copying
    copyFile(sourceArtifactFile, targetArtifactFile);
    // pom file copying
    // TODO need to use path translator to get the pom file path
    // String fileName = artifactMetadata.getProject() + "-" + artifactMetadata.getVersion() + ".pom";
    // 
    // File sourcePomFile =
    // pathTranslator.toFile( new File( sourceRepoPath ), artifactMetadata.getId(), artifactMetadata.getProject(),
    // artifactMetadata.getVersion(), fileName );
    // 
    // String relativePathToPomFile = sourcePomFile.getAbsolutePath().split( sourceRepoPath )[1];
    // File targetPomFile = new File( targetRepoPath, relativePathToPomFile );
    // pom file copying  (file path is taken with out using path translator)
    String index = artifactPath.substring(lastIndex + 1);
    int last = index.lastIndexOf('.');
    Path sourcePomFile = Paths.get(sourceRepoPath, artifactPath.substring(0, lastIndex) + "/" + artifactPath.substring(lastIndex + 1).substring(0, last) + ".pom");
    Path targetPomFile = Paths.get(targetRepoPath, artifactPath.substring(0, lastIndex) + "/" + artifactPath.substring(lastIndex + 1).substring(0, last) + ".pom");
    if (!Files.exists(targetPomFile) && Files.exists(sourcePomFile)) {
        copyFile(sourcePomFile, targetPomFile);
    }
    // explicitly update only if metadata-updater consumer is not enabled!
    if (!config.getRepositoryScanning().getKnownContentConsumers().contains("metadata-updater")) {
        // updating version metadata files
        FilesystemStorage fsStorage = new FilesystemStorage(Paths.get(sourceRepoPath), new DefaultFileLockManager());
        StorageAsset versionMetaDataFileInSourceRepo = pathTranslator.toFile(new FilesystemAsset(fsStorage, "", Paths.get(sourceRepoPath)), artifactMetadata.getNamespace(), artifactMetadata.getProject(), artifactMetadata.getVersion(), METADATA_FILENAME);
        if (versionMetaDataFileInSourceRepo.exists()) {
            // Pattern quote for windows path
            String relativePathToVersionMetadataFile = getRelativeAssetPath(versionMetaDataFileInSourceRepo);
            Path versionMetaDataFileInTargetRepo = Paths.get(targetRepoPath, relativePathToVersionMetadataFile);
            if (!Files.exists(versionMetaDataFileInTargetRepo)) {
                copyFile(versionMetaDataFileInSourceRepo.getFilePath(), versionMetaDataFileInTargetRepo);
            } else {
                updateVersionMetadata(versionMetaDataFileInTargetRepo, artifactMetadata, lastUpdatedTimestamp);
            }
        }
        // updating project meta data file
        StorageAsset projectDirectoryInSourceRepo = versionMetaDataFileInSourceRepo.getParent().getParent();
        StorageAsset projectMetadataFileInSourceRepo = projectDirectoryInSourceRepo.resolve(METADATA_FILENAME);
        if (projectMetadataFileInSourceRepo.exists()) {
            String relativePathToProjectMetadataFile = getRelativeAssetPath(projectMetadataFileInSourceRepo);
            Path projectMetadataFileInTargetRepo = Paths.get(targetRepoPath, relativePathToProjectMetadataFile);
            if (!Files.exists(projectMetadataFileInTargetRepo)) {
                copyFile(projectMetadataFileInSourceRepo.getFilePath(), projectMetadataFileInTargetRepo);
            } else {
                updateProjectMetadata(projectMetadataFileInTargetRepo, artifactMetadata, lastUpdatedTimestamp, timestamp);
            }
        }
    }
}
Also used : Path(java.nio.file.Path) ArchivaConfiguration(org.apache.archiva.configuration.provider.ArchivaConfiguration) ManagedRepositoryConfiguration(org.apache.archiva.configuration.model.ManagedRepositoryConfiguration) Configuration(org.apache.archiva.configuration.model.Configuration) ManagedRepositoryConfiguration(org.apache.archiva.configuration.model.ManagedRepositoryConfiguration) FilesystemStorage(org.apache.archiva.repository.storage.fs.FilesystemStorage) FilesystemAsset(org.apache.archiva.repository.storage.fs.FilesystemAsset) Date(java.util.Date) TimeZone(java.util.TimeZone) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) StorageAsset(org.apache.archiva.repository.storage.StorageAsset) DefaultFileLockManager(org.apache.archiva.common.filelock.DefaultFileLockManager) SimpleDateFormat(java.text.SimpleDateFormat)

Example 12 with FilesystemAsset

use of org.apache.archiva.repository.storage.fs.FilesystemAsset in project archiva by apache.

the class ArchivaIndexManagerMock method getIndexPath.

private StorageAsset getIndexPath(Repository repo) throws IOException {
    IndexCreationFeature icf = repo.getFeature(IndexCreationFeature.class);
    Path repoDir = repo.getRoot().getFilePath();
    URI indexDir = icf.getIndexPath();
    String indexPath = indexDir.getPath();
    Path indexDirectory = null;
    FilesystemStorage filesystemStorage = (FilesystemStorage) repo.getRoot().getStorage();
    if (!StringUtils.isEmpty(indexDir.toString())) {
        indexDirectory = PathUtil.getPathFromUri(indexDir);
        // not absolute so create it in repository directory
        if (indexDirectory.isAbsolute()) {
            indexPath = indexDirectory.getFileName().toString();
            filesystemStorage = new FilesystemStorage(indexDirectory.getParent(), new DefaultFileLockManager());
        } else {
            indexDirectory = repoDir.resolve(indexDirectory);
        }
    } else {
        indexDirectory = repoDir.resolve(".index");
        indexPath = ".index";
    }
    if (!Files.exists(indexDirectory)) {
        Files.createDirectories(indexDirectory);
    }
    return new FilesystemAsset(filesystemStorage, indexPath, indexDirectory);
}
Also used : Path(java.nio.file.Path) IndexCreationFeature(org.apache.archiva.repository.features.IndexCreationFeature) FilesystemStorage(org.apache.archiva.repository.storage.fs.FilesystemStorage) FilesystemAsset(org.apache.archiva.repository.storage.fs.FilesystemAsset) DefaultFileLockManager(org.apache.archiva.common.filelock.DefaultFileLockManager) URI(java.net.URI)

Example 13 with FilesystemAsset

use of org.apache.archiva.repository.storage.fs.FilesystemAsset in project archiva by apache.

the class ArchivaIndexManagerMock method getIndexPath.

private StorageAsset getIndexPath(Repository repo) throws IOException {
    IndexCreationFeature icf = repo.getFeature(IndexCreationFeature.class);
    Path repoDir = repo.getRoot().getFilePath();
    URI indexDir = icf.getIndexPath();
    String indexPath = indexDir.getPath();
    Path indexDirectory = null;
    FilesystemStorage fsStorage = (FilesystemStorage) repo.getRoot().getStorage();
    if (!StringUtils.isEmpty(indexDir.toString())) {
        indexDirectory = PathUtil.getPathFromUri(indexDir);
        // not absolute so create it in repository directory
        if (indexDirectory.isAbsolute()) {
            indexPath = indexDirectory.getFileName().toString();
            fsStorage = new FilesystemStorage(indexDirectory.getParent(), new DefaultFileLockManager());
        } else {
            indexDirectory = repoDir.resolve(indexDirectory);
        }
    } else {
        indexDirectory = repoDir.resolve(".index");
        indexPath = ".index";
    }
    if (!Files.exists(indexDirectory)) {
        Files.createDirectories(indexDirectory);
    }
    return new FilesystemAsset(fsStorage, indexPath, indexDirectory);
}
Also used : Path(java.nio.file.Path) IndexCreationFeature(org.apache.archiva.repository.features.IndexCreationFeature) FilesystemStorage(org.apache.archiva.repository.storage.fs.FilesystemStorage) FilesystemAsset(org.apache.archiva.repository.storage.fs.FilesystemAsset) DefaultFileLockManager(org.apache.archiva.common.filelock.DefaultFileLockManager) URI(java.net.URI)

Example 14 with FilesystemAsset

use of org.apache.archiva.repository.storage.fs.FilesystemAsset in project archiva by apache.

the class ArtifactContentEntriesTests method readArtifactContentEntriesRootPathNull.

@Test
public void readArtifactContentEntriesRootPathNull() throws Exception {
    FilesystemStorage filesystemStorage = new FilesystemStorage(Paths.get(getBasedir()), new DefaultFileLockManager());
    Path file = Paths.get(getBasedir(), "src/test/repo-with-osgi/commons-logging/commons-logging/1.1/commons-logging-1.1.jar");
    List<ArtifactContentEntry> artifactContentEntries = browseService.readFileEntries(new FilesystemAsset(filesystemStorage, file.toString(), file), null, "foo");
    log.info("artifactContentEntries: {}", artifactContentEntries);
    assertThat(artifactContentEntries).isNotNull().isNotEmpty().hasSize(2).contains(new ArtifactContentEntry("org", false, 0, "foo"), new ArtifactContentEntry("META-INF", false, 0, "foo"));
}
Also used : Path(java.nio.file.Path) FilesystemStorage(org.apache.archiva.repository.storage.fs.FilesystemStorage) ArtifactContentEntry(org.apache.archiva.rest.api.model.ArtifactContentEntry) FilesystemAsset(org.apache.archiva.repository.storage.fs.FilesystemAsset) DefaultFileLockManager(org.apache.archiva.common.filelock.DefaultFileLockManager) Test(org.junit.Test)

Example 15 with FilesystemAsset

use of org.apache.archiva.repository.storage.fs.FilesystemAsset in project archiva by apache.

the class ArtifactContentEntriesTests method readArtifactContentEntriesSecondDepthOnlyOneDirectory.

@Test
public void readArtifactContentEntriesSecondDepthOnlyOneDirectory() throws Exception {
    FilesystemStorage filesystemStorage = new FilesystemStorage(Paths.get(getBasedir()), new DefaultFileLockManager());
    Path file = Paths.get(getBasedir(), "src/test/repo-with-osgi/commons-logging/commons-logging/1.1/commons-logging-1.1.jar");
    List<ArtifactContentEntry> artifactContentEntries = browseService.readFileEntries(new FilesystemAsset(filesystemStorage, file.toString(), file), "org", "foo");
    log.info("artifactContentEntries: {}", artifactContentEntries);
    assertThat(artifactContentEntries).isNotNull().isNotEmpty().hasSize(1).contains(new ArtifactContentEntry("org/apache", false, 1, "foo"));
}
Also used : Path(java.nio.file.Path) FilesystemStorage(org.apache.archiva.repository.storage.fs.FilesystemStorage) ArtifactContentEntry(org.apache.archiva.rest.api.model.ArtifactContentEntry) FilesystemAsset(org.apache.archiva.repository.storage.fs.FilesystemAsset) DefaultFileLockManager(org.apache.archiva.common.filelock.DefaultFileLockManager) Test(org.junit.Test)

Aggregations

FilesystemAsset (org.apache.archiva.repository.storage.fs.FilesystemAsset)26 Test (org.junit.Test)20 Path (java.nio.file.Path)13 DefaultFileLockManager (org.apache.archiva.common.filelock.DefaultFileLockManager)13 FilesystemStorage (org.apache.archiva.repository.storage.fs.FilesystemStorage)13 ArtifactContentEntry (org.apache.archiva.rest.api.model.ArtifactContentEntry)6 OutputStream (java.io.OutputStream)5 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 URI (java.net.URI)3 IndexCreationFeature (org.apache.archiva.repository.features.IndexCreationFeature)3 Instant (java.time.Instant)2 Before (org.junit.Before)2 Files (java.nio.file.Files)1 Paths (java.nio.file.Paths)1 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 TimeZone (java.util.TimeZone)1 Configuration (org.apache.archiva.configuration.model.Configuration)1