Mirrors3Resource.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.util.List;
import java.util.stream.Collectors;

import org.ctan.site.services.DateUtils;
import org.ctan.site.services.mirrors.MirrorService;

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.core.MediaType;
import lombok.Builder;
import lombok.Getter;
import lombok.NonNull;

/**
 * The class <code>Mirror3Resource</code> contains the controller for the
 * mirrors resource.
 *
 * @author <a href="mailto:gene@ctan.org">Gerd Neugebauer</a>
 */
@Path("/3.0")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
// @Slf4j
public class Mirrors3Resource {

    /**
     * The class <code>MirrorTo</code> contains the transport object for the
     * license resource in the licenses list.
     */
    @Getter
    @Builder
    protected static class MirrorTo {

        private Long id;

        private String hostname;

        private String path;

        private String continent;

        private String country;

        private String region;

        private String town;

        // private Mirrors masterhost;
        private String frequency;

        // private int services;
        private String dateAdded;

        private String dateRemoved;

        private String status;

        private String httpUrl;

        private String rsyncUrl;

        private String note;
    }
    // /**
    // * The field <code>timezone</code> contains the properties of timezones.
    // */
    // private Properties timezone = new Properties();

    /**
     * The field <code>store</code> contains the underlying repository.
     */
    private MirrorService 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 Mirrors3Resource(@NonNull MirrorService service) {

        this.service = service;
        // try {
        // this.timezone.load(Mirrors3Resource.class
        // .getResourceAsStream("/data/timezone2country.properties"));
        // } catch (IOException e) {
        // log.error("error loading timezones", e);
        // }
    }

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

        return service.findAll()
            .stream()
            .filter(m -> !"inactive".equals(m.getStatus().getDescription()))
            .map(mirror -> {
                return MirrorTo.builder()
                    .id(mirror.getId())
                    .hostname(mirror.getHostname())
                    .path(mirror.getPath())
                    .continent(service.mapContinent(
                        mirror.getContinent().getName()))
                    .country(service
                        .mapCountry(mirror.getCountry()
                            .getNameEnglish()))
                    .region(mirror.getRegion())
                    .town(mirror.getTown())
                    // private Mirrors masterhost;
                    .frequency(mirror.getFrequency())
                    // .services(mirror.getServices())
                    .dateAdded(DateUtils.formatDateTime(mirror.getDateAdded()))
                    .dateRemoved(
                        DateUtils.formatDateTime(mirror.getDateRemoved()))
                    .status(mirror.getStatus().getDescription())
                    .httpUrl(mirror.getHttpUrl())
                    .rsyncUrl(mirror.getRsyncUrl())
                    .note(mirror.getNote())
                    .build();
            })
            .collect(Collectors.toList());
    }
}