XmlLicenseResource.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 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.DefaultValue;
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.NonNull;
/**
* The class <code>XmlLicenseResource</code> contains the controller for the
* author resource.
*
* @author <a href="mailto:gene@ctan.org">Gerd Neugebauer</a>
*/
@Path("/")
@Produces(MediaType.APPLICATION_XML)
public class XmlLicenseResource {
/**
* The field <code>store</code> contains the underlying repository.
*/
private LicenseStore store;
/**
* This is the constructor for the class <code>XmlLicenseResource</code>.
*
* @param store the underlying store
*/
@SuppressFBWarnings(value = {"CT_CONSTRUCTOR_THROW", "EI_EXPOSE_REP2"})
public XmlLicenseResource(@NonNull LicenseStore store) {
this.store = store;
}
/**
* The method <code>getLicenses</code> provides means to retrieve a list of
* licenses starting with a given pattern.
*
* @param vers the version number
* @param key the key
* @return a list of matching license summaries
*/
@GET
@Path("/xml/{vers}/licenses")
@PermitAll
@UnitOfWork(value = "siteDb")
public String getLicenses(
@NonNull @PathParam("vers") String vers,
@QueryParam("key") String key,
@QueryParam("no-dtd") @DefaultValue("false") Boolean noDtd,
@QueryParam("no-xml") @DefaultValue("false") Boolean noXml) {
switch (vers) {
case "1.1", "1.2", "1.3", "2.0", "2.1":
break;
default:
throw new WebApplicationException(Status.NOT_FOUND);
}
var xml = new XmlWriter();
if (!noXml) {
xml.out("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
}
if (!noDtd) {
xml.out("<!DOCTYPE licenses SYSTEM 'http://www.ctan.org/xml/" + vers
+ "/catalogue.dtd'>\n");
}
var list = store.findAllByKeyStartingWith(key != null ? key : "");
xml.out("<licenses>\n");
for (var lic : list) {
xml.out(" <license key=\"", lic.getKey(), "\"");
xml.outIf(true, "name", lic.getName());
var free = lic.getFree();
xml.outIf(true, "free",
free == null ? null : free ? "true" : "false");
xml.out(" />\n");
}
xml.out("</licenses>\n");
return xml.toString();
}
}