Skip to content

Commit 2b4b916

Browse files
committed
Code review
1 parent 8c9bc07 commit 2b4b916

File tree

3 files changed

+42
-19
lines changed

3 files changed

+42
-19
lines changed

solid/src/main/java/com/inrupt/client/solid/SolidContainer.java

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,10 @@ public SolidContainer(final URI identifier, final Dataset dataset, final Metadat
8383
*/
8484
public Set<SolidResource> getResources() {
8585
// As defined by the Solid Protocol, containers always end with a slash.
86-
if (getIdentifier().getPath().endsWith("/")) {
87-
final String container = normalize(getIdentifier());
88-
final Node node = new Node(rdf.createIRI(getIdentifier().toString()), getGraph());
86+
final URI base = getIdentifier().normalize();
87+
if (isContainer(base)) {
88+
final String container = normalize(base);
89+
final Node node = new Node(rdf.createIRI(base.toString()), getGraph());
8990
try (final Stream<Node.TypedNode> stream = node.getResources()) {
9091
return stream.filter(child -> verifyContainmentIri(container, child)).map(child -> {
9192
final Metadata.Builder builder = Metadata.newBuilder();
@@ -102,12 +103,13 @@ public Set<SolidResource> getResources() {
102103
public ValidationResult validate() {
103104
final List<String> messages = new ArrayList<>();
104105
// Verify that the container URI path ends with a slash
105-
if (!getIdentifier().getPath().endsWith("/")) {
106+
final URI base = getIdentifier().normalize();
107+
if (!isContainer(base)) {
106108
messages.add("Container URI does not end in a slash");
107109
}
108110

109111
// Get the normalized container URI
110-
final String container = normalize(getIdentifier());
112+
final String container = normalize(base);
111113
// Verify that all ldp:contains triples align with Solid expectations
112114
getGraph().stream(null, rdf.createIRI(LDP.contains.toString()), null)
113115
.collect(Collectors.partitioningBy(verifyContainmentTriple(container)))
@@ -121,6 +123,10 @@ public ValidationResult validate() {
121123
return new ValidationResult(false, messages);
122124
}
123125

126+
static boolean isContainer(final URI uri) {
127+
return uri.normalize().getPath().endsWith("/");
128+
}
129+
124130
static String normalize(final URI uri) {
125131
return uri.normalize().toString().split("#")[0].split("\\?")[0];
126132
}
@@ -142,29 +148,30 @@ static Predicate<Triple> verifyContainmentTriple(final String container) {
142148

143149
static boolean verifyContainmentIri(final String container, final IRI object) {
144150

145-
// 1 URI Structure Tests
151+
// URI Structure Tests
152+
final URI base = URI.create(container).normalize();
146153
final URI normalized = URI.create(object.getIRIString()).normalize();
147154

148-
// 1.A Query strings are not allowed in object URI
149-
if (normalized.getQuery() != null) {
155+
// Query strings are not allowed in subject or object URI
156+
if (base.getQuery() != null || normalized.getQuery() != null) {
150157
return false;
151158
}
152159

153-
// 1.B URI fragments are not allowed in object URI
154-
if (normalized.getFragment() != null) {
160+
// URI fragments are not allowed in subject or object URI
161+
if (base.getFragment() != null || normalized.getFragment() != null) {
155162
return false;
156163
}
157164

158-
// 2 Relative path tests
159-
final URI base = URI.create(container).normalize();
160-
final URI relative = base.relativize(normalized);
161-
162-
// 2.A Base URI cannot equal the object URI
163-
if (base.equals(normalized)) {
165+
// Base URI cannot equal the object URI
166+
if (base.getScheme().equals(normalized.getScheme()) &&
167+
base.getSchemeSpecificPart().equals(normalized.getSchemeSpecificPart())) {
164168
return false;
165169
}
166170

167-
// 2.B Object URI must be relative to (contained in) the base URI
171+
// Relative path tests
172+
final URI relative = base.relativize(normalized);
173+
174+
// Object URI must be relative to (contained in) the base URI
168175
if (relative.isAbsolute()) {
169176
return false;
170177
}
@@ -173,7 +180,7 @@ static boolean verifyContainmentIri(final String container, final IRI object) {
173180
final String normalizedPath = relativePath.endsWith("/") ?
174181
relativePath.substring(0, relativePath.length() - 1) : relativePath;
175182

176-
// 2.C Containment cannot skip intermediate nodes
183+
// Containment cannot skip intermediate nodes
177184
if (normalizedPath.contains("/")) {
178185
return false;
179186
}

solid/src/test/java/com/inrupt/client/solid/SolidClientTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,8 @@ void testLowLevelSolidContainer() {
231231
expected.add(URIBuilder.newBuilder(uri).path("newContainer/").build());
232232
expected.add(URIBuilder.newBuilder(uri).path("test.txt").build());
233233
expected.add(URIBuilder.newBuilder(uri).path("test2.txt").build());
234+
expected.add(URIBuilder.newBuilder(uri).path("test3").build());
235+
expected.add(URIBuilder.newBuilder(uri).path("test4").build());
234236

235237
client.send(Request.newBuilder(uri).build(), SolidResourceHandlers.ofSolidContainer())
236238
.thenAccept(response -> {

solid/src/test/resources/__files/container.ttl

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@
77
a ldp:BasicContainer ;
88
dct:modified "2022-11-25T10:36:36Z"^^xsd:dateTime;
99
ldp:contains <newContainer/>, <test.txt>, <test2.txt> .
10+
<intermediate/..>
11+
a ldp:BasicContainer ;
12+
dct:modified "2022-11-25T10:38:12Z"^^xsd:dateTime ;
13+
ldp:contains <test3> .
14+
<intermediate/../>
15+
a ldp:BasicContainer ;
16+
dct:modified "2022-11-25T10:38:47Z"^^xsd:dateTime ;
17+
ldp:contains <test4> .
1018
<newContainer/>
1119
a ldp:BasicContainer ;
1220
dct:modified "2022-11-25T10:36:36Z"^^xsd:dateTime .
@@ -16,10 +24,16 @@
1624
<test2.txt>
1725
a pl:Resource, ldp:NonRDFSource;
1826
dct:modified "2022-11-25T10:37:06Z"^^xsd:dateTime .
27+
<test3>
28+
a ldp:RDFSource ;
29+
dct:modified "2022-11-25T10:37:31Z"^^xsd:dateTime .
30+
<test4>
31+
a ldp:RDFSource ;
32+
dct:modified "2022-11-25T10:39:22Z"^^xsd:dateTime .
1933

2034
# These containment triples should not be included in a getResources response
2135
<>
22-
ldp:contains <https://example.com/other> , <newContainer/child> , <> , <./> ,
36+
ldp:contains <https://example.com/other> , <newContainer/child> , <newContainer%2Fchild2> , <> , <./> ,
2337
<?foo> , <#bar> , <?foo#bar> , <./?foo> , <./#bar> , <./?foo#bar> ,
2438
<../?foo> , <../#bar> , <../?foo#bar> , <child?foo> , <child#bar> , <child?foo#bar> .
2539
<https://example.test/container/>

0 commit comments

Comments
 (0)