ShelfAjaxController.java
package info.textgrid.rep.shelf;
import info.textgrid.rep.session.SessionService;
import jakarta.enterprise.context.RequestScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.CookieParam;
import jakarta.ws.rs.FormParam;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.NewCookie;
import jakarta.ws.rs.core.Response;
@Path("/service/shelf")
@RequestScoped
public class ShelfAjaxController {
@Inject
private SessionService sessionService;
@POST
@Path("/add")
@Produces(MediaType.APPLICATION_JSON)
public Response addToShelf(
@CookieParam("session") String sessionId,
@FormParam("uri") String textgridUri) {
String actualSessionId = sessionService.getOrCreateSessionId(sessionId);
Shelf shelf = sessionService.getShelf(actualSessionId);
int count = shelf.addItem(textgridUri);
NewCookie sessionCookie = sessionService.createSessionCookie(actualSessionId);
return Response.ok(count).cookie(sessionCookie).build();
}
@POST
@Path("/remove")
@Produces(MediaType.APPLICATION_JSON)
public Response removeFromShelf(
@CookieParam("session") String sessionId,
@FormParam("uri") String textgridUri) {
String actualSessionId = sessionService.getOrCreateSessionId(sessionId);
Shelf shelf = sessionService.getShelf(actualSessionId);
long count = shelf.remove(textgridUri);
NewCookie sessionCookie = sessionService.createSessionCookie(actualSessionId);
return Response.ok(count).cookie(sessionCookie).build();
}
@POST
@Path("/clear")
@Produces(MediaType.APPLICATION_JSON)
public Response clearShelf(@CookieParam("session") String sessionId) {
String actualSessionId = sessionService.getOrCreateSessionId(sessionId);
Shelf shelf = sessionService.getShelf(actualSessionId);
long count = shelf.clear();
NewCookie sessionCookie = sessionService.createSessionCookie(actualSessionId);
return Response.ok(count).cookie(sessionCookie).build();
}
@GET
@Path("/count")
@Produces(MediaType.APPLICATION_JSON)
public long shelfCount(@CookieParam("session") String sessionId) {
Shelf shelf = sessionService.getShelf(sessionId);
return shelf != null ? shelf.size() : 0;
}
}