improved slightly link generation code

This commit is contained in:
Ger Roza 2019-01-21 23:14:03 -02:00
parent 14b65b327f
commit b50ef0b400
1 changed files with 34 additions and 21 deletions

View File

@ -1,5 +1,7 @@
package com.baeldung.web.hateoas.listener;
import java.util.StringJoiner;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationListener;
@ -27,32 +29,32 @@ class PaginatedResultsRetrievedDiscoverabilityListener implements ApplicationLis
public final void onApplicationEvent(final PaginatedResultsRetrievedEvent ev) {
Preconditions.checkNotNull(ev);
addLinkHeaderOnPagedResourceRetrieval(ev.getUriBuilder(), ev.getResponse(), ev.getClazz(), ev.getPage(), ev.getTotalPages(), ev.getPageSize());
addLinkHeaderOnPagedResourceRetrieval(ev.getUriBuilder(), ev.getResponse(), ev.getClazz(), ev.getPage(),
ev.getTotalPages(), ev.getPageSize());
}
// - note: at this point, the URI is transformed into plural (added `s`) in a hardcoded way - this will change in the future
final void addLinkHeaderOnPagedResourceRetrieval(final UriComponentsBuilder uriBuilder, final HttpServletResponse response, final Class clazz, final int page, final int totalPages, final int pageSize) {
final void addLinkHeaderOnPagedResourceRetrieval(final UriComponentsBuilder uriBuilder,
final HttpServletResponse response, final Class clazz, final int page, final int totalPages,
final int pageSize) {
plural(uriBuilder, clazz);
final StringBuilder linkHeader = new StringBuilder();
final StringJoiner linkHeader = new StringJoiner(", ");
if (hasNextPage(page, totalPages)) {
final String uriForNextPage = constructNextPageUri(uriBuilder, page, pageSize);
linkHeader.append(LinkUtil.createLinkHeader(uriForNextPage, LinkUtil.REL_NEXT));
linkHeader.add(LinkUtil.createLinkHeader(uriForNextPage, LinkUtil.REL_NEXT));
}
if (hasPreviousPage(page)) {
final String uriForPrevPage = constructPrevPageUri(uriBuilder, page, pageSize);
appendCommaIfNecessary(linkHeader);
linkHeader.append(LinkUtil.createLinkHeader(uriForPrevPage, LinkUtil.REL_PREV));
linkHeader.add(LinkUtil.createLinkHeader(uriForPrevPage, LinkUtil.REL_PREV));
}
if (hasFirstPage(page)) {
final String uriForFirstPage = constructFirstPageUri(uriBuilder, pageSize);
appendCommaIfNecessary(linkHeader);
linkHeader.append(LinkUtil.createLinkHeader(uriForFirstPage, LinkUtil.REL_FIRST));
linkHeader.add(LinkUtil.createLinkHeader(uriForFirstPage, LinkUtil.REL_FIRST));
}
if (hasLastPage(page, totalPages)) {
final String uriForLastPage = constructLastPageUri(uriBuilder, totalPages, pageSize);
appendCommaIfNecessary(linkHeader);
linkHeader.append(LinkUtil.createLinkHeader(uriForLastPage, LinkUtil.REL_LAST));
linkHeader.add(LinkUtil.createLinkHeader(uriForLastPage, LinkUtil.REL_LAST));
}
if (linkHeader.length() > 0) {
@ -61,19 +63,35 @@ class PaginatedResultsRetrievedDiscoverabilityListener implements ApplicationLis
}
final String constructNextPageUri(final UriComponentsBuilder uriBuilder, final int page, final int size) {
return uriBuilder.replaceQueryParam(PAGE, page + 1).replaceQueryParam("size", size).build().encode().toUriString();
return uriBuilder.replaceQueryParam(PAGE, page + 1)
.replaceQueryParam("size", size)
.build()
.encode()
.toUriString();
}
final String constructPrevPageUri(final UriComponentsBuilder uriBuilder, final int page, final int size) {
return uriBuilder.replaceQueryParam(PAGE, page - 1).replaceQueryParam("size", size).build().encode().toUriString();
return uriBuilder.replaceQueryParam(PAGE, page - 1)
.replaceQueryParam("size", size)
.build()
.encode()
.toUriString();
}
final String constructFirstPageUri(final UriComponentsBuilder uriBuilder, final int size) {
return uriBuilder.replaceQueryParam(PAGE, 0).replaceQueryParam("size", size).build().encode().toUriString();
return uriBuilder.replaceQueryParam(PAGE, 0)
.replaceQueryParam("size", size)
.build()
.encode()
.toUriString();
}
final String constructLastPageUri(final UriComponentsBuilder uriBuilder, final int totalPages, final int size) {
return uriBuilder.replaceQueryParam(PAGE, totalPages).replaceQueryParam("size", size).build().encode().toUriString();
return uriBuilder.replaceQueryParam(PAGE, totalPages)
.replaceQueryParam("size", size)
.build()
.encode()
.toUriString();
}
final boolean hasNextPage(final int page, final int totalPages) {
@ -92,16 +110,11 @@ class PaginatedResultsRetrievedDiscoverabilityListener implements ApplicationLis
return (totalPages > 1) && hasNextPage(page, totalPages);
}
final void appendCommaIfNecessary(final StringBuilder linkHeader) {
if (linkHeader.length() > 0) {
linkHeader.append(", ");
}
}
// template
protected void plural(final UriComponentsBuilder uriBuilder, final Class clazz) {
final String resourceName = clazz.getSimpleName().toLowerCase() + "s";
final String resourceName = clazz.getSimpleName()
.toLowerCase() + "s";
uriBuilder.path("/auth/" + resourceName);
}