Search in sources :

Example 1 with ChecksummingTransferDecorator

use of org.commonjava.maven.galley.io.ChecksummingTransferDecorator in project indy by Commonjava.

the class InfinispanGalleyStorageProvider 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);
    transferDecorator = new ChecksummingTransferDecorator(Collections.singleton(TransferOperation.GENERATE), specialPathManager, new Md5GeneratorFactory(), new Sha1GeneratorFactory());
    this.cacheProvider = new GridFileSystemCacheProvider(pathGenerator, fileEventManager, transferDecorator, cacheFs);
}
Also used : Sha1GeneratorFactory(org.commonjava.maven.galley.io.checksum.Sha1GeneratorFactory) GridFileSystemCacheProvider(org.commonjava.maven.galley.cache.infinispan.GridFileSystemCacheProvider) FilePatternMatcher(org.commonjava.maven.galley.model.FilePatternMatcher) SpecialPathInfo(org.commonjava.maven.galley.model.SpecialPathInfo) ChecksummingTransferDecorator(org.commonjava.maven.galley.io.ChecksummingTransferDecorator) Md5GeneratorFactory(org.commonjava.maven.galley.io.checksum.Md5GeneratorFactory) PostConstruct(javax.annotation.PostConstruct)

Example 2 with ChecksummingTransferDecorator

use of org.commonjava.maven.galley.io.ChecksummingTransferDecorator in project galley by Commonjava.

the class ChecksummingTransferDecoratorTest method noChecksumOnReadWhenChecksumsAreDisabledForReads.

@Test
public void noChecksumOnReadWhenChecksumsAreDisabledForReads() throws Exception {
    fixture.setDecorator(new ChecksummingTransferDecorator(Collections.<TransferOperation>emptySet(), new SpecialPathManagerImpl(), false, false, metadataConsumer, new Md5GeneratorFactory()));
    fixture.initMissingComponents();
    fixture.getCache().startReporting();
    String path = "my-path.txt";
    final Transfer txfr = fixture.getCache().getTransfer(new ConcreteResource(new SimpleLocation("test:uri"), path));
    File f = new File(temp.getRoot(), "cache/test:uri");
    f = new File(f, path);
    byte[] data = "This is a test with a bunch of data and some other stuff, in a big box sealed with chewing gum".getBytes();
    FileUtils.writeByteArrayToFile(f, data);
    logger.info("Opening transfer input stream");
    EventMetadata forceEventMetadata = new EventMetadata();
    try (InputStream stream = txfr.openInputStream(false, forceEventMetadata)) {
        logger.info("Reading stream");
        byte[] resultData = IOUtils.toByteArray(stream);
        logger.debug("Result is {} bytes", resultData.length);
        assertThat(Arrays.equals(resultData, data), equalTo(true));
    }
    logger.debug("Verifying .md5 file is missing");
    final Transfer md5Txfr = txfr.getSiblingMeta(".md5");
    assertThat(md5Txfr.exists(), equalTo(false));
    logger.debug("Verifying MD5 in metadata consumer is missing");
    TransferMetadata metadata = metadataConsumer.getMetadata(txfr);
    assertThat(metadata, nullValue());
}
Also used : InputStream(java.io.InputStream) SpecialPathManagerImpl(org.commonjava.maven.galley.io.SpecialPathManagerImpl) EventMetadata(org.commonjava.maven.galley.event.EventMetadata) Transfer(org.commonjava.maven.galley.model.Transfer) ConcreteResource(org.commonjava.maven.galley.model.ConcreteResource) ChecksummingTransferDecorator(org.commonjava.maven.galley.io.ChecksummingTransferDecorator) SimpleLocation(org.commonjava.maven.galley.model.SimpleLocation) TransferOperation(org.commonjava.maven.galley.model.TransferOperation) File(java.io.File) Test(org.junit.Test)

Example 3 with ChecksummingTransferDecorator

use of org.commonjava.maven.galley.io.ChecksummingTransferDecorator in project galley by Commonjava.

the class ChecksummingTransferDecoratorTest method customChecksumReaderFilter.

@Test
public void customChecksumReaderFilter() throws Exception {
    String path = "my-path.txt";
    fixture.setDecorator(new ChecksummingTransferDecorator(new TestDecoratorAdvisor(), new DisabledChecksummingDecoratorAdvisor(), new SpecialPathManagerImpl(), metadataConsumer, new Md5GeneratorFactory()));
    fixture.initMissingComponents();
    fixture.getCache().startReporting();
    final Transfer txfr = fixture.getCache().getTransfer(new ConcreteResource(new SimpleLocation("test:uri"), path));
    File f = new File(temp.getRoot(), "cache/test:uri");
    f = new File(f, path);
    byte[] data = "This is a test with a bunch of data and some other stuff, in a big box sealed with chewing gum".getBytes();
    FileUtils.writeByteArrayToFile(f, data);
    EventMetadata em = new EventMetadata();
    logger.debug("Reading stream with EventMetadata advice: {}", em.get(DO_CHECKSUMS));
    assertRead(txfr, data, em, false, false);
    em = new EventMetadata().set(DO_CHECKSUMS, ChecksummingDecoratorAdvisor.ChecksumAdvice.CALCULATE_NO_WRITE);
    logger.debug("Reading stream with EventMetadata advice: {}", em.get(DO_CHECKSUMS));
    assertRead(txfr, data, em, false, true);
    logger.debug("Removing checksum metadata from consumer");
    metadataConsumer.removeMetadata(txfr);
    em = new EventMetadata().set(DO_CHECKSUMS, ChecksummingDecoratorAdvisor.ChecksumAdvice.CALCULATE_AND_WRITE);
    logger.debug("Reading stream with EventMetadata advice: {}", em.get(DO_CHECKSUMS));
    assertRead(txfr, data, em, true, true);
}
Also used : TestDecoratorAdvisor(org.commonjava.maven.galley.io.checksum.testutil.TestDecoratorAdvisor) Transfer(org.commonjava.maven.galley.model.Transfer) ConcreteResource(org.commonjava.maven.galley.model.ConcreteResource) ChecksummingTransferDecorator(org.commonjava.maven.galley.io.ChecksummingTransferDecorator) SimpleLocation(org.commonjava.maven.galley.model.SimpleLocation) File(java.io.File) SpecialPathManagerImpl(org.commonjava.maven.galley.io.SpecialPathManagerImpl) EventMetadata(org.commonjava.maven.galley.event.EventMetadata) Test(org.junit.Test)

Example 4 with ChecksummingTransferDecorator

use of org.commonjava.maven.galley.io.ChecksummingTransferDecorator in project galley by Commonjava.

the class ChecksummingTransferDecoratorTest method forceChecksumOnReadWhenChecksumsAreDisabledForReads.

@Test
public void forceChecksumOnReadWhenChecksumsAreDisabledForReads() throws Exception {
    fixture.setDecorator(new ChecksummingTransferDecorator(Collections.<TransferOperation>emptySet(), new SpecialPathManagerImpl(), false, false, metadataConsumer, new Md5GeneratorFactory()));
    fixture.initMissingComponents();
    fixture.getCache().startReporting();
    String path = "my-path.txt";
    final Transfer txfr = fixture.getCache().getTransfer(new ConcreteResource(new SimpleLocation("test:uri"), path));
    File f = new File(temp.getRoot(), "cache/test:uri");
    f = new File(f, path);
    byte[] data = "This is a test with a bunch of data and some other stuff, in a big box sealed with chewing gum".getBytes();
    FileUtils.writeByteArrayToFile(f, data);
    logger.info("Opening transfer input stream");
    EventMetadata forceEventMetadata = new EventMetadata().set(FORCE_CHECKSUM, TRUE);
    try (InputStream stream = txfr.openInputStream(false, forceEventMetadata)) {
        logger.info("Reading stream");
        byte[] resultData = IOUtils.toByteArray(stream);
        logger.debug("Result is {} bytes", resultData.length);
        assertThat(Arrays.equals(resultData, data), equalTo(true));
    }
    final MessageDigest md = MessageDigest.getInstance("MD5");
    md.update(data);
    final byte[] digest = md.digest();
    final String digestHex = Hex.encodeHexString(digest);
    logger.debug("Verifying MD5 in metadata consumer");
    TransferMetadata metadata = metadataConsumer.getMetadata(txfr);
    assertThat(metadata, notNullValue());
    Map<ContentDigest, String> digests = metadata.getDigests();
    assertThat(digests, CoreMatchers.<Map<ContentDigest, String>>notNullValue());
    assertThat(digests.get(MD5), equalTo(digestHex));
}
Also used : InputStream(java.io.InputStream) SpecialPathManagerImpl(org.commonjava.maven.galley.io.SpecialPathManagerImpl) EventMetadata(org.commonjava.maven.galley.event.EventMetadata) Transfer(org.commonjava.maven.galley.model.Transfer) ConcreteResource(org.commonjava.maven.galley.model.ConcreteResource) ChecksummingTransferDecorator(org.commonjava.maven.galley.io.ChecksummingTransferDecorator) SimpleLocation(org.commonjava.maven.galley.model.SimpleLocation) MessageDigest(java.security.MessageDigest) TransferOperation(org.commonjava.maven.galley.model.TransferOperation) File(java.io.File) Test(org.junit.Test)

Example 5 with ChecksummingTransferDecorator

use of org.commonjava.maven.galley.io.ChecksummingTransferDecorator 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

ChecksummingTransferDecorator (org.commonjava.maven.galley.io.ChecksummingTransferDecorator)5 File (java.io.File)4 EventMetadata (org.commonjava.maven.galley.event.EventMetadata)3 SpecialPathManagerImpl (org.commonjava.maven.galley.io.SpecialPathManagerImpl)3 ConcreteResource (org.commonjava.maven.galley.model.ConcreteResource)3 SimpleLocation (org.commonjava.maven.galley.model.SimpleLocation)3 Transfer (org.commonjava.maven.galley.model.Transfer)3 TransferOperation (org.commonjava.maven.galley.model.TransferOperation)3 Test (org.junit.Test)3 InputStream (java.io.InputStream)2 PostConstruct (javax.annotation.PostConstruct)2 Md5GeneratorFactory (org.commonjava.maven.galley.io.checksum.Md5GeneratorFactory)2 Sha1GeneratorFactory (org.commonjava.maven.galley.io.checksum.Sha1GeneratorFactory)2 FilePatternMatcher (org.commonjava.maven.galley.model.FilePatternMatcher)2 SpecialPathInfo (org.commonjava.maven.galley.model.SpecialPathInfo)2 MessageDigest (java.security.MessageDigest)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Optional (java.util.Optional)1 ExecutorService (java.util.concurrent.ExecutorService)1