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

com.synopsys.integration.blackduck.http.transform.BlackDuckResponsesTransformer 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: 66.2.19
Show newest version
/*
 * blackduck-common
 *
 * Copyright (c) 2023 Synopsys, Inc.
 *
 * Use subject to the terms and conditions of the Synopsys End User Software License and Maintenance Agreement. All rights reserved worldwide.
 */
package com.synopsys.integration.blackduck.http.transform;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import org.apache.commons.lang3.math.NumberUtils;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.synopsys.integration.blackduck.api.core.BlackDuckResponse;
import com.synopsys.integration.blackduck.api.core.response.UrlMultipleResponses;
import com.synopsys.integration.blackduck.exception.BlackDuckIntegrationException;
import com.synopsys.integration.blackduck.http.BlackDuckPageResponse;
import com.synopsys.integration.blackduck.http.BlackDuckRequestBuilder;
import com.synopsys.integration.blackduck.http.client.BlackDuckHttpClient;
import com.synopsys.integration.blackduck.service.request.BlackDuckRequest;
import com.synopsys.integration.exception.IntegrationException;
import com.synopsys.integration.rest.response.Response;

public class BlackDuckResponsesTransformer {
    private final BlackDuckHttpClient blackDuckHttpClient;
    private final BlackDuckJsonTransformer blackDuckJsonTransformer;

    public BlackDuckResponsesTransformer(BlackDuckHttpClient blackDuckHttpClient, BlackDuckJsonTransformer blackDuckJsonTransformer) {
        this.blackDuckHttpClient = blackDuckHttpClient;
        this.blackDuckJsonTransformer = blackDuckJsonTransformer;
    }

    public  BlackDuckPageResponse getSomeMatchingResponses(BlackDuckRequest> requestMultiple, Predicate predicate, int totalLimit) throws IntegrationException {
        return getInternalMatchingResponse(requestMultiple, totalLimit, predicate);
    }

    public  BlackDuckPageResponse getAllResponses(BlackDuckRequest> requestMultiple) throws IntegrationException {
        return getInternalMatchingResponse(requestMultiple, Integer.MAX_VALUE, alwaysTrue());
    }

    public  BlackDuckPageResponse getSomeResponses(BlackDuckRequest> requestMultiple, int totalLimit) throws IntegrationException {
        return getInternalMatchingResponse(requestMultiple, totalLimit, alwaysTrue());
    }

    public  BlackDuckPageResponse getOnePageOfResponses(BlackDuckRequest> requestMultiple) throws IntegrationException {
        return getInternalMatchingResponse(requestMultiple, getLimit(requestMultiple), alwaysTrue());
    }

    private  Predicate alwaysTrue() {
        return (blackDuckResponse) -> true;
    }

    private  BlackDuckPageResponse getInternalMatchingResponse(BlackDuckRequest> requestMultiple, int maxToReturn, Predicate predicate) throws IntegrationException {
        List allResponses = new LinkedList<>();
        int totalCount = 0;

        int limit = getLimit(requestMultiple);
        int offset = getOffset(requestMultiple);
        try (Response initialResponse = blackDuckHttpClient.execute(requestMultiple)) {
            blackDuckHttpClient.throwExceptionForError(initialResponse);
            String initialJsonResponse = initialResponse.getContentString();
            BlackDuckPageResponse blackDuckPageResponse = blackDuckJsonTransformer.getResponses(initialJsonResponse, requestMultiple.getResponseClass());

            allResponses.addAll(this.matchPredicate(blackDuckPageResponse, predicate));

            totalCount = blackDuckPageResponse.getTotalCount();
            int totalItemsToRetrieve = Math.min(totalCount, maxToReturn);

            while (allResponses.size() < totalItemsToRetrieve && offset < totalCount) {
                offset = offset + limit;
                requestMultiple = nextPage(requestMultiple, offset);
                try (Response response = blackDuckHttpClient.execute(requestMultiple)) {
                    blackDuckHttpClient.throwExceptionForError(response);
                    String jsonResponse = response.getContentString();
                    blackDuckPageResponse = blackDuckJsonTransformer.getResponses(jsonResponse, requestMultiple.getResponseClass());
                    allResponses.addAll(this.matchPredicate(blackDuckPageResponse, predicate));
                } catch (IOException e) {
                    throw new BlackDuckIntegrationException(e);
                }
            }

            allResponses = onlyReturnMaxRequested(maxToReturn, allResponses);
            return new BlackDuckPageResponse<>(totalCount, allResponses);
        } catch (IOException e) {
            throw new BlackDuckIntegrationException(e.getMessage(), e);
        }
    }

    private  BlackDuckRequest> nextPage(BlackDuckRequest> blackDuckRequest, int offset) {
        BlackDuckRequestBuilder blackDuckRequestBuilder = new BlackDuckRequestBuilder(blackDuckRequest);
        blackDuckRequestBuilder.setOffset(offset);

        return new BlackDuckRequest<>(blackDuckRequestBuilder, blackDuckRequest.getUrlResponse());
    }

    @NotNull
    private  List onlyReturnMaxRequested(int maxToReturn, List allResponses) {
        return allResponses.stream().limit(maxToReturn).collect(Collectors.toList());
    }

    private  List matchPredicate(BlackDuckPageResponse blackDuckPageResponse, Predicate predicate) {
        return blackDuckPageResponse
                   .getItems()
                   .stream()
                   .filter(predicate)
                   .collect(Collectors.toList());
    }

    public int getLimit(BlackDuckRequest blackDuckRequest) {
        return retrieveValue(blackDuckRequest.getRequest().getQueryParameters()::get, BlackDuckRequestBuilder.LIMIT_PARAMETER, BlackDuckRequestBuilder.DEFAULT_LIMIT);
    }

    public int getOffset(BlackDuckRequest blackDuckRequest) {
        return retrieveValue(blackDuckRequest.getRequest().getQueryParameters()::get, BlackDuckRequestBuilder.OFFSET_PARAMETER, BlackDuckRequestBuilder.DEFAULT_OFFSET);
    }

    private int retrieveValue(Function> valueCollection, String key, int defaultValue) {
        return NumberUtils.toInt(valueCollection.apply(key).stream().findFirst().orElse(Integer.toString(defaultValue)));
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy