Skip to content

Commit

Permalink
Replace EntityEncoderFilter with per-entity object read filter #501
Browse files Browse the repository at this point in the history
refactoring
  • Loading branch information
andrus committed Nov 28, 2021
1 parent 7dcf992 commit 46c64bc
Show file tree
Hide file tree
Showing 20 changed files with 71 additions and 69 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.agrest.cayenne.compiler;

import io.agrest.compiler.AnnotationsAgEntityBuilder;
import io.agrest.filter.SelectFilter;
import io.agrest.filter.ReadFilter;
import io.agrest.meta.AgAttribute;
import io.agrest.meta.AgDataMap;
import io.agrest.meta.AgEntity;
Expand Down Expand Up @@ -205,7 +205,7 @@ protected AgEntity<T> buildEntity() {
rootDataResolver != null ? rootDataResolver : ThrowingRootDataResolver.getInstance(),

// TODO: support Exp filters via annotations?
SelectFilter.allowsAllFilter());
ReadFilter.allowsAllFilter());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import io.agrest.cayenne.cayenne.main.E4;
import io.agrest.cayenne.unit.AgCayenneTester;
import io.agrest.cayenne.unit.DbTest;
import io.agrest.filter.SelectFilter;
import io.agrest.filter.ReadFilter;
import io.agrest.meta.AgEntity;
import io.bootique.junit5.BQTestTool;
import org.apache.cayenne.Cayenne;
Expand All @@ -21,23 +21,23 @@
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;

public class GET_SelectFilterIT extends DbTest {
public class GET_ReadFilterIT extends DbTest {

@BQTestTool
static final AgCayenneTester tester = tester(Resource.class)
.entities(E2.class, E3.class, E4.class)
.agCustomizer(ab -> ab
.entityOverlay(AgEntity.overlay(E2.class).selectFilter(evenFilter()))
.entityOverlay(AgEntity.overlay(E3.class).selectFilter(oddFilter()))
.entityOverlay(AgEntity.overlay(E4.class).selectFilter(evenFilter()))
.entityOverlay(AgEntity.overlay(E2.class).readFilter(evenFilter()))
.entityOverlay(AgEntity.overlay(E3.class).readFilter(oddFilter()))
.entityOverlay(AgEntity.overlay(E4.class).readFilter(evenFilter()))
)
.build();

static <T extends DataObject> SelectFilter<T> evenFilter() {
static <T extends DataObject> ReadFilter<T> evenFilter() {
return o -> Cayenne.intPKForObject(o) % 2 == 0;
}

static <T extends DataObject> SelectFilter<T> oddFilter() {
static <T extends DataObject> ReadFilter<T> oddFilter() {
return o -> Cayenne.intPKForObject(o) % 2 != 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class GET_SelectFilter_EntityEncoderFilterIT extends DbTest {
public class GET_ReadFilter_EntityEncoderFilterIT extends DbTest {

@BQTestTool
static final AgCayenneTester tester = tester(Resource.class)
Expand All @@ -36,8 +36,8 @@ public class GET_SelectFilter_EntityEncoderFilterIT extends DbTest {
static EntityEncoderFilter evenFilter() {
return EntityEncoderFilter
.forEntity(E4.class)
.objectCondition(GET_SelectFilter_EntityEncoderFilterIT::willEncodeEven)
.encoder(GET_SelectFilter_EntityEncoderFilterIT::encodeEven)
.objectCondition(GET_ReadFilter_EntityEncoderFilterIT::willEncodeEven)
.encoder(GET_ReadFilter_EntityEncoderFilterIT::encodeEven)
.build();
}

Expand Down
6 changes: 3 additions & 3 deletions agrest-engine/src/main/java/io/agrest/ResourceEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import io.agrest.base.protocol.Exp;
import io.agrest.base.protocol.Sort;
import io.agrest.encoder.EntityEncoderFilter;
import io.agrest.filter.SelectFilter;
import io.agrest.filter.ReadFilter;
import io.agrest.meta.AgAttribute;
import io.agrest.meta.AgEntity;

Expand Down Expand Up @@ -229,12 +229,12 @@ public void setFetchLimit(int fetchLimit) {
* @since 1.23
*/
public boolean isFiltered() {
return !(entityEncoderFilters.isEmpty() && agEntity.getSelectFilter().allowsAll());
return !(entityEncoderFilters.isEmpty() && agEntity.getReadFilter().allowsAll());
}

/**
* @since 3.4
* @deprecated since 4.8 in favor of {@link SelectFilter}.
* @deprecated since 4.8 in favor of {@link ReadFilter}.
*/
@Deprecated
public List<EntityEncoderFilter> getEntityEncoderFilters() {
Expand Down
8 changes: 4 additions & 4 deletions agrest-engine/src/main/java/io/agrest/SelectBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import io.agrest.constraints.Constraint;
import io.agrest.encoder.Encoder;
import io.agrest.encoder.EntityEncoderFilter;
import io.agrest.filter.SelectFilter;
import io.agrest.filter.ReadFilter;
import io.agrest.filter.PropertyFilter;
import io.agrest.meta.AgEntity;
import io.agrest.meta.AgEntityOverlay;
Expand Down Expand Up @@ -46,7 +46,7 @@ public interface SelectBuilder<T> {
* @return this builder instance
* @see AgBuilder#entityEncoderFilter(EntityEncoderFilter)
* @since 3.4
* @deprecated since 4.8 in favor of {@link #filter(Class, SelectFilter)}.
* @deprecated since 4.8 in favor of {@link #filter(Class, ReadFilter)}.
*/
@Deprecated
SelectBuilder<T> entityEncoderFilter(EntityEncoderFilter filter);
Expand Down Expand Up @@ -117,8 +117,8 @@ default <A> SelectBuilder<T> propFilter(Class<A> entityType, PropertyFilter filt
* @return this builder instance
* @since 4.8
*/
default <A> SelectBuilder<T> filter(Class<A> entityType, SelectFilter<A> filter) {
return entityOverlay(AgEntity.overlay(entityType).selectFilter(filter));
default <A> SelectBuilder<T> filter(Class<A> entityType, ReadFilter<A> filter) {
return entityOverlay(AgEntity.overlay(entityType).readFilter(filter));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import io.agrest.annotation.AgRelationship;
import io.agrest.base.reflect.BeanAnalyzer;
import io.agrest.base.reflect.PropertyGetter;
import io.agrest.filter.SelectFilter;
import io.agrest.filter.ReadFilter;
import io.agrest.meta.*;
import io.agrest.resolver.ReaderBasedResolver;
import io.agrest.resolver.RootDataResolver;
Expand Down Expand Up @@ -209,7 +209,7 @@ protected AgEntity<T> buildEntity() {
rootDataResolver != null ? rootDataResolver : ThrowingRootDataResolver.getInstance(),

// TODO: support Exp filters via annotations?
SelectFilter.allowsAllFilter());
ReadFilter.allowsAllFilter());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

import com.fasterxml.jackson.core.JsonGenerator;
import io.agrest.ResourceEntity;
import io.agrest.filter.SelectFilter;
import io.agrest.filter.ReadFilter;

import java.io.IOException;

/**
* Encoder filter that delegates individual encoder methods to custom functions.
*
* @since 3.4
* @deprecated since 4.8 in favor of {@link SelectFilter}.
* @deprecated since 4.8 in favor of {@link ReadFilter}.
*/
@Deprecated
class CompositeEntityEncoderFilter implements EntityEncoderFilter {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package io.agrest.encoder;

import io.agrest.ResourceEntity;
import io.agrest.filter.SelectFilter;
import io.agrest.filter.ReadFilter;

/**
* @since 3.4
* @deprecated since 4.8 in favor of {@link SelectFilter}.
* @deprecated since 4.8 in favor of {@link ReadFilter}.
*/
@Deprecated
@FunctionalInterface
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package io.agrest.encoder;

import com.fasterxml.jackson.core.JsonGenerator;
import io.agrest.filter.SelectFilter;
import io.agrest.filter.ReadFilter;

import java.io.IOException;

/**
* @param <T>
* @since 3.4
* @deprecated since 4.8 in favor of {@link SelectFilter}.
* @deprecated since 4.8 in favor of {@link ReadFilter}.
*/
@Deprecated
@FunctionalInterface
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package io.agrest.encoder;

import io.agrest.filter.SelectFilter;
import io.agrest.filter.ReadFilter;

/**
* @param <T>
* @since 3.4
* @deprecated since 4.8 in favor of {@link SelectFilter}.
* @deprecated since 4.8 in favor of {@link ReadFilter}.
*/
@Deprecated
@FunctionalInterface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

import com.fasterxml.jackson.core.JsonGenerator;
import io.agrest.ResourceEntity;
import io.agrest.filter.SelectFilter;
import io.agrest.filter.ReadFilter;

import java.io.IOException;

/**
* An interceptor for custom encoding of specific entities. An application may define a filter that suppresses
* certain objects based on security constraints, or may provide a custom encoder for a given object, etc.
*
* @deprecated since 4.8 in favor of {@link SelectFilter}.
* @deprecated since 4.8 in favor of {@link ReadFilter}.
*/
@Deprecated
public interface EntityEncoderFilter {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package io.agrest.encoder;

import io.agrest.filter.SelectFilter;
import io.agrest.filter.ReadFilter;

import java.util.Objects;

/**
* A helper class to build EntityEncoderFilters. Usually created via static methods on {@link EntityEncoderFilter}.
*
* @since 3.4
* @deprecated since 4.8 in favor of {@link SelectFilter}.
* @deprecated since 4.8 in favor of {@link ReadFilter}.
*/
@Deprecated
public class EntityEncoderFilterBuilder {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.agrest.encoder;

import com.fasterxml.jackson.core.JsonGenerator;
import io.agrest.filter.SelectFilter;
import io.agrest.filter.ReadFilter;

import java.io.IOException;
import java.util.List;
Expand All @@ -10,7 +10,7 @@
* An encoder that passes encoding request through a chain of {@link EntityEncoderFilter} filters before it gets to the real
* Encoder.
*
* @deprecated since 4.8 in favor of {@link SelectFilter}.
* @deprecated since 4.8 in favor of {@link ReadFilter}.
*/
@Deprecated
public class FilterChainEncoder implements Encoder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
/**
* @since 4.8
*/
final class AllowAllSelectFilter<T> implements SelectFilter<T> {
final class AllowAllReadFilter<T> implements ReadFilter<T> {

static final AllowAllSelectFilter instance = new AllowAllSelectFilter<>();
static final AllowAllReadFilter instance = new AllowAllReadFilter<>();

private AllowAllSelectFilter() {
private AllowAllReadFilter() {
}

@Override
Expand All @@ -21,7 +21,7 @@ public boolean allowsAll() {
}

@Override
public SelectFilter<T> andThen(SelectFilter<T> another) {
public ReadFilter<T> andThen(ReadFilter<T> another) {
return another;
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package io.agrest.filter;

/**
* A filter of objects that defines an access policy for a given entity.
* Per-entity filter used to implement object READ access policies.
*
* @since 4.8
*/
@FunctionalInterface
public interface SelectFilter<T> {
public interface ReadFilter<T> {

static <T> SelectFilter<T> allowsAllFilter() {
return AllowAllSelectFilter.instance;
static <T> ReadFilter<T> allowsAllFilter() {
return AllowAllReadFilter.instance;
}

boolean isAllowed(T object);
Expand All @@ -21,7 +21,7 @@ default boolean allowsAll() {
return false;
}

default SelectFilter<T> andThen(SelectFilter<T> another) {
default ReadFilter<T> andThen(ReadFilter<T> another) {
if (another.allowsAll()) {
return this;
}
Expand Down
4 changes: 2 additions & 2 deletions agrest-engine/src/main/java/io/agrest/meta/AgEntity.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.agrest.meta;

import io.agrest.filter.SelectFilter;
import io.agrest.filter.ReadFilter;
import io.agrest.property.PropertyReader;
import io.agrest.resolver.RootDataResolver;

Expand Down Expand Up @@ -80,5 +80,5 @@ default PropertyReader getIdReader() {
*
* @since 4.8
*/
SelectFilter<T> getSelectFilter();
ReadFilter<T> getReadFilter();
}
18 changes: 10 additions & 8 deletions agrest-engine/src/main/java/io/agrest/meta/AgEntityOverlay.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.agrest.meta;

import io.agrest.filter.SelectFilter;
import io.agrest.filter.ReadFilter;
import io.agrest.filter.PropertyFilteringRulesBuilder;
import io.agrest.filter.PropertyFilter;
import io.agrest.property.PropertyReader;
Expand Down Expand Up @@ -34,13 +34,13 @@ public class AgEntityOverlay<T> {
private PropertyFilter readablePropFilter;
private PropertyFilter writablePropFilter;

private SelectFilter<T> selectFilter;
private ReadFilter<T> readFilter;

public AgEntityOverlay(Class<T> type) {
this.type = type;
this.attributes = new HashMap<>();
this.relationships = new HashMap<>();
this.selectFilter = SelectFilter.allowsAllFilter();
this.readFilter = ReadFilter.allowsAllFilter();
}

private static PropertyReader fromFunction(Function<?, ?> f) {
Expand Down Expand Up @@ -95,7 +95,7 @@ public AgEntity<T> resolve(AgDataMap agDataMap, AgEntity<T> maybeOverlaid) {
resolver.attributes,
resolver.relationships,
rootDataResolver != null ? rootDataResolver : maybeOverlaid.getDataResolver(),
maybeOverlaid.getSelectFilter().andThen(selectFilter)
maybeOverlaid.getReadFilter().andThen(readFilter)
);
}

Expand All @@ -104,7 +104,7 @@ private boolean isEmpty() {
&& relationships.isEmpty()
&& readablePropFilter == null
&& writablePropFilter == null
&& selectFilter == null;
&& readFilter == null;
}

/**
Expand All @@ -130,7 +130,7 @@ public AgEntityOverlay<T> merge(AgEntityOverlay<T> anotherOverlay) {
this.rootDataResolver = anotherOverlay.getRootDataResolver();
}

this.selectFilter = this.selectFilter.andThen(anotherOverlay.selectFilter);
this.readFilter = this.readFilter.andThen(anotherOverlay.readFilter);

return this;
}
Expand Down Expand Up @@ -195,10 +195,12 @@ public AgEntityOverlay<T> writablePropFilter(PropertyFilter filter) {
}

/**
* Adds an object READ filter to the existing filters.
*
* @since 4.8
*/
public AgEntityOverlay<T> selectFilter(SelectFilter<T> filter) {
this.selectFilter = this.selectFilter.andThen(filter);
public AgEntityOverlay<T> readFilter(ReadFilter<T> filter) {
this.readFilter = this.readFilter.andThen(filter);
return this;
}

Expand Down
Loading

0 comments on commit 46c64bc

Please sign in to comment.