MirrorRegistration3Resource.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 org.ctan.site.domain.mirrors.MirrorRegistration;
import org.ctan.site.services.DateUtils;
import org.ctan.site.services.mail.MailException;
import org.ctan.site.services.mirrors.MirrorRegistrationService;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import io.dropwizard.hibernate.UnitOfWork;
import jakarta.annotation.security.PermitAll;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;

/**
 * The class <code>MirrorRegistration3Resource</code> contains the controller
 * for the mirror registration resource.
 *
 * @author <a href="mailto:gene@ctan.org">Gerd Neugebauer</a>
 */
@Path("/3.0")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Slf4j
public class MirrorRegistration3Resource {

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

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

        this.service = service;
    }

    /**
     * The method <code>postMirrorRegister</code> provides means to save a
     * mirror registration.
     *
     * @param registration the registration
     * @return The current date as String
     */
    @POST
    @Path("/mirror/register")
    @PermitAll
    @UnitOfWork(value = "siteDb")
    public String mirrorRegistration(
        @NonNull MirrorRegistration registration) {

        try {
            registration = service.registration(registration);
        } catch (MailException e) {
            log.error(e.getMessage());
            return "\"\"";
        }
        return "\""
            + DateUtils.formatDate(registration.getDateCreated())
            + "\"";
    }
}