Skip to content

Commit b2d115c

Browse files
authored
Merge pull request #453 from trellis-ldp/trellis-449
Add default implementations for http.core interfaces
2 parents 8643db9 + d3600a1 commit b2d115c

File tree

14 files changed

+162
-87
lines changed

14 files changed

+162
-87
lines changed

components/app-triplestore/src/main/java/org/trellisldp/app/triplestore/TrellisServiceBundler.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
import org.trellisldp.constraint.LdpConstraints;
4141
import org.trellisldp.file.FileBinaryService;
4242
import org.trellisldp.file.FileMementoService;
43+
import org.trellisldp.http.core.DefaultEtagGenerator;
44+
import org.trellisldp.http.core.DefaultTimemapGenerator;
4345
import org.trellisldp.http.core.EtagGenerator;
4446
import org.trellisldp.http.core.ServiceBundler;
4547
import org.trellisldp.http.core.TimemapGenerator;
@@ -76,8 +78,8 @@ public class TrellisServiceBundler implements ServiceBundler {
7678
public TrellisServiceBundler(final AppConfiguration config, final Environment environment) {
7779
agentService = new SimpleAgentService();
7880
mementoService = new FileMementoService(config.getMementos());
79-
etagGenerator = new EtagGenerator() { };
80-
timemapGenerator = new TimemapGenerator() { };
81+
etagGenerator = new DefaultEtagGenerator();
82+
timemapGenerator = new DefaultTimemapGenerator();
8183
constraintServices = singletonList(new LdpConstraints());
8284
auditService = resourceService = buildResourceService(config, environment);
8385
binaryService = buildBinaryService(config);

components/app/src/test/java/org/trellisldp/app/SimpleServiceBundler.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
import org.trellisldp.api.ResourceService;
3737
import org.trellisldp.constraint.LdpConstraints;
3838
import org.trellisldp.file.FileBinaryService;
39+
import org.trellisldp.http.core.DefaultEtagGenerator;
40+
import org.trellisldp.http.core.DefaultTimemapGenerator;
3941
import org.trellisldp.http.core.EtagGenerator;
4042
import org.trellisldp.http.core.ServiceBundler;
4143
import org.trellisldp.http.core.TimemapGenerator;
@@ -51,8 +53,8 @@ public class SimpleServiceBundler implements ServiceBundler {
5153
private final MementoService mementoService = new NoopMementoService();
5254
private final EventService eventService = new NoopEventService();
5355
private final AgentService agentService = new SimpleAgentService();
54-
private final EtagGenerator etagGenerator = new EtagGenerator() { };
55-
private final TimemapGenerator timemapGenerator = new TimemapGenerator() { };
56+
private final EtagGenerator etagGenerator = new DefaultEtagGenerator();
57+
private final TimemapGenerator timemapGenerator = new DefaultTimemapGenerator();
5658
private final List<ConstraintService> constraintServices = singletonList(new LdpConstraints());
5759
private final IOService ioService = new JenaIOService(new NoopNamespaceService(), null, new NoopProfileCache(),
5860
emptySet(), emptySet());

components/webdav/src/test/java/org/trellisldp/webdav/AbstractWebDAVTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
import org.trellisldp.api.ResourceService;
9494
import org.trellisldp.api.RuntimeTrellisException;
9595
import org.trellisldp.audit.DefaultAuditService;
96-
import org.trellisldp.http.core.EtagGenerator;
96+
import org.trellisldp.http.core.DefaultEtagGenerator;
9797
import org.trellisldp.http.core.ServiceBundler;
9898
import org.trellisldp.io.JenaIOService;
9999
import org.trellisldp.vocabulary.ACL;
@@ -823,7 +823,7 @@ private void setUpBundler() {
823823
when(mockBundler.getBinaryService()).thenReturn(mockBinaryService);
824824
when(mockBundler.getEventService()).thenReturn(new NoopEventService());
825825
when(mockBundler.getMementoService()).thenReturn(new NoopMementoService());
826-
when(mockBundler.getEtagGenerator()).thenReturn(new EtagGenerator() { });
826+
when(mockBundler.getEtagGenerator()).thenReturn(new DefaultEtagGenerator());
827827
}
828828

829829
private void setUpBinaryService() {

components/webdav/src/test/java/org/trellisldp/webdav/TrellisWebDAVRequestFilterTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
import org.mockito.Mock;
4141
import org.trellisldp.api.Resource;
4242
import org.trellisldp.api.ResourceService;
43-
import org.trellisldp.http.core.EtagGenerator;
43+
import org.trellisldp.http.core.DefaultEtagGenerator;
4444
import org.trellisldp.http.core.ServiceBundler;
4545

4646
public class TrellisWebDAVRequestFilterTest {
@@ -80,7 +80,7 @@ public void setUp() {
8080
initMocks(this);
8181

8282
when(mockBundler.getResourceService()).thenReturn(mockResourceService);
83-
when(mockBundler.getEtagGenerator()).thenReturn(new EtagGenerator() { });
83+
when(mockBundler.getEtagGenerator()).thenReturn(new DefaultEtagGenerator());
8484
when(mockResourceService.get(eq(rdf.createIRI(TRELLIS_DATA_PREFIX + PATH))))
8585
.thenAnswer(inv -> completedFuture(MISSING_RESOURCE));
8686
when(mockContext.getMethod()).thenReturn(PUT);
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
package org.trellisldp.http.core;
15+
16+
import static org.apache.commons.codec.digest.DigestUtils.md5Hex;
17+
18+
import javax.enterprise.context.ApplicationScoped;
19+
20+
import org.trellisldp.api.Resource;
21+
22+
/**
23+
* A default EtagGenerator.
24+
*/
25+
@ApplicationScoped
26+
public class DefaultEtagGenerator implements EtagGenerator {
27+
28+
@Override
29+
public String getValue(final Resource resource) {
30+
return md5Hex(resource.getModified().getNano() + "." + resource.getIdentifier());
31+
}
32+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
package org.trellisldp.http.core;
15+
16+
import static java.time.ZonedDateTime.parse;
17+
import static java.time.format.DateTimeFormatter.RFC_1123_DATE_TIME;
18+
import static org.trellisldp.api.TrellisUtils.getInstance;
19+
import static org.trellisldp.http.core.HttpConstants.DATETIME;
20+
import static org.trellisldp.http.core.HttpConstants.FROM;
21+
import static org.trellisldp.http.core.HttpConstants.MEMENTO;
22+
import static org.trellisldp.http.core.HttpConstants.UNTIL;
23+
import static org.trellisldp.vocabulary.RDF.type;
24+
25+
import java.util.ArrayList;
26+
import java.util.List;
27+
import java.util.stream.Stream;
28+
29+
import javax.enterprise.context.ApplicationScoped;
30+
import javax.ws.rs.core.Link;
31+
32+
import org.apache.commons.rdf.api.IRI;
33+
import org.apache.commons.rdf.api.RDF;
34+
import org.apache.commons.rdf.api.Triple;
35+
import org.trellisldp.vocabulary.Memento;
36+
import org.trellisldp.vocabulary.Time;
37+
import org.trellisldp.vocabulary.XSD;
38+
39+
40+
41+
/**
42+
* A default TimemapGenerator.
43+
*/
44+
@ApplicationScoped
45+
public class DefaultTimemapGenerator implements TimemapGenerator {
46+
47+
private static final String TIME_IRI_PREFIX = "http://reference.data.gov.uk/id/gregorian-instant/";
48+
private static final RDF rdf = getInstance();
49+
50+
@Override
51+
public Stream<Triple> asRdf(final String identifier, final List<Link> mementos) {
52+
final IRI originalResource = rdf.createIRI(identifier);
53+
final List<Triple> descriptions = new ArrayList<>();
54+
55+
descriptions.add(rdf.createTriple(originalResource, type, Memento.OriginalResource));
56+
descriptions.add(rdf.createTriple(originalResource, type, Memento.TimeGate));
57+
descriptions.add(rdf.createTriple(originalResource, Memento.timegate, originalResource));
58+
descriptions.add(rdf.createTriple(originalResource, Memento.timemap,
59+
rdf.createIRI(identifier + "?ext=timemap")));
60+
61+
mementos.stream().filter(link -> link.getRels().contains(MEMENTO))
62+
.map(link -> rdf.createTriple(originalResource, Memento.memento, rdf.createIRI(link.getUri().toString())))
63+
.forEach(descriptions::add);
64+
65+
return Stream.concat(descriptions.stream(), mementos.stream().flatMap(link -> {
66+
final String linkUri = link.getUri().toString();
67+
final IRI iri = rdf.createIRI(linkUri);
68+
final Stream.Builder<Triple> buffer = Stream.builder();
69+
70+
// TimeMap triples
71+
if (link.getParams().containsKey(FROM)) {
72+
buffer.add(rdf.createTriple(iri, type, Memento.TimeMap));
73+
buffer.add(rdf.createTriple(iri, Time.hasBeginning, rdf.createIRI(TIME_IRI_PREFIX +
74+
parse(link.getParams().get(FROM), RFC_1123_DATE_TIME).toString())));
75+
}
76+
if (link.getParams().containsKey(UNTIL)) {
77+
buffer.add(rdf.createTriple(iri, Time.hasEnd, rdf.createIRI(TIME_IRI_PREFIX +
78+
parse(link.getParams().get(UNTIL), RFC_1123_DATE_TIME).toString())));
79+
}
80+
81+
// Memento triples
82+
if (isMementoLink(link)) {
83+
final IRI original = rdf.createIRI(linkUri.split("\\?")[0]);
84+
final IRI timemapUrl = rdf.createIRI(linkUri.split("\\?")[0] + "?ext=timemap");
85+
buffer.add(rdf.createTriple(iri, type, Memento.Memento));
86+
buffer.add(rdf.createTriple(iri, Memento.original, original));
87+
buffer.add(rdf.createTriple(iri, Memento.timegate, original));
88+
buffer.add(rdf.createTriple(iri, Memento.timemap, timemapUrl));
89+
buffer.add(rdf.createTriple(iri, Time.hasTime, rdf.createIRI(TIME_IRI_PREFIX +
90+
parse(link.getParams().get(DATETIME), RFC_1123_DATE_TIME).toString())));
91+
buffer.add(rdf.createTriple(iri, Memento.mementoDatetime, rdf.createLiteral(parse(
92+
link.getParams().get(DATETIME), RFC_1123_DATE_TIME).toString(), XSD.dateTime)));
93+
}
94+
return buffer.build();
95+
}));
96+
}
97+
}

core/http/src/main/java/org/trellisldp/http/core/EtagGenerator.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
*/
1414
package org.trellisldp.http.core;
1515

16-
import static org.apache.commons.codec.digest.DigestUtils.md5Hex;
17-
1816
import org.trellisldp.api.Resource;
1917

2018
public interface EtagGenerator {
@@ -28,7 +26,5 @@ public interface EtagGenerator {
2826
* @param resource the Trellis resource
2927
* @return the value for the entity tag
3028
*/
31-
default String getValue(final Resource resource) {
32-
return md5Hex(resource.getModified().getNano() + "." + resource.getIdentifier());
33-
}
29+
String getValue(Resource resource);
3430
}

core/http/src/main/java/org/trellisldp/http/core/TimemapGenerator.java

Lines changed: 1 addition & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,15 @@
1313
*/
1414
package org.trellisldp.http.core;
1515

16-
import static java.time.ZonedDateTime.parse;
17-
import static java.time.format.DateTimeFormatter.RFC_1123_DATE_TIME;
18-
import static org.trellisldp.api.TrellisUtils.getInstance;
1916
import static org.trellisldp.http.core.HttpConstants.DATETIME;
20-
import static org.trellisldp.http.core.HttpConstants.FROM;
2117
import static org.trellisldp.http.core.HttpConstants.MEMENTO;
22-
import static org.trellisldp.http.core.HttpConstants.UNTIL;
23-
import static org.trellisldp.vocabulary.RDF.type;
2418

25-
import java.util.ArrayList;
2619
import java.util.List;
2720
import java.util.stream.Stream;
2821

2922
import javax.ws.rs.core.Link;
3023

31-
import org.apache.commons.rdf.api.IRI;
32-
import org.apache.commons.rdf.api.RDF;
3324
import org.apache.commons.rdf.api.Triple;
34-
import org.trellisldp.vocabulary.Memento;
35-
import org.trellisldp.vocabulary.Time;
36-
import org.trellisldp.vocabulary.XSD;
3725

3826
/**
3927
* A service to generate a stream of Triples from a list of mementos.
@@ -46,54 +34,7 @@ public interface TimemapGenerator {
4634
* @param mementos the mementos
4735
* @return a stream of triples
4836
*/
49-
default Stream<Triple> asRdf(String identifier, List<Link> mementos) {
50-
final String timeIriPrefix = "http://reference.data.gov.uk/id/gregorian-instant/";
51-
final RDF rdf = getInstance();
52-
final IRI originalResource = rdf.createIRI(identifier);
53-
final List<Triple> descriptions = new ArrayList<>();
54-
55-
descriptions.add(rdf.createTriple(originalResource, type, Memento.OriginalResource));
56-
descriptions.add(rdf.createTriple(originalResource, type, Memento.TimeGate));
57-
descriptions.add(rdf.createTriple(originalResource, Memento.timegate, originalResource));
58-
descriptions.add(rdf.createTriple(originalResource, Memento.timemap,
59-
rdf.createIRI(identifier + "?ext=timemap")));
60-
61-
mementos.stream().filter(link -> link.getRels().contains(MEMENTO))
62-
.map(link -> rdf.createTriple(originalResource, Memento.memento, rdf.createIRI(link.getUri().toString())))
63-
.forEach(descriptions::add);
64-
65-
return Stream.concat(descriptions.stream(), mementos.stream().flatMap(link -> {
66-
final String linkUri = link.getUri().toString();
67-
final IRI iri = rdf.createIRI(linkUri);
68-
final Stream.Builder<Triple> buffer = Stream.builder();
69-
70-
// TimeMap triples
71-
if (link.getParams().containsKey(FROM)) {
72-
buffer.add(rdf.createTriple(iri, type, Memento.TimeMap));
73-
buffer.add(rdf.createTriple(iri, Time.hasBeginning, rdf.createIRI(timeIriPrefix +
74-
parse(link.getParams().get(FROM), RFC_1123_DATE_TIME).toString())));
75-
}
76-
if (link.getParams().containsKey(UNTIL)) {
77-
buffer.add(rdf.createTriple(iri, Time.hasEnd, rdf.createIRI(timeIriPrefix +
78-
parse(link.getParams().get(UNTIL), RFC_1123_DATE_TIME).toString())));
79-
}
80-
81-
// Memento triples
82-
if (isMementoLink(link)) {
83-
final IRI original = rdf.createIRI(linkUri.split("\\?")[0]);
84-
final IRI timemapUrl = rdf.createIRI(linkUri.split("\\?")[0] + "?ext=timemap");
85-
buffer.add(rdf.createTriple(iri, type, Memento.Memento));
86-
buffer.add(rdf.createTriple(iri, Memento.original, original));
87-
buffer.add(rdf.createTriple(iri, Memento.timegate, original));
88-
buffer.add(rdf.createTriple(iri, Memento.timemap, timemapUrl));
89-
buffer.add(rdf.createTriple(iri, Time.hasTime, rdf.createIRI(timeIriPrefix +
90-
parse(link.getParams().get(DATETIME), RFC_1123_DATE_TIME).toString())));
91-
buffer.add(rdf.createTriple(iri, Memento.mementoDatetime, rdf.createLiteral(parse(
92-
link.getParams().get(DATETIME), RFC_1123_DATE_TIME).toString(), XSD.dateTime)));
93-
}
94-
return buffer.build();
95-
}));
96-
}
37+
Stream<Triple> asRdf(String identifier, List<Link> mementos);
9738

9839
/**
9940
* Check whether the provided link is to be accepted as a memento link.

core/http/src/test/java/org/trellisldp/http/BaseTrellisHttpResourceTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@
7272
import org.trellisldp.api.Resource;
7373
import org.trellisldp.api.ResourceService;
7474
import org.trellisldp.constraint.LdpConstraints;
75-
import org.trellisldp.http.core.EtagGenerator;
75+
import org.trellisldp.http.core.DefaultEtagGenerator;
76+
import org.trellisldp.http.core.DefaultTimemapGenerator;
7677
import org.trellisldp.http.core.ServiceBundler;
77-
import org.trellisldp.http.core.TimemapGenerator;
7878
import org.trellisldp.io.JenaIOService;
7979
import org.trellisldp.vocabulary.ACL;
8080
import org.trellisldp.vocabulary.DC;
@@ -197,8 +197,8 @@ private void setUpBundler() {
197197
when(mockBundler.getAuditService()).thenReturn(auditService);
198198
when(mockBundler.getEventService()).thenReturn(mockEventService);
199199
when(mockBundler.getConstraintServices()).thenReturn(singletonList(new LdpConstraints()));
200-
when(mockBundler.getEtagGenerator()).thenReturn(new EtagGenerator() { });
201-
when(mockBundler.getTimemapGenerator()).thenReturn(new TimemapGenerator() { });
200+
when(mockBundler.getEtagGenerator()).thenReturn(new DefaultEtagGenerator());
201+
when(mockBundler.getTimemapGenerator()).thenReturn(new DefaultTimemapGenerator());
202202
}
203203

204204
private void setUpResourceService() {

core/http/src/test/java/org/trellisldp/http/core/TimemapGeneratorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class TimemapGeneratorTest {
2727

2828
@Test
2929
public void testIsMementoLink() {
30-
final TimemapGenerator svc = new TimemapGenerator() { };
30+
final TimemapGenerator svc = new DefaultTimemapGenerator();
3131
assertTrue(svc.isMementoLink(fromUri(url).rel("memento")
3232
.param("datetime", "Fri, 11 May 2018 15:29:25 GMT").build()), "Valid Memento Link header skipped!");
3333
assertFalse(svc.isMementoLink(fromUri(url).rel("foo")

core/http/src/test/java/org/trellisldp/http/impl/BaseTestHandler.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@
9292
import org.trellisldp.api.ResourceService;
9393
import org.trellisldp.api.RuntimeTrellisException;
9494
import org.trellisldp.constraint.LdpConstraints;
95-
import org.trellisldp.http.core.EtagGenerator;
95+
import org.trellisldp.http.core.DefaultEtagGenerator;
96+
import org.trellisldp.http.core.DefaultTimemapGenerator;
9697
import org.trellisldp.http.core.ServiceBundler;
97-
import org.trellisldp.http.core.TimemapGenerator;
9898
import org.trellisldp.http.core.TrellisRequest;
9999
import org.trellisldp.vocabulary.LDP;
100100

@@ -253,8 +253,8 @@ private void setUpBundler() {
253253
when(mockBundler.getAgentService()).thenReturn(agentService);
254254
when(mockBundler.getEventService()).thenReturn(mockEventService);
255255
when(mockBundler.getConstraintServices()).thenReturn(singletonList(new LdpConstraints()));
256-
when(mockBundler.getTimemapGenerator()).thenReturn(new TimemapGenerator() { });
257-
when(mockBundler.getEtagGenerator()).thenReturn(new EtagGenerator() { });
256+
when(mockBundler.getTimemapGenerator()).thenReturn(new DefaultTimemapGenerator());
257+
when(mockBundler.getEtagGenerator()).thenReturn(new DefaultEtagGenerator());
258258
}
259259

260260
private void setUpIoService() {

core/http/src/test/java/org/trellisldp/http/impl/GetHandlerTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
import org.junit.jupiter.api.Test;
7979
import org.junit.jupiter.api.function.Executable;
8080
import org.trellisldp.api.BinaryMetadata;
81+
import org.trellisldp.http.core.DefaultEtagGenerator;
8182
import org.trellisldp.http.core.EtagGenerator;
8283
import org.trellisldp.http.core.Prefer;
8384
import org.trellisldp.vocabulary.LDP;
@@ -89,7 +90,7 @@
8990
*/
9091
public class GetHandlerTest extends BaseTestHandler {
9192

92-
private static final EtagGenerator etagGenerator = new EtagGenerator() { };
93+
private static final EtagGenerator etagGenerator = new DefaultEtagGenerator();
9394

9495
private BinaryMetadata testBinary = BinaryMetadata.builder(rdf.createIRI("file:///testResource.txt"))
9596
.mimeType("text/plain").build();

0 commit comments

Comments
 (0)