/** * Creates a new link to the given URI with the self rel. * * @see IanaLinkRelation#SELF * @param href must not be {@literal null} or empty. */ public Link(String href) { this(href, IanaLinkRelation.SELF.value()); }
/** * Is this relation an IANA standard? * * Per RFC8288, parsing of link relations is case insensitive. * * @param rel * @return boolean */ public static boolean isIanaRel(String rel) { return rel != null && LINK_RELATIONS.stream().anyMatch(linkRelation -> linkRelation.value().equalsIgnoreCase(rel)); }
/** * Convert a string-based link relation to a {@link IanaLinkRelation}. * * Per RFC8288, parsing of link relations is case insensitive. * * @param rel as a string * @return rel as a {@link IanaLinkRelation} */ public static IanaLinkRelation parse(String rel) { return LINK_RELATIONS.stream() .filter(linkRelation -> linkRelation.value().equalsIgnoreCase(rel)) .findFirst() .orElseThrow(() -> new IllegalArgumentException(rel + " is not a valid IANA link relation!")); } }
/** * Returns the {@link Link} with a rel of {@link IanaLinkRelation#SELF}. */ @JsonIgnore public Optional<Link> getId() { return getLink(IanaLinkRelation.SELF.value()); }
private static List<Link> withoutSelfLink(List<Link> links) { return links.stream() .filter(link -> !link.getRel().equals(IanaLinkRelation.SELF.value())) .collect(Collectors.toList()); }
/** * Returns a {@link Link} pointing to the same URI but with the {@code self} relation. * * @return */ public Link withSelfRel() { return withRel(IanaLinkRelation.SELF.value()); }
/** * Returns the Link pointing to the next page (if set). * * @return */ @JsonIgnore public Optional<Link> getNextLink() { return getLink(IanaLinkRelation.NEXT.value()); }
/** * Returns the Link pointing to the previous page (if set). * * @return */ @JsonIgnore public Optional<Link> getPreviousLink() { return getLink(IanaLinkRelation.PREV.value()); }
public Link withSelfRel() { return withRel(IanaLinkRelation.SELF.value()); }
private Link findSelfLink(String representation) { return this.selfLinkDiscoverer.findLinkWithRel(IanaLinkRelation.SELF.value(), representation); }
private Link findSelfLink(InputStream representation) { return this.selfLinkDiscoverer.findLinkWithRel(IanaLinkRelation.SELF.value(), representation); }
@Override public Link findLinkWithRel(String rel, String representation) { if (rel.equals(IanaLinkRelation.SELF.value())) { return findSelfLink(representation); } else { return super.findLinkWithRel(rel, representation); } }
@Override public Link findLinkWithRel(String rel, InputStream representation) { if (rel.equals(IanaLinkRelation.SELF.value())) { return findSelfLink(representation); } else { return super.findLinkWithRel(rel, representation); } }
@Override public List<Link> findLinksWithRel(String rel, String representation) { if (rel.equals(IanaLinkRelation.SELF.value())) { return addSelfLink(super.findLinksWithRel(rel, representation), representation); } else { return super.findLinksWithRel(rel, representation); } }
@Override public List<Link> findLinksWithRel(String rel, InputStream representation) { if (rel.equals(IanaLinkRelation.SELF.value())) { return addSelfLink(super.findLinksWithRel(rel, representation), representation); } else { return super.findLinksWithRel(rel, representation); } }
/** * Verify that the resource's self link and the affordance's URI have the same relative path. * * @param resource * @param model */ private static void validate(ResourceSupport resource, HalFormsAffordanceModel model) { String affordanceUri = model.getURI(); String selfLinkUri = resource.getRequiredLink(IanaLinkRelation.SELF.value()).expand().getHref(); if (!affordanceUri.equals(selfLinkUri)) { throw new IllegalStateException("Affordance's URI " + affordanceUri + " doesn't match self link " + selfLinkUri + " as expected in HAL-FORMS"); } } }
private static List<CollectionJsonItem<?>> resourcesToCollectionJsonItems(Resources<?> resources) { return resources.getContent().stream() .map(content -> { if (ClassUtils.isAssignableValue(Resource.class, content)) { Resource resource = (Resource) content; return new CollectionJsonItem<>() .withHref(resource.getRequiredLink(IanaLinkRelation.SELF.value()).getHref()) .withLinks(withoutSelfLink(resource.getLinks())) .withRawData(resource.getContent()); } else { return new CollectionJsonItem<>().withRawData(content); } }) .collect(Collectors.toList()); }
@Override public void serialize(List<Link> value, JsonGenerator jgen, SerializerProvider provider) throws IOException { ResourceSupport resource = new ResourceSupport(); resource.add(value); CollectionJson<?> collectionJson = new CollectionJson() .withVersion("1.0") .withHref(resource.getRequiredLink(IanaLinkRelation.SELF.value()).expand().getHref()) .withLinks(withoutSelfLink(value)) .withItems(Collections.EMPTY_LIST); provider .findValueSerializer(CollectionJson.class, property) .serialize(collectionJson, jgen, provider); }
@Override public void serialize(Resources<?> value, JsonGenerator jgen, SerializerProvider provider) throws IOException { CollectionJson<?> collectionJson = new CollectionJson() .withVersion("1.0") .withHref(value.getRequiredLink(IanaLinkRelation.SELF.value()).getHref()) .withLinks(withoutSelfLink(value.getLinks())) .withItems(resourcesToCollectionJsonItems(value)) .withQueries(findQueries(value)) .withTemplate(findTemplate(value)); CollectionJsonDocument<?> doc = new CollectionJsonDocument<>(collectionJson); provider .findValueSerializer(CollectionJsonDocument.class, property) .serialize(doc, jgen, provider); }
@Override public void serialize(PagedResources<?> value, JsonGenerator jgen, SerializerProvider provider) throws IOException { CollectionJson<?> collectionJson = new CollectionJson() .withVersion("1.0") .withHref(value.getRequiredLink(IanaLinkRelation.SELF.value()).getHref()) .withLinks(withoutSelfLink(value.getLinks())) .withItems(resourcesToCollectionJsonItems(value)) .withQueries(findQueries(value)) .withTemplate(findTemplate(value)); CollectionJsonDocument<?> doc = new CollectionJsonDocument<>(collectionJson); provider .findValueSerializer(CollectionJsonDocument.class, property) .serialize(doc, jgen, provider); }