Buggy code:
public class HALUtils { public static void addData ( Representation rep , DBObject data ) { } public static TreeMap < String , String > getPaginationLinks ( HttpServerExchange exchange , RequestContext context , long size ) throws IllegalQueryParamenterException { String requestPath = URLUtilis . removeTrailingSlashes ( exchange . getRequestPath ( ) ) ; String queryString = exchange . getQueryString ( ) ; int page = context . getPage ( ) ; int pagesize = context . getPagesize ( ) ; long totalPages = 0 ; if ( size >= 0 ) { float _size = size + 0.0F ; float _pagesize = pagesize + 0.0F ; totalPages = Math . max ( 1 , Math . round ( Math . ceil ( ( _size // _pagesize ) ) ) ) ; } TreeMap < String , String > links = new TreeMap < > ( ) ; if ( ( queryString == null ) || ( queryString . isEmpty ( ) ) ) { if ( ( totalPages > 0 ) && ( page < totalPages ) ) { links . put ( "next" , ( ( ( ( requestPath + "?page=" ) + ( page + 1 ) ) + "&pagesize=" ) + pagesize ) ) ; } } else { String queryStringNoPagingProps = URLUtilis . getQueryStringRemovingParams ( exchange , "page" , "pagesize" ) ; if ( ( queryStringNoPagingProps == null ) || ( queryStringNoPagingProps . isEmpty ( ) ) ) { links . put ( "first" , ( ( requestPath + "?pagesize=" ) + pagesize ) ) ; links . put ( "next" , ( ( ( ( requestPath + "?page=" ) + ( page + 1 ) ) + "&pagesize=" ) + pagesize ) ) ; if ( totalPages > 0 ) { if ( page < totalPages ) { links . put ( "last" , ( ( ( requestPath + ( totalPages != 1 ? "?page=" + totalPages : "" ) ) + "&pagesize=" ) + pagesize ) ) ; links . put ( "next" , ( ( ( ( ( ( requestPath + "?page=" ) + ( page + 1 ) ) + "&pagesize=" ) + pagesize ) + "&" ) + queryStringNoPagingProps ) ) ; } else { links . put ( "last" , ( ( ( requestPath + ( totalPages != 1 ? "?page=" + totalPages : "" ) ) + "&pagesize=" ) + pagesize ) ) ; } } if ( page > 1 ) { <START_BUG> links . put ( "previous" , ( ( requestPath + ( page >= 2 ? "?page=" + ( page - 1 ) : "" ) ) + ( page > 2 ? "&pagesize=" + pagesize : "?pagesize=" + pagesize ) ) ) ; <END_BUG> } } else { links . put ( "first" , ( ( ( ( requestPath + "?pagesize=" ) + pagesize ) + "&" ) + queryStringNoPagingProps ) ) ; if ( totalPages <= 0 ) { links . put ( "next" , ( ( ( ( ( ( requestPath + "?page=" ) + ( page + 1 ) ) + "&pagesize=" ) + pagesize ) + "&" ) + queryStringNoPagingProps ) ) ; } if ( totalPages > 0 ) { if ( page < totalPages ) { links . put ( "last" , ( ( ( ( ( requestPath + ( totalPages != 1 ? "?page=" + totalPages : "" ) ) + "&pagesize=" ) + pagesize ) + "&" ) + queryStringNoPagingProps ) ) ; links . put ( "next" , ( ( ( ( ( ( requestPath + "?page=" ) + ( page + 1 ) ) + "&pagesize=" ) + pagesize ) + "&" ) + queryStringNoPagingProps ) ) ; } else { links . put ( "last" , ( ( ( ( ( requestPath + ( totalPages != 1 ? "?page=" + totalPages : "" ) ) + "&pagesize=" ) + pagesize ) + "&" ) + queryStringNoPagingProps ) ) ; } } if ( page > 1 ) { links . put ( "previous" , ( ( ( ( requestPath + ( page >= 2 ? "?page=" + ( page - 1 ) : "" ) ) + ( page >= 2 ? "&pagesize=" + pagesize : "?pagesize=" + pagesize ) ) + "&" ) + queryStringNoPagingProps ) ) ; } } } return links ; } }

Fix:
links . put ( "previous" , ( ( requestPath + ( page >= 2 ? "?page=" + ( page - 1 ) : "" ) ) + ( page >= 2 ? "&pagesize=" + pagesize : "?pagesize=" + pagesize ) ) ) ;