improved slightly link generation code
This commit is contained in:
parent
14b65b327f
commit
b50ef0b400
|
@ -1,5 +1,7 @@
|
||||||
package com.baeldung.web.hateoas.listener;
|
package com.baeldung.web.hateoas.listener;
|
||||||
|
|
||||||
|
import java.util.StringJoiner;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
import org.springframework.context.ApplicationListener;
|
import org.springframework.context.ApplicationListener;
|
||||||
|
@ -27,32 +29,32 @@ class PaginatedResultsRetrievedDiscoverabilityListener implements ApplicationLis
|
||||||
public final void onApplicationEvent(final PaginatedResultsRetrievedEvent ev) {
|
public final void onApplicationEvent(final PaginatedResultsRetrievedEvent ev) {
|
||||||
Preconditions.checkNotNull(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
|
// - 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);
|
plural(uriBuilder, clazz);
|
||||||
|
|
||||||
final StringBuilder linkHeader = new StringBuilder();
|
final StringJoiner linkHeader = new StringJoiner(", ");
|
||||||
if (hasNextPage(page, totalPages)) {
|
if (hasNextPage(page, totalPages)) {
|
||||||
final String uriForNextPage = constructNextPageUri(uriBuilder, page, pageSize);
|
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)) {
|
if (hasPreviousPage(page)) {
|
||||||
final String uriForPrevPage = constructPrevPageUri(uriBuilder, page, pageSize);
|
final String uriForPrevPage = constructPrevPageUri(uriBuilder, page, pageSize);
|
||||||
appendCommaIfNecessary(linkHeader);
|
linkHeader.add(LinkUtil.createLinkHeader(uriForPrevPage, LinkUtil.REL_PREV));
|
||||||
linkHeader.append(LinkUtil.createLinkHeader(uriForPrevPage, LinkUtil.REL_PREV));
|
|
||||||
}
|
}
|
||||||
if (hasFirstPage(page)) {
|
if (hasFirstPage(page)) {
|
||||||
final String uriForFirstPage = constructFirstPageUri(uriBuilder, pageSize);
|
final String uriForFirstPage = constructFirstPageUri(uriBuilder, pageSize);
|
||||||
appendCommaIfNecessary(linkHeader);
|
linkHeader.add(LinkUtil.createLinkHeader(uriForFirstPage, LinkUtil.REL_FIRST));
|
||||||
linkHeader.append(LinkUtil.createLinkHeader(uriForFirstPage, LinkUtil.REL_FIRST));
|
|
||||||
}
|
}
|
||||||
if (hasLastPage(page, totalPages)) {
|
if (hasLastPage(page, totalPages)) {
|
||||||
final String uriForLastPage = constructLastPageUri(uriBuilder, totalPages, pageSize);
|
final String uriForLastPage = constructLastPageUri(uriBuilder, totalPages, pageSize);
|
||||||
appendCommaIfNecessary(linkHeader);
|
linkHeader.add(LinkUtil.createLinkHeader(uriForLastPage, LinkUtil.REL_LAST));
|
||||||
linkHeader.append(LinkUtil.createLinkHeader(uriForLastPage, LinkUtil.REL_LAST));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (linkHeader.length() > 0) {
|
if (linkHeader.length() > 0) {
|
||||||
|
@ -61,19 +63,35 @@ class PaginatedResultsRetrievedDiscoverabilityListener implements ApplicationLis
|
||||||
}
|
}
|
||||||
|
|
||||||
final String constructNextPageUri(final UriComponentsBuilder uriBuilder, final int page, final int size) {
|
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) {
|
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) {
|
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) {
|
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) {
|
final boolean hasNextPage(final int page, final int totalPages) {
|
||||||
|
@ -92,16 +110,11 @@ class PaginatedResultsRetrievedDiscoverabilityListener implements ApplicationLis
|
||||||
return (totalPages > 1) && hasNextPage(page, totalPages);
|
return (totalPages > 1) && hasNextPage(page, totalPages);
|
||||||
}
|
}
|
||||||
|
|
||||||
final void appendCommaIfNecessary(final StringBuilder linkHeader) {
|
|
||||||
if (linkHeader.length() > 0) {
|
|
||||||
linkHeader.append(", ");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// template
|
// template
|
||||||
|
|
||||||
protected void plural(final UriComponentsBuilder uriBuilder, final Class clazz) {
|
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);
|
uriBuilder.path("/auth/" + resourceName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue