Skip to content

Commit cc0901f

Browse files
authored
Make Metadata class more extensible (#1085)
Resolves #1084
1 parent 6cc163b commit cc0901f

File tree

3 files changed

+60
-4
lines changed

3 files changed

+60
-4
lines changed

core/api/src/main/java/org/trellisldp/api/Metadata.java

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@
1616
package org.trellisldp.api;
1717

1818
import static java.util.Collections.emptySet;
19+
import static java.util.Collections.unmodifiableMap;
1920
import static java.util.Objects.requireNonNull;
2021
import static java.util.Optional.ofNullable;
2122

23+
import java.util.HashMap;
24+
import java.util.Map;
2225
import java.util.Optional;
2326
import java.util.Set;
2427

@@ -36,9 +39,11 @@ public final class Metadata {
3639
private final IRI membershipResource;
3740
private final IRI memberOfRelation;
3841
private final IRI insertedContentRelation;
42+
private final IRI agent;
3943
private final BinaryMetadata binary;
4044
private final Set<IRI> graphNames;
4145
private final String revision;
46+
private final Map<String, String> properties;
4247

4348
/**
4449
* A Metadata-bearing data structure for use with resource manipulation.
@@ -50,26 +55,31 @@ public final class Metadata {
5055
* @param memberRelation an LDP hasMemberRelation predicate, may be {@code null}
5156
* @param memberOfRelation an LDP isMemberOfRelation predicate, may be {@code null}
5257
* @param insertedContentRelation an LDP insertedContentRelation, may be {@code null}
58+
* @param agent the agent associated with the operation, may be {@code null}
5359
* @param binary metadata about a BinaryMetadata, may be {@code null}
5460
* @param revision a revision value, may be {@code null}. This value may be used by a
5561
* {@link ResourceService} implementation for additional concurrency control.
5662
* This value would typically be used in tandem with the {@link Resource#getRevision}
5763
* method.
5864
* @param graphNames a collection of metadata graphNames
65+
* @param properties a collection of additional properties
5966
*/
6067
private Metadata(final IRI identifier, final IRI ixnModel, final IRI container, final IRI membershipResource,
61-
final IRI memberRelation, final IRI memberOfRelation, final IRI insertedContentRelation,
62-
final BinaryMetadata binary, final String revision, final Set<IRI> graphNames) {
68+
final IRI memberRelation, final IRI memberOfRelation, final IRI insertedContentRelation, final IRI agent,
69+
final BinaryMetadata binary, final String revision, final Set<IRI> graphNames,
70+
final Map<String, String> properties) {
6371
this.identifier = requireNonNull(identifier, "Identifier cannot be null!");
6472
this.ixnModel = requireNonNull(ixnModel, "Interaction model cannot be null!");
6573
this.container = container;
6674
this.membershipResource = membershipResource;
6775
this.memberRelation = memberRelation;
6876
this.memberOfRelation = memberOfRelation;
6977
this.insertedContentRelation = insertedContentRelation;
78+
this.agent = agent;
7079
this.binary = binary;
7180
this.revision = revision;
7281
this.graphNames = graphNames;
82+
this.properties = properties;
7383
}
7484

7585
/**
@@ -129,6 +139,14 @@ public Optional<IRI> getContainer() {
129139
return ofNullable(container);
130140
}
131141

142+
/**
143+
* Retrieve the agent associated with this opertation, if known.
144+
* @return the agent identified with a WebID
145+
*/
146+
public Optional<IRI> getAgent() {
147+
return ofNullable(agent);
148+
}
149+
132150
/**
133151
* Retrieve the membership resource if this is an LDP Direct or Indirect container.
134152
*
@@ -194,6 +212,14 @@ public Optional<String> getRevision() {
194212
return ofNullable(revision);
195213
}
196214

215+
/**
216+
* Retrieve any additional properties.
217+
* @return an immutable collection of properties
218+
*/
219+
public Map<String, String> getProperties() {
220+
return properties;
221+
}
222+
197223
/**
198224
* A mutable builder for a {@link Metadata} object.
199225
*/
@@ -205,9 +231,11 @@ public static final class Builder {
205231
private IRI membershipResource;
206232
private IRI memberOfRelation;
207233
private IRI insertedContentRelation;
234+
private IRI agent;
208235
private BinaryMetadata binary;
209236
private String revision;
210237
private Set<IRI> graphNames = emptySet();
238+
private Map<String, String> properties = new HashMap<>();
211239

212240
/**
213241
* Create a Metadata builder with the provided identifier.
@@ -277,6 +305,16 @@ public Builder insertedContentRelation(final IRI insertedContentRelation) {
277305
return this;
278306
}
279307

308+
/**
309+
* Set the agent value.
310+
* @param agent the agent associated with the operation
311+
* @return this builder
312+
*/
313+
public Builder agent(final IRI agent) {
314+
this.agent = agent;
315+
return this;
316+
}
317+
280318
/**
281319
* Set the binary metadata.
282320
* @param binary the binary metadata
@@ -307,13 +345,24 @@ public Builder revision(final String revision) {
307345
return this;
308346
}
309347

348+
/**
349+
* Set a property on the resource metadata.
350+
* @param key the property key
351+
* @param value the property value
352+
* @return this builder
353+
*/
354+
public Builder property(final String key, final String value) {
355+
this.properties.put(key, value);
356+
return this;
357+
}
358+
310359
/**
311360
* Build the Metadata object, transitioning this builder to the built state.
312361
* @return the built Metadata
313362
*/
314363
public Metadata build() {
315364
return new Metadata(identifier, ixnModel, container, membershipResource, memberRelation, memberOfRelation,
316-
insertedContentRelation, binary, revision, graphNames);
365+
insertedContentRelation, agent, binary, revision, graphNames, unmodifiableMap(properties));
317366
}
318367
}
319368
}

core/api/src/test/java/org/trellisldp/api/MetadataTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class MetadataTest {
3636
private static final IRI identifier = rdf.createIRI("trellis:data/resource");
3737
private static final IRI member = rdf.createIRI("trellis:data/member");
3838
private static final IRI root = rdf.createIRI("trellis:data/");
39+
private static final IRI agent = rdf.createIRI("https://example.com/agent#i");
3940

4041
@Test
4142
void testMetadataIndirectContainer() {
@@ -44,6 +45,7 @@ void testMetadataIndirectContainer() {
4445
.container(root).memberRelation(LDP.member)
4546
.membershipResource(member)
4647
.insertedContentRelation(FOAF.primaryTopic)
48+
.property("key", "value")
4749
.revision("blahblahblah").build();
4850
assertEquals(identifier, metadata.getIdentifier());
4951
assertEquals(LDP.IndirectContainer, metadata.getInteractionModel());
@@ -54,20 +56,24 @@ void testMetadataIndirectContainer() {
5456
assertFalse(metadata.getMemberOfRelation().isPresent());
5557
assertTrue(metadata.getMetadataGraphNames().isEmpty());
5658
assertFalse(metadata.getBinary().isPresent());
59+
assertFalse(metadata.getAgent().isPresent());
5760
assertEquals(of("blahblahblah"), metadata.getRevision());
61+
assertEquals("value", metadata.getProperties().get("key"));
5862
}
5963

6064
@Test
6165
void testMetadataDirectContainer() {
6266
final Metadata metadata = Metadata.builder(identifier)
6367
.interactionModel(LDP.DirectContainer)
68+
.agent(agent)
6469
.container(root).memberOfRelation(DC.isPartOf)
6570
.membershipResource(member).metadataGraphNames(singleton(Trellis.PreferAccessControl)).build();
6671
assertEquals(identifier, metadata.getIdentifier());
6772
assertEquals(LDP.DirectContainer, metadata.getInteractionModel());
6873
assertEquals(of(root), metadata.getContainer());
6974
assertEquals(of(member), metadata.getMembershipResource());
7075
assertEquals(of(DC.isPartOf), metadata.getMemberOfRelation());
76+
assertEquals(of(agent), metadata.getAgent());
7177
assertFalse(metadata.getInsertedContentRelation().isPresent());
7278
assertFalse(metadata.getMemberRelation().isPresent());
7379
assertFalse(metadata.getBinary().isPresent());

core/http/src/main/java/org/trellisldp/http/impl/MutatingLdpHandler.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,8 @@ protected CompletionStage<Void> persistBinaryContent(final BinaryMetadata metada
258258
}
259259

260260
protected Metadata.Builder metadataBuilder(final IRI identifier, final IRI ixnModel, final Dataset mutable) {
261-
final Metadata.Builder builder = Metadata.builder(identifier).interactionModel(ixnModel);
261+
final Metadata.Builder builder = Metadata.builder(identifier).interactionModel(ixnModel)
262+
.agent(getSession().getAgent());
262263
mutable.getGraph(Trellis.PreferUserManaged).ifPresent(graph -> {
263264
graph.stream(null, LDP.membershipResource, null)
264265
.filter(triple -> matchIdentifier(triple.getSubject(), identifier)).findFirst().map(Triple::getObject)

0 commit comments

Comments
 (0)