JsonLicenseResource.java
/*
* Copyright © 2024-2025 The CTAN Team and individual authors
*
* This file is distributed under the 3-clause BSD license.
* See file LICENSE for details.
*/
package org.ctan.site.resources.catalogue.api;
import java.util.List;
import java.util.stream.Collectors;
import org.ctan.site.stores.LicenseStore;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import io.dropwizard.hibernate.UnitOfWork;
import jakarta.annotation.security.PermitAll;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response.Status;
import lombok.Builder;
import lombok.Getter;
import lombok.NonNull;
/**
* The class <code>LicenseResource</code> contains the controller for the
* license resource.
*
* @author <a href="mailto:gene@ctan.org">Gerd Neugebauer</a>
*/
@Path("/")
@Produces(MediaType.APPLICATION_JSON)
public class JsonLicenseResource {
/**
* The class <code>AuthorSummaryTo</code> contains the transport object for
* the author resource in the summary list.
*/
@Getter
@Builder
protected static class LicenseSummaryTo {
private String key;
private String name;
private boolean free;
}
/**
* The constant <code>values</code> contains the versions which support this
* end-point.
*/
static List<String> values =
List.of("1.1", "1.2", "1.3", "2.0", "2.1");
/**
* The field <code>store</code> contains the underlying repository.
*/
private LicenseStore store;
/**
* This is the constructor for <code>LicenseResource</code>.
*
* @param store the underlying store
*/
@SuppressFBWarnings(value = {"CT_CONSTRUCTOR_THROW", "EI_EXPOSE_REP2"})
public JsonLicenseResource(@NonNull LicenseStore store) {
this.store = store;
}
/**
* The method <code>getLicenses</code> provides means to retrieve the list
* of licenses.
*
* @param vers the version
* @param key the key
*
* @return the list of licenses
*/
@GET
@Path("/json/{vers}/licenses")
@PermitAll
@UnitOfWork(value = "siteDb")
public List<LicenseSummaryTo> getLicenses(
@NonNull @PathParam("vers") String vers,
@QueryParam("key") String key) {
if (!values.contains(vers)) {
throw new WebApplicationException(Status.NOT_FOUND);
}
return store.findAllByKeyStartingWith(key == null ? "" : key)
.stream()
.map(lic -> {
return LicenseSummaryTo.builder()
.key(lic.getKey())
.name(lic.getName())
.free(lic.getFree())
.build();
})
.collect(Collectors.toList());
}
}