Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ivanfioravanti/5227194 to your computer and use it in GitHub Desktop.
Save ivanfioravanti/5227194 to your computer and use it in GitHub Desktop.
@Component
class PaginatedResultsRetrievedEventDiscoverabilityListener implements ApplicationListener< PaginatedResultsRetrievedEvent >{
@Override
public void onApplicationEvent( PaginatedResultsRetrievedEvent ev ){
Preconditions.checkNotNull( ev );
this.addLinkHeaderOnPagedResourceRetrieval(
ev.getUriBuilder(), ev.getResponse(), ev.getClazz(), ev.getPage(), ev.getTotalPages(), ev.getPageSize() );
}
void addLinkHeaderOnPagedResourceRetrieval( UriComponentsBuilder uriBuilder, HttpServletResponse response,
Class clazz, int page, int totalPages, final int pageSize ){
String resourceName = clazz.getSimpleName().toString().toLowerCase();
uriBuilder.path( "/admin/" + resourceName );
StringBuilder linkHeader = new StringBuilder();
if( this.hasNextPage( page, totalPages ) ){
String uriForNextPage = this.constructNextPageUri( uriBuilder, page, pageSize );
linkHeader.append( createLinkHeader( uriForNextPage, REL_NEXT ) );
}
if( this.hasPreviousPage( page ) ){
String uriForPrevPage = this.constructPrevPageUri( uriBuilder, page, pageSize );
appendCommaIfNecessary( linkHeader );
linkHeader.append( createLinkHeader( uriForPrevPage, REL_PREV ) );
}
if( this.hasFirstPage( page ) ){
String uriForFirstPage = this.constructFirstPageUri( uriBuilder, pageSize );
appendCommaIfNecessary( linkHeader );
linkHeader.append( createLinkHeader( uriForFirstPage, REL_FIRST ) );
}
if( this.hasLastPage( page, totalPages ) ){
String uriForLastPage = this.constructLastPageUri( uriBuilder, totalPages, pageSize );
appendCommaIfNecessary( linkHeader );
linkHeader.append( createLinkHeader( uriForLastPage, REL_LAST ) );
}
response.addHeader( HttpConstants.LINK_HEADER, linkHeader.toString() );
}
String constructNextPageUri( UriComponentsBuilder uriBuilder, int page, final int size ){
return uriBuilder.replaceQueryParam( "page", page + 1 ).replaceQueryParam( "size", size ).build().encode().toUriString();
}
String constructPrevPageUri( UriComponentsBuilder uriBuilder, int page, final int size ){
return uriBuilder.replaceQueryParam( "page", page - 1 ).replaceQueryParam( "size", size ).build().encode().toUriString();
}
String constructFirstPageUri( UriComponentsBuilder uriBuilder, final int size ){
return uriBuilder.replaceQueryParam( "page", 0 ).replaceQueryParam( "size", size ).build().encode().toUriString();
}
String constructLastPageUri( UriComponentsBuilder uriBuilder, int totalPages, final int size ){
return uriBuilder.replaceQueryParam( "page", totalPages ).replaceQueryParam( "size", size ).build().encode().toUriString();
}
boolean hasNextPage( int page, int totalPages ){
return page < totalPages - 1;
}
boolean hasPreviousPage( int page ){
return page > 0;
}
boolean hasFirstPage( int page ){
return this.hasPreviousPage( page );
}
boolean hasLastPage( int page, int totalPages ){
return totalPages > 1 && this.hasNextPage( page, totalPages );
}
void appendCommaIfNecessary( final StringBuilder linkHeader ){
if( linkHeader.length() > 0 ){
linkHeader.append( ", " );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment