Skip to content

Commit b49d133

Browse files
authored
Better RDFa template lookup (#1320)
Resolves #1318
1 parent 1cad9d2 commit b49d133

File tree

4 files changed

+17
-14
lines changed

4 files changed

+17
-14
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ ext {
3838
jenaVersion = "4.0.0"
3939
jsonldVersion = "0.13.0"
4040
liquibaseVersion = "4.3.4"
41-
mustacheVersion = "0.9.6"
41+
mustacheVersion = "0.9.7"
4242
quarkusVersion = "1.13.2.Final"
4343
rxjavaVersion = "2.2.21"
4444
snakeyamlVersion = "1.28"

components/rdfa/src/main/java/org/trellisldp/rdfa/DefaultRdfaWriterService.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,15 @@
1818
import static java.nio.charset.StandardCharsets.UTF_8;
1919
import static java.util.Collections.emptyList;
2020
import static java.util.stream.Collectors.toList;
21+
import static org.slf4j.LoggerFactory.getLogger;
2122

2223
import com.github.mustachejava.DefaultMustacheFactory;
2324
import com.github.mustachejava.Mustache;
2425
import com.github.mustachejava.MustacheFactory;
2526

26-
import java.io.File;
2727
import java.io.IOException;
28-
import java.io.InputStreamReader;
2928
import java.io.OutputStream;
3029
import java.io.OutputStreamWriter;
31-
import java.io.Reader;
3230
import java.io.UncheckedIOException;
3331
import java.io.Writer;
3432
import java.util.Collections;
@@ -42,6 +40,7 @@
4240

4341
import org.apache.commons.rdf.api.Triple;
4442
import org.eclipse.microprofile.config.inject.ConfigProperty;
43+
import org.slf4j.Logger;
4544
import org.trellisldp.api.NamespaceService;
4645
import org.trellisldp.api.RDFaWriterService;
4746

@@ -51,7 +50,7 @@
5150
@ApplicationScoped
5251
public class DefaultRdfaWriterService implements RDFaWriterService {
5352

54-
private static final MustacheFactory mf = new DefaultMustacheFactory();
53+
private static final Logger LOGGER = getLogger(DefaultRdfaWriterService.class);
5554

5655
/** The configuration key controlling the HTML template to use. */
5756
public static final String CONFIG_RDFA_TEMPLATE = "trellis.rdfa.template";
@@ -65,6 +64,7 @@ public class DefaultRdfaWriterService implements RDFaWriterService {
6564
/** The configuration key controlling the JS URLs to use. */
6665
public static final String CONFIG_RDFA_JS = "trellis.rdfa.js";
6766

67+
private MustacheFactory mf;
6868
private Mustache template;
6969

7070
@Inject
@@ -90,9 +90,10 @@ public class DefaultRdfaWriterService implements RDFaWriterService {
9090

9191
@PostConstruct
9292
void init() {
93-
final String templatePath = templateLocation.orElse("org/trellisldp/rdfa/resource.mustache");
94-
final File file = new File(templatePath);
95-
template = file.exists() ? mf.compile(templatePath) : mf.compile(getReader(templatePath), templatePath);
93+
final String resource = templateLocation.orElse("org/trellisldp/rdfa/resource.mustache");
94+
LOGGER.info("Using RDFa writer template: {}", resource);
95+
mf = new DefaultMustacheFactory();
96+
template = mf.compile(resource);
9697
}
9798

9899
/**
@@ -116,9 +117,4 @@ public void write(final Stream<Triple> triples, final OutputStream out, final St
116117
throw new UncheckedIOException(ex);
117118
}
118119
}
119-
120-
static Reader getReader(final String template) {
121-
return new InputStreamReader(Thread.currentThread().getContextClassLoader()
122-
.getResourceAsStream(template), UTF_8);
123-
}
124120
}

components/rdfa/src/main/resources/META-INF/services/org.trellisldp.api.RDFaWriterService

Lines changed: 0 additions & 1 deletion
This file was deleted.

components/test/src/main/java/org/trellisldp/test/LdpRdfTests.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545

4646
import javax.ws.rs.ProcessingException;
4747
import javax.ws.rs.core.EntityTag;
48+
import javax.ws.rs.core.MediaType;
4849
import javax.ws.rs.core.Response;
4950

5051
import org.apache.commons.rdf.api.Graph;
@@ -109,6 +110,7 @@ default Stream<Executable> runTests() throws Exception {
109110
return Stream.of(this::testGetJsonLdDefault,
110111
this::testGetJsonLdCompacted,
111112
this::testGetNTriples,
113+
this::testGetRDFa,
112114
this::testGetRDF,
113115
this::testRdfContainment,
114116
this::testPostJsonLd,
@@ -219,6 +221,12 @@ default void testGetNTriples() {
219221
}
220222
}
221223

224+
default void testGetRDFa() {
225+
try (final Response res = target(getResourceLocation()).request().accept(MediaType.TEXT_HTML).get()) {
226+
assertAll("Check for RDFa", checkRdfResponse(res, LDP.RDFSource, MediaType.TEXT_HTML_TYPE));
227+
}
228+
}
229+
222230
/**
223231
* Test POSTing an RDF resource.
224232
*/

0 commit comments

Comments
 (0)