Search in sources :

Example 11 with SpecialPathInfo

use of org.commonjava.maven.galley.model.SpecialPathInfo in project galley by Commonjava.

the class TransferManagerImpl method doDelete.

private Boolean doDelete(final Transfer item, final EventMetadata eventMetadata) throws TransferException {
    if (!item.exists()) {
        return false;
    }
    Logger contentLogger = LoggerFactory.getLogger(DELETE_CONTENT_LOG);
    contentLogger.info("BEGIN: Delete {} ({})", item.getResource(), eventMetadata);
    SpecialPathInfo specialPathInfo = specialPathManager.getSpecialPathInfo(item, eventMetadata.getPackageType());
    if (specialPathInfo != null && !specialPathInfo.isDeletable()) {
        throw new TransferException("Deleting not allowed for: %s", item);
    }
    if (item.isDirectory()) {
        String[] listing;
        try {
            listing = item.list();
        } catch (final IOException e) {
            throw new TransferException("Delete failed: {}. Reason: cannot list directory due to: {}", e, item, e.getMessage());
        }
        for (final String sub : listing) {
            if (!doDelete(item.getChild(sub), eventMetadata)) {
                contentLogger.info("FAIL: Delete: {}", item.getResource());
                return false;
            }
        }
    } else {
        try {
            if (!item.delete(true, eventMetadata)) {
                throw new TransferException("Failed to delete: {}.", item);
            }
        } catch (final IOException e) {
            throw new TransferException("Failed to delete stored location: {}. Reason: {}", e, item, e.getMessage());
        }
    }
    contentLogger.info("FINISH: Delete: {}", item.getResource());
    return true;
}
Also used : TransferException(org.commonjava.maven.galley.TransferException) SpecialPathInfo(org.commonjava.maven.galley.model.SpecialPathInfo) JoinString(org.commonjava.maven.atlas.ident.util.JoinString) IOException(java.io.IOException) Logger(org.slf4j.Logger)

Example 12 with SpecialPathInfo

use of org.commonjava.maven.galley.model.SpecialPathInfo in project galley by Commonjava.

the class TransferManagerImpl method store.

@Override
public Transfer store(final ConcreteResource resource, final InputStream stream, final EventMetadata eventMetadata) throws TransferException {
    SpecialPathInfo specialPathInfo = specialPathManager.getSpecialPathInfo(resource, eventMetadata.getPackageType());
    if (!resource.allowsStoring() || (specialPathInfo != null && !specialPathInfo.isStorable())) {
        throw new TransferException("Storing not allowed for: {}", resource);
    }
    final Transfer target = getCacheReference(resource);
    logger.info("STORE {}", target.getResource());
    OutputStream out = null;
    try {
        out = target.openOutputStream(TransferOperation.UPLOAD, true, eventMetadata);
        copy(stream, out);
    } catch (final IOException e) {
        throw new TransferException("Failed to store: {}. Reason: {}", e, resource, e.getMessage());
    } finally {
        closeQuietly(out);
    }
    return target;
}
Also used : TransferException(org.commonjava.maven.galley.TransferException) SpecialPathInfo(org.commonjava.maven.galley.model.SpecialPathInfo) OutputStream(java.io.OutputStream) Transfer(org.commonjava.maven.galley.model.Transfer) IOException(java.io.IOException)

Example 13 with SpecialPathInfo

use of org.commonjava.maven.galley.model.SpecialPathInfo in project galley by Commonjava.

the class TransferManagerImpl method doList.

