CrudUpload3Resource.java
/*
* Copyright © 2024-2026 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.admin;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Map;
import org.ctan.site.domain.catalogue.Upload;
import org.ctan.site.services.upload.UploadService;
import org.ctan.site.stores.base.GeneralPage;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import io.dropwizard.hibernate.UnitOfWork;
import jakarta.ws.rs.DELETE;
import jakarta.ws.rs.DefaultValue;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
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>CrudUpload3Resource</code> contains the CRUD controller for
* the upload resource.
*
* @author <a href="mailto:gene@ctan.org">Gerd Neugebauer</a>
*/
@Path("/3.0/admin")
@Produces(MediaType.APPLICATION_JSON)
// TODO @RolesAllowed("ADMIN")
public class CrudUpload3Resource {
/**
* The field <code>service</code> contains the underlying service.
*/
private UploadService service;
/**
* This is the constructor for the class <code>CrudUpload3Resource</code>.
*
* @param uploadService the underlying service
*/
@SuppressFBWarnings(value = {"CT_CONSTRUCTOR_THROW", "EI_EXPOSE_REP2"})
public CrudUpload3Resource(@NonNull UploadService uploadService) {
this.service = uploadService;
}
/**
* The method <code>incomingData</code> provides means to retrieve the data
* for an incoming package.
*
* @param name the name of the directory
* @return the data {@code null}
*/
@GET
@Path("/incoming/{name}")
public Map<String, String> incomingData(
@NonNull @PathParam("name") String name) {
try {
return service.incomingData(name);
} catch (FileNotFoundException e) {
throw new WebApplicationException(e.getMessage(),
Status.NOT_FOUND);
} catch (IOException e) {
throw new WebApplicationException(e.getMessage(),
Status.BAD_REQUEST);
}
}
/**
* The method <code>incomingList</code> provides means to retrieve a page of
* incomings.
*
* @param q the name pattern
* @param page the page
* @param size the page size
* @param orderBy the order
* @param asc the indicator for ascending
* @return the page for the uploads or {@code null}
* @throws FileNotFoundException in case of a missing file
*/
@GET
@Path("/incomings")
public GeneralPage incomingList(@QueryParam("q") String q,
@DefaultValue("1") @QueryParam("page") int page,
@DefaultValue("64") @QueryParam("size") int size,
@QueryParam("order") String orderBy,
@QueryParam("asc") boolean asc)
throws FileNotFoundException {
return service.listIncoming(q, page - 1, size, orderBy, asc);
}
/**
* The method <code>uploadCreate</code> provides an end-point to create an
* upload.
*
* @param upload the upload object to store
* @return the updated entity
*/
@POST
@Path("/upload")
@UnitOfWork(value = "siteDb")
public Upload uploadCreate(@NonNull Upload upload) {
return service.save(upload);
}
/**
* The method <code>uploadList</code> provides means to retrieve a page of
* uploads.
*
* @param q the name pattern
* @param page the page
* @param size the page size
* @param orderBy the order
* @param asc the indicator for ascending
* @return the page for the uploads or {@code null}
*/
@GET
@Path("/uploads")
@UnitOfWork(value = "siteDb")
public GeneralPage uploadList(@QueryParam("q") String q,
@DefaultValue("1") @QueryParam("page") int page,
@DefaultValue("64") @QueryParam("size") int size,
@QueryParam("order") String orderBy,
@QueryParam("asc") boolean asc) {
return service.list(q, page - 1, size, orderBy, asc);
}
/**
* The method <code>remove</code> provides an end-point to remove a user.
*
* @param id the id
* @return {@code true} iff the user has existed
*/
@DELETE
@Path("/upload/{id}")
@UnitOfWork(value = "siteDb")
public boolean uploadRemove(@NonNull @PathParam("id") Long id) {
return service.remove(id);
}
// /**
// * The method <code>retrieve</code> provides means to retrieve a user.
// *
// * @param stop the stop word
// * @return {@code true} iff the user exists
// */
// @GET
// @Path("/upload/by-word/{stopword}")
// @PermitAll
// @UnitOfWork(value = "siteDb")
// public Upload retrieve(
// @NonNull @PathParam("stopword") String stopword) {
//
// return store.getByStopWord(stopword);
// }
// /**
// * The method <code>set</code> provides means to set a single attribute of
// a
// * user.
// *
// * @param account the account
// * @param map the mapping containing key and value
// * @return {@code true} iff the setting has succeeded
// */
// @PUT
// @Path("/upload/{id}")
// @PermitAll
// @UnitOfWork(value = "siteDb")
// public boolean set(@NonNull @PathParam("id") Long id,
// @NonNull String value) {
//
// return store.set(id, value);
// }
/**
* The method <code>uploadRetrieve</code> provides means to retrieve a
* upload.
*
* @param id the id
* @return {@code true} iff the upload exists
*/
@GET
@Path("/upload/{id}")
@UnitOfWork(value = "siteDb")
public Upload uploadRetrieve(@NonNull @PathParam("id") Long id) {
return service.get(id);
}
}