MirrMon3Resource.java

/*
 * Copyright © 2023-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.mirrors;

import java.io.IOException;
import java.net.URISyntaxException;

import org.ctan.site.services.mirrors.MirrMonService;

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.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>MirrMon3Resource</code> contains the controller for the
 * MirrMon resource.
 *
 * @author <a href="mailto:gene@ctan.org">Gerd Neugebauer</a>
 */
@Path("/3.0")
@Produces({MediaType.TEXT_HTML})
public class MirrMon3Resource {

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

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

        this.service = service;
    }

    /**
     * The method <code>getAllMirrors</code> provides means to retrieve a list
     * of all mirror servers.
     *
     * @return a list of matching mirrors
     */
    @GET
    @Path("/mirr-mon")
    @PermitAll
    @UnitOfWork(value = "mirrors")
    public String getMirrMon() {

        try {
            return service.get();
        } catch (IOException | URISyntaxException e) {
            throw new WebApplicationException(e.getMessage(),
                Status.INTERNAL_SERVER_ERROR);
        }
    }
}