private ListingResult doList(final ConcreteResource resource, final boolean suppressFailures, EventMetadata metadata) throws TransferException {
    final Transfer cachedListing = getCacheReference(resource.getChild(".listing.txt"));
    Set<String> filenames = new HashSet<String>();
    if (cachedListing.exists()) {
        InputStream stream = null;
        try {
            stream = cachedListing.openInputStream();
            filenames.addAll(IOUtils.readLines(stream, "UTF-8"));
            Logger logger = LoggerFactory.getLogger(getClass());
            logger.debug("Got cached listing:\n\n{}\n\n", filenames);
        } catch (final IOException e) {
            throw new TransferException("Failed to read listing from cached file: %s. Reason: %s", e, cachedListing, e.getMessage());
        } finally {
            closeQuietly(stream);
        }
    } else {
        final Transfer cached = getCacheReference(resource);
        if (cached.exists()) {
            if (cached.isFile()) {
                throw new TransferException("Cannot list: {}. It does not appear to be a directory.", resource);
            } else {
                try {
                    // This is fairly stupid, but we need to append '/' to the end of directories in the listing so content processors can figure
                    // out what to do with them.
                    String[] fnames = cached.list();
                    if (fnames != null && fnames.length > 0) {
                        for (String fname : fnames) {
                            final ConcreteResource child = resource.getChild(fname);
                            final Transfer childRef = getCacheReference(child);
                            if (childRef.isFile()) {
                                filenames.add(fname);
                            } else {
                                filenames.add(fname + "/");
                            }
                        }
                    }
                } catch (final IOException e) {
                    throw new TransferException("Listing failed: {}. Reason: {}", e, resource, e.getMessage());
                }
            }
        }
        if (resource.getLocation().allowsDownloading()) {
            final int timeoutSeconds = getTimeoutSeconds(resource);
            Transport transport = getTransport(resource);
            final ListingResult remoteResult = lister.list(resource, cachedListing, timeoutSeconds, transport, suppressFailures);
            if (remoteResult != null) {
                String[] remoteListing = remoteResult.getListing();
                if (remoteListing != null) {
                    final TransferDecorator decorator = cachedListing.getDecorator();
                    if (decorator != null) {
                        try {
                            Logger logger = LoggerFactory.getLogger(getClass());
                            logger.debug("Un-decorated listing:\n\n{}\n\n", remoteListing);
                            remoteListing = decorator.decorateListing(cachedListing.getParent(), remoteListing, metadata);
                        } catch (final IOException e) {
                            logger.error("Failed to decorate directory listing for: {}. Reason: {}", e, resource, e.getMessage());
                            remoteListing = null;
                        }
                    }
                }
                if (remoteListing != null) {
                    if (transport.allowsCaching()) {
                        OutputStream stream = null;
                        try {
                            Logger logger = LoggerFactory.getLogger(getClass());
                            logger.debug("Writing listing:\n\n{}\n\nto: {}", remoteListing, cachedListing);
                            stream = cachedListing.openOutputStream(TransferOperation.DOWNLOAD);
                            stream.write(join(remoteListing, "\n").getBytes("UTF-8"));
                        } catch (final IOException e) {
                            logger.debug("Failed to store directory listing for: {}. Reason: {}", e, resource, e.getMessage());
                        } finally {
                            closeQuietly(stream);
                        }
                    }
                    filenames.addAll(Arrays.asList(remoteListing));
                }
            }
        }
    }
    Logger logger = LoggerFactory.getLogger(getClass());
    logger.debug("Listing before non-listable file removal:\n\n{}\n\n", filenames);
    List<String> resultingNames = new ArrayList<String>(filenames.size());
    for (String fname : filenames) {
        ConcreteResource child = resource.getChild(fname);
        SpecialPathInfo specialPathInfo = specialPathManager.getSpecialPathInfo(child, metadata.getPackageType());
        if (specialPathInfo != null && !specialPathInfo.isListable()) {
            continue;
        }
        resultingNames.add(fname);
    }
    logger.debug("Final listing result:\n\n{}\n\n", resultingNames);
    return new ListingResult(resource, resultingNames.toArray(new String[resultingNames.size()]));
}
Also used : InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) JoinString(org.commonjava.maven.atlas.ident.util.JoinString) IOException(java.io.IOException) Logger(org.slf4j.Logger) ListingResult(org.commonjava.maven.galley.model.ListingResult) TransferDecorator(org.commonjava.maven.galley.spi.io.TransferDecorator) TransferException(org.commonjava.maven.galley.TransferException) SpecialPathInfo(org.commonjava.maven.galley.model.SpecialPathInfo) Transfer(org.commonjava.maven.galley.model.Transfer) ConcreteResource(org.commonjava.maven.galley.model.ConcreteResource) Transport(org.commonjava.maven.galley.spi.transport.Transport) HashSet(java.util.HashSet)

Example 14 with SpecialPathInfo

use of org.commonjava.maven.galley.model.SpecialPathInfo in project indy by Commonjava.

the class DefaultContentManager method getTransfer.

