java.lang.Object
de.kamillionlabs.hateoflux.model.link.Link

public class Link extends Object
Represents a hypermedia link with various attributes defining aspects of the link such as href, templated nature, and media type among others.
  • Methodendetails

    • of

      public static Link of(String href)
      Creates a new Link instance with the specified href but without any IANA relation. This is useful for creating links that do not need to express a specific relationship type.
      Parameter:
      href - Hypertext REFerence, commonly known as a URL (e.g., https://www.github.com).
      Gibt zurück:
      A new instance of Link with no IANA relation.
    • of

      public static Link of(IanaRelation relation, String href)
      Creates a new Link instance and associates it with a specified IANA relation. This method defines the type of relationship between the current resource and the linked resource.
      Parameter:
      relation - IANA relation of the link, which must not be null (see IanaRelation for details).
      href - Hypertext REFerence, commonly known as a URL (e.g., https://www.github.com).
      Gibt zurück:
      A new instance of Link with the specified IANA relation.
    • linkAsSelfOf

      public static Link linkAsSelfOf(String href)
      Creates a new Link instance with an IANA relation of type IanaRelation.SELF. This type indicates that the link's URI is a reference to the resource itself.
      Parameter:
      href - Hypertext REFerence, commonly known as a URL (e.g., https://www.github.com).
      Gibt zurück:
      A new instance of Link with a "self" IANA relation.
    • isTemplated

      public boolean isTemplated()
      Indicates whether the href is a URI template that should be templated with variables.
      Gibt zurück:
      true if href of the link is templated; false otherwise
    • withRel

      public Link withRel(String relation)
      The relationship between the current resource and the linked resource. Common values include "self", "next", "previous", etc. Custom relations can also be used.
      Parameter:
      relation - A string specifying the IANA relation of the link.
      Gibt zurück:
      Returns a new Link that is a copy of the current link with the specified relation added.
    • withRel

      public Link withRel(IanaRelation relation)
      The relationship between the current resource and the linked resource. Common values include "self", "next", "prev", etc. Custom relations can also be used.
      Parameter:
      relation - The IANA relation of the link.
      Gibt zurück:
      Returns a new Link that is a copy of the current link with the specified IANA relation added.
    • withSelfRel

      public Link withSelfRel()
      The relationship between the current resource and the linked resource. Common values include "self", "next", "prev", etc. Custom relations can also be used.
      Gibt zurück:
      Returns a new Link that is a copy of the current link with the "self" relation.
    • withHref

      public Link withHref(String href)
      The URI of the linked resource. This is a required attribute and is the actual URL where the resource can be accessed.
      Parameter:
      href - The new href for the link.
      Gibt zurück:
      Returns a new Link that is a copy of the current link with the updated href.
    • withTitle

      public Link withTitle(String title)
      A human-readable title for the link, which can be used for labeling the link in user interfaces.
      Parameter:
      title - The new title for the link.
      Gibt zurück:
      Returns a new Link that is a copy of the current link with the updated title.
    • withName

      public Link withName(String name)
      An identifier or label for the link, used for documentation or as additional metadata in client applications.
      Parameter:
      name - The new name for the link.
      Gibt zurück:
      Returns a new Link that is a copy of the current link with the updated name.
    • withMedia

      public Link withMedia(String media)
      Describes the media type of the linked resource, often used to specify the type of content that the client can expect at the URL, such as "application/json" or "text/html".
      Parameter:
      media - The new media type for the link.
      Gibt zurück:
      Returns a new Link that is a copy of the current link with the updated media type.
    • withType

      public Link withType(String type)
      Further specifies the MIME type of the linked resource's expected content. This can be used to indicate more specific formats when multiple representations are available.
      Parameter:
      type - The new MIME type for the link.
      Gibt zurück:
      Returns a new Link that is a copy of the current link with the updated type.
    • withDeprecation

      public Link withDeprecation(String deprecation)
      A URL that provides information about the deprecation of the link, useful for alerting API consumers that a resource is outdated or scheduled for removal.
      Parameter:
      deprecation - The new deprecation URL for the link.
      Gibt zurück:
      Returns a new Link that is a copy of the current link with the updated deprecation URL.
    • withProfile

      public Link withProfile(String profile)
      A hint about the profile (or schema) that the linked resource conforms to, providing additional semantics about the linked resource.
      Parameter:
      profile - The new profile URL for the link.
      Gibt zurück:
      Returns a new Link that is a copy of the current link with the updated profile.
    • withHreflang

      public Link withHreflang(String hreflang)
      Specifies the language of the linked resource, useful for applications supporting multiple languages.
      Parameter:
      hreflang - The new language code for the link.
      Gibt zurück:
      Returns a new Link that is a copy of the current link with the updated language code.
    • deriveNavigationLinks

      public List<Link> deriveNavigationLinks(HalPageInfo pagingInformation, SortCriteria... sortCriteria)
      Derives a list of navigational links (i.e., self, first, prev, next, last) based on the provided pagination and sorting information. This method generates appropriate links to navigate through paginated resources while preserving sorting parameters.

      The method considers the current page specified in the pagingInformation and creates the necessary navigation links accordingly. It includes the page, size, and SortCriteria query parameters in the generated links.

      Edge Cases Handling:

      • Single Page Result: Only the self link is generated.
      • First Page of Multiple: Generates self, next, and last links.
      • Last Page of Multiple: Generates self, first, and prev links.
      • Middle Page: Generates self, first, prev, next, and last links.
      • Empty Page: Only the self link is generated.

      Usage Example:

      
       //HalPageInfo:  size, totalElements, totalPages, number
       HalPageInfo pageInfo = new HalPageInfo(10, 50L, 5, 2);
       Sort sort1 = Sort.by("name", SortDirection.ASCENDING);
       Sort sort2 = Sort.by("age", SortDirection.DESCENDING);
       Link link = Link.of("http://example.com/resource");
      
       List<Link> navigationLinks = link.deriveNavigationLinks(pageInfo, sort1, sort2);
       

      This will generate links with hrefs including the appropriate page, size, and sort parameters, such as:

      • http://example.com/resource?page=2&size=10&sort=name,asc&sort=age,desc (self link)
      • http://example.com/resource?page=0&size=10&sort=name,asc&sort=age,desc (first link)
      • http://example.com/resource?page=1&size=10&sort=name,asc&sort=age,desc (prev link)
      • http://example.com/resource?page=3&size=10&sort=name,asc&sort=age,desc (next link)
      • http://example.com/resource?page=4&size=10&sort=name,asc&sort=age,desc (last link)
      Parameter:
      pagingInformation - the pagination information of the self link on which all other links will be based on
      sortCriteria - optional sorting parameters of self link on which all other links will be based on; each SortCriteria
      Gibt zurück:
      a list of Link objects representing the navigational links appropriate for the current page and sorting context
      Löst aus:
      IllegalArgumentException - if the link's href is templated (contains unexpanded variables)
      Siehe auch:
    • deriveNavigationLinks

      public List<Link> deriveNavigationLinks(HalPageInfo pagingInformation, List<SortCriteria> sortCriteria)
      Variation of the deriveNavigationLinks(HalPageInfo, SortCriteria...) method. Please refer to the mentioned method for full documentation and usage examples. This method calls the aforementioned method with the conversion of sortCriteria into an array.
      Parameter:
      pagingInformation - the pagination information of the self link on which all other links will be based on
      sortCriteria - optional sorting parameters of self link on which all other links will be based on; each SortCriteria
      Gibt zurück:
      a list of Link objects representing the navigational links appropriate for the current page and sorting context
      Löst aus:
      IllegalArgumentException - if the link's href is templated (contains unexpanded variables)
    • slash

      public Link slash(String uriPart)
      Appends a specified URI part to the current Link's href. The method ensures proper formatting with slashes.
      Parameter:
      uriPart - The URI part to be appended.
      Gibt zurück:
      A new Link object with the appended URI part.
    • prependBaseUrl

      public Link prependBaseUrl(org.springframework.http.server.reactive.ServerHttpRequest httpRequest)
      Extracts the base URL from the given ServerHttpRequest and prepends it to the current href.
      Parameter:
      httpRequest - The request that provides the data to extract the base URL from.
      Gibt zurück:
      A new Link object with the prepended base URL.
    • prependBaseUrl

      public Link prependBaseUrl(org.springframework.web.server.ServerWebExchange exchangeWithBaseUrl)
      Extracts the base URL from the given ServerWebExchange and prepends it to the current href.
      Parameter:
      exchangeWithBaseUrl - The exchange that provides the data to extract the base URL from.
      Gibt zurück:
      A new Link object with the prepended base URL.
    • prependBaseUrl

      public Link prependBaseUrl(String baseUrl)
      Prepends the provided base URL to the current href.
      Parameter:
      baseUrl - Base URL to prepend to current link
      Gibt zurück:
      A new Link object with the prepended base URL.
    • expand

      public Link expand(Object... parameters)
      Utility method that serves as a proxy for UriExpander.expand(String, Object...). Please refer to mentioned method for full documentation.
      Parameter:
      parameters - parameters to expand in templated href
      Gibt zurück:
      The expanded or original URI if expansion is not applicable
    • expand

      public Link expand(Map<String,?> parameters, boolean collectionRenderedAsComposite)
      Utility method that serves as a proxy for UriExpander.expand(String, Map, boolean). Please refer to mentioned method for full documentation.
      Parameter:
      parameters - to expand in templated href
      collectionRenderedAsComposite - specifies whether the collection should be rendered as composite (true) or non-composite (false)
      Gibt zurück:
      the expanded or original URI if expansion is not applicable
    • expand

      public Link expand(Map<String,?> parameters)
      Utility method that serves as a proxy for UriExpander.expand(String, Map). Please refer to mentioned method for full documentation.
      Parameter:
      parameters - to expand in templated href
      Gibt zurück:
      the expanded or original URI if expansion is not applicable