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

com.metaeffekt.mirror.contents.msrcdata.MsrcSupersedeNodeRelations Maven / Gradle / Ivy

There is a newer version: 0.132.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 com.metaeffekt.mirror.contents.msrcdata;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

public class MsrcSupersedeNodeRelations {
    private final Set affectsVulnerabilities = new HashSet<>();
    private final Set supersededBy = new HashSet<>();
    private final Set supersedes = new HashSet<>();

    public void addAffectsVulnerability(String vid) {
        affectsVulnerabilities.add(vid);
    }

    public void addAffectsVulnerability(Collection vids) {
        affectsVulnerabilities.addAll(vids);
    }

    public void addSupersededBy(MsrcSupersedeNode node) {
        supersededBy.add(node);
    }

    public void addSupersededBy(Collection supersededBy) {
        this.supersededBy.addAll(supersededBy);
    }

    public void addSupersedes(MsrcSupersedeNode node) {
        supersedes.add(node);
    }

    public void addSupersedes(Collection supersedes) {
        this.supersedes.addAll(supersedes);
    }

    public Set getAffectsVulnerabilities() {
        return affectsVulnerabilities;
    }

    public Set getSupersededBy() {
        return supersededBy;
    }

    public Set getSupersedes() {
        return supersedes;
    }

    public JSONObject toJson() {
        final JSONObject json = new JSONObject();
        json.put("vuln", new JSONArray(affectsVulnerabilities));
        json.put("supBy", new JSONArray(supersededBy.stream().map(MsrcSupersedeNode::getKbId).collect(Collectors.toList())));
        json.put("sup", new JSONArray(supersedes.stream().map(MsrcSupersedeNode::getKbId).collect(Collectors.toList())));
        return json;
    }

    public static MsrcSupersedeNodeRelations fromJson(JSONObject json, Map nodes) {
        final MsrcSupersedeNodeRelations node = new MsrcSupersedeNodeRelations();

        node.addAffectsVulnerability(json.getJSONArray("vuln").toList().stream().map(Object::toString).collect(Collectors.toList()));
        node.addSupersededBy(json.getJSONArray("supBy").toList().stream()
                .map(Object::toString)
                .map(s -> nodes.computeIfAbsent(s, MsrcSupersedeNode::new))
                .collect(Collectors.toList()));
        node.addSupersedes(json.getJSONArray("sup").toList().stream()
                .map(Object::toString)
                .map(s -> nodes.computeIfAbsent(s, MsrcSupersedeNode::new))
                .collect(Collectors.toList()));

        return node;
    }

    @Override
    public String toString() {
        return toJson().toString();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy