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

com.synopsys.integration.blackduck.service.BlackDuckService Maven / Gradle / Ivy

Go to download

A library for using various capabilities of Black Duck, notably the REST API and signature scanning.

There is a newer version: 45.0.7
Show newest version
/**
 * blackduck-common
 *
 * Copyright (c) 2019 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.net.MalformedURLException;
import java.net.URL;
import java.util.*;
import java.util.function.Function;
import java.util.function.Supplier;

import com.synopsys.integration.function.ThrowingBiFunction;
import com.synopsys.integration.function.ThrowingFunction;
import org.apache.commons.lang3.StringUtils;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.synopsys.integration.blackduck.api.UriSingleResponse;
import com.synopsys.integration.blackduck.api.core.BlackDuckComponent;
import com.synopsys.integration.blackduck.api.core.BlackDuckPath;
import com.synopsys.integration.blackduck.api.core.BlackDuckPathMultipleResponses;
import com.synopsys.integration.blackduck.api.core.BlackDuckPathSingleResponse;
import com.synopsys.integration.blackduck.api.core.BlackDuckResponse;
import com.synopsys.integration.blackduck.api.core.BlackDuckView;
import com.synopsys.integration.blackduck.api.core.LinkMultipleResponses;
import com.synopsys.integration.blackduck.api.core.LinkSingleResponse;
import com.synopsys.integration.blackduck.exception.BlackDuckIntegrationException;
import com.synopsys.integration.blackduck.rest.BlackDuckHttpClient;
import com.synopsys.integration.blackduck.service.model.PagedRequest;
import com.synopsys.integration.blackduck.service.model.RequestFactory;
import com.synopsys.integration.exception.IntegrationException;
import com.synopsys.integration.log.IntLogger;
import com.synopsys.integration.rest.HttpMethod;
import com.synopsys.integration.rest.request.Request;
import com.synopsys.integration.rest.request.Response;

public class BlackDuckService {
    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");

    private final BlackDuckHttpClient blackDuckHttpClient;
    private final BlackDuckJsonTransformer blackDuckJsonTransformer;
    private final BlackDuckResponseTransformer blackDuckResponseTransformer;
    private final BlackDuckResponsesTransformer blackDuckResponsesTransformer;
    private final String blackDuckBaseUrl;
    private final Gson gson;

    @Deprecated
    /**
     * @deprecated Please use BlackDuckService(BlackDuckHttpClient blackDuckHttpClient, Gson gson, BlackDuckJsonTransformer blackDuckJsonTransformer, BlackDuckResponseTransformer blackDuckResponseTransformer, BlackDuckResponsesTransformer blackDuckResponsesTransformer)
     */
    public BlackDuckService(IntLogger logger, BlackDuckHttpClient blackDuckHttpClient, Gson gson, ObjectMapper objectMapper) {
        this.blackDuckHttpClient = blackDuckHttpClient;
        this.blackDuckBaseUrl = blackDuckHttpClient.getBaseUrl();
        this.gson = gson;
        this.blackDuckJsonTransformer = new BlackDuckJsonTransformer(gson, objectMapper, logger);
        this.blackDuckResponseTransformer = new BlackDuckResponseTransformer(blackDuckHttpClient, blackDuckJsonTransformer);
        this.blackDuckResponsesTransformer = new BlackDuckResponsesTransformer(blackDuckHttpClient, blackDuckJsonTransformer);
    }

    public BlackDuckService(BlackDuckHttpClient blackDuckHttpClient, Gson gson, BlackDuckJsonTransformer blackDuckJsonTransformer, BlackDuckResponseTransformer blackDuckResponseTransformer, BlackDuckResponsesTransformer blackDuckResponsesTransformer) {
        this.blackDuckHttpClient = blackDuckHttpClient;
        this.blackDuckBaseUrl = blackDuckHttpClient.getBaseUrl();
        this.gson = gson;
        this.blackDuckJsonTransformer = blackDuckJsonTransformer;
        this.blackDuckResponseTransformer = blackDuckResponseTransformer;
        this.blackDuckResponsesTransformer = blackDuckResponsesTransformer;
    }

    @Deprecated
    /**
     * @deprecated This is a dependency, not an actual offering of BlackDuckService.
     */
    public BlackDuckHttpClient getBlackDuckHttpClient() {
        return blackDuckHttpClient;
    }

    @Deprecated
    /**
     * @deprecated This is a dependency, not an actual offering of BlackDuckService.
     */
    public String getBlackDuckBaseUrl() {
        return blackDuckBaseUrl;
    }

    @Deprecated
    /**
     * @deprecated This is a dependency, not an actual offering of BlackDuckService.
     */
    public Gson getGson() {
        return gson;
    }

    public  T transformResponse(Response response, Class clazz) throws IntegrationException {
        return blackDuckJsonTransformer.getResponse(response, clazz);
    }

    public String convertToJson(Object obj) {
        return gson.toJson(obj);
    }

    public String getUri(BlackDuckPath path) throws IntegrationException {
        return pieceTogetherUri(blackDuckBaseUrl, path.getPath());
    }

    // ------------------------------------------------
    // getting responses from a 'path', which we define as something that looks like '/api/codelocations'
    // ------------------------------------------------
    public  List getAllResponses(BlackDuckPathMultipleResponses blackDuckPathMultipleResponses) throws IntegrationException {
        return getAllResponses(blackDuckPathMultipleResponses, RequestFactory.createCommonGetRequestBuilder());
    }

    public  List getAllResponses(BlackDuckPathMultipleResponses blackDuckPathMultipleResponses, Request.Builder requestBuilder) throws IntegrationException {
        return getBlackDuckPathResponses(blackDuckPathMultipleResponses, requestBuilder, blackDuckResponsesTransformer::getAllResponses);
    }

    public  List getSomeResponses(BlackDuckPathMultipleResponses blackDuckPathMultipleResponses, int totalLimit) throws IntegrationException {
        return getSomeResponses(blackDuckPathMultipleResponses, RequestFactory.createCommonGetRequestBuilder(), totalLimit);
    }

    public  List getSomeResponses(BlackDuckPathMultipleResponses blackDuckPathMultipleResponses, Request.Builder requestBuilder, int totalLimit) throws IntegrationException {
        return getBlackDuckPathResponses(blackDuckPathMultipleResponses, requestBuilder, (pagedRequest, responseClass) -> blackDuckResponsesTransformer.getSomeResponses(pagedRequest, responseClass, totalLimit));
    }

    @Deprecated
    /**
     * @deprecated The BlackDuckResponsesTransformer should be used directly if BlackDuckPageResponse is needed.
     */
    public  BlackDuckPageResponse getAllPageResponses(BlackDuckPathMultipleResponses blackDuckPathMultipleResponses) throws IntegrationException {
        return getPageResponses(blackDuckPathMultipleResponses, true);
    }

    @Deprecated
    /**
     * @deprecated The BlackDuckResponsesTransformer should be used directly if BlackDuckPageResponse is needed.
     */
    public  BlackDuckPageResponse getAllPageResponses(BlackDuckPathMultipleResponses blackDuckPathMultipleResponses, Request.Builder requestBuilder) throws IntegrationException {
        return getPageResponses(blackDuckPathMultipleResponses, requestBuilder, true);
    }

    @Deprecated
    /**
     * @deprecated Please use the appropriate getAll or getSome method
     */
    public  List getResponses(BlackDuckPathMultipleResponses blackDuckPathMultipleResponses, boolean getAll) throws IntegrationException {
        return getPageResponses(blackDuckPathMultipleResponses, getAll).getItems();
    }

    @Deprecated
    /**
     * @deprecated The BlackDuckResponsesTransformer should be used directly if BlackDuckPageResponse is needed.
     */
    public  BlackDuckPageResponse getPageResponses(BlackDuckPathMultipleResponses blackDuckPathMultipleResponses, boolean getAll) throws IntegrationException {
        String uri = pieceTogetherUri(blackDuckBaseUrl, blackDuckPathMultipleResponses.getBlackDuckPath().getPath());
        Request.Builder requestBuilder = RequestFactory.createCommonGetRequestBuilder(uri);
        return blackDuckResponsesTransformer.getResponses(new PagedRequest(requestBuilder), blackDuckPathMultipleResponses.getResponseClass(), getAll);
    }

    @Deprecated
    /**
     * @deprecated Please use the appropriate getAll or getSome method
     */
    public  List getResponses(BlackDuckPathMultipleResponses blackDuckPathMultipleResponses, Request.Builder requestBuilder, boolean getAll)
            throws IntegrationException {
        return getPageResponses(blackDuckPathMultipleResponses, requestBuilder, getAll).getItems();
    }

    @Deprecated
    /**
     * @deprecated The BlackDuckResponsesTransformer should be used directly if BlackDuckPageResponse is needed.
     */
    public  BlackDuckPageResponse getPageResponses(BlackDuckPathMultipleResponses blackDuckPathMultipleResponses, Request.Builder requestBuilder, boolean getAll)
            throws IntegrationException {
        String uri = pieceTogetherUri(blackDuckBaseUrl, blackDuckPathMultipleResponses.getBlackDuckPath().getPath());
        requestBuilder.uri(uri);
        return blackDuckResponsesTransformer.getResponses(new PagedRequest(requestBuilder), blackDuckPathMultipleResponses.getResponseClass(), getAll);
    }

    public  T getResponse(BlackDuckPathSingleResponse blackDuckPathSingleResponse, Request.Builder requestBuilder) throws IntegrationException {
        String uri = pieceTogetherUri(blackDuckBaseUrl, blackDuckPathSingleResponse.getBlackDuckPath().getPath());
        requestBuilder.uri(uri);
        return blackDuckResponseTransformer.getResponse(requestBuilder.build(), blackDuckPathSingleResponse.getResponseClass());
    }

    public  T getResponse(BlackDuckPathSingleResponse blackDuckPathSingleResponse) throws IntegrationException {
        String uri = pieceTogetherUri(blackDuckBaseUrl, blackDuckPathSingleResponse.getBlackDuckPath().getPath());
        Request request = RequestFactory.createCommonGetRequest(uri);
        return blackDuckResponseTransformer.getResponse(request, blackDuckPathSingleResponse.getResponseClass());
    }

    // ------------------------------------------------
    // getting responses from a BlackDuckView
    // ------------------------------------------------
    public  List getAllResponses(BlackDuckView blackDuckView, LinkMultipleResponses linkMultipleResponses) throws IntegrationException {
        return getAllResponses(blackDuckView, linkMultipleResponses, RequestFactory.createCommonGetRequestBuilder());
    }

    public  List getAllResponses(BlackDuckView blackDuckView, LinkMultipleResponses linkMultipleResponses, Request.Builder 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, RequestFactory.createCommonGetRequestBuilder(), totalLimit);
    }

    public  List getSomeResponses(BlackDuckView blackDuckView, LinkMultipleResponses linkMultipleResponses, Request.Builder requestBuilder, int totalLimit) throws IntegrationException {
        return getBlackDuckViewResponses(blackDuckView, linkMultipleResponses, requestBuilder, (pagedRequest, responseClass) -> blackDuckResponsesTransformer.getSomeResponses(pagedRequest, responseClass, totalLimit));
    }

    @Deprecated
    /**
     * @deprecated Please use the appropriate getAll or getSome method
     */
    public  List getResponses(BlackDuckView blackDuckView, LinkMultipleResponses linkMultipleResponses, boolean getAll) throws IntegrationException {
        Optional uri = blackDuckView.getFirstLink(linkMultipleResponses.getLink());
        if (!uri.isPresent() || StringUtils.isBlank(uri.get())) {
            return Collections.emptyList();
        }
        Request.Builder requestBuilder = RequestFactory.createCommonGetRequestBuilder(uri.get());
        return blackDuckResponsesTransformer.getResponses(new PagedRequest(requestBuilder), linkMultipleResponses.getResponseClass(), getAll).getItems();
    }

    @Deprecated
    /**
     * @deprecated Please use the appropriate getAll or getSome method
     */
    public  List getResponses(BlackDuckView blackDuckView, LinkMultipleResponses linkMultipleResponses, Request.Builder requestBuilder, boolean getAll) throws IntegrationException {
        Optional uri = blackDuckView.getFirstLink(linkMultipleResponses.getLink());
        if (!uri.isPresent() || StringUtils.isBlank(uri.get())) {
            return Collections.emptyList();
        }
        requestBuilder.uri(uri.get());
        return blackDuckResponsesTransformer.getResponses(new PagedRequest(requestBuilder), linkMultipleResponses.getResponseClass(), getAll).getItems();
    }

    public  Optional getResponse(BlackDuckView blackDuckView, LinkSingleResponse linkSingleResponse) throws IntegrationException {
        Optional uri = blackDuckView.getFirstLink(linkSingleResponse.getLink());
        if (!uri.isPresent() || StringUtils.isBlank(uri.get())) {
            return Optional.empty();
        }
        Request request = RequestFactory.createCommonGetRequest(uri.get());
        return Optional.of(blackDuckResponseTransformer.getResponse(request, linkSingleResponse.getResponseClass()));
    }

    // ------------------------------------------------
    // getting responses from a uri
    // ------------------------------------------------
    public  List getAllResponses(String uri, Class responseClass) throws IntegrationException {
        Request.Builder requestBuilder = RequestFactory.createCommonGetRequestBuilder(uri);
        return blackDuckResponsesTransformer.getAllResponses(new PagedRequest(requestBuilder), responseClass).getItems();
    }

    public  List getSomeResponses(String uri, Class responseClass, int totalLimit) throws IntegrationException {
        Request.Builder requestBuilder = RequestFactory.createCommonGetRequestBuilder(uri);
        return blackDuckResponsesTransformer.getSomeResponses(new PagedRequest(requestBuilder), responseClass, totalLimit).getItems();
    }

    // ------------------------------------------------
    // getting responses from a Request.Builder
    // ------------------------------------------------
    public  List getAllResponses(Request.Builder requestBuilder, Class responseClass) throws IntegrationException {
        return blackDuckResponsesTransformer.getAllResponses(new PagedRequest(requestBuilder), responseClass).getItems();
    }

    public  List getSomeResponses(Request.Builder requestBuilder, Class responseClass, int totalLimit) throws IntegrationException {
        return blackDuckResponsesTransformer.getSomeResponses(new PagedRequest(requestBuilder), responseClass, totalLimit).getItems();
    }

    @Deprecated
    /**
     * @deprecated Please use the appropriate getAll or getSome method
     */
    public  List getResponses(String uri, Class responseClass, boolean getAll) throws IntegrationException {
        Request.Builder requestBuilder = RequestFactory.createCommonGetRequestBuilder(uri);
        return getResponses(requestBuilder, responseClass, getAll);
    }

    @Deprecated
    /**
     * @deprecated Please use the appropriate getAll or getSome method
     */
    public  List getResponses(Request.Builder requestBuilder, Class responseClass, boolean getAll) throws IntegrationException {
        return blackDuckResponsesTransformer.getResponses(new PagedRequest(requestBuilder), responseClass, getAll).getItems();
    }

    public  T getResponse(String uri, Class responseClass) throws IntegrationException {
        Request request = RequestFactory.createCommonGetRequest(uri);
        return blackDuckResponseTransformer.getResponse(request, responseClass);
    }

    // ------------------------------------------------
    // getting responses from a UriSingleResponse
    // ------------------------------------------------
    public  T getResponse(UriSingleResponse uriSingleResponse) throws IntegrationException {
        Request request = RequestFactory.createCommonGetRequest(uriSingleResponse.getUri());
        return blackDuckResponseTransformer.getResponse(request, uriSingleResponse.getResponseClass());
    }

    // ------------------------------------------------
    // handling generic post
    // ------------------------------------------------
    public String post(BlackDuckPath blackDuckPath, BlackDuckComponent blackDuckComponent) throws IntegrationException {
        String uri = pieceTogetherUri(blackDuckBaseUrl, blackDuckPath.getPath());
        return post(uri, blackDuckComponent);
    }

    public String post(String uri, BlackDuckComponent blackDuckComponent) throws IntegrationException {
        String json = gson.toJson(blackDuckComponent);
        Request request = RequestFactory.createCommonPostRequestBuilder(json).uri(uri).build();
        return executePostRequestAndRetrieveURL(request);
    }

    // ------------------------------------------------
    // handling generic delete
    // ------------------------------------------------
    public void delete(BlackDuckView blackDuckView) throws IntegrationException {
        if (blackDuckView.getHref().isPresent()) {
            String url = blackDuckView.getHref().get();
            delete(url);
        }
    }

    public void delete(String url) throws IntegrationException {
        Request.Builder requestBuilder = new Request.Builder().method(HttpMethod.DELETE).uri(url);
        try (Response response = execute(requestBuilder.build())) {
        } catch (IOException e) {
            throw new IntegrationException(e.getMessage(), e);
        }
    }

    // ------------------------------------------------
    // handling generic put
    // ------------------------------------------------
    public void put(BlackDuckView blackDuckView) throws IntegrationException {
        if (blackDuckView.getHref().isPresent()) {
            String uri = blackDuckView.getHref().get();
            // add the 'missing' pieces back from view that could have been lost
            String json = blackDuckJsonTransformer.producePatchedJson(blackDuckView);
            Request request = RequestFactory.createCommonPutRequestBuilder(json).uri(uri).build();
            try (Response response = execute(request)) {
            } catch (IOException e) {
                throw new IntegrationException(e.getMessage(), e);
            }
        }
    }

    // ------------------------------------------------
    // handling generic get
    // ------------------------------------------------
    public Response get(String uri) throws IntegrationException {
        Request request = RequestFactory.createCommonGetRequest(uri);
        return execute(request);
    }

    public Response get(BlackDuckPath path) throws IntegrationException {
        String uri = pieceTogetherUri(blackDuckBaseUrl, path.getPath());
        Request request = RequestFactory.createCommonGetRequest(uri);
        return execute(request);
    }

    // ------------------------------------------------
    // handling plain requests
    // ------------------------------------------------
    public Response execute(BlackDuckPath path, Request.Builder requestBuilder) throws IntegrationException {
        String uri = pieceTogetherUri(blackDuckBaseUrl, path.getPath());
        requestBuilder.uri(uri);
        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 String executePostRequestAndRetrieveURL(BlackDuckPath path, Request.Builder requestBuilder) throws IntegrationException {
        String uri = pieceTogetherUri(blackDuckBaseUrl, path.getPath());
        requestBuilder.uri(uri);
        return executePostRequestAndRetrieveURL(requestBuilder.build());
    }

    public String executePostRequestAndRetrieveURL(Request request) throws IntegrationException {
        try (Response response = execute(request)) {
            return response.getHeaderValue("location");
        } catch (IOException e) {
            throw new IntegrationException(e.getMessage(), e);
        }
    }

    private  List getBlackDuckPathResponses(BlackDuckPathMultipleResponses blackDuckPathMultipleResponses, Request.Builder requestBuilder, ThrowingBiFunction, BlackDuckPageResponse, IntegrationException> responsesTransformer) throws IntegrationException {
        return getSpecialResponses((obj) -> Optional.ofNullable(pieceTogetherUri(blackDuckBaseUrl, blackDuckPathMultipleResponses.getBlackDuckPath().getPath())), blackDuckPathMultipleResponses.getResponseClass(), requestBuilder, responsesTransformer);
    }

    private  List getBlackDuckViewResponses(BlackDuckView blackDuckView, LinkMultipleResponses linkMultipleResponses, Request.Builder requestBuilder, ThrowingBiFunction, BlackDuckPageResponse, IntegrationException> responsesTransformer) throws IntegrationException {
        return getSpecialResponses((obj) -> blackDuckView.getFirstLink(linkMultipleResponses.getLink()), linkMultipleResponses.getResponseClass(), requestBuilder, responsesTransformer);
    }

    //TODO replace with ThrowingSupplier once it exists
    private  List getSpecialResponses(ThrowingFunction, BlackDuckIntegrationException> uriSupplier, Class responseClass, Request.Builder requestBuilder, ThrowingBiFunction, BlackDuckPageResponse, IntegrationException> responsesTransformer) throws IntegrationException {
        Optional uri = uriSupplier.apply(null);
        if (!uri.isPresent() || StringUtils.isBlank(uri.get())) {
            return Collections.emptyList();
        }
        requestBuilder.uri(uri.get());

        return responsesTransformer.apply(new PagedRequest(requestBuilder), responseClass).getItems();
    }

    private String pieceTogetherUri(String baseUrl, String spec) throws BlackDuckIntegrationException {
        URL url;
        try {
            URL baseURL = new URL(baseUrl);
            url = new URL(baseURL, spec);
        } catch (MalformedURLException e) {
            throw new BlackDuckIntegrationException(String.format("Could not construct the URL from %s and %s", baseUrl, spec), e);
        }
        return url.toString();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy