Search in sources :

Example 1 with IContentData

use of org.eclipse.dash.licenses.IContentData in project dash-licenses by eclipse.

the class EclipseFoundationSupport method queryLicenseData.

@Override
public void queryLicenseData(Collection<IContentId> ids, Consumer<IContentData> consumer) {
    if (ids.isEmpty())
        return;
    String url = settings.getLicenseCheckUrl();
    if (url.isBlank()) {
        logger.debug("Bypassing Eclipse Foundation.");
        return;
    }
    logger.info("Querying Eclipse Foundation for license data for {} items.", ids.size());
    String form = encodeRequestPayload(ids);
    int code = httpClientService.post(url, "application/x-www-form-urlencoded", form, response -> {
        AtomicInteger counter = new AtomicInteger();
        JsonReader reader = Json.createReader(new StringReader(response));
        JsonObject read = (JsonObject) reader.read();
        JsonObject approved = read.getJsonObject("approved");
        if (approved != null)
            approved.forEach((key, each) -> {
                FoundationData data = new FoundationData(each.asJsonObject());
                logger.debug("EF approved: {} ({}) score: {} {} {}", data.getId(), data.getRule(), data.getScore(), data.getLicense(), data.getAuthority());
                consumer.accept(data);
                counter.incrementAndGet();
            });
        JsonObject restricted = read.getJsonObject("restricted");
        if (restricted != null)
            restricted.forEach((key, each) -> {
                FoundationData data = new FoundationData(each.asJsonObject());
                logger.debug("EF restricted: {} score: {} {} {}", data.getId(), data.getScore(), data.getLicense(), data.getAuthority());
                consumer.accept(data);
                counter.incrementAndGet();
            });
        logger.info("Found {} items.", counter.get());
    });
    if (code != 200) {
        logger.error("Error response from the Eclipse Foundation {}", code);
        throw new RuntimeException("Received an error response from the Eclipse Foundation.");
    }
}
Also used : Logger(org.slf4j.Logger) Collection(java.util.Collection) LoggerFactory(org.slf4j.LoggerFactory) JsonArrayBuilder(jakarta.json.JsonArrayBuilder) IContentId(org.eclipse.dash.licenses.IContentId) ISettings(org.eclipse.dash.licenses.ISettings) StandardCharsets(java.nio.charset.StandardCharsets) Json(jakarta.json.Json) Consumer(java.util.function.Consumer) Inject(javax.inject.Inject) IContentData(org.eclipse.dash.licenses.IContentData) JsonObjectBuilder(jakarta.json.JsonObjectBuilder) URLEncoder(java.net.URLEncoder) StringReader(java.io.StringReader) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) JsonObject(jakarta.json.JsonObject) IHttpClientService(org.eclipse.dash.licenses.http.IHttpClientService) JsonReader(jakarta.json.JsonReader) JsonArray(jakarta.json.JsonArray) ILicenseDataProvider(org.eclipse.dash.licenses.ILicenseDataProvider) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StringReader(java.io.StringReader) JsonReader(jakarta.json.JsonReader) JsonObject(jakarta.json.JsonObject)

Example 2 with IContentData

use of org.eclipse.dash.licenses.IContentData in project dash-licenses by eclipse.

the class ClearlyDefinedSupportTests method testMatchRestricted.

@Test
void testMatchRestricted() {
    List<IContentData> results = new ArrayList<>();
    clearlyDefined.queryLicenseData(Collections.singleton(ContentId.getContentId("npm/npmjs/@yarnpkg/lockfile/1.1.0")), data -> results.add(data));
    assertEquals(1, results.size());
    IContentData write = results.get(0);
    assertEquals("npm/npmjs/@yarnpkg/lockfile/1.1.0", write.getId().toString());
    assertEquals("clearlydefined", write.getAuthority());
    assertEquals("BSD-2-Clause", write.getLicense());
    assertEquals("https://clearlydefined.io/definitions/npm/npmjs/@yarnpkg/lockfile/1.1.0", write.getUrl());
    assertEquals(53, write.getScore());
    assertEquals(LicenseSupport.Status.Restricted, write.getStatus());
}
Also used : IContentData(org.eclipse.dash.licenses.IContentData) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test)

Example 3 with IContentData

use of org.eclipse.dash.licenses.IContentData in project dash-licenses by eclipse.

the class ClearlyDefinedSupportTests method testMatchApproved.

@Test
void testMatchApproved() {
    List<IContentData> results = new ArrayList<>();
    clearlyDefined.queryLicenseData(Collections.singleton(ContentId.getContentId("npm/npmjs/-/write/1.0.3")), data -> results.add(data));
    assertEquals(1, results.size());
    IContentData write = results.get(0);
    assertEquals("npm/npmjs/-/write/1.0.3", write.getId().toString());
    assertEquals("clearlydefined", write.getAuthority());
    assertEquals("MIT", write.getLicense());
    assertEquals("https://clearlydefined.io/definitions/npm/npmjs/-/write/1.0.3", write.getUrl());
    assertEquals(94, write.getScore());
    assertEquals(LicenseSupport.Status.Approved, write.getStatus());
}
Also used : IContentData(org.eclipse.dash.licenses.IContentData) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test)

Aggregations

IContentData (org.eclipse.dash.licenses.IContentData)3 ArrayList (java.util.ArrayList)2 Test (org.junit.jupiter.api.Test)2 Json (jakarta.json.Json)1 JsonArray (jakarta.json.JsonArray)1 JsonArrayBuilder (jakarta.json.JsonArrayBuilder)1 JsonObject (jakarta.json.JsonObject)1 JsonObjectBuilder (jakarta.json.JsonObjectBuilder)1 JsonReader (jakarta.json.JsonReader)1 StringReader (java.io.StringReader)1 URLEncoder (java.net.URLEncoder)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Collection (java.util.Collection)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Consumer (java.util.function.Consumer)1 Inject (javax.inject.Inject)1 IContentId (org.eclipse.dash.licenses.IContentId)1 ILicenseDataProvider (org.eclipse.dash.licenses.ILicenseDataProvider)1 ISettings (org.eclipse.dash.licenses.ISettings)1 IHttpClientService (org.eclipse.dash.licenses.http.IHttpClientService)1