Search in sources :

Example 11 with StorageAsset

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

the class ArchivaIndexManagerMock method removeArtifactsFromIndex.

@Override
public void removeArtifactsFromIndex(ArchivaIndexingContext context, Collection<URI> artifactReference) throws IndexUpdateFailedException {
    final StorageAsset ctxUri = context.getPath();
    executeUpdateFunction(context, indexingContext -> {
        Collection<ArtifactContext> artifacts = artifactReference.stream().map(r -> artifactContextProducer.getArtifactContext(indexingContext, Paths.get(ctxUri.getFilePath().toUri().resolve(r)).toFile())).collect(Collectors.toList());
        try {
            indexer.deleteArtifactsFromIndex(artifacts, indexingContext);
        } catch (IOException e) {
            log.error("IOException while removing artifact {}", e.getMessage(), e);
            throw new IndexUpdateFailedException("Error occured while removing artifact from index of " + context.getId() + (StringUtils.isNotEmpty(e.getMessage()) ? ": " + e.getMessage() : ""));
        }
    });
}
Also used : AuthenticationException(org.apache.maven.wagon.authentication.AuthenticationException) ArtifactContext(org.apache.maven.index.ArtifactContext) PathUtil(org.apache.archiva.common.utils.PathUtil) UnsupportedBaseContextException(org.apache.archiva.indexer.UnsupportedBaseContextException) StorageAsset(org.apache.archiva.repository.storage.StorageAsset) FilesystemStorage(org.apache.archiva.repository.storage.fs.FilesystemStorage) LoggerFactory(org.slf4j.LoggerFactory) IndexCreator(org.apache.maven.index.context.IndexCreator) ArchivaIndexManager(org.apache.archiva.indexer.ArchivaIndexManager) StringUtils(org.apache.commons.lang3.StringUtils) Map(java.util.Map) FileUtils(org.apache.archiva.common.utils.FileUtils) IndexCreationFailedException(org.apache.archiva.indexer.IndexCreationFailedException) UnsupportedRepositoryTypeException(org.apache.archiva.repository.UnsupportedRepositoryTypeException) RemoteIndexFeature(org.apache.archiva.repository.features.RemoteIndexFeature) URI(java.net.URI) TransferFailedException(org.apache.maven.wagon.TransferFailedException) Path(java.nio.file.Path) ArchivaIndexingContext(org.apache.archiva.indexer.ArchivaIndexingContext) AbstractHttpClientWagon(org.apache.maven.wagon.shared.http.AbstractHttpClientWagon) TransferEvent(org.apache.maven.wagon.events.TransferEvent) IndexCreationFeature(org.apache.archiva.repository.features.IndexCreationFeature) HttpMethodConfiguration(org.apache.maven.wagon.shared.http.HttpMethodConfiguration) ProxyInfo(org.apache.maven.wagon.proxy.ProxyInfo) Collection(java.util.Collection) ConnectionException(org.apache.maven.wagon.ConnectionException) AuthorizationException(org.apache.maven.wagon.authorization.AuthorizationException) NetworkProxy(org.apache.archiva.proxy.model.NetworkProxy) WagonFactory(org.apache.archiva.maven.common.proxy.WagonFactory) PasswordCredentials(org.apache.archiva.repository.base.PasswordCredentials) Collectors(java.util.stream.Collectors) Indexer(org.apache.maven.index.Indexer) FileNotFoundException(java.io.FileNotFoundException) ManagedRepository(org.apache.archiva.repository.ManagedRepository) IndexUpdateRequest(org.apache.maven.index.updater.IndexUpdateRequest) IndexUpdateFailedException(org.apache.archiva.indexer.IndexUpdateFailedException) List(java.util.List) HttpConfiguration(org.apache.maven.wagon.shared.http.HttpConfiguration) IndexFormatTooOldException(org.apache.maven.index_shaded.lucene.index.IndexFormatTooOldException) IndexerEngine(org.apache.maven.index.IndexerEngine) EditableRepository(org.apache.archiva.repository.EditableRepository) Scanner(org.apache.maven.index.Scanner) ArchivaConfiguration(org.apache.archiva.configuration.provider.ArchivaConfiguration) ArtifactContextProducer(org.apache.maven.index.ArtifactContextProducer) FilesystemAsset(org.apache.archiva.repository.storage.fs.FilesystemAsset) Inject(javax.inject.Inject) Repository(org.apache.archiva.repository.Repository) RemoteRepository(org.apache.archiva.repository.RemoteRepository) Service(org.springframework.stereotype.Service) WagonFactoryRequest(org.apache.archiva.maven.common.proxy.WagonFactoryRequest) IndexPackingRequest(org.apache.maven.index.packer.IndexPackingRequest) ResourceFetcher(org.apache.maven.index.updater.ResourceFetcher) StreamWagon(org.apache.maven.wagon.StreamWagon) Logger(org.slf4j.Logger) IndexPacker(org.apache.maven.index.packer.IndexPacker) MalformedURLException(java.net.MalformedURLException) Files(java.nio.file.Files) DefaultScannerListener(org.apache.maven.index.DefaultScannerListener) ScanningRequest(org.apache.maven.index.ScanningRequest) AuthenticationInfo(org.apache.maven.wagon.authentication.AuthenticationInfo) IOException(java.io.IOException) RepositoryType(org.apache.archiva.repository.RepositoryType) IndexingContext(org.apache.maven.index.context.IndexingContext) ConcurrentSkipListSet(java.util.concurrent.ConcurrentSkipListSet) ResourceDoesNotExistException(org.apache.maven.wagon.ResourceDoesNotExistException) Paths(java.nio.file.Paths) TransferListener(org.apache.maven.wagon.events.TransferListener) DefaultFileLockManager(org.apache.archiva.common.filelock.DefaultFileLockManager) Wagon(org.apache.maven.wagon.Wagon) WagonFactoryException(org.apache.archiva.maven.common.proxy.WagonFactoryException) ScanningResult(org.apache.maven.index.ScanningResult) InputStream(java.io.InputStream) StorageAsset(org.apache.archiva.repository.storage.StorageAsset) ArtifactContext(org.apache.maven.index.ArtifactContext) IOException(java.io.IOException) IndexUpdateFailedException(org.apache.archiva.indexer.IndexUpdateFailedException)

Example 12 with StorageAsset

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

the class ArchivaIndexManagerMock method addArtifactsToIndex.

@Override
public void addArtifactsToIndex(final ArchivaIndexingContext context, final Collection<URI> artifactReference) throws IndexUpdateFailedException {
    StorageAsset ctxUri = context.getPath();
    executeUpdateFunction(context, indexingContext -> {
        Collection<ArtifactContext> artifacts = artifactReference.stream().map(r -> artifactContextProducer.getArtifactContext(indexingContext, Paths.get(ctxUri.getFilePath().toUri().resolve(r)).toFile())).collect(Collectors.toList());
        try {
            indexer.addArtifactsToIndex(artifacts, indexingContext);
        } catch (IOException e) {
            log.error("IOException while adding artifact {}", e.getMessage(), e);
            throw new IndexUpdateFailedException("Error occured while adding artifact to index of " + context.getId() + (StringUtils.isNotEmpty(e.getMessage()) ? ": " + e.getMessage() : ""));
        }
    });
}
Also used : AuthenticationException(org.apache.maven.wagon.authentication.AuthenticationException) ArtifactContext(org.apache.maven.index.ArtifactContext) PathUtil(org.apache.archiva.common.utils.PathUtil) UnsupportedBaseContextException(org.apache.archiva.indexer.UnsupportedBaseContextException) StorageAsset(org.apache.archiva.repository.storage.StorageAsset) FilesystemStorage(org.apache.archiva.repository.storage.fs.FilesystemStorage) LoggerFactory(org.slf4j.LoggerFactory) IndexCreator(org.apache.maven.index.context.IndexCreator) ArchivaIndexManager(org.apache.archiva.indexer.ArchivaIndexManager) StringUtils(org.apache.commons.lang3.StringUtils) Map(java.util.Map) FileUtils(org.apache.archiva.common.utils.FileUtils) IndexCreationFailedException(org.apache.archiva.indexer.IndexCreationFailedException) UnsupportedRepositoryTypeException(org.apache.archiva.repository.UnsupportedRepositoryTypeException) RemoteIndexFeature(org.apache.archiva.repository.features.RemoteIndexFeature) URI(java.net.URI) TransferFailedException(org.apache.maven.wagon.TransferFailedException) Path(java.nio.file.Path) ArchivaIndexingContext(org.apache.archiva.indexer.ArchivaIndexingContext) AbstractHttpClientWagon(org.apache.maven.wagon.shared.http.AbstractHttpClientWagon) TransferEvent(org.apache.maven.wagon.events.TransferEvent) IndexCreationFeature(org.apache.archiva.repository.features.IndexCreationFeature) HttpMethodConfiguration(org.apache.maven.wagon.shared.http.HttpMethodConfiguration) ProxyInfo(org.apache.maven.wagon.proxy.ProxyInfo) Collection(java.util.Collection) ConnectionException(org.apache.maven.wagon.ConnectionException) AuthorizationException(org.apache.maven.wagon.authorization.AuthorizationException) NetworkProxy(org.apache.archiva.proxy.model.NetworkProxy) WagonFactory(org.apache.archiva.maven.common.proxy.WagonFactory) PasswordCredentials(org.apache.archiva.repository.base.PasswordCredentials) Collectors(java.util.stream.Collectors) Indexer(org.apache.maven.index.Indexer) FileNotFoundException(java.io.FileNotFoundException) ManagedRepository(org.apache.archiva.repository.ManagedRepository) IndexUpdateRequest(org.apache.maven.index.updater.IndexUpdateRequest) IndexUpdateFailedException(org.apache.archiva.indexer.IndexUpdateFailedException) List(java.util.List) HttpConfiguration(org.apache.maven.wagon.shared.http.HttpConfiguration) IndexFormatTooOldException(org.apache.maven.index_shaded.lucene.index.IndexFormatTooOldException) IndexerEngine(org.apache.maven.index.IndexerEngine) EditableRepository(org.apache.archiva.repository.EditableRepository) Scanner(org.apache.maven.index.Scanner) ArchivaConfiguration(org.apache.archiva.configuration.provider.ArchivaConfiguration) ArtifactContextProducer(org.apache.maven.index.ArtifactContextProducer) FilesystemAsset(org.apache.archiva.repository.storage.fs.FilesystemAsset) Inject(javax.inject.Inject) Repository(org.apache.archiva.repository.Repository) RemoteRepository(org.apache.archiva.repository.RemoteRepository) Service(org.springframework.stereotype.Service) WagonFactoryRequest(org.apache.archiva.maven.common.proxy.WagonFactoryRequest) IndexPackingRequest(org.apache.maven.index.packer.IndexPackingRequest) ResourceFetcher(org.apache.maven.index.updater.ResourceFetcher) StreamWagon(org.apache.maven.wagon.StreamWagon) Logger(org.slf4j.Logger) IndexPacker(org.apache.maven.index.packer.IndexPacker) MalformedURLException(java.net.MalformedURLException) Files(java.nio.file.Files) DefaultScannerListener(org.apache.maven.index.DefaultScannerListener) ScanningRequest(org.apache.maven.index.ScanningRequest) AuthenticationInfo(org.apache.maven.wagon.authentication.AuthenticationInfo) IOException(java.io.IOException) RepositoryType(org.apache.archiva.repository.RepositoryType) IndexingContext(org.apache.maven.index.context.IndexingContext) ConcurrentSkipListSet(java.util.concurrent.ConcurrentSkipListSet) ResourceDoesNotExistException(org.apache.maven.wagon.ResourceDoesNotExistException) Paths(java.nio.file.Paths) TransferListener(org.apache.maven.wagon.events.TransferListener) DefaultFileLockManager(org.apache.archiva.common.filelock.DefaultFileLockManager) Wagon(org.apache.maven.wagon.Wagon) WagonFactoryException(org.apache.archiva.maven.common.proxy.WagonFactoryException) ScanningResult(org.apache.maven.index.ScanningResult) InputStream(java.io.InputStream) StorageAsset(org.apache.archiva.repository.storage.StorageAsset) ArtifactContext(org.apache.maven.index.ArtifactContext) IOException(java.io.IOException) IndexUpdateFailedException(org.apache.archiva.indexer.IndexUpdateFailedException)

Example 13 with StorageAsset

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

the class ArchivaIndexManagerMock method move.

@Override
public ArchivaIndexingContext move(ArchivaIndexingContext context, Repository repo) throws IndexCreationFailedException {
    if (context == null) {
        return null;
    }
    if (context.supports(IndexingContext.class)) {
        try {
            StorageAsset newPath = getIndexPath(repo);
            IndexingContext ctx = context.getBaseContext(IndexingContext.class);
            Path oldPath = ctx.getIndexDirectoryFile().toPath();
            if (oldPath.equals(newPath)) {
                // Nothing to do, if path does not change
                return context;
            }
            if (!Files.exists(oldPath)) {
                return createContext(repo);
            } else if (context.isEmpty()) {
                context.close();
                return createContext(repo);
            } else {
                context.close(false);
                Files.move(oldPath, newPath.getFilePath());
                return createContext(repo);
            }
        } catch (IOException e) {
            log.error("IOException while moving index directory {}", e.getMessage(), e);
            throw new IndexCreationFailedException("Could not recreated the index.", e);
        } catch (UnsupportedBaseContextException e) {
            throw new IndexCreationFailedException("The given context, is not a maven context.");
        }
    } else {
        throw new IndexCreationFailedException("Bad context type. This is not a maven context.");
    }
}
Also used : Path(java.nio.file.Path) UnsupportedBaseContextException(org.apache.archiva.indexer.UnsupportedBaseContextException) IndexCreationFailedException(org.apache.archiva.indexer.IndexCreationFailedException) StorageAsset(org.apache.archiva.repository.storage.StorageAsset) ArchivaIndexingContext(org.apache.archiva.indexer.ArchivaIndexingContext) IndexingContext(org.apache.maven.index.context.IndexingContext) IOException(java.io.IOException)

Example 14 with StorageAsset

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

the class ArchivaIndexManagerMock method createManagedContext.

private IndexingContext createManagedContext(ManagedRepository repository) throws IOException {
    IndexingContext context;
    // take care first about repository location as can be relative
    Path repositoryDirectory = repository.getRoot().getFilePath();
    if (!Files.exists(repositoryDirectory)) {
        try {
            Files.createDirectories(repositoryDirectory);
        } catch (IOException e) {
            log.error("Could not create directory {}", repositoryDirectory);
        }
    }
    StorageAsset indexDirectory = null;
    if (repository.supportsFeature(IndexCreationFeature.class)) {
        indexDirectory = getIndexPath(repository);
        String indexUrl = repositoryDirectory.toUri().toURL().toExternalForm();
        try {
            context = getIndexingContext(repository, repository.getId(), repositoryDirectory, indexDirectory, indexUrl);
            context.setSearchable(repository.isScanned());
        } catch (IndexFormatTooOldException e) {
            // existing index with an old lucene format so we need to delete it!!!
            // delete it first then recreate it.
            // 
            log.warn(// 
            "the index of repository {} is too old we have to delete and recreate it", repository.getId());
            org.apache.archiva.common.utils.FileUtils.deleteDirectory(indexDirectory.getFilePath());
            context = getIndexingContext(repository, repository.getId(), repositoryDirectory, indexDirectory, indexUrl);
            context.setSearchable(repository.isScanned());
        }
        return context;
    } else {
        throw new IOException("No repository index defined");
    }
}
Also used : Path(java.nio.file.Path) IndexFormatTooOldException(org.apache.maven.index_shaded.lucene.index.IndexFormatTooOldException) StorageAsset(org.apache.archiva.repository.storage.StorageAsset) ArchivaIndexingContext(org.apache.archiva.indexer.ArchivaIndexingContext) IndexingContext(org.apache.maven.index.context.IndexingContext) IOException(java.io.IOException)