@Override
public Transfer getTransfer(final ArtifactStore store, final String path, final TransferOperation op) throws IndyWorkflowException {
    logger.debug("Getting transfer for: {}/{} (op: {})", store.getKey(), path, op);
    if (group == store.getKey().getType()) {
        KeyedLocation location = LocationUtils.toLocation(store);
        SpecialPathInfo spInfo = specialPathManager.getSpecialPathInfo(location, path, store.getPackageType());
        if (spInfo == null || !spInfo.isMergable()) {
            try {
                final List<ArtifactStore> allMembers = storeManager.query().packageType(store.getPackageType()).enabledState(true).getOrderedConcreteStoresInGroup(store.getName());
                logger.debug("Trying to retrieve suitable transfer for: {} in group: {} members:\n{}", path, store.getName(), allMembers);
                return getTransfer(allMembers, path, op);
            } catch (final IndyDataException e) {
                throw new IndyWorkflowException("Failed to lookup concrete members of: %s. Reason: %s", e, store, e.getMessage());
            }
        } else {
            logger.debug("Detected mergable special path: {}/{}.", store.getKey(), path);
        }
    }
    logger.debug("Retrieving storage reference (Transfer) directly for: {}/{}", store.getKey(), path);
    return downloadManager.getStorageReference(store, path, op);
}
Also used : IndyDataException(org.commonjava.indy.data.IndyDataException) KeyedLocation(org.commonjava.indy.model.galley.KeyedLocation) SpecialPathInfo(org.commonjava.maven.galley.model.SpecialPathInfo) ArtifactStore(org.commonjava.indy.model.core.ArtifactStore) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException)

Example 15 with SpecialPathInfo

use of org.commonjava.maven.galley.model.SpecialPathInfo in project indy by Commonjava.

the class DefaultGalleyStorageProvider method setup.

@PostConstruct
public void setup() {
    SpecialPathInfo infoSpi = SpecialPathInfo.from(new FilePatternMatcher(".+\\.info")).setDecoratable(false).setDeletable(false).setListable(false).setPublishable(false).setRetrievable(false).setStorable(false).build();
    specialPathManager.registerSpecialPathInfo(infoSpi);
    ChecksummingDecoratorAdvisor readAdvisor = (transfer, op, eventMetadata) -> {
        ChecksummingDecoratorAdvisor.ChecksumAdvice result = NO_DECORATE;
        if (checksumAdvisors != null) {
            for (IndyChecksumAdvisor advisor : checksumAdvisors) {
                Optional<ChecksummingDecoratorAdvisor.ChecksumAdvice> advice = advisor.getChecksumReadAdvice(transfer, op, eventMetadata);
                if (advice.isPresent()) {
                    ChecksummingDecoratorAdvisor.ChecksumAdvice checksumAdvice = advice.get();
                    if (checksumAdvice.ordinal() > result.ordinal()) {
                        result = checksumAdvice;
                        if (checksumAdvice == CALCULATE_AND_WRITE) {
                            break;
                        }
                    }
                }
            }
        }
        logger.debug("Advising {} for {} of: {}", result, op, transfer);
        return result;
    };
    ChecksummingDecoratorAdvisor writeAdvisor = (transfer, op, eventMetadata) -> {
        ChecksummingDecoratorAdvisor.ChecksumAdvice result = NO_DECORATE;
        if (TransferOperation.GENERATE == op) {
            result = CALCULATE_AND_WRITE;
        } else if (checksumAdvisors != null) {
            for (IndyChecksumAdvisor advisor : checksumAdvisors) {
                Optional<ChecksummingDecoratorAdvisor.ChecksumAdvice> advice = advisor.getChecksumWriteAdvice(transfer, op, eventMetadata);
                if (advice.isPresent()) {
                    ChecksummingDecoratorAdvisor.ChecksumAdvice checksumAdvice = advice.get();
                    if (checksumAdvice.ordinal() > result.ordinal()) {
                        result = checksumAdvice;
                        if (checksumAdvice == CALCULATE_AND_WRITE) {
                            break;
                        }
                    }
                }
            }
        }
        logger.debug("Advising {} for {} of: {}", result, op, transfer);
        return result;
    };
    transferDecorator = new TransferDecoratorPipeline(new ChecksummingTransferDecorator(readAdvisor, writeAdvisor, specialPathManager, contentMetadataConsumer, new Md5GeneratorFactory(), new Sha1GeneratorFactory(), new Sha256GeneratorFactory()), new ContentsFilteringTransferDecorator(), new NoCacheTransferDecorator(specialPathManager));
    final File storeRoot = config.getStorageRootDirectory();
    cacheProviderFactory = new PartyLineCacheProviderFactory(storeRoot);
    final File nfsBasedir = config.getNFSStorageRootDirectory();
    if (nfsBasedir != null) {
        if (!nfsBasedir.exists()) {
            nfsBasedir.mkdirs();
        }
        // nfs root can not be created due to some security reason(like permission), will bypass FastLocal provider and use PartyLine
        if (nfsBasedir.exists()) {
            final FastLocalCacheProviderFactory fastLocalFac = new FastLocalCacheProviderFactory(storeRoot, nfsBasedir, new CacheInstanceAdapter(nfsOwnerCache), fastLocalExecutors);
            cacheProviderFactory = new RoutingCacheProviderFactory((resource) -> {
                if (resource != null) {
                    final Location loc = resource.getLocation();
                    // looking for KeyedLocation and StoreType.hosted should be faster than regex on the URI.
                    return ((loc instanceof KeyedLocation) && hosted == ((KeyedLocation) loc).getKey().getType());
                }
                return false;
            }, fastLocalFac, cacheProviderFactory);
        } else {
            logger.warn("[Indy] nfs base dir {} can not be created correctly due to some unknown reasons, will use partyline cache provider as default", nfsBasedir);
        }
    }
    // TODO: Tie this into a config file!
    transportManagerConfig = new TransportManagerConfig();
}
Also used : TransportManagerConfig(org.commonjava.maven.galley.config.TransportManagerConfig) Produces(javax.enterprise.inject.Produces) KeyedLocation(org.commonjava.indy.model.galley.KeyedLocation) CacheProvider(org.commonjava.maven.galley.spi.cache.CacheProvider) CALCULATE_AND_WRITE(org.commonjava.maven.galley.io.checksum.ChecksummingDecoratorAdvisor.ChecksumAdvice.CALCULATE_AND_WRITE) GalleyInitException(org.commonjava.maven.galley.GalleyInitException) LoggerFactory(org.slf4j.LoggerFactory) PathGenerator(org.commonjava.maven.galley.spi.io.PathGenerator) ContentsFilteringTransferDecorator(org.commonjava.maven.galley.transport.htcli.ContentsFilteringTransferDecorator) FilePatternMatcher(org.commonjava.maven.galley.model.FilePatternMatcher) SpecialPathInfo(org.commonjava.maven.galley.model.SpecialPathInfo) IndyChecksumAdvisor(org.commonjava.indy.content.IndyChecksumAdvisor) RoutingCacheProviderFactory(org.commonjava.maven.galley.cache.routes.RoutingCacheProviderFactory) Inject(javax.inject.Inject) DefaultStorageProviderConfiguration(org.commonjava.indy.filer.def.conf.DefaultStorageProviderConfiguration) ChecksummingTransferDecorator(org.commonjava.maven.galley.io.ChecksummingTransferDecorator) WeftManaged(org.commonjava.cdi.util.weft.WeftManaged) ExecutorConfig(org.commonjava.cdi.util.weft.ExecutorConfig) TransferOperation(org.commonjava.maven.galley.model.TransferOperation) NO_DECORATE(org.commonjava.maven.galley.io.checksum.ChecksummingDecoratorAdvisor.ChecksumAdvice.NO_DECORATE) TransferMetadataConsumer(org.commonjava.maven.galley.io.checksum.TransferMetadataConsumer) Sha256GeneratorFactory(org.commonjava.maven.galley.io.checksum.Sha256GeneratorFactory) Location(org.commonjava.maven.galley.model.Location) Instance(javax.enterprise.inject.Instance) ExecutorService(java.util.concurrent.ExecutorService) ChecksummingDecoratorAdvisor(org.commonjava.maven.galley.io.checksum.ChecksummingDecoratorAdvisor) Default(javax.enterprise.inject.Default) Logger(org.slf4j.Logger) NoCacheTransferDecorator(org.commonjava.maven.galley.io.NoCacheTransferDecorator) PartyLineCacheProviderFactory(org.commonjava.maven.galley.cache.partyline.PartyLineCacheProviderFactory) FastLocalCacheProviderFactory(org.commonjava.maven.galley.cache.infinispan.FastLocalCacheProviderFactory) Collection(java.util.Collection) CDI(javax.enterprise.inject.spi.CDI) SpecialPathManager(org.commonjava.maven.galley.spi.io.SpecialPathManager) CacheHandle(org.commonjava.indy.subsys.infinispan.CacheHandle) File(java.io.File) CacheProviderFactory(org.commonjava.maven.galley.cache.CacheProviderFactory) TransferDecoratorPipeline(org.commonjava.maven.galley.io.TransferDecoratorPipeline) Sha1GeneratorFactory(org.commonjava.maven.galley.io.checksum.Sha1GeneratorFactory) TransferDecorator(org.commonjava.maven.galley.spi.io.TransferDecorator) PostConstruct(javax.annotation.PostConstruct) Optional(java.util.Optional) ApplicationScoped(javax.enterprise.context.ApplicationScoped) StoreType.hosted(org.commonjava.indy.model.core.StoreType.hosted) FileEventManager(org.commonjava.maven.galley.spi.event.FileEventManager) Collections(java.util.Collections) Md5GeneratorFactory(org.commonjava.maven.galley.io.checksum.Md5GeneratorFactory) BeanManager(javax.enterprise.inject.spi.BeanManager) IndyChecksumAdvisor(org.commonjava.indy.content.IndyChecksumAdvisor) FastLocalCacheProviderFactory(org.commonjava.maven.galley.cache.infinispan.FastLocalCacheProviderFactory) Sha1GeneratorFactory(org.commonjava.maven.galley.io.checksum.Sha1GeneratorFactory) KeyedLocation(org.commonjava.indy.model.galley.KeyedLocation) ChecksummingDecoratorAdvisor(org.commonjava.maven.galley.io.checksum.ChecksummingDecoratorAdvisor) Optional(java.util.Optional) NoCacheTransferDecorator(org.commonjava.maven.galley.io.NoCacheTransferDecorator) TransportManagerConfig(org.commonjava.maven.galley.config.TransportManagerConfig) ContentsFilteringTransferDecorator(org.commonjava.maven.galley.transport.htcli.ContentsFilteringTransferDecorator) PartyLineCacheProviderFactory(org.commonjava.maven.galley.cache.partyline.PartyLineCacheProviderFactory) FilePatternMatcher(org.commonjava.maven.galley.model.FilePatternMatcher) SpecialPathInfo(org.commonjava.maven.galley.model.SpecialPathInfo) TransferDecoratorPipeline(org.commonjava.maven.galley.io.TransferDecoratorPipeline) RoutingCacheProviderFactory(org.commonjava.maven.galley.cache.routes.RoutingCacheProviderFactory) ChecksummingTransferDecorator(org.commonjava.maven.galley.io.ChecksummingTransferDecorator) Sha256GeneratorFactory(org.commonjava.maven.galley.io.checksum.Sha256GeneratorFactory) File(java.io.File) Md5GeneratorFactory(org.commonjava.maven.galley.io.checksum.Md5GeneratorFactory) KeyedLocation(org.commonjava.indy.model.galley.KeyedLocation) Location(org.commonjava.maven.galley.model.Location) PostConstruct(javax.annotation.PostConstruct)

Aggregations

SpecialPathInfo (org.commonjava.maven.galley.model.SpecialPathInfo)15 Transfer (org.commonjava.maven.galley.model.Transfer)5 Logger (org.slf4j.Logger)5 IOException (java.io.IOException)4 TransferException (org.commonjava.maven.galley.TransferException)4 IndyDataException (org.commonjava.indy.data.IndyDataException)3 KeyedLocation (org.commonjava.indy.model.galley.KeyedLocation)3 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 PostConstruct (javax.annotation.PostConstruct)2 Inject (javax.inject.Inject)2 IndyWorkflowException (org.commonjava.indy.IndyWorkflowException)2 ArtifactStore (org.commonjava.indy.model.core.ArtifactStore)2 StoreKey (org.commonjava.indy.model.core.StoreKey)2 StoreType (org.commonjava.indy.model.core.StoreType)2 JoinString (org.commonjava.maven.atlas.ident.util.JoinString)2 ChecksummingDecoratorAdvisor (org.commonjava.maven.galley.io.checksum.ChecksummingDecoratorAdvisor)2 ConcreteResource (org.commonjava.maven.galley.model.ConcreteResource)2