#812 - General overhaul and refactorings.

Cleanups in Affordance API and implementations of hypermedia type (de)serializers. Added a lot more domain methods and types to Link, ResourceSupport etc. to be able to move a lot of representation building logic into those.

AffordanceModelFactory is not a Spring Plugin anymore as that functionality is not needed currently as we statically look up all factories via the SpringFactoriesLoader mechanism.

Redesigned LinkRelation to become a first class abstraction in the codebase. IanaLinkRelations is now a collection of constants. Link now keeps a LinkRelation instance around instead of a plain String.

Tweaked LinkDiscoverer API to return Optional and Links instead of nullable Link and List<Link>.

Tweaked API of CurieProvider to make use of the newly introduced HalLinkRelation based on the general LinkRelation.

Additional fixes for the Kotlin extension functions. Make Kotlin build setup compile with JDK 8. Removed Objects helper class in favor of Spring's already existing Assert and it's usage in ResourceAssemblerSupport.
This commit is contained in:
Oliver Drotbohm
2019-02-12 22:04:36 +01:00
committed by Oliver Drotbohm
parent 38b98af786
commit 9e5db1874e
116 changed files with 3282 additions and 2594 deletions

View File

@@ -15,6 +15,8 @@
*/
package org.springframework.hateoas.client;
import java.util.Optional;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.LinkDiscoverer;
import org.springframework.hateoas.LinkDiscoverers;
@@ -25,7 +27,7 @@ import com.jayway.jsonpath.JsonPath;
/**
* Helper class to find {@link Link} instances in representations.
*
*
* @author Oliver Gierke
* @author Greg Turnquist
* @since 0.11
@@ -34,7 +36,7 @@ class Rels {
/**
* Creates a new {@link Rel} for the given relation name and {@link LinkDiscoverers}.
*
*
* @param rel must not be {@literal null} or empty.
* @param discoverers must not be {@literal null}.
* @return
@@ -55,17 +57,17 @@ class Rels {
/**
* Returns the link contained in the given representation of the given {@link MediaType}.
*
*
* @param representation
* @param mediaType
* @return
*/
Link findInResponse(String representation, MediaType mediaType);
Optional<Link> findInResponse(String representation, MediaType mediaType);
}
/**
* {@link Rel} to using a {@link LinkDiscoverer} based on the given {@link MediaType}.
*
*
* @author Oliver Gierke
*/
private static class LinkDiscovererRel implements Rel {
@@ -75,7 +77,7 @@ class Rels {
/**
* Creates a new {@link LinkDiscovererRel} for the given relation name and {@link LinkDiscoverers}.
*
*
* @param rel must not be {@literal null} or empty.
* @param discoverers must not be {@literal null}.
*/
@@ -88,21 +90,16 @@ class Rels {
this.discoverers = discoverers;
}
/*
/*
* (non-Javadoc)
* @see org.springframework.hateoas.client.Rels.Rel#findInResponse(java.lang.String, org.springframework.http.MediaType)
*/
@Override
public Link findInResponse(String response, MediaType mediaType) {
public Optional<Link> findInResponse(String response, MediaType mediaType) {
LinkDiscoverer discoverer = discoverers.getLinkDiscovererFor(mediaType);
if (discoverer == null) {
throw new IllegalStateException(String.format("Did not find LinkDiscoverer supporting media type %s!",
mediaType));
}
return discoverer.findLinkWithRel(rel, response);
return discoverers //
.getRequiredLinkDiscovererFor(mediaType) //
.findLinkWithRel(rel, response);
}
/*
@@ -118,7 +115,7 @@ class Rels {
/**
* A relation that's being looked up by a JSONPath expression.
*
*
* @author Oliver Gierke
*/
private static class JsonPathRel implements Rel {
@@ -128,7 +125,7 @@ class Rels {
/**
* Creates a new {@link JsonPathRel} for the given JSON path.
*
*
* @param jsonPath must not be {@literal null} or empty.
*/
private JsonPathRel(String jsonPath) {
@@ -141,13 +138,13 @@ class Rels {
this.rel = lastSegment.contains("[") ? lastSegment.substring(0, lastSegment.indexOf("[")) : lastSegment;
}
/*
/*
* (non-Javadoc)
* @see org.springframework.hateoas.client.Rels.Rel#findInResponse(java.lang.String, org.springframework.http.MediaType)
*/
@Override
public Link findInResponse(String representation, MediaType mediaType) {
return new Link(JsonPath.read(representation, jsonPath).toString(), rel);
public Optional<Link> findInResponse(String representation, MediaType mediaType) {
return Optional.of(new Link(JsonPath.read(representation, jsonPath).toString(), rel));
}
}
}

View File

@@ -453,14 +453,11 @@ public class Traverson {
String responseBody = responseEntity.getBody();
Hop thisHop = rels.next();
Rel rel = Rels.getRelFor(thisHop.getRel(), discoverers);
Link link = rel.findInResponse(responseBody, contentType);
if (link == null) {
throw new IllegalStateException(
String.format("Expected to find link with rel '%s' in response %s!", rel, responseBody));
}
Link link = rel.findInResponse(responseBody, contentType) //
.orElseThrow(() -> new IllegalStateException(
String.format("Expected to find link with rel '%s' in response %s!", rel, responseBody)));
/*
* Don't expand if the parameters are empty