From b50ef0b4002f61131de4d6496f869940eb584d16 Mon Sep 17 00:00:00 2001 From: Ger Roza Date: Mon, 21 Jan 2019 23:14:03 -0200 Subject: [PATCH] improved slightly link generation code --- ...sultsRetrievedDiscoverabilityListener.java | 55 ++++++++++++------- 1 file changed, 34 insertions(+), 21 deletions(-) diff --git a/spring-boot-rest/src/main/java/com/baeldung/web/hateoas/listener/PaginatedResultsRetrievedDiscoverabilityListener.java b/spring-boot-rest/src/main/java/com/baeldung/web/hateoas/listener/PaginatedResultsRetrievedDiscoverabilityListener.java index bb60bab171..31555ef353 100644 --- a/spring-boot-rest/src/main/java/com/baeldung/web/hateoas/listener/PaginatedResultsRetrievedDiscoverabilityListener.java +++ b/spring-boot-rest/src/main/java/com/baeldung/web/hateoas/listener/PaginatedResultsRetrievedDiscoverabilityListener.java @@ -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); }