DATAREST-1213 - ETag creation now uses proxy target for projections.

Original pull request: #355.
This commit is contained in:
Dario Seidl
2019-05-26 18:48:56 +02:00
committed by Oliver Drotbohm
parent 3cb683c137
commit e3a88ff9ca
9 changed files with 190 additions and 4 deletions

View File

@@ -37,6 +37,7 @@ import org.springframework.util.Assert;
* instances.
*
* @author Oliver Gierke
* @author Dario Seidl
* @soundtrack Ron Spielman Trio - Matchstick
*/
@RequiredArgsConstructor
@@ -59,7 +60,7 @@ public class HttpHeadersPreparer {
public HttpHeaders prepareHeaders(Optional<PersistentEntityResource> resource) {
return resource//
.map(it -> prepareHeaders(it.getPersistentEntity(), it.getContent()))//
.map(it -> prepareHeaders(it.getPersistentEntity(), it.getTargetEntity()))//
.orElseGet(() -> new HttpHeaders());
}
@@ -75,6 +76,8 @@ public class HttpHeadersPreparer {
Assert.notNull(entity, "PersistentEntity must not be null!");
Assert.notNull(value, "Entity value must not be null!");
Assert.isInstanceOf(entity.getType(), value, () ->
String.format("Target bean of type %s is not of type of the persistent entity (%s)!", value.getClass().getName(), entity.getType().getName()));
// Add ETag
HttpHeaders headers = ETag.from(entity, value).addTo(new HttpHeaders());

View File

@@ -24,6 +24,7 @@ import java.util.List;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.mapping.PersistentProperty;
import org.springframework.data.mapping.PersistentPropertyAccessor;
import org.springframework.data.projection.TargetAware;
import org.springframework.hateoas.CollectionModel;
import org.springframework.hateoas.EntityModel;
import org.springframework.hateoas.Link;
@@ -38,6 +39,7 @@ import com.fasterxml.jackson.annotation.JsonIgnore;
*
* @author Jon Brisbin
* @author Oliver Gierke
* @author Dario Seidl
*/
public class PersistentEntityResource extends EntityModel<Object> {
@@ -88,13 +90,27 @@ public class PersistentEntityResource extends EntityModel<Object> {
return entity;
}
/**
* Returns the underlying instance. If the instance is a dynamic JDK proxy, the proxy target is returned.
*
* @return
*/
public Object getTargetEntity() {
Object content = getContent();
if (content instanceof TargetAware) {
return ((TargetAware) content).getTarget();
} else {
return content;
}
}
/**
* Returns the {@link PersistentPropertyAccessor} for the underlying content bean.
*
* @return
*/
public PersistentPropertyAccessor<?> getPropertyAccessor() {
return entity.getPropertyAccessor(getContent());
return entity.getPropertyAccessor(getTargetEntity());
}
/**

View File

@@ -32,6 +32,7 @@ import org.springframework.util.Assert;
* A value object to represent ETags.
*
* @author Oliver Gierke
* @author Dario Seidl
*/
@EqualsAndHashCode
public final class ETag {
@@ -74,7 +75,7 @@ public final class ETag {
Assert.notNull(resource, "PersistentEntityResource must not be null!");
return from(resource.getPersistentEntity(), resource.getContent());
return from(resource.getPersistentEntity(), resource.getTargetEntity());
}
/**