/*
 * This file is part of Dependency-Track.
 *
 * 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.
 *
 * SPDX-License-Identifier: Apache-2.0
 * Copyright (c) Steve Springett. All Rights Reserved.
 */
package org.dependencytrack.resources.v1;

import alpine.persistence.PaginatedResult;
import alpine.server.auth.PermissionRequired;
import alpine.server.resources.AlpineResource;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import io.swagger.annotations.Authorization;
import io.swagger.annotations.ResponseHeader;
import org.dependencytrack.auth.Permissions;
import org.dependencytrack.model.AffectedVersionAttribution;
import org.dependencytrack.model.Component;
import org.dependencytrack.model.Cwe;
import org.dependencytrack.model.Project;
import org.dependencytrack.model.Vulnerability;
import org.dependencytrack.model.VulnerableSoftware;
import org.dependencytrack.persistence.QueryManager;
import org.dependencytrack.resources.v1.vo.AffectedComponent;
import org.dependencytrack.tasks.scanners.AnalyzerIdentity;
import org.dependencytrack.util.VulnerabilityUtil;
import us.springett.cvss.Cvss;
import us.springett.cvss.Score;
import us.springett.owasp.riskrating.MissingFactorException;
import us.springett.owasp.riskrating.OwaspRiskRating;

import javax.validation.Validator;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * JAX-RS resources for processing vulnerabilities.
 *
 * @author Steve Springett
 * @since 3.0.0
 */
@Path("/v1/vulnerability")
@Api(value = "vulnerability", authorizations = @Authorization(value = "X-Api-Key"))
public class VulnerabilityResource extends AlpineResource {

    @GET
    @Path("/component/{uuid}")
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(
            value = "Returns a list of all vulnerabilities for a specific component",
            response = Vulnerability.class,
            responseContainer = "List",
            responseHeaders = @ResponseHeader(name = TOTAL_COUNT_HEADER, response = Long.class, description = "The total number of vulnerabilities")
    )
    @ApiResponses(value = {
            @ApiResponse(code = 401, message = "Unauthorized"),
            @ApiResponse(code = 403, message = "Access to the specified component is forbidden"),
            @ApiResponse(code = 404, message = "The component could not be found")
    })
    @PermissionRequired(Permissions.Constants.VIEW_PORTFOLIO)
    public Response getVulnerabilitiesByComponent(@PathParam("uuid") String uuid,
                                                  @ApiParam(value = "Optionally includes suppressed vulnerabilities")
                                                  @QueryParam("suppressed") boolean suppressed) {
        try (QueryManager qm = new QueryManager(getAlpineRequest())) {
            final Component component = qm.getObjectByUuid(Component.class, uuid);
            if (component != null) {
                if (qm.hasAccess(super.getPrincipal(), component.getProject())) {
                    final PaginatedResult result = qm.getVulnerabilities(component, suppressed);
                    return Response.ok(result.getObjects()).header(TOTAL_COUNT_HEADER, result.getTotal()).build();
                } else {
                    return Response.status(Response.Status.FORBIDDEN).entity("Access to the specified component is forbidden").build();
                }
            } else {
                return Response.status(Response.Status.NOT_FOUND).entity("The component could not be found.").build();
            }
        }
    }

    @GET
    @Path("/project/{uuid}")
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(
            value = "Returns a list of all vulnerabilities for a specific project",
            response = Vulnerability.class,
            responseContainer = "List",
            responseHeaders = @ResponseHeader(name = TOTAL_COUNT_HEADER, response = Long.class, description = "The total number of vulnerabilities")
    )
    @ApiResponses(value = {
            @ApiResponse(code = 401, message = "Unauthorized"),
            @ApiResponse(code = 403, message = "Access to the specified project is forbidden"),
            @ApiResponse(code = 404, message = "The project could not be found")
    })
    @PermissionRequired(Permissions.Constants.VIEW_PORTFOLIO)
    public Response getVulnerabilitiesByProject(@PathParam("uuid") String uuid,
                                                @ApiParam(value = "Optionally includes suppressed vulnerabilities")
                                                @QueryParam("suppressed") boolean suppressed) {
        try (QueryManager qm = new QueryManager(getAlpineRequest())) {
            final Project project = qm.getObjectByUuid(Project.class, uuid);
            if (project != null) {
                if (qm.hasAccess(super.getPrincipal(), project)) {
                    final List<Vulnerability> vulnerabilities = qm.getVulnerabilities(project, suppressed);
                    return Response.ok(vulnerabilities).header(TOTAL_COUNT_HEADER, vulnerabilities.size()).build();
                } else {
                    return Response.status(Response.Status.FORBIDDEN).entity("Access to the specified project is forbidden").build();
                }
            } else {
                return Response.status(Response.Status.NOT_FOUND).entity("The project could not be found.").build();
            }
        }
    }

    @GET
    @Path("/{uuid}")
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(
            value = "Returns a specific vulnerability",
            response = Vulnerability.class
    )
    @ApiResponses(value = {
            @ApiResponse(code = 401, message = "Unauthorized"),
            @ApiResponse(code = 404, message = "The vulnerability could not be found")
    })
    @PermissionRequired(Permissions.Constants.VULNERABILITY_MANAGEMENT)
    public Response getVulnerabilityByUuid(@ApiParam(value = "The UUID of the vulnerability", required = true)
                                             @PathParam("uuid") String uuid) {
        try (QueryManager qm = new QueryManager()) {
            final Vulnerability vulnerability = qm.getObjectByUuid(Vulnerability.class, uuid);
            if (vulnerability != null) {
                return Response.ok(vulnerability).build();
            } else {
                return Response.status(Response.Status.NOT_FOUND).entity("The vulnerability could not be found.").build();
            }
        }
    }

    @GET
    @Path("/source/{source}/vuln/{vuln}")
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(
            value = "Returns a specific vulnerability",
            response = Vulnerability.class
    )
    @ApiResponses(value = {
            @ApiResponse(code = 401, message = "Unauthorized"),
            @ApiResponse(code = 404, message = "The vulnerability could not be found")
    })
    @PermissionRequired(Permissions.Constants.VIEW_PORTFOLIO)
    public Response getVulnerabilityByVulnId(@PathParam("source") String source,
                                             @PathParam("vuln") String vuln) {
        try (QueryManager qm = new QueryManager()) {
            final Vulnerability vulnerability = qm.getVulnerabilityByVulnId(source, vuln);
            if (vulnerability != null) {
                final List<AffectedComponent> affectedComponents = new ArrayList<>();
                for (final VulnerableSoftware vs: vulnerability.getVulnerableSoftware()) {
                    AffectedComponent affectedComponent = new AffectedComponent(vs);
                    final List<AffectedVersionAttribution> attributions = qm.getAffectedVersionAttributions(vulnerability, vs);
                    affectedComponent.setAffectedVersionAttributions(attributions);
                    affectedComponents.add(affectedComponent);
                }
                vulnerability.setAffectedComponents(affectedComponents);
                return Response.ok(vulnerability).build();
            } else {
                return Response.status(Response.Status.NOT_FOUND).entity("The vulnerability could not be found.").build();
            }
        }
    }

    @GET
    @Path("/source/{source}/vuln/{vuln}/projects")
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(
            value = "Returns a list of all projects affected by a specific vulnerability",
            response = Project.class,
            responseContainer = "List",
            responseHeaders = @ResponseHeader(name = TOTAL_COUNT_HEADER, response = Long.class, description = "The total number of projects")
    )
    @ApiResponses(value = {
            @ApiResponse(code = 401, message = "Unauthorized"),
            @ApiResponse(code = 404, message = "The vulnerability could not be found")
    })
    @PermissionRequired(Permissions.Constants.VIEW_PORTFOLIO)
    public Response getAffectedProject(@PathParam("source") String source,
                                                @PathParam("vuln") String vuln) {
        try (QueryManager qm = new QueryManager(getAlpineRequest())) {
            final Vulnerability vulnerability = qm.getVulnerabilityByVulnId(source, vuln);
            if (vulnerability != null) {
                final List<Project> projects = qm.detach(qm.getProjects(vulnerability));
                final long totalCount = projects.size();
                return Response.ok(projects).header(TOTAL_COUNT_HEADER, totalCount).build();
            } else {
                return Response.status(Response.Status.NOT_FOUND).entity("The vulnerability could not be found.").build();
            }
        }
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(
            value = "Returns a list of all vulnerabilities",
            response = Vulnerability.class,
            responseContainer = "List",
            responseHeaders = @ResponseHeader(name = TOTAL_COUNT_HEADER, response = Long.class, description = "The total number of vulnerabilities")
    )
    @ApiResponses(value = {
            @ApiResponse(code = 401, message = "Unauthorized")
    })
    @PermissionRequired(Permissions.Constants.VIEW_PORTFOLIO)
    public Response getAllVulnerabilities() {
        try (QueryManager qm = new QueryManager(getAlpineRequest())) {
            final PaginatedResult result = qm.getVulnerabilities();
            return Response.ok(result.getObjects()).header(TOTAL_COUNT_HEADER, result.getTotal()).build();
        }
    }

    @PUT
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(
            value = "Creates a new vulnerability",
            response = Vulnerability.class,
            code = 201
    )
    @ApiResponses(value = {
            @ApiResponse(code = 401, message = "Unauthorized"),
            @ApiResponse(code = 409, message = "A vulnerability with the specified vulnId already exists")
    })
    @PermissionRequired(Permissions.Constants.VULNERABILITY_MANAGEMENT)
    public Response createVulnerability(Vulnerability jsonVulnerability) {
        final Validator validator = super.getValidator();
        failOnValidationError(
                validator.validateProperty(jsonVulnerability, "vulnId"),
                validator.validateProperty(jsonVulnerability, "title"),
                validator.validateProperty(jsonVulnerability, "subTitle"),
                validator.validateProperty(jsonVulnerability, "description"),
                validator.validateProperty(jsonVulnerability, "recommendation"),
                validator.validateProperty(jsonVulnerability, "references"),
                validator.validateProperty(jsonVulnerability, "credits"),
                validator.validateProperty(jsonVulnerability, "created"),
                validator.validateProperty(jsonVulnerability, "published"),
                validator.validateProperty(jsonVulnerability, "updated"),
                validator.validateProperty(jsonVulnerability, "cvssV2Vector"),
                validator.validateProperty(jsonVulnerability, "cvssV3Vector"),
                validator.validateProperty(jsonVulnerability, "owaspRRVector"),
                validator.validateProperty(jsonVulnerability, "vulnerableVersions"),
                validator.validateProperty(jsonVulnerability, "patchedVersions")
        );

        try (QueryManager qm = new QueryManager()) {
            Vulnerability vulnerability = qm.getVulnerabilityByVulnId(
                    Vulnerability.Source.INTERNAL, jsonVulnerability.getVulnId().trim());
            if (vulnerability == null) {
                final List<Integer> cweIds = new ArrayList<>();
                if (jsonVulnerability.getCwes() != null) {
                    for (int i=0; i<jsonVulnerability.getCwes().size(); i++) {
                        final Cwe cwe = qm.getCweById(jsonVulnerability.getCwes().get(i));
                        if (cwe != null) {
                            cweIds.add(cwe.getCweId());
                        }
                    }
                    jsonVulnerability.setCwes(cweIds);
                }
                final List<VulnerableSoftware> vsList = new ArrayList<>();
                if (jsonVulnerability.getAffectedComponents() != null) {
                    for (final AffectedComponent ac: jsonVulnerability.getAffectedComponents()) {
                        final VulnerableSoftware vs = ac.toVulnerableSoftware();
                        if (vs != null) {
                            vsList.add(vs);
                        }
                    }
                }
                recalculateScoresFromVector(jsonVulnerability);
                jsonVulnerability.setSource(Vulnerability.Source.INTERNAL);
                vulnerability = qm.createVulnerability(jsonVulnerability, true);
                qm.persist(vsList);
                qm.updateAffectedVersionAttributions(vulnerability, vsList, Vulnerability.Source.INTERNAL);
                vulnerability.setVulnerableSoftware(vsList);
                qm.persist(vulnerability);
                return Response.status(Response.Status.CREATED).entity(vulnerability).build();
            } else {
                return Response.status(Response.Status.CONFLICT).entity("A vulnerability with the specified vulnId already exists.").build();
            }
        } catch (MissingFactorException|IllegalArgumentException exception) {
            return Response.status(Response.Status.BAD_REQUEST).entity(exception.getMessage()).build();
        }
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(
            value = "Updates an internal vulnerability",
            response = Project.class
    )
    @ApiResponses(value = {
            @ApiResponse(code = 401, message = "Unauthorized"),
            @ApiResponse(code = 404, message = "The vulnerability could not be found"),
            @ApiResponse(code = 406, message = "The vulnId may not be changed")
    })
    @PermissionRequired(Permissions.Constants.VULNERABILITY_MANAGEMENT)
    public Response updateVulnerability(Vulnerability jsonVuln) {
        final Validator validator = super.getValidator();
        failOnValidationError(
                validator.validateProperty(jsonVuln, "title"),
                validator.validateProperty(jsonVuln, "subTitle"),
                validator.validateProperty(jsonVuln, "description"),
                validator.validateProperty(jsonVuln, "recommendation"),
                validator.validateProperty(jsonVuln, "references"),
                validator.validateProperty(jsonVuln, "credits"),
                validator.validateProperty(jsonVuln, "created"),
                validator.validateProperty(jsonVuln, "published"),
                validator.validateProperty(jsonVuln, "updated"),
                validator.validateProperty(jsonVuln, "cvssV2Vector"),
                validator.validateProperty(jsonVuln, "cvssV3Vector"),
                validator.validateProperty(jsonVuln, "owaspRRVector"),
                validator.validateProperty(jsonVuln, "vulnerableVersions"),
                validator.validateProperty(jsonVuln, "patchedVersions")
        );
        try (QueryManager qm = new QueryManager()) {
            Vulnerability vulnerability = qm.getObjectByUuid(Vulnerability.class, jsonVuln.getUuid());
            if (vulnerability != null && Vulnerability.Source.INTERNAL.name().equals(vulnerability.getSource())) {
                if (!vulnerability.getVulnId().equals(jsonVuln.getVulnId())) {
                    return Response.status(Response.Status.NOT_ACCEPTABLE).entity("The vulnId may not be changed.").build();
                }

                final List<Integer> cweIds = new ArrayList<>();
                if (jsonVuln.getCwes() != null) {
                    for (int i=0; i<jsonVuln.getCwes().size(); i++) {
                        final Cwe cwe = qm.getCweById(jsonVuln.getCwes().get(i));
                        if (cwe != null) {
                            cweIds.add(cwe.getCweId());
                        }
                    }
                    jsonVuln.setCwes(cweIds);
                }

                final List<VulnerableSoftware> vsListOld = qm.getVulnerableSoftwareByVulnId(vulnerability.getSource(), vulnerability.getVulnId());
                List<VulnerableSoftware> vsList = new ArrayList<>();
                if (jsonVuln.getAffectedComponents() != null) {
                    for (final AffectedComponent ac: jsonVuln.getAffectedComponents()) {
                        final VulnerableSoftware vs = ac.toVulnerableSoftware();
                        if (vs != null) {
                            vsList.add(vs);
                        }
                    }
                }
                recalculateScoresFromVector(jsonVuln);
                vulnerability = qm.updateVulnerability(jsonVuln, true);
                qm.persist(vsList);
                vsList = qm.reconcileVulnerableSoftware(vulnerability, vsListOld, vsList, Vulnerability.Source.INTERNAL);
                vulnerability.setVulnerableSoftware(vsList);
                qm.persist(vulnerability);
                return Response.ok(vulnerability).build();
            } else {
                return Response.status(Response.Status.NOT_FOUND).entity("The vulnerability could not be found.").build();
            }
        } catch (MissingFactorException missingFactorException) {
            return Response.status(Response.Status.BAD_REQUEST).entity(missingFactorException.getMessage()).build();
        }
    }

    @DELETE
    @Path("/{uuid}")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(
            value = "Deletes a vulnerability",
            code = 204
    )
    @ApiResponses(value = {
            @ApiResponse(code = 401, message = "Unauthorized"),
            @ApiResponse(code = 403, message = "Access to the specified vulnerability is forbidden"),
            @ApiResponse(code = 404, message = "The UUID of the vulnerability could not be found"),
            @ApiResponse(code = 412, message = "Portfolio components or services are affected by this vulnerability. Unable to delete.")
    })
    @PermissionRequired(Permissions.Constants.VULNERABILITY_MANAGEMENT)
    public Response deleteVulnerability(
            @ApiParam(value = "The UUID of the vulnerability to delete", required = true)
            @PathParam("uuid") String uuid) {
        try (QueryManager qm = new QueryManager()) {
            final Vulnerability vulnerability = qm.getObjectByUuid(Vulnerability.class, uuid);
            if (vulnerability != null) {
                if (Vulnerability.Source.INTERNAL.name().equals(vulnerability.getSource())) {
                    if (vulnerability.getComponents().size() > 0) {
                        return Response.status(Response.Status.PRECONDITION_FAILED).entity("Portfolio components or services are affected by this vulnerability. Unable to delete.").build();
                    } else {
                        qm.deleteAffectedVersionAttributions(vulnerability);
                        qm.delete(vulnerability);
                        return Response.status(Response.Status.NO_CONTENT).build();
                    }
                } else {
                    return Response.status(Response.Status.FORBIDDEN).entity("Access to the specified vulnerability is forbidden.").build();
                }
            } else {
                return Response.status(Response.Status.NOT_FOUND).entity("The UUID of the component could not be found.").build();
            }
        }
    }

    @GET
    @Path("/vulnId")
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(
            value = "Generates an internal vulnerability identifier",
            response = String.class
    )
    @ApiResponses(value = {
            @ApiResponse(code = 401, message = "Unauthorized")
    })
    @PermissionRequired(Permissions.Constants.PORTFOLIO_MANAGEMENT)
    public Response generateInternalVulnerabilityIdentifier() {
        final String vulnId = VulnerabilityUtil.randomInternalId();
        return Response.ok(vulnId).build();
    }

    public void recalculateScoresFromVector(Vulnerability vuln) throws MissingFactorException {
        // Recalculate V2 score based on vector passed to resource and normalize vector
        final Cvss v2 = Cvss.fromVector(vuln.getCvssV2Vector());
        if (v2 != null) {
            final Score score = v2.calculateScore();
            vuln.setCvssV2BaseScore(BigDecimal.valueOf(score.getBaseScore()));
            vuln.setCvssV2ImpactSubScore(BigDecimal.valueOf(score.getImpactSubScore()));
            vuln.setCvssV2ExploitabilitySubScore(BigDecimal.valueOf(score.getExploitabilitySubScore()));
            vuln.setCvssV2Vector(v2.getVector());
        }

        // Recalculate V3 score based on vector passed to resource and normalize vector
        final Cvss v3 = Cvss.fromVector(vuln.getCvssV3Vector());
        if (v3 != null) {
            final Score score = v3.calculateScore();
            vuln.setCvssV3BaseScore(BigDecimal.valueOf(score.getBaseScore()));
            vuln.setCvssV3ImpactSubScore(BigDecimal.valueOf(score.getImpactSubScore()));
            vuln.setCvssV3ExploitabilitySubScore(BigDecimal.valueOf(score.getExploitabilitySubScore()));
            vuln.setCvssV3Vector(v3.getVector());
        }

        // Recalculate OWASP RR score based on vector passed to resource
        if (vuln.getOwaspRRVector() != null) {
            final OwaspRiskRating rr = OwaspRiskRating.fromVector(vuln.getOwaspRRVector());
            final us.springett.owasp.riskrating.Score score = rr.calculateScore();
            vuln.setOwaspRRLikelihoodScore(BigDecimal.valueOf(score.getLikelihoodScore()));
            vuln.setOwaspRRTechnicalImpactScore(BigDecimal.valueOf(score.getTechnicalImpactScore()));
            vuln.setOwaspRRBusinessImpactScore(BigDecimal.valueOf(score.getBusinessImpactScore()));
        }
    }

    @POST
    @Path("/source/{source}/vuln/{vulnId}/component/{component}")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(
            value = "Assigns a vulnerability to a component"
    )
    @ApiResponses(value = {
            @ApiResponse(code = 401, message = "Unauthorized"),
            @ApiResponse(code = 403, message = "Access to the specified component is forbidden"),
            @ApiResponse(code = 404, message = "The vulnerability or component could not be found")
    })
    @PermissionRequired(Permissions.Constants.PORTFOLIO_MANAGEMENT)
    public Response assignVulnerability(@ApiParam(value = "The vulnerability source", required = true)
                                        @PathParam("source") String source,
                                        @ApiParam(value = "The vulnId", required = true)
                                        @PathParam("vulnId") String vulnId,
                                        @ApiParam(value = "The UUID of the component", required = true)
                                        @PathParam("component") String componentUuid) {
        try (QueryManager qm = new QueryManager()) {
            Vulnerability vulnerability = qm.getVulnerabilityByVulnId(source, vulnId);
            if (vulnerability == null) {
                return Response.status(Response.Status.NOT_FOUND).entity("The vulnerability could not be found.").build();
            }
            final Component component = qm.getObjectByUuid(Component.class, componentUuid);
            if (component != null) {
                if (qm.hasAccess(super.getPrincipal(), component.getProject())) {
                    qm.addVulnerability(vulnerability, component, AnalyzerIdentity.NONE);
                    return Response.ok().build();
                } else {
                    return Response.status(Response.Status.FORBIDDEN).entity("Access to the specified component is forbidden").build();
                }
            } else {
                return Response.status(Response.Status.NOT_FOUND).entity("The component could not be found.").build();
            }
        }
    }

    @POST
    @Path("/{uuid}/component/{component}")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(
            value = "Assigns a vulnerability to a component"
    )
    @ApiResponses(value = {
            @ApiResponse(code = 401, message = "Unauthorized"),
            @ApiResponse(code = 403, message = "Access to the specified component is forbidden"),
            @ApiResponse(code = 404, message = "The vulnerability or component could not be found")
    })
    @PermissionRequired(Permissions.Constants.PORTFOLIO_MANAGEMENT)
    public Response assignVulnerability(@ApiParam(value = "The UUID of the vulnerability", required = true)
                                        @PathParam("uuid") String uuid,
                                        @ApiParam(value = "The UUID of the component", required = true)
                                        @PathParam("component") String componentUuid) {
        try (QueryManager qm = new QueryManager()) {
            Vulnerability vulnerability = qm.getObjectByUuid(Vulnerability.class, uuid);
            if (vulnerability == null) {
                return Response.status(Response.Status.NOT_FOUND).entity("The vulnerability could not be found.").build();
            }
            final Component component = qm.getObjectByUuid(Component.class, componentUuid);
            if (component != null) {
                if (qm.hasAccess(super.getPrincipal(), component.getProject())) {
                    qm.addVulnerability(vulnerability, component, AnalyzerIdentity.NONE);
                    return Response.ok().build();
                } else {
                    return Response.status(Response.Status.FORBIDDEN).entity("Access to the specified component is forbidden").build();
                }
            } else {
                return Response.status(Response.Status.NOT_FOUND).entity("The component could not be found.").build();
            }
        }
    }

    @DELETE
    @Path("/source/{source}/vuln/{vulnId}/component/{component}")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(
            value = "Removes assignment of a vulnerability from a component"
    )
    @ApiResponses(value = {
            @ApiResponse(code = 401, message = "Unauthorized"),
            @ApiResponse(code = 403, message = "Access to the specified component is forbidden"),
            @ApiResponse(code = 404, message = "The vulnerability or component could not be found")
    })
    @PermissionRequired(Permissions.Constants.PORTFOLIO_MANAGEMENT)
    public Response unassignVulnerability(@ApiParam(value = "The vulnerability source", required = true)
                                          @PathParam("source") String source,
                                          @ApiParam(value = "The vulnId", required = true)
                                          @PathParam("vulnId") String vulnId,
                                          @ApiParam(value = "The UUID of the component", required = true)
                                          @PathParam("component") String componentUuid) {
        try (QueryManager qm = new QueryManager()) {
            Vulnerability vulnerability = qm.getVulnerabilityByVulnId(source, vulnId);
            if (vulnerability == null) {
                return Response.status(Response.Status.NOT_FOUND).entity("The vulnerability could not be found.").build();
            }
            final Component component = qm.getObjectByUuid(Component.class, componentUuid);
            if (component != null) {
                if (qm.hasAccess(super.getPrincipal(), component.getProject())) {
                    qm.removeVulnerability(vulnerability, component);
                    return Response.ok().build();
                } else {
                    return Response.status(Response.Status.FORBIDDEN).entity("Access to the specified component is forbidden").build();
                }
            } else {
                return Response.status(Response.Status.NOT_FOUND).entity("The component could not be found.").build();
            }
        }
    }

    @DELETE
    @Path("/{uuid}/component/{component}")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(
            value = "Removes assignment of a vulnerability from a component"
    )
    @ApiResponses(value = {
            @ApiResponse(code = 401, message = "Unauthorized"),
            @ApiResponse(code = 403, message = "Access to the specified component is forbidden"),
            @ApiResponse(code = 404, message = "The vulnerability or component could not be found")
    })
    @PermissionRequired(Permissions.Constants.PORTFOLIO_MANAGEMENT)
    public Response unassignVulnerability(@ApiParam(value = "The UUID of the vulnerability", required = true)
                                          @PathParam("uuid") String uuid,
                                          @ApiParam(value = "The UUID of the component", required = true)
                                          @PathParam("component") String componentUuid) {
        try (QueryManager qm = new QueryManager()) {
            Vulnerability vulnerability = qm.getObjectByUuid(Vulnerability.class, uuid);
            if (vulnerability == null) {
                return Response.status(Response.Status.NOT_FOUND).entity("The vulnerability could not be found.").build();
            }
            Component component = qm.getObjectByUuid(Component.class, componentUuid);
            if (component != null) {
                if (qm.hasAccess(super.getPrincipal(), component.getProject())) {
                    qm.removeVulnerability(vulnerability, component);
                    return Response.ok().build();
                } else {
                    return Response.status(Response.Status.FORBIDDEN).entity("Access to the specified component is forbidden").build();
                }
            } else {
                return Response.status(Response.Status.NOT_FOUND).entity("The component could not be found.").build();
            }
        }
    }
}
