@@ -0,0 +1,2573 @@
+package com.sdcuike.spring.extend.web.util;%0A%0Aimport org.springframework.data.domain.Page;%0Aimport org.springframework.http.HttpHeaders;%0Aimport org.springframework.web.util.UriComponentsBuilder;%0A%0Aimport javax.servlet.http.HttpServletResponse;%0Aimport java.net.URISyntaxException;%0Aimport java.util.List;%0Aimport java.util.Map.Entry;%0A%0A/**%0A * Utility class for handling pagination.%0A * %3Cp%3E%0A * %3Cp%3E%0A * Pagination uses the same principles as the %3Ca href=%22https://developer.github.com/v3/#pagination%22%3EGithub API%3C/a%3E,%0A * and follow %3Ca href=%22http://tools.ietf.org/html/rfc5988%22%3ERFC 5988 (Link header)%3C/a%3E.%0A */%0Apublic final class PaginationUtil %7B%0A    %0A    public static void setPaginationHttpHeaders(Page%3C?%3E page, String baseUrl, HttpServletResponse response) throws URISyntaxException %7B%0A        HttpHeaders httpHeaders = generatePaginationHttpHeaders(page, baseUrl);%0A        %0A        for (Entry%3CString, List%3CString%3E%3E entry : httpHeaders.entrySet()) %7B%0A            String name = entry.getKey();%0A            for (String value : entry.getValue()) %7B%0A                response.addHeader(name, value);%0A            %7D%0A        %7D%0A    %7D%0A    %0A    public static HttpHeaders generatePaginationHttpHeaders(Page page, String baseUrl)%0A            throws URISyntaxException %7B%0A        %0A        HttpHeaders headers = new HttpHeaders();%0A        headers.add(%22X-Total-Count%22, %22%22 + Long.toString(page.getTotalElements()));%0A        String link = %22%22;%0A        if ((page.getNumber() + 1) %3C page.getTotalPages()) %7B%0A            link = %22%3C%22 + generateUri(baseUrl, page.getNumber() + 1, page.getSize()) + %22%3E; rel=%5C%22next%5C%22,%22;%0A        %7D%0A        // prev link%0A        if ((page.getNumber()) %3E 0) %7B%0A            link += %22%3C%22 + generateUri(baseUrl, page.getNumber() - 1, page.getSize()) + %22%3E; rel=%5C%22prev%5C%22,%22;%0A        %7D%0A        // last and first link%0A        int lastPage = 0;%0A        if (page.getTotalPages() %3E 0) %7B%0A            lastPage = page.getTotalPages() - 1;%0A        %7D%0A        link += %22%3C%22 + generateUri(baseUrl, lastPage, page.getSize()) + %22%3E; rel=%5C%22last%5C%22,%22;%0A        link += %22%3C%22 + generateUri(baseUrl, 0, page.getSize()) + %22%3E; rel=%5C%22first%5C%22%22;%0A        headers.add(HttpHeaders.LINK, link);%0A        return headers;%0A    %7D%0A    %0A    private static String generateUri(String baseUrl, int page, int size) throws URISyntaxException %7B%0A        return UriComponentsBuilder%0A                .fromUriString(baseUrl)%0A                .queryParam(%22page%22, page)%0A                .queryParam(%22size%22, size)%0A                .toUriString();%0A    %7D%0A    %0A    %0A    private PaginationUtil() %7B%0A        throw new UnsupportedOperationException();%0A    %7D%0A%7D%0A

Add PaginationUtil