tech.jhipster.web.util.PaginationUtil Maven / Gradle / Ivy
/*
* Copyright 2016-2023 the original author or authors from the JHipster project.
*
* This file is part of the JHipster project, see https://www.jhipster.tech/
* for more information.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package tech.jhipster.web.util;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpHeaders;
import org.springframework.web.util.UriComponentsBuilder;
/**
* Utility class for handling pagination.
*
*
* Pagination uses the same principles as the GitHub API,
* and follow RFC 5988 (Link header).
*/
public final class PaginationUtil {
private static final String HEADER_X_TOTAL_COUNT = "X-Total-Count";
private static LinkHeaderUtil linkHeaderUtil = new LinkHeaderUtil();
private PaginationUtil() {}
/**
* Generate pagination headers for a Spring Data {@link org.springframework.data.domain.Page} object.
*
* @param uriBuilder The URI builder.
* @param page The page.
* @param The type of object.
* @return http header.
*/
public static HttpHeaders generatePaginationHttpHeaders(UriComponentsBuilder uriBuilder, Page page) {
HttpHeaders headers = new HttpHeaders();
headers.add(HEADER_X_TOTAL_COUNT, Long.toString(page.getTotalElements()));
headers.add(HttpHeaders.LINK, linkHeaderUtil.prepareLinkHeaders(uriBuilder, page));
return headers;
}
}