Search in sources :

Example 1 with UriTemplate

use of org.springframework.hateoas.UriTemplate in project spring-data-commons by spring-projects.

the class PagedResourcesAssembler method createLink.

/**
 * Creates a {@link Link} with the given rel that will be based on the given {@link UriTemplate} but enriched with the
 * values of the given {@link Pageable} (if not {@literal null}).
 *
 * @param base must not be {@literal null}.
 * @param pageable can be {@literal null}
 * @param rel must not be {@literal null} or empty.
 * @return
 */
private Link createLink(UriTemplate base, Pageable pageable, String rel) {
    UriComponentsBuilder builder = fromUri(base.expand());
    pageableResolver.enhance(builder, getMethodParameter(), pageable);
    return new Link(new UriTemplate(builder.build().toString()), rel);
}
Also used : UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) ServletUriComponentsBuilder(org.springframework.web.servlet.support.ServletUriComponentsBuilder) UriTemplate(org.springframework.hateoas.UriTemplate) Link(org.springframework.hateoas.Link)

Example 2 with UriTemplate

use of org.springframework.hateoas.UriTemplate in project spring-data-commons by spring-projects.

the class PagedResourcesAssembler method addPaginationLinks.

private <R> PagedResources<R> addPaginationLinks(PagedResources<R> resources, Page<?> page, Optional<Link> link) {
    UriTemplate base = getUriTemplate(link);
    boolean isNavigable = page.hasPrevious() || page.hasNext();
    if (isNavigable || forceFirstAndLastRels) {
        resources.add(createLink(base, PageRequest.of(0, page.getSize(), page.getSort()), Link.REL_FIRST));
    }
    if (page.hasPrevious()) {
        resources.add(createLink(base, page.previousPageable(), Link.REL_PREVIOUS));
    }
    Link selfLink = // 
    link.map(it -> it.withSelfRel()).orElseGet(() -> createLink(base, page.getPageable(), Link.REL_SELF));
    resources.add(selfLink);
    if (page.hasNext()) {
        resources.add(createLink(base, page.nextPageable(), Link.REL_NEXT));
    }
    if (isNavigable || forceFirstAndLastRels) {
        int lastIndex = page.getTotalPages() == 0 ? 0 : page.getTotalPages() - 1;
        resources.add(createLink(base, PageRequest.of(lastIndex, page.getSize(), page.getSort()), Link.REL_LAST));
    }
    return resources;
}
Also used : UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) Link(org.springframework.hateoas.Link) ServletUriComponentsBuilder(org.springframework.web.servlet.support.ServletUriComponentsBuilder) EmbeddedWrapper(org.springframework.hateoas.core.EmbeddedWrapper) PageMetadata(org.springframework.hateoas.PagedResources.PageMetadata) PageRequest(org.springframework.data.domain.PageRequest) Page(org.springframework.data.domain.Page) EmbeddedWrappers(org.springframework.hateoas.core.EmbeddedWrappers) Resource(org.springframework.hateoas.Resource) ResourceSupport(org.springframework.hateoas.ResourceSupport) ArrayList(java.util.ArrayList) PagedResources(org.springframework.hateoas.PagedResources) List(java.util.List) UriTemplate(org.springframework.hateoas.UriTemplate) ResourceAssembler(org.springframework.hateoas.ResourceAssembler) MethodParameter(org.springframework.core.MethodParameter) Optional(java.util.Optional) Pageable(org.springframework.data.domain.Pageable) Nullable(org.springframework.lang.Nullable) Collections(java.util.Collections) Assert(org.springframework.util.Assert) UriComponents(org.springframework.web.util.UriComponents) UriTemplate(org.springframework.hateoas.UriTemplate) Link(org.springframework.hateoas.Link)

Example 3 with UriTemplate

use of org.springframework.hateoas.UriTemplate in project sw360portal by sw360.

the class AttachmentController method process.

@Override
public RepositoryLinksResource process(RepositoryLinksResource resource) {
    final ControllerLinkBuilder controllerLinkBuilder = linkTo(AttachmentController.class);
    final Link attachments = new Link(new UriTemplate(controllerLinkBuilder.toUri().toString() + "/api/attachments{?sha1}"), "attachments");
    resource.add(attachments);
    return resource;
}
Also used : ControllerLinkBuilder(org.springframework.hateoas.mvc.ControllerLinkBuilder) UriTemplate(org.springframework.hateoas.UriTemplate) Link(org.springframework.hateoas.Link)

Aggregations

Link (org.springframework.hateoas.Link)3 UriTemplate (org.springframework.hateoas.UriTemplate)3 ServletUriComponentsBuilder (org.springframework.web.servlet.support.ServletUriComponentsBuilder)2 UriComponentsBuilder (org.springframework.web.util.UriComponentsBuilder)2 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 List (java.util.List)1 Optional (java.util.Optional)1 MethodParameter (org.springframework.core.MethodParameter)1 Page (org.springframework.data.domain.Page)1 PageRequest (org.springframework.data.domain.PageRequest)1 Pageable (org.springframework.data.domain.Pageable)1 PagedResources (org.springframework.hateoas.PagedResources)1 PageMetadata (org.springframework.hateoas.PagedResources.PageMetadata)1 Resource (org.springframework.hateoas.Resource)1 ResourceAssembler (org.springframework.hateoas.ResourceAssembler)1 ResourceSupport (org.springframework.hateoas.ResourceSupport)1 EmbeddedWrapper (org.springframework.hateoas.core.EmbeddedWrapper)1 EmbeddedWrappers (org.springframework.hateoas.core.EmbeddedWrappers)1 ControllerLinkBuilder (org.springframework.hateoas.mvc.ControllerLinkBuilder)1