Search in sources :

Example 26 with SpecialPathInfo

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

the class PathMappedMavenGACacheGroupRepositoryFilter method getGAPath.

private String getGAPath(String rawPath) {
    if (isBlank(rawPath)) {
        return null;
    }
    Path gaPath = null;
    Path parent = Paths.get(rawPath).getParent();
    if (parent != null) {
        SpecialPathInfo pathInfo = specialPathManager.getSpecialPathInfo(rawPath);
        if (pathInfo != null && pathInfo.isMetadata()) {
            // Metadata may be at two levels, e.g., foo/bar/maven-metadata.xml, foo/bar/3.0.0-SNAPSHOT/maven-metadata.xml
            if (parent.endsWith(LOCAL_SNAPSHOT_VERSION_PART)) {
                gaPath = parent.getParent();
            } else {
                gaPath = parent;
            }
        } else {
            // gaPath will be two layers upwards, e.g., foo/bar/3.0.0/bar-3.0.0.pom
            gaPath = parent.getParent();
        }
    }
    if (gaPath != null) {
        return gaPath.toString();
    }
    return null;
}
Also used : Path(java.nio.file.Path) SpecialPathInfo(org.commonjava.maven.galley.model.SpecialPathInfo)

Example 27 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<>();
    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) {
                            // this will avoid some package.json redirect path empty errors, especially for the group.
                            if (metadata.get(STORAGE_PATH) != null) {
                                filenames.add(fname);
                                continue;
                            }
                            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 && remoteListing.length > 0) {
                    final TransferDecorator decorator = cachedListing.getDecorator();
                    if (decorator != null) {
                        try {
                            Logger logger = LoggerFactory.getLogger(getClass());
                            // noinspection ConfusingArgumentToVarargsMethod
                            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 && remoteListing.length > 0) {
                    if (transport != null && 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<>(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 28 with SpecialPathInfo

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

the class TransferManagerImpl method store.

@Override
public Transfer store(ConcreteResource resource, final InputStream stream, final EventMetadata eventMetadata) throws TransferException {
    if (eventMetadata.get(STORAGE_PATH) != null) {
        resource = ResourceUtils.storageResource(resource, eventMetadata);
    }
    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);
        nfc.clearMissing(resource);
    } 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 29 with SpecialPathInfo

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

the class NoCacheTransferDecorator method decorateRead.

@SuppressWarnings("RedundantThrows")
@Override
public InputStream decorateRead(final InputStream stream, final Transfer transfer, final EventMetadata eventMetadata) throws IOException {
    SpecialPathInfo specialPathInfo = specialPathManager.getSpecialPathInfo(transfer, eventMetadata.getPackageType());
    logger.trace("SpecialPathInfo for: {} is: {} (cachable? {})", transfer, specialPathInfo, (specialPathInfo == null || specialPathInfo.isCachable()));
    if (specialPathInfo != null && !specialPathInfo.isCachable()) {
        logger.trace("Decorating read with NoCacheTransferDecorator for: {}", transfer);
        return new NoCacheInputStream(stream, transfer);
    }
    return stream;
}
Also used : NoCacheInputStream(org.commonjava.maven.galley.io.nocache.NoCacheInputStream) SpecialPathInfo(org.commonjava.maven.galley.model.SpecialPathInfo)

Example 30 with SpecialPathInfo

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

the class UploadMetadataGenTransferDecorator method decorateWrite.

@Override
public OutputStream decorateWrite(OutputStream stream, Transfer transfer, TransferOperation op, EventMetadata metadata) throws IOException {
    final SpecialPathInfo specialPathInfo = specialPathManager.getSpecialPathInfo(transfer, metadata.getPackageType());
    final Boolean isMetadata = specialPathInfo != null && specialPathInfo.isMetadata();
    final Boolean isUpload = op == TransferOperation.UPLOAD;
    final Boolean hasRequestHeaders = metadata.get(STORE_HTTP_HEADERS) != null && metadata.get(STORE_HTTP_HEADERS) instanceof Map;
    // We only care about non-metadata artifact uploading for http-metadata generation
    if (isUpload && !isMetadata && hasRequestHeaders) {
        @SuppressWarnings("unchecked") Map<String, List<String>> storeHttpHeaders = (Map<String, List<String>>) metadata.get(STORE_HTTP_HEADERS);
        writeMetadata(transfer, new ObjectMapper(), storeHttpHeaders);
    }
    return super.decorateWrite(stream, transfer, op, metadata);
}
Also used : SpecialPathInfo(org.commonjava.maven.galley.model.SpecialPathInfo) List(java.util.List) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

SpecialPathInfo (org.commonjava.maven.galley.model.SpecialPathInfo)30 Transfer (org.commonjava.maven.galley.model.Transfer)10 IOException (java.io.IOException)7 ConcreteResource (org.commonjava.maven.galley.model.ConcreteResource)6 IndyWorkflowException (org.commonjava.indy.IndyWorkflowException)5 TransferException (org.commonjava.maven.galley.TransferException)5 Logger (org.slf4j.Logger)5 InputStream (java.io.InputStream)4 ArrayList (java.util.ArrayList)4 IndyDataException (org.commonjava.indy.data.IndyDataException)4 RemoteRepository (org.commonjava.indy.model.core.RemoteRepository)4 StoreKey (org.commonjava.indy.model.core.StoreKey)4 StoreType (org.commonjava.indy.model.core.StoreType)4 List (java.util.List)3 Map (java.util.Map)3 ArtifactStore (org.commonjava.indy.model.core.ArtifactStore)3 EventMetadata (org.commonjava.maven.galley.event.EventMetadata)3 OutputStream (java.io.OutputStream)2 HashSet (java.util.HashSet)2 PostConstruct (javax.annotation.PostConstruct)2