Example 15 with StorageAsset

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

the class StorageUtil method copyRecursively.

/**
 * Deletes the given asset and all child assets recursively.
 * @param srcAsset The source directory
 * @param destAsset The destination directory
 * @param stopOnError if <code>true</code> the traversal stops, if an exception is encountered
 * @return returns <code>true</code>, if every item was removed. If an IOException was encountered during
 * traversal it returns <code>false</code>
 */
public static final boolean copyRecursively(final StorageAsset srcAsset, final StorageAsset destAsset, final boolean stopOnError) throws IOException {
    try {
        if (srcAsset.isFileBased() && destAsset.isFileBased()) {
            Path src = srcAsset.getFilePath();
            Path dest = destAsset.getFilePath();
            return Files.walk(src).map(source -> {
                try {
                    Files.copy(source, dest.resolve(src.relativize(source)), StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES);
                    return Boolean.TRUE;
                } catch (IOException e) {
                    if (stopOnError) {
                        throw new RuntimeException(e);
                    } else {
                        return Boolean.FALSE;
                    }
                }
            }).reduce((a, b) -> Boolean.logicalAnd(a, b)).get();
        } else {
            try (Stream<StorageAsset> stream = newAssetStream(srcAsset)) {
                if (!destAsset.exists() && srcAsset.isContainer()) {
                    destAsset.create(AssetType.CONTAINER);
                }
                return stream.map(a -> {
                    try {
                        String relativePath = destAsset.relativize(a);
                        System.out.println("Destination relative: " + relativePath);
                        StorageAsset destFile = destAsset.resolve(relativePath);
                        assert destFile != null;
                        System.out.println("Destination " + destFile.getPath() + " " + a.isContainer());
                        if (a.isContainer()) {
                            destFile.create(AssetType.CONTAINER);
                        } else {
                            if (!destFile.getParent().exists()) {
                                System.out.println("Creating parent " + destFile.getParent());
                                destFile.getParent().create(AssetType.CONTAINER);
                            }
                            System.out.println("Copying " + a.getPath() + "->" + destFile.getPath());
                            copy(a.getReadChannel(), destFile.getWriteChannel(true));
                        }
                        return Boolean.TRUE;
                    } catch (IOException e) {
                        LOG.error("Could not copy asset {}: {}", a.getPath(), e.getMessage(), e);
                        // Returning true, if exception
                        if (stopOnError) {
                            throw new RuntimeException(e);
                        } else {
                            return Boolean.FALSE;
                        }
                    }
                }).reduce((a, b) -> Boolean.logicalAnd(a, b)).orElse(Boolean.FALSE);
            }
        }
    } catch (RuntimeException e) {
        System.err.println("Exception " + e.getMessage());
        e.printStackTrace();
        return false;
    }
}
Also used : Path(java.nio.file.Path) ReadableByteChannel(java.nio.channels.ReadableByteChannel) Logger(org.slf4j.Logger) Files(java.nio.file.Files) StorageAsset(org.apache.archiva.repository.storage.StorageAsset) LoggerFactory(org.slf4j.LoggerFactory) IOException(java.io.IOException) StringUtils(org.apache.commons.lang3.StringUtils) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) ByteBuffer(java.nio.ByteBuffer) StandardCopyOption(java.nio.file.StandardCopyOption) Consumer(java.util.function.Consumer) List(java.util.List) Stream(java.util.stream.Stream) AssetType(org.apache.archiva.repository.storage.AssetType) Paths(java.nio.file.Paths) WritableByteChannel(java.nio.channels.WritableByteChannel) Optional(java.util.Optional) StreamSupport(java.util.stream.StreamSupport) FileChannel(java.nio.channels.FileChannel) Path(java.nio.file.Path) RepositoryStorage(org.apache.archiva.repository.storage.RepositoryStorage) CopyOption(java.nio.file.CopyOption) StorageAsset(org.apache.archiva.repository.storage.StorageAsset) IOException(java.io.IOException)

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