Search in sources :

Example 1 with ArtifactPathInfo

use of org.commonjava.atlas.maven.ident.util.ArtifactPathInfo in project indy by Commonjava.

the class MavenContentAdvisor method getContentQuality.

@Override
public ContentQuality getContentQuality(String path) {
    final SpecialPathInfo info = specialPathManager.getSpecialPathInfo(path);
    if (info != null && info.isMetadata()) {
        return ContentQuality.METADATA;
    }
    final ArtifactPathInfo pathInfo = ArtifactPathInfo.parse(path);
    if (pathInfo != null) {
        if (pathInfo.isSnapshot()) {
            return ContentQuality.SNAPSHOT;
        }
        return ContentQuality.RELEASE;
    }
    return null;
}
Also used : SpecialPathInfo(org.commonjava.maven.galley.model.SpecialPathInfo) ArtifactPathInfo(org.commonjava.atlas.maven.ident.util.ArtifactPathInfo)

Example 2 with ArtifactPathInfo

use of org.commonjava.atlas.maven.ident.util.ArtifactPathInfo in project indy by Commonjava.

the class MavenMetadataGenerator method writeVersionMetadata.

private boolean writeVersionMetadata(final List<StoreResource> firstLevelFiles, final ArtifactStore store, final String path, final EventMetadata eventMetadata) throws IndyWorkflowException {
    ArtifactPathInfo samplePomInfo = null;
    logger.debug("writeVersionMetadata, firstLevelFiles:{}, store:{}", firstLevelFiles, store.getKey());
    // first level will contain version directories...for each directory, we need to verify the presence of a .pom file before including
    // as a valid version
    final List<SingleVersion> versions = new ArrayList<>();
    nextTopResource: for (final StoreResource topResource : firstLevelFiles) {
        final String topPath = topResource.getPath();
        if (topPath.endsWith("/")) {
            final List<StoreResource> secondLevelListing = fileManager.listRaw(store, topPath);
            for (final StoreResource fileResource : secondLevelListing) {
                if (fileResource.getPath().endsWith(".pom")) {
                    ArtifactPathInfo filePomInfo = ArtifactPathInfo.parse(fileResource.getPath());
                    // check if the pom is valid for the path
                    if (filePomInfo != null) {
                        versions.add(VersionUtils.createSingleVersion(new File(topPath).getName()));
                        if (samplePomInfo == null) {
                            samplePomInfo = filePomInfo;
                        }
                        continue nextTopResource;
                    }
                }
            }
        }
    }
    if (versions.isEmpty()) {
        logger.debug("writeVersionMetadata, versions is empty, store:{}", store.getKey());
        return false;
    }
    logger.debug("writeVersionMetadata, versions: {}, store:{}", versions, store.getKey());
    Collections.sort(versions);
    final Transfer metadataFile = fileManager.getTransfer(store, path);
    OutputStream stream = null;
    try {
        final Document doc = xml.newDocumentBuilder().newDocument();
        final Map<String, String> coordMap = new HashMap<>();
        coordMap.put(ARTIFACT_ID, samplePomInfo == null ? null : samplePomInfo.getArtifactId());
        coordMap.put(GROUP_ID, samplePomInfo == null ? null : samplePomInfo.getGroupId());
        final String lastUpdated = generateUpdateTimestamp(getCurrentTimestamp());
        doc.appendChild(doc.createElementNS(doc.getNamespaceURI(), "metadata"));
        xml.createElement(doc.getDocumentElement(), null, coordMap);
        final Map<String, String> versioningMap = new HashMap<>();
        versioningMap.put(LAST_UPDATED, lastUpdated);
        final SingleVersion latest = versions.get(versions.size() - 1);
        versioningMap.put(LATEST, latest.renderStandard());
        SingleVersion release = null;
        for (int i = versions.size() - 1; i >= 0; i--) {
            final SingleVersion r = versions.get(i);
            if (r.isRelease()) {
                release = r;
                break;
            }
        }
        if (release != null) {
            versioningMap.put(RELEASE, release.renderStandard());
        }
        xml.createElement(doc, "versioning", versioningMap);
        final Element versionsElem = xml.createElement(doc, "versioning/versions", Collections.emptyMap());
        for (final SingleVersion version : versions) {
            final Element vElem = doc.createElement(VERSION);
            vElem.setTextContent(version.renderStandard());
            versionsElem.appendChild(vElem);
        }
        final String xmlStr = xml.toXML(doc, true);
        logger.debug("writeVersionMetadata, xmlStr: {}", xmlStr);
        stream = metadataFile.openOutputStream(TransferOperation.GENERATE, true, eventMetadata);
        stream.write(xmlStr.getBytes(UTF_8));
    } catch (final GalleyMavenXMLException e) {
        throw new IndyWorkflowException("Failed to generate maven metadata file: %s. Reason: %s", e, path, e.getMessage());
    } catch (final IOException e) {
        throw new IndyWorkflowException("Failed to write generated maven metadata file: %s. Reason: %s", e, metadataFile, e.getMessage());
    } finally {
        closeQuietly(stream);
    }
    logger.debug("writeVersionMetadata, DONE, store: {}", store.getKey());
    return true;
}
Also used : HashMap(java.util.HashMap) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) GalleyMavenXMLException(org.commonjava.maven.galley.maven.parse.GalleyMavenXMLException) IOException(java.io.IOException) Document(org.w3c.dom.Document) StoreResource(org.commonjava.indy.content.StoreResource) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) ArtifactPathInfo(org.commonjava.atlas.maven.ident.util.ArtifactPathInfo) Transfer(org.commonjava.maven.galley.model.Transfer) List(java.util.List) ArrayList(java.util.ArrayList) SingleVersion(org.commonjava.atlas.maven.ident.version.SingleVersion) File(java.io.File)

Example 3 with ArtifactPathInfo

use of org.commonjava.atlas.maven.ident.util.ArtifactPathInfo in project indy by Commonjava.

the class KojiUtils method isVersionSignatureAllowedWithPath.

public boolean isVersionSignatureAllowedWithPath(String path) {
    final ArtifactPathInfo pathInfo = ArtifactPathInfo.parse(path);
    // skip those files without standard GAV format path
    if (pathInfo == null) {
        return true;
    }
    ProjectVersionRef versionRef = pathInfo.getProjectId();
    return isVersionSignatureAllowedWithVersion(versionRef.getVersionStringRaw());
}
Also used : ProjectVersionRef(org.commonjava.atlas.maven.ident.ref.ProjectVersionRef) ArtifactPathInfo(org.commonjava.atlas.maven.ident.util.ArtifactPathInfo)

Example 4 with ArtifactPathInfo

use of org.commonjava.atlas.maven.ident.util.ArtifactPathInfo in project indy by Commonjava.

the class MavenMetadataGenerator method writeSnapshotMetadata.

/**
 * First level will contain files that have the timestamp-buildNumber version suffix, e.g., 'o11yphant-metrics-api-1.0-20200805.065728-1.pom'
 * we need to parse each this info and add them to snapshot versions.
 */
private boolean writeSnapshotMetadata(final ArtifactPathInfo info, final List<StoreResource> firstLevelFiles, final ArtifactStore store, final String path, final EventMetadata eventMetadata) throws IndyWorkflowException {
    final Map<SnapshotPart, Set<ArtifactPathInfo>> infosBySnap = new HashMap<>();
    for (final StoreResource resource : firstLevelFiles) {
        final ArtifactPathInfo resInfo = ArtifactPathInfo.parse(resource.getPath());
        if (resInfo != null) {
            final SnapshotPart snap = resInfo.getSnapshotInfo();
            Set<ArtifactPathInfo> infos = infosBySnap.computeIfAbsent(snap, k -> new HashSet<>());
            infos.add(resInfo);
        }
    }
    if (infosBySnap.isEmpty()) {
        return false;
    }
    final List<SnapshotPart> snaps = new ArrayList<>(infosBySnap.keySet());
    Collections.sort(snaps);
    final Transfer metadataFile = fileManager.getTransfer(store, path);
    OutputStream stream = null;
    try {
        final Document doc = xml.newDocumentBuilder().newDocument();
        final Map<String, String> coordMap = new HashMap<>();
        coordMap.put(ARTIFACT_ID, info.getArtifactId());
        coordMap.put(GROUP_ID, info.getGroupId());
        coordMap.put(VERSION, info.getReleaseVersion() + LOCAL_SNAPSHOT_VERSION_PART);
        doc.appendChild(doc.createElementNS(doc.getNamespaceURI(), "metadata"));
        xml.createElement(doc.getDocumentElement(), null, coordMap);
        // the last one is the most recent
        SnapshotPart snap = snaps.get(snaps.size() - 1);
        Map<String, String> snapMap = new HashMap<>();
        if (snap.isLocalSnapshot()) {
            snapMap.put(LOCAL_COPY, Boolean.TRUE.toString());
        } else {
            snapMap.put(TIMESTAMP, SnapshotUtils.generateSnapshotTimestamp(snap.getTimestamp()));
            snapMap.put(BUILD_NUMBER, Integer.toString(snap.getBuildNumber()));
        }
        final Date currentTimestamp = getCurrentTimestamp();
        final String lastUpdated = getUpdateTimestamp(snap, currentTimestamp);
        xml.createElement(doc, "versioning", Collections.singletonMap(LAST_UPDATED, lastUpdated));
        xml.createElement(doc, "versioning/snapshot", snapMap);
        for (SnapshotPart snap1 : snaps) {
            final Set<ArtifactPathInfo> infos = infosBySnap.get(snap1);
            for (final ArtifactPathInfo pathInfo : infos) {
                snapMap = new HashMap<>();
                final TypeAndClassifier tc = new SimpleTypeAndClassifier(pathInfo.getType(), pathInfo.getClassifier());
                final TypeMapping mapping = typeMapper.lookup(tc);
                final String classifier = mapping == null ? pathInfo.getClassifier() : mapping.getClassifier();
                if (classifier != null && classifier.length() > 0) {
                    snapMap.put(CLASSIFIER, classifier);
                }
                snapMap.put(EXTENSION, mapping == null ? pathInfo.getType() : mapping.getExtension());
                snapMap.put(VALUE, pathInfo.getVersion());
                snapMap.put(UPDATED, getUpdateTimestamp(pathInfo.getSnapshotInfo(), currentTimestamp));
                xml.createElement(doc, "versioning/snapshotVersions/snapshotVersion", snapMap);
            }
        }
        final String xmlStr = xml.toXML(doc, true);
        stream = metadataFile.openOutputStream(TransferOperation.GENERATE, true, eventMetadata);
        stream.write(xmlStr.getBytes(UTF_8));
    } catch (final GalleyMavenXMLException e) {
        throw new IndyWorkflowException("Failed to generate maven metadata file: %s. Reason: %s", e, path, e.getMessage());
    } catch (final IOException e) {
        throw new IndyWorkflowException("Failed to write generated maven metadata file: %s. Reason: %s", e, metadataFile, e.getMessage());
    } finally {
        closeQuietly(stream);
    }
    return true;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) GalleyMavenXMLException(org.commonjava.maven.galley.maven.parse.GalleyMavenXMLException) IOException(java.io.IOException) Document(org.w3c.dom.Document) Date(java.util.Date) StoreResource(org.commonjava.indy.content.StoreResource) SnapshotPart(org.commonjava.atlas.maven.ident.version.part.SnapshotPart) TypeAndClassifier(org.commonjava.atlas.maven.ident.ref.TypeAndClassifier) SimpleTypeAndClassifier(org.commonjava.atlas.maven.ident.ref.SimpleTypeAndClassifier) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) ArtifactPathInfo(org.commonjava.atlas.maven.ident.util.ArtifactPathInfo) Transfer(org.commonjava.maven.galley.model.Transfer) SimpleTypeAndClassifier(org.commonjava.atlas.maven.ident.ref.SimpleTypeAndClassifier) TypeMapping(org.commonjava.maven.galley.model.TypeMapping)

Example 5 with ArtifactPathInfo

use of org.commonjava.atlas.maven.ident.util.ArtifactPathInfo in project indy by Commonjava.

the class FoloPomDownloadListener method onFileUpload.

public void onFileUpload(@Observes final FileStorageEvent event) {
    // check for a TransferOperation of DOWNLOAD
    final TransferOperation op = event.getType();
    if (op != TransferOperation.DOWNLOAD) {
        logger.trace("Not a download transfer operation. No pom existence check performed.");
        return;
    }
    // check if it is a path that doesn't end in with ".pom"
    final Transfer transfer = event.getTransfer();
    if (transfer == null) {
        logger.trace("No transfer. No pom existence check performed.");
        return;
    }
    String txfrPath = transfer.getPath();
    if (txfrPath.endsWith(".pom")) {
        logger.trace("This is a pom download.");
        return;
    }
    // use ArtifactPathInfo to parse into a GAV, just to verify that it's looking at an artifact download
    ArtifactPathInfo artPathInfo = ArtifactPathInfo.parse(txfrPath);
    if (artPathInfo == null) {
        logger.trace("Not an artifact download ({}). No pom existence check performed.", txfrPath);
        return;
    }
    // verify that the associated .pom file exists
    String pomFilename = String.format("%s-%s.pom", artPathInfo.getArtifactId(), artPathInfo.getVersion());
    ConcreteResource pomResource = transfer.getResource().getParent().getChild(pomFilename);
    if (cacheProvider.exists(pomResource)) {
        logger.trace("Pom {} already exists.", cacheProvider.getFilePath(pomResource));
        return;
    }
    // trigger pom download by requesting it from the same repository as the original artifact
    StoreKey storeKey = StoreKey.fromString(transfer.getLocation().getName());
    ArtifactStore store;
    try {
        store = storeManager.getArtifactStore(storeKey);
    } catch (final IndyDataException ex) {
        logger.error("Error retrieving artifactStore with key " + storeKey, ex);
        return;
    }
    try {
        logger.debug("Downloading POM as automatic response to associated artifact download: {}/{}", storeKey, pomResource.getPath());
        contentManager.retrieve(store, pomResource.getPath(), event.getEventMetadata());
    } catch (final IndyWorkflowException ex) {
        logger.error("Error while retrieving pom artifact " + pomResource.getPath() + " from store " + store, ex);
        return;
    }
}
Also used : IndyDataException(org.commonjava.indy.data.IndyDataException) ArtifactStore(org.commonjava.indy.model.core.ArtifactStore) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) Transfer(org.commonjava.maven.galley.model.Transfer) ArtifactPathInfo(org.commonjava.atlas.maven.ident.util.ArtifactPathInfo) ConcreteResource(org.commonjava.maven.galley.model.ConcreteResource) TransferOperation(org.commonjava.maven.galley.model.TransferOperation) StoreKey(org.commonjava.indy.model.core.StoreKey)

Aggregations

ArtifactPathInfo (org.commonjava.atlas.maven.ident.util.ArtifactPathInfo)8 IndyWorkflowException (org.commonjava.indy.IndyWorkflowException)5 StoreResource (org.commonjava.indy.content.StoreResource)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 IOException (java.io.IOException)3 OutputStream (java.io.OutputStream)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Transfer (org.commonjava.maven.galley.model.Transfer)3 Measure (org.commonjava.o11yphant.metrics.annotation.Measure)3 File (java.io.File)2 Date (java.util.Date)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Set (java.util.Set)2 SimpleTypeAndClassifier (org.commonjava.atlas.maven.ident.ref.SimpleTypeAndClassifier)2 TypeAndClassifier (org.commonjava.atlas.maven.ident.ref.TypeAndClassifier)2 GalleyMavenXMLException (org.commonjava.maven.galley.maven.parse.GalleyMavenXMLException)2 Document (org.w3c.dom.Document)2 InputStream (java.io.InputStream)1