XmlTopicResource.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.stream.Collectors;
import org.ctan.site.stores.TopicStore;
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>XmlTopicResource</code> contains the controller for the topic
* resource.
*
* @author <a href="mailto:gene@ctan.org">Gerd Neugebauer</a>
*/
@Path("/")
@Produces(MediaType.APPLICATION_XML)
public class XmlTopicResource {
/**
* The field <code>store</code> contains the underlying repository.
*/
private TopicStore store;
/**
* This is the constructor for the class <code>XmlAuthorResource</code>.
*
* @param store the underlying store
*/
@SuppressFBWarnings(value = {"CT_CONSTRUCTOR_THROW", "EI_EXPOSE_REP2"})
public XmlTopicResource(@NonNull TopicStore store) {
this.store = store;
}
/**
* The method <code>getTopicByKey</code> provides means to retrieve a topic.
*
* @param vers the version
* @param id the key of the author
* @param ref the indicator whether or not to return the references to the
* packages authored by the author
* @return an author or {@code null}
*/
@GET
@Path("/xml/{vers}/topic/{id}")
@PermitAll
@UnitOfWork(value = "siteDb")
public String getTopicByKey(
@NonNull @PathParam("vers") String vers,
@NonNull @PathParam("id") String id,
@QueryParam("ref") @DefaultValue("false") Boolean ref,
@QueryParam("no-dtd") @DefaultValue("false") Boolean noDtd,
@QueryParam("no-xml") @DefaultValue("false") Boolean noXml) {
switch (vers) {
case "1.0", "1.1", "1.2", "1.3", "2.0", "2.1":
break;
default:
throw new WebApplicationException(Status.NOT_FOUND);
}
var top = store.getByKey(id);
if (top == null) {
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 topic SYSTEM 'http://www.ctan.org/xml/" + vers
+ "/catalogue.dtd'>\n");
}
xml.out("<topic key=\"", id,
"\" " + "details=\"" + top.getDetails("en") + "\"");
if (ref) {
xml.out(">\n");
var packages = top.getPackages().stream()
.map(r -> r.getKey())
.sorted()
.collect(Collectors.toList())
.toArray(new String[]{});
for (var p : packages) {
xml.out(" <package key=\"", p, "\" >\n");
}
xml.out("</topic>\n");
} else {
xml.out(" />\n");
}
return xml.toString();
}
/**
* The method <code>getTopics</code> provides means to retrieve a list of
* topics starting with a given pattern.
*
* @param vers the version number
* @param key the key
* @return a list of matching author summaries
*/
@GET
@Path("/xml/{vers}/topics")
@PermitAll
@UnitOfWork(value = "siteDb")
public String getTopics(
@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.0", "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 topics SYSTEM 'http://www.ctan.org/xml/" + vers
+ "/catalogue.dtd'>\n");
}
var list = store.findAllByKeyStartingWith(key != null ? key : "");
xml.out("<topics>\n");
for (var a : list) {
xml.out(" <topic key=\"", a.getKey(),
"\" " + "details=\"" + a.getDetails("en") + "\" />\n");
}
xml.out("</topics>\n");
return xml.toString();
}
}