XmlVersionResource.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 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.WebApplicationException;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response.Status;
import lombok.NonNull;

/**
 * The class <code>XmlVersionResource</code> contains the controller for the
 * version resource.
 *
 * @author <a href="mailto:gene@ctan.org">Gerd Neugebauer</a>
 */
@Path("/")
@Produces({MediaType.APPLICATION_XML})
public class XmlVersionResource {

    /**
     * The constant <code>VALUES</code> contains the acceptable values for the
     * version number.
     */
    private static final List<String> VALUES =
        List.of("1.1", "1.2", "1.3", "2.0", "2.1", "3.0");

    /**
     * The method <code>getVersion</code> provides means to retrieve the version
     * number of the API in use.
     *
     * @param vers the version
     * @return the version number
     */
    @GET
    @Path("/xml/{vers}/version")
    @PermitAll
    public String getVersion(@NonNull @PathParam("vers") String vers) {

        if (!VALUES.contains(vers)) {
            throw new WebApplicationException(Status.NOT_FOUND);
        }
        var xml = new XmlWriter();
        xml.out("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
        xml.out("<version>", vers, "</version>\n");
        return xml.toString();
    }
}