Bibtex3Resource.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;

import org.ctan.site.services.catalogue.BibtexService;
import org.ctan.site.services.catalogue.BibtexService.BibtexConfig;

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.NonNull;

/**
 * The class <code>Bibtex3Resource</code> contains the controller for the bibtex
 * resource.
 *
 * @author <a href="mailto:gene@ctan.org">Gerd Neugebauer</a>
 */
@Path("/3.0")
public class Bibtex3Resource {

    /**
     * The field <code>service</code> contains the underlying service.
     */
    private BibtexService service;

    /**
     * This is the constructor for the class <code>Bibtex3Resource</code>.
     *
     * @param service the underlying service
     */
    @SuppressFBWarnings(value = {"CT_CONSTRUCTOR_THROW", "EI_EXPOSE_REP2"})
    public Bibtex3Resource(@NonNull BibtexService service) {

        this.service = service;
    }

    /**
     * The method <code>getBibtex</code> contains the end-point for /bibtex/key.
     * It returns the BibTeX file.
     *
     * @param key the key
     * @param keyPrefix the key prefix
     * @param omitAbstract indicator to omit the abstract
     * @param useString indicator to use the @String definition
     * @param type the type of the entry
     * @return the BibTeX file
     */
    @GET
    @Path("/bibtex/{key}")
    @PermitAll
    @UnitOfWork(value = "siteDb")
    @Produces(MediaType.TEXT_PLAIN)
    public String getBibtex(@NonNull @PathParam("key") String key,
        @QueryParam("key-prefix") String keyPrefix,
        @QueryParam("omit-abstract") boolean omitAbstract,
        @QueryParam("use-string") boolean useString,
        @QueryParam("type") String type) {

        var cfg = BibtexConfig.builder()
            .addAbstract(!omitAbstract)
            .useString(useString)
            .keyPrefix(keyPrefix != null ? keyPrefix : "pkg:")
            .type(type)
            .build();
        var bibtex = service.toBibtex(cfg, new String[]{key});
        if (bibtex == null) {
            throw new WebApplicationException(Status.NOT_FOUND);
        }
        return bibtex;
    }

    /**
     * The method <code>getBibtexAll</code> contains the end-point for /bibtex.
     * It returns the BibTeX file.
     *
     * <p>
     * <b>Attention</b>: This service is too slow to be exposable!
     * </p>
     *
     * @param keyPrefix the key prefix
     * @param omitAbstract indicator to omit the abstract
     * @param useString indicator to use the @String definition
     * @param type the type of the entry
     * @return the BibTeX file
     */
    @GET
    @Path("/bibtex")
    @PermitAll
    @UnitOfWork(value = "siteDb")
    @Produces("text/plain;charset=UTF-8")
    public String getBibtexAll(
        @QueryParam("key-prefix") String keyPrefix,
        @QueryParam("omit-abstract") boolean omitAbstract,
        @QueryParam("use-string") boolean useString,
        @QueryParam("type") String type) {

        var cfg = BibtexConfig.builder()
            .addAbstract(!omitAbstract)
            .useString(useString)
            .transitiveClosure(false)
            .keyPrefix(keyPrefix != null ? keyPrefix : "pkg:")
            .type(type)
            .build();
        return service.toBibtex(cfg);
    }
}