GuestBook3Resource.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.site;
import java.time.LocalDateTime;
import org.ctan.site.domain.site.GuestBook;
import org.ctan.site.stores.GuestBookStore;
import org.ctan.site.stores.base.GeneralPage;
import com.fasterxml.jackson.annotation.JsonFormat;
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.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.core.MediaType;
import lombok.Builder;
import lombok.Getter;
import lombok.NonNull;
/**
* The class <code>GuestBookResource</code> contains the controller for the
* guest book resource.
*
* @author <a href="mailto:gene@ctan.org">Gerd Neugebauer</a>
*/
@Path("/3.0")
@Produces(MediaType.APPLICATION_JSON)
public class GuestBook3Resource {
/**
* The class <code>GuestBookTo</code> contains the transport object for the
* guest book resource.
*/
@Getter
@Builder
@SuppressFBWarnings(value = "EI_EXPOSE_REP")
protected static class GuestBookItemTo {
private long id;
private String title;
private String text;
private String name;
private String email;
@JsonFormat(shape = JsonFormat.Shape.STRING,
pattern = "yyyy-MM-dd HH:mm")
private LocalDateTime created;
}
/**
* The field <code>store</code> contains the guest book store.
*/
private GuestBookStore store;
/**
* This is the constructor for <code>GuestBook3Resource</code>.
*
* @param store the guest book store
*/
@SuppressFBWarnings(value = {"CT_CONSTRUCTOR_THROW", "EI_EXPOSE_REP2"})
public GuestBook3Resource(@NonNull GuestBookStore store) {
this.store = store;
}
/**
* The method <code>retrieve</code> provides means to retrieve a guest book
* item.
*
* @param id the id
* @return the object or {@code null}
*/
@GET
@Path("/guest-book/item/{id}")
@PermitAll
@UnitOfWork(value = "siteDb")
public GuestBook get(@NonNull @PathParam("id") Long id) {
return store.get(id);
}
/**
* The method <code>list</code> provides means to retrieve a paged list of
* guest book entries.
*
* @param q the query
* @param page the page
* @param size the page size
* @param orderBy order column
* @param asc indicator for ascending
* @return the page
*/
@GET
@Path("/guest-book/items")
@PermitAll
@UnitOfWork(value = "siteDb")
public GeneralPage list(@QueryParam("q") String q,
@QueryParam("page") int page,
@QueryParam("size") int size,
@QueryParam("order") String orderBy,
@QueryParam("asc") boolean asc) {
return store.list(q, page - 1, size, orderBy, asc);
}
// /**
// * The method <code>unhtml</code> provides means to sanitise HTML.
// *
// * @param s the string to sanitise
// * @return the sanitised version
// */
// private String unhtml(String s) {
//
// return s
// .replaceAll("<[^<]*>", "")
// .replaceAll("<", "<")
// .replaceAll(">", ">")
// .replaceAll(""", "'")
// .replaceAll("&[^;]*;", "");
// }
/**
* The method <code>save</code> provides an end-point to create a guest book
* item.
*
* @param guestbook the object to store
* @return the adjusted entity
*/
@POST
@Path("/guest-book/item")
@PermitAll
@UnitOfWork(value = "siteDb")
public GuestBook save(@NonNull GuestBook guestbook) {
var date = LocalDateTime.now();
if (guestbook.getDateCreated() == null) {
guestbook.setDateCreated(date);
}
guestbook.setLastUpdated(date);
return store.save(guestbook);
}
}