com.synopsys.integration.blackduck.service.dataservice.ComponentService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of blackduck-common Show documentation
Show all versions of blackduck-common Show documentation
A library for using various capabilities of Black Duck, notably the REST API and signature scanning.
/**
* blackduck-common
*
* Copyright (c) 2021 Synopsys, Inc.
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.synopsys.integration.blackduck.service.dataservice;
import java.util.List;
import java.util.Optional;
import java.util.function.BiFunction;
import java.util.function.Function;
import org.apache.commons.lang3.StringUtils;
import com.synopsys.integration.bdio.model.externalid.ExternalId;
import com.synopsys.integration.blackduck.api.core.response.LinkSingleResponse;
import com.synopsys.integration.blackduck.api.generated.deprecated.response.RemediationOptionsView;
import com.synopsys.integration.blackduck.api.generated.discovery.ApiDiscovery;
import com.synopsys.integration.blackduck.api.generated.response.ComponentVersionRemediatingView;
import com.synopsys.integration.blackduck.api.generated.response.ComponentVersionUpgradeGuidanceView;
import com.synopsys.integration.blackduck.api.generated.response.ComponentsView;
import com.synopsys.integration.blackduck.api.generated.view.ComponentVersionView;
import com.synopsys.integration.blackduck.api.generated.view.ComponentView;
import com.synopsys.integration.blackduck.api.generated.view.VulnerabilityView;
import com.synopsys.integration.blackduck.http.BlackDuckMediaTypes;
import com.synopsys.integration.blackduck.http.BlackDuckQuery;
import com.synopsys.integration.blackduck.http.BlackDuckRequestBuilder;
import com.synopsys.integration.blackduck.http.BlackDuckRequestFactory;
import com.synopsys.integration.blackduck.service.BlackDuckApiClient;
import com.synopsys.integration.blackduck.service.DataService;
import com.synopsys.integration.blackduck.service.model.ComponentVersionVulnerabilities;
import com.synopsys.integration.exception.IntegrationException;
import com.synopsys.integration.log.IntLogger;
import com.synopsys.integration.rest.HttpUrl;
public class ComponentService extends DataService {
public static final String REMEDIATING_LINK = "remediating";
public static final LinkSingleResponse REMEDIATION_OPTIONS_LINK_RESPONSE = new LinkSingleResponse<>("remediating", RemediationOptionsView.class);
public static final Function, Optional> FIRST_OR_EMPTY_RESULT =
(list) ->
Optional.ofNullable(list)
.filter(notEmptyList -> notEmptyList.size() > 0)
.map(notEmptyList -> notEmptyList.get(0));
public static final Function, Optional> SINGLE_OR_EMPTY_RESULT =
(list) ->
Optional.ofNullable(list)
.filter(notEmptyList -> notEmptyList.size() == 1)
.map(listOfSingleElement -> listOfSingleElement.get(0));
public ComponentService(BlackDuckApiClient blackDuckApiClient, BlackDuckRequestFactory blackDuckRequestFactory, IntLogger logger) {
super(blackDuckApiClient, blackDuckRequestFactory, logger);
}
public List getAllSearchResults(ExternalId externalId) throws IntegrationException {
String forge = externalId.getForge().getName();
String originId = externalId.createExternalId();
String componentQuery = String.format("%s|%s", forge, originId);
Optional blackDuckQuery = BlackDuckQuery.createQuery("id", componentQuery);
BlackDuckRequestBuilder requestBuilder = blackDuckRequestFactory.createCommonGetRequestBuilder(blackDuckQuery);
List allSearchResults = blackDuckApiClient.getAllResponses(ApiDiscovery.COMPONENTS_LINK_RESPONSE, requestBuilder);
return allSearchResults;
}
public Optional getSingleOrEmptyResult(ExternalId externalId) throws IntegrationException {
return getFilteredSearchResults(getAllSearchResults(externalId), SINGLE_OR_EMPTY_RESULT);
}
public Optional getFirstOrEmptyResult(ExternalId externalId) throws IntegrationException {
return getFilteredSearchResults(getAllSearchResults(externalId), FIRST_OR_EMPTY_RESULT);
}
public T getFilteredSearchResults(List searchResults, Function, T> filterFunction) {
return filterFunction.apply(searchResults);
}
public T getFilteredSearchResults(ExternalId externalId, List searchResults, BiFunction, T> filterFunction) {
return filterFunction.apply(externalId, searchResults);
}
public Optional getComponentVersionView(ComponentsView searchResult) throws IntegrationException {
if (StringUtils.isNotBlank(searchResult.getVersion())) {
HttpUrl url = new HttpUrl(searchResult.getVersion());
return Optional.ofNullable(blackDuckApiClient.getResponse(url, ComponentVersionView.class));
} else {
return Optional.empty();
}
}
public Optional getComponentView(ComponentsView searchResult) throws IntegrationException {
if (StringUtils.isNotBlank(searchResult.getVersion())) {
HttpUrl url = new HttpUrl(searchResult.getVersion());
return Optional.ofNullable(blackDuckApiClient.getResponse(url, ComponentView.class));
} else {
return Optional.empty();
}
}
public ComponentVersionVulnerabilities getComponentVersionVulnerabilities(ComponentVersionView componentVersion) throws IntegrationException {
BlackDuckRequestBuilder requestBuilder = blackDuckRequestFactory.createCommonGetRequestBuilder()
.acceptMimeType(BlackDuckMediaTypes.VULNERABILITY_REQUEST_SERVICE_V1);
List vulnerabilityList = blackDuckApiClient.getAllResponses(componentVersion, ComponentVersionView.VULNERABILITIES_LINK_RESPONSE, requestBuilder);
return new ComponentVersionVulnerabilities(componentVersion, vulnerabilityList);
}
public Optional getUpgradeGuidance(ComponentVersionView componentVersionView) throws IntegrationException {
return blackDuckApiClient.getResponse(componentVersionView, ComponentVersionView.UPGRADE_GUIDANCE_LINK_RESPONSE);
}
@Deprecated
/**
* @deprecated ComponentVersionRemediatingView is no longer available as of Black Duck 2020.10.0
*/
public boolean canRetrieveRemediationInformation(ComponentVersionView componentVersionView) {
try {
simplyRetrieveRemediationInformation(componentVersionView);
return true;
} catch (IntegrationException e) {
return false;
}
}
@Deprecated
/**
* @deprecated ComponentVersionRemediatingView is no longer available as of Black Duck 2020.10.0
*/
public Optional getRemediationInformation(ComponentVersionView componentVersionView) throws IntegrationException {
if (canRetrieveRemediationInformation(componentVersionView)) {
return simplyRetrieveRemediationInformation(componentVersionView);
}
return Optional.empty();
}
private Optional simplyRetrieveRemediationInformation(ComponentVersionView componentVersionView) throws IntegrationException {
HttpUrl remediatingUrl = componentVersionView.getHref().appendRelativeUrl(ComponentService.REMEDIATING_LINK);
return Optional.ofNullable(blackDuckApiClient.getResponse(remediatingUrl, ComponentVersionRemediatingView.class));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy