All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.metaeffekt.artifact.resolver.rpm.RpmArtifactResolver Maven / Gradle / Ivy

There is a newer version: 0.134.0
Show newest version
/*
 * Copyright 2021-2024 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.metaeffekt.artifact.resolver.rpm;

import com.github.packageurl.MalformedPackageURLException;
import com.github.packageurl.PackageURL;
import lombok.NonNull;
import org.apache.commons.lang3.StringUtils;
import org.metaeffekt.artifact.resolver.ArtifactResolver;
import org.metaeffekt.artifact.resolver.download.WebAccess;
import org.metaeffekt.artifact.resolver.model.*;
import org.metaeffekt.artifact.resolver.rpm.index.RpmIndexConfig;
import org.metaeffekt.core.inventory.processor.model.Artifact;

import java.io.File;
import java.util.Collection;
import java.util.HashSet;

import static org.metaeffekt.artifact.resolver.model.ArtifactPartType.BINARY_ARTIFACT;
import static org.metaeffekt.artifact.resolver.model.ArtifactPartType.SOURCE_ARTIFACT;

public class RpmArtifactResolver implements ArtifactResolver {

    private RpmRepositoryAdapter repositoryAdapter;

    public RpmArtifactResolver(DownloadLocation downloadLocation, WebAccess webAccess, RpmIndexConfig rpmIndexConfig) {
        this.repositoryAdapter = new RpmRepositoryAdapter(downloadLocation, webAccess, rpmIndexConfig);
    }

    @Override
    public ArtifactPartResolvers collectResolvers(@NonNull Artifact artifact) {
        final Collection parts = new HashSet<>();
        addArtifactResolverPartForPurl(parts, artifact);
        addArtifactResolverPartForNV(parts, artifact);
        return new ArtifactPartResolvers(parts);
    }

    /**
     * Adds artifact resolver parts based on Name-Version.
     *
     * @param parts    modifiable collection to add to
     * @param artifact artifact to process
     */
    private void addArtifactResolverPartForNV(Collection parts, Artifact artifact) {
        addPartResolvers(artifact, parts, new RpmArtifactReference(artifact));
    }

    private void addPartResolvers(Artifact artifact, Collection parts, RpmArtifactReference artifactReference) {
        if (artifactReference.isValid()) {
            parts.add(new ArtifactPartResolver(artifact, BINARY_ARTIFACT,
                    // supplier
                    () -> repositoryAdapter.resolveBinaryArtifact(artifactReference),
                    // enricher
                    arp -> enrich(artifactReference, arp))
            );
            parts.add(new ArtifactPartResolver(artifact, SOURCE_ARTIFACT,
                    // supplier
                    () -> repositoryAdapter.resolveSourceArtifact(artifactReference),
                    // enricher
                    arp -> enrich(artifactReference, arp))
            );
        }
    }

    private Artifact enrich(RpmArtifactReference reference, ResolvedArtifactPart resolverArtifactPart) {

        final Artifact enrichedArtifact = new Artifact(resolverArtifactPart.getOriginalArtifact());

        // NOTE: we take the information as specified by the reference
        enrichedArtifact.setVersion(reference.getVersion());
        enrichedArtifact.setId(reference.getName() + "-" + reference.getVersion());

        // consumer resolved file
        final File resolvedFile = resolverArtifactPart.getResolvedFile();
        if (resolvedFile != null) {
            final ArtifactPartType artifactPartType = resolverArtifactPart.getArtifactPartType();
            enrichedArtifact.set(artifactPartType.modulatePathAttribute(), resolvedFile.getAbsolutePath());
            final Artifact metadataArtifact = repositoryAdapter.extractMetaData(resolvedFile);
            enrichedArtifact.merge(metadataArtifact);
        }

        return enrichedArtifact;
    }

    /**
     * Adds resolver parts based on a derived purl.
     *
     * @param parts    modifiable collection to add to
     * @param artifact artifact to process
     */
    private void addArtifactResolverPartForPurl(Collection parts, Artifact artifact) {
        final String purl = artifact.get("PURL");
        if (!StringUtils.isEmpty(purl)) {
            try {
                final RpmArtifactReference artifactReference = new RpmArtifactReference(new PackageURL(purl));
                addPartResolvers(artifact, parts, artifactReference);
            } catch (MalformedPackageURLException e) {
                artifact.append("Errors", String.format("[PURL [%s] malformed.]", purl), "\n");
            }
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy