Skip to content
Snippets Groups Projects
Commit 8cabb545 authored by Emmanuel Bruno's avatar Emmanuel Bruno
Browse files

fixes pages computation.

parent 4da52086
No related branches found
No related tags found
No related merge requests found
......@@ -20,7 +20,7 @@ public class Page<T> {
this.pageNumber = pageNumber;
this.elementTotal = elementTotal;
this.content = content;
this.pageTotal = elementTotal / pageSize;
this.pageTotal = (elementTotal+pageSize-1) / pageSize;
}
public static <V> Page<V> newInstance(long pageSize, long pageNumber, long elementTotal, List<V> content) {
......
package fr.univtln.bruno.samples.jaxrs.security.filter.response;
import fr.univtln.bruno.samples.jaxrs.model.Page;
import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.container.ContainerRequestContext;
import jakarta.ws.rs.container.ContainerResponseContext;
import jakarta.ws.rs.container.ContainerResponseFilter;
import jakarta.ws.rs.core.Link;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.UriInfo;
import jakarta.ws.rs.ext.Provider;
import lombok.extern.java.Log;
import java.util.ArrayList;
import java.util.List;
@Provider
@Log
public class PaginationLinkFilter implements ContainerResponseFilter {
public static final String JAXRS_SAMPLE_TOTAL_COUNT = "JAXRS_Sample-Total-Count";
public static final String JAXRS_SAMPLE_PAGE_COUNT = "JAXRS_Sample-Page-Count";
......@@ -34,6 +38,10 @@ public class PaginationLinkFilter implements ContainerResponseFilter {
UriInfo uriInfo = requestContext.getUriInfo();
Page entity = (Page) responseContext.getEntity();
log.info("-->"+entity.getPageNumber()+"/"+entity.getPageTotal());
if (entity.getPageNumber()>entity.getPageTotal())
throw new WebApplicationException(Response.Status.NOT_FOUND);
//We replace the entity by the content of the page (we remove the envelope).
responseContext.setEntity(entity.getContent());
......@@ -41,7 +49,7 @@ public class PaginationLinkFilter implements ContainerResponseFilter {
//We add the need semantic links in the header
//Not on the first page
if (entity.getPageSize() > FIRST_PAGE) {
if (entity.getPageNumber() > FIRST_PAGE) {
linksList.add(Link.fromUriBuilder(uriInfo.getRequestUriBuilder()
.replaceQueryParam(PAGE_QUERY_PARAM,
entity.getPageNumber() - 1))
......@@ -54,7 +62,7 @@ public class PaginationLinkFilter implements ContainerResponseFilter {
.build());
}
//Not on the last
if (entity.getPageSize() < entity.getPageTotal()) {
if (entity.getPageNumber() < entity.getPageTotal()) {
linksList.add(Link.fromUriBuilder(uriInfo.getRequestUriBuilder()
.replaceQueryParam(PAGE_QUERY_PARAM,
entity.getPageNumber() + 1))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment