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

org.metaeffekt.artifact.resolver.npm.NpmArtifactReference Maven / Gradle / Ivy

/*
 * 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.npm;

import com.github.packageurl.PackageURL;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;
import org.metaeffekt.artifact.resolver.generic.FileLocation;
import org.metaeffekt.core.inventory.processor.model.Artifact;
import org.metaeffekt.core.inventory.processor.model.Constants;

public class NpmArtifactReference {

    @Getter
    private String group;

    @Getter
    private String name;

    @Getter
    private String version;

    public NpmArtifactReference(String group, String name, String version) {
        this.group = group;
        this.name = name;
        this.version = version;
    }

    public NpmArtifactReference(PackageURL packageURL) {
        if (packageURL.getType().equalsIgnoreCase(PackageURL.StandardTypes.NPM)) {
            group = packageURL.getNamespace();
            name = packageURL.getName();
            version = packageURL.getVersion();
        } else {
            group = null;
            name = null;
            version = null;
        }
    }

    public NpmArtifactReference(Artifact artifact) {
        if (!Constants.ARTIFACT_TYPE_NODEJS_MODULE.equals(artifact.get(Constants.KEY_TYPE))) return;

        final String id = artifact.getId();
        if (id != null) {
            final int slashEndIndex = id.lastIndexOf("/");
            version = artifact.getVersion();
            if (slashEndIndex != -1) {
                group = id.substring(0, slashEndIndex);
                name = id.substring(slashEndIndex + 1).replace("-" + version, "");
            } else {
                group = null;
                name = id.replace("-" + version, "");
            }
        } else {
            group = null;
            name = null;
            version = null;
        }
    }

    public String deriveUrlPart() {
        if (StringUtils.isNotEmpty(group)) {
            return String.format("%s/%s/-/%s-%s.tgz", group, name, name, version);
        } else {
            return String.format("%s/-/%s-%s.tgz", name, name, version);
        }
    }

    public boolean isValid() {
        return StringUtils.isNotEmpty(name) && StringUtils.isNotEmpty(version);
    }

    public String deriveDownloadFileName() {
        return String.format("%s-%s.tgz", name, version);
    }

    public FileLocation deriveFileLocation() {
        final String filename = deriveDownloadFileName();

        final FileLocation fileLocation = new FileLocation();
        fileLocation.setEcosystem("npm");
        fileLocation.setFilename(filename);
        if (StringUtils.isBlank(group)) {
            fileLocation.setResourceQualifier(filename);
        } else {
            fileLocation.setResourceQualifier(String.format("%s-%s", group, filename));
        }
        if (StringUtils.isBlank(group)) {
            fileLocation.setDispatchName(name);
        } else {
            fileLocation.setDispatchName(String.format("%s-%s", group, name));
        }
        return fileLocation;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy