com.synopsys.integration.blackduck.service.BlackDuckApiClient 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;
import java.io.IOException;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.function.Predicate;
import com.google.gson.Gson;
import com.synopsys.integration.blackduck.api.core.BlackDuckComponent;
import com.synopsys.integration.blackduck.api.core.BlackDuckPath;
import com.synopsys.integration.blackduck.api.core.BlackDuckResponse;
import com.synopsys.integration.blackduck.api.core.BlackDuckView;
import com.synopsys.integration.blackduck.api.core.response.BlackDuckPathMultipleResponses;
import com.synopsys.integration.blackduck.api.core.response.BlackDuckPathSingleResponse;
import com.synopsys.integration.blackduck.api.core.response.LinkMultipleResponses;
import com.synopsys.integration.blackduck.api.core.response.LinkSingleResponse;
import com.synopsys.integration.blackduck.http.BlackDuckPageDefinition;
import com.synopsys.integration.blackduck.http.BlackDuckPageResponse;
import com.synopsys.integration.blackduck.http.BlackDuckRequestBuilder;
import com.synopsys.integration.blackduck.http.BlackDuckRequestFactory;
import com.synopsys.integration.blackduck.http.PagedRequest;
import com.synopsys.integration.blackduck.http.client.BlackDuckHttpClient;
import com.synopsys.integration.blackduck.http.transform.BlackDuckJsonTransformer;
import com.synopsys.integration.blackduck.http.transform.BlackDuckResponseTransformer;
import com.synopsys.integration.blackduck.http.transform.BlackDuckResponsesTransformer;
import com.synopsys.integration.exception.IntegrationException;
import com.synopsys.integration.function.ThrowingBiFunction;
import com.synopsys.integration.rest.HttpMethod;
import com.synopsys.integration.rest.HttpUrl;
import com.synopsys.integration.rest.request.Request;
import com.synopsys.integration.rest.response.Response;
public class BlackDuckApiClient {
public static final BlackDuckPath BOMIMPORT_PATH = new BlackDuckPath("/api/bom-import");
public static final BlackDuckPath SCANSUMMARIES_PATH = new BlackDuckPath("/api/scan-summaries");
public static final BlackDuckPath UPLOADS_PATH = new BlackDuckPath("/api/uploads");
public static final BlackDuckPath SCAN_DATA_PATH = new BlackDuckPath("/api/scan/data/"); // TODO: Determine why this endpoint requires a slash at the end.
public static final BlackDuckPath SCAN_DEVELOPER_MODE_PATH = new BlackDuckPath("/api/developer-scans");
private final BlackDuckHttpClient blackDuckHttpClient;
private final BlackDuckJsonTransformer blackDuckJsonTransformer;
private final BlackDuckResponseTransformer blackDuckResponseTransformer;
private final BlackDuckResponsesTransformer blackDuckResponsesTransformer;
private final HttpUrl blackDuckBaseUrl;
private final Gson gson;
private final BlackDuckRequestFactory blackDuckRequestFactory;
public BlackDuckApiClient(BlackDuckHttpClient blackDuckHttpClient, Gson gson, BlackDuckJsonTransformer blackDuckJsonTransformer, BlackDuckResponseTransformer blackDuckResponseTransformer,
BlackDuckResponsesTransformer blackDuckResponsesTransformer, BlackDuckRequestFactory blackDuckRequestFactory) {
this.blackDuckHttpClient = blackDuckHttpClient;
this.blackDuckBaseUrl = blackDuckHttpClient.getBaseUrl();
this.gson = gson;
this.blackDuckJsonTransformer = blackDuckJsonTransformer;
this.blackDuckResponseTransformer = blackDuckResponseTransformer;
this.blackDuckResponsesTransformer = blackDuckResponsesTransformer;
this.blackDuckRequestFactory = blackDuckRequestFactory;
}
public T transformResponse(Response response, Class clazz) throws IntegrationException {
return blackDuckJsonTransformer.getResponse(response, clazz);
}
public String convertToJson(Object obj) {
return gson.toJson(obj);
}
public HttpUrl getUrl(BlackDuckPath path) throws IntegrationException {
return fullBlackDuckUrl(path);
}
// ------------------------------------------------
// getting responses from a 'path', which we define as something that looks like '/api/codelocations'
// ------------------------------------------------
public List getSomeMatchingResponses(BlackDuckPathMultipleResponses blackDuckPathMultipleResponses, Predicate predicate, int totalLimit)
throws IntegrationException {
return getSomeMatchingResponses(blackDuckPathMultipleResponses, blackDuckRequestFactory.createCommonGetRequestBuilder(), predicate, totalLimit);
}
public List getSomeMatchingResponses(BlackDuckPathMultipleResponses blackDuckPathMultipleResponses, BlackDuckRequestBuilder requestBuilder, Predicate predicate, int totalLimit)
throws IntegrationException {
return getBlackDuckPathResponses(blackDuckPathMultipleResponses, requestBuilder, (pagedRequest, responseClass) -> blackDuckResponsesTransformer.getSomeMatchingResponses(pagedRequest, responseClass, predicate, totalLimit));
}
public List getAllResponses(BlackDuckPathMultipleResponses blackDuckPathMultipleResponses) throws IntegrationException {
return getAllResponses(blackDuckPathMultipleResponses, blackDuckRequestFactory.createCommonGetRequestBuilder());
}
public List getAllResponses(BlackDuckPathMultipleResponses blackDuckPathMultipleResponses, BlackDuckRequestBuilder requestBuilder) throws IntegrationException {
return getBlackDuckPathResponses(blackDuckPathMultipleResponses, requestBuilder, blackDuckResponsesTransformer::getAllResponses);
}
public List getSomeResponses(BlackDuckPathMultipleResponses blackDuckPathMultipleResponses, int totalLimit) throws IntegrationException {
return getSomeResponses(blackDuckPathMultipleResponses, blackDuckRequestFactory.createCommonGetRequestBuilder(), totalLimit);
}
public List getSomeResponses(BlackDuckPathMultipleResponses blackDuckPathMultipleResponses, BlackDuckRequestBuilder requestBuilder, int totalLimit) throws IntegrationException {
return getBlackDuckPathResponses(blackDuckPathMultipleResponses, requestBuilder, (pagedRequest, responseClass) -> blackDuckResponsesTransformer.getSomeResponses(pagedRequest, responseClass, totalLimit));
}
public BlackDuckPageResponse getPageResponse(BlackDuckPathMultipleResponses blackDuckPathMultipleResponses, BlackDuckPageDefinition blackDuckPageDefinition) throws IntegrationException {
HttpUrl url = fullBlackDuckUrl(blackDuckPathMultipleResponses.getBlackDuckPath());
Class responseClass = blackDuckPathMultipleResponses.getResponseClass();
return getPageResponse(url, responseClass, blackDuckPageDefinition);
}
public T getResponse(BlackDuckPathSingleResponse blackDuckPathSingleResponse, BlackDuckRequestBuilder requestBuilder) throws IntegrationException {
HttpUrl url = fullBlackDuckUrl(blackDuckPathSingleResponse.getBlackDuckPath());
requestBuilder.url(url);
return blackDuckResponseTransformer.getResponse(requestBuilder.build(), blackDuckPathSingleResponse.getResponseClass());
}
public T getResponse(BlackDuckPathSingleResponse blackDuckPathSingleResponse) throws IntegrationException {
return getResponse(blackDuckPathSingleResponse, blackDuckRequestFactory.createCommonGetRequestBuilder());
}
// ------------------------------------------------
// getting responses from a BlackDuckView
// ------------------------------------------------
public List getSomeMatchingResponses(BlackDuckView blackDuckView, LinkMultipleResponses linkMultipleResponses, Predicate predicate, int totalLimit)
throws IntegrationException {
return getSomeMatchingResponses(blackDuckView, linkMultipleResponses, blackDuckRequestFactory.createCommonGetRequestBuilder(), predicate, totalLimit);
}
public List getSomeMatchingResponses(BlackDuckView blackDuckView, LinkMultipleResponses linkMultipleResponses, BlackDuckRequestBuilder requestBuilder, Predicate predicate, int totalLimit)
throws IntegrationException {
return getBlackDuckViewResponses(blackDuckView, linkMultipleResponses, requestBuilder, (pagedRequest, responseClass) -> blackDuckResponsesTransformer.getSomeMatchingResponses(pagedRequest, responseClass, predicate, totalLimit));
}
public List getAllResponses(BlackDuckView blackDuckView, LinkMultipleResponses linkMultipleResponses) throws IntegrationException {
return getAllResponses(blackDuckView, linkMultipleResponses, blackDuckRequestFactory.createCommonGetRequestBuilder());
}
public List getAllResponses(BlackDuckView blackDuckView, LinkMultipleResponses linkMultipleResponses, BlackDuckRequestBuilder requestBuilder) throws IntegrationException {
return getBlackDuckViewResponses(blackDuckView, linkMultipleResponses, requestBuilder, blackDuckResponsesTransformer::getAllResponses);
}
public List getSomeResponses(BlackDuckView blackDuckView, LinkMultipleResponses linkMultipleResponses, int totalLimit) throws IntegrationException {
return getSomeResponses(blackDuckView, linkMultipleResponses, blackDuckRequestFactory.createCommonGetRequestBuilder(), totalLimit);
}
public List getSomeResponses(BlackDuckView blackDuckView, LinkMultipleResponses linkMultipleResponses, BlackDuckRequestBuilder requestBuilder, int totalLimit) throws IntegrationException {
return getBlackDuckViewResponses(blackDuckView, linkMultipleResponses, requestBuilder, (pagedRequest, responseClass) -> blackDuckResponsesTransformer.getSomeResponses(pagedRequest, responseClass, totalLimit));
}
public Optional getResponse(BlackDuckView blackDuckView, LinkSingleResponse linkSingleResponse) throws IntegrationException {
try {
HttpUrl url = blackDuckView.getFirstLink(linkSingleResponse.getLink());
Request request = blackDuckRequestFactory.createCommonGetRequest(url);
return Optional.of(blackDuckResponseTransformer.getResponse(request, linkSingleResponse.getResponseClass()));
} catch (NoSuchElementException e) {
return Optional.empty();
}
}
// ------------------------------------------------
// getting responses from a uri
// ------------------------------------------------
public List getAllResponses(HttpUrl url, Class responseClass) throws IntegrationException {
BlackDuckRequestBuilder requestBuilder = blackDuckRequestFactory.createCommonGetRequestBuilder(url);
return blackDuckResponsesTransformer.getAllResponses(new PagedRequest(requestBuilder), responseClass).getItems();
}
public List getSomeResponses(HttpUrl url, Class responseClass, int totalLimit) throws IntegrationException {
BlackDuckRequestBuilder requestBuilder = blackDuckRequestFactory.createCommonGetRequestBuilder(url);
return blackDuckResponsesTransformer.getSomeResponses(new PagedRequest(requestBuilder), responseClass, totalLimit).getItems();
}
public BlackDuckPageResponse getPageResponse(HttpUrl url, Class responseClass, BlackDuckPageDefinition blackDuckPageDefinition) throws IntegrationException {
BlackDuckRequestBuilder requestBuilder = blackDuckRequestFactory.createCommonGetRequestBuilder(url);
return getPageResponse(requestBuilder, responseClass, blackDuckPageDefinition);
}
// ------------------------------------------------
// getting responses from a Request.Builder
// ------------------------------------------------
public List getAllResponses(BlackDuckRequestBuilder requestBuilder, Class responseClass) throws IntegrationException {
return blackDuckResponsesTransformer.getAllResponses(new PagedRequest(requestBuilder), responseClass).getItems();
}
public List getSomeResponses(BlackDuckRequestBuilder requestBuilder, Class responseClass, int totalLimit) throws IntegrationException {
return blackDuckResponsesTransformer.getSomeResponses(new PagedRequest(requestBuilder), responseClass, totalLimit).getItems();
}
public BlackDuckPageResponse getPageResponse(BlackDuckRequestBuilder requestBuilder, Class responseClass, BlackDuckPageDefinition blackDuckPageDefinition) throws IntegrationException {
PagedRequest pagedRequest = new PagedRequest(requestBuilder, blackDuckPageDefinition);
return blackDuckResponsesTransformer.getOnePageOfResponses(pagedRequest, responseClass);
}
public T getResponse(HttpUrl url, Class responseClass) throws IntegrationException {
Request request = blackDuckRequestFactory.createCommonGetRequest(url);
return blackDuckResponseTransformer.getResponse(request, responseClass);
}
// ------------------------------------------------
// getting responses from a LinkSingleResponse
// ------------------------------------------------
/**
* @deprecated This uses LinkSingleResponse incorrectly. Please use getResponse(HttpUrl url, Class responseClass) instead.
*/
@Deprecated
public T getResponse(LinkSingleResponse linkSingleResponse) throws IntegrationException {
HttpUrl url = new HttpUrl(linkSingleResponse.getLink());
Request request = blackDuckRequestFactory.createCommonGetRequest(url);
return blackDuckResponseTransformer.getResponse(request, linkSingleResponse.getResponseClass());
}
// ------------------------------------------------
// handling generic post
// ------------------------------------------------
public HttpUrl post(BlackDuckPath blackDuckPath, BlackDuckComponent blackDuckComponent) throws IntegrationException {
HttpUrl url = fullBlackDuckUrl(blackDuckPath);
return post(url, blackDuckComponent);
}
public HttpUrl post(HttpUrl url, BlackDuckComponent blackDuckComponent) throws IntegrationException {
String json = gson.toJson(blackDuckComponent);
Request request = blackDuckRequestFactory.createCommonPostRequestBuilder(url, json).build();
return executePostRequestAndRetrieveURL(request);
}
// ------------------------------------------------
// handling generic delete
// ------------------------------------------------
public void delete(BlackDuckView blackDuckView) throws IntegrationException {
HttpUrl url = blackDuckView.getHref();
delete(url);
}
public void delete(HttpUrl url) throws IntegrationException {
Request request = new Request.Builder(url, HttpMethod.DELETE).build();
try (Response response = execute(request)) {
} catch (IOException e) {
throw new IntegrationException(e.getMessage(), e);
}
}
// ------------------------------------------------
// handling generic put
// ------------------------------------------------
public void put(BlackDuckView blackDuckView) throws IntegrationException {
HttpUrl url = blackDuckView.getHref();
// add the 'missing' pieces back from view that could have been lost
String json = blackDuckJsonTransformer.producePatchedJson(blackDuckView);
Request request = blackDuckRequestFactory.createCommonPutRequestBuilder(url, json).build();
try (Response response = execute(request)) {
} catch (IOException e) {
throw new IntegrationException(e.getMessage(), e);
}
}
// ------------------------------------------------
// handling generic get
// ------------------------------------------------
public Response get(HttpUrl url) throws IntegrationException {
Request request = blackDuckRequestFactory.createCommonGetRequest(url);
return execute(request);
}
public Response get(BlackDuckPath path) throws IntegrationException {
HttpUrl url = fullBlackDuckUrl(path);
Request request = blackDuckRequestFactory.createCommonGetRequest(url);
return execute(request);
}
// ------------------------------------------------
// handling plain requests
// ------------------------------------------------
public Response execute(BlackDuckPath path, BlackDuckRequestBuilder requestBuilder) throws IntegrationException {
HttpUrl url = fullBlackDuckUrl(path);
requestBuilder.url(url);
Request request = requestBuilder.build();
return execute(request);
}
public Response execute(Request request) throws IntegrationException {
Response response = blackDuckHttpClient.execute(request);
blackDuckHttpClient.throwExceptionForError(response);
return response;
}
// ------------------------------------------------
// posting and getting location header
// ------------------------------------------------
public HttpUrl executePostRequestAndRetrieveURL(BlackDuckPath path, BlackDuckRequestBuilder requestBuilder) throws IntegrationException {
HttpUrl url = fullBlackDuckUrl(path);
requestBuilder.url(url);
return executePostRequestAndRetrieveURL(requestBuilder.build());
}
public HttpUrl executePostRequestAndRetrieveURL(Request request) throws IntegrationException {
try (Response response = execute(request)) {
return new HttpUrl(response.getHeaderValue("location"));
} catch (IOException e) {
throw new IntegrationException(e.getMessage(), e);
}
}
private List getBlackDuckPathResponses(BlackDuckPathMultipleResponses blackDuckPathMultipleResponses, BlackDuckRequestBuilder requestBuilder,
ThrowingBiFunction, BlackDuckPageResponse, IntegrationException> responsesTransformer) throws IntegrationException {
HttpUrl url = fullBlackDuckUrl(blackDuckPathMultipleResponses.getBlackDuckPath());
Class responseClass = blackDuckPathMultipleResponses.getResponseClass();
return getSpecialResponses(url, responseClass, requestBuilder, responsesTransformer);
}
private List getBlackDuckViewResponses(BlackDuckView blackDuckView, LinkMultipleResponses linkMultipleResponses, BlackDuckRequestBuilder requestBuilder,
ThrowingBiFunction, BlackDuckPageResponse, IntegrationException> responsesTransformer) throws IntegrationException {
HttpUrl url = blackDuckView.getFirstLink(linkMultipleResponses.getLink());
Class responseClass = linkMultipleResponses.getResponseClass();
return getSpecialResponses(url, responseClass, requestBuilder, responsesTransformer);
}
private List getSpecialResponses(HttpUrl httpUrl, Class responseClass, BlackDuckRequestBuilder requestBuilder,
ThrowingBiFunction, BlackDuckPageResponse, IntegrationException> responsesTransformer) throws IntegrationException {
requestBuilder.url(httpUrl);
return responsesTransformer.apply(new PagedRequest(requestBuilder), responseClass).getItems();
}
private HttpUrl fullBlackDuckUrl(BlackDuckPath blackDuckPath) throws IntegrationException {
return blackDuckPath.getFullBlackDuckUrl(blackDuckBaseUrl);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy