Skip to content

Commit 8265e95

Browse files
committed
Allow configuration of data sources and prefixes.
1 parent 802b2fb commit 8265e95

File tree

4 files changed

+107
-8
lines changed

4 files changed

+107
-8
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.settings
22
build
3+
WebContent/WEB-INF/config.json
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"datasources": {
3+
"dbpedia": {
4+
"path": "/path/to/dataset.hdt",
5+
},
6+
},
7+
8+
"prefixes": {
9+
"rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
10+
"rdfs": "http://www.w3.org/2000/01/rdf-schema#",
11+
"owl": "http://www.w3.org/2002/07/owl#",
12+
"skos": "http://www.w3.org/2004/02/skos/core#",
13+
"xsd": "http://www.w3.org/2001/XMLSchema#",
14+
"dc": "http://purl.org/dc/terms/",
15+
"dcterms": "http://purl.org/dc/terms/",
16+
"dc11": "http://purl.org/dc/elements/1.1/",
17+
"foaf": "http://xmlns.com/foaf/0.1/",
18+
"geo": "http://www.w3.org/2003/01/geo/wgs84_pos#",
19+
"dbpedia": "http://dbpedia.org/resource/",
20+
"dbpedia-owl": "http://dbpedia.org/ontology/",
21+
"dbpprop": "http://dbpedia.org/property/",
22+
"hydra": "http://www.w3.org/ns/hydra/core#",
23+
"void": "http://rdfs.org/ns/void#"
24+
}
25+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package org.linkeddatafragments.config;
2+
3+
import java.io.File;
4+
import java.io.FileReader;
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
import java.util.Map.Entry;
8+
9+
import com.google.gson.JsonElement;
10+
import com.google.gson.JsonObject;
11+
import com.google.gson.JsonParser;
12+
13+
/**
14+
* Reads the configuration of a Linked Data Fragments server.
15+
* @author Ruben Verborgh
16+
*/
17+
public class ConfigReader {
18+
private final Map<String, String> dataSources = new HashMap<String, String>();
19+
private final Map<String, String> prefixes = new HashMap<String, String>();
20+
21+
/**
22+
* Creates a new configuration reader.
23+
* @param configFile the name of the configuration file
24+
* @throws Exception if the configuration cannot be loaded
25+
*/
26+
public ConfigReader(String configFile) throws Exception {
27+
// check the configuration file
28+
if (configFile == null || configFile.length() == 0)
29+
throw new Exception("No configuration file name specified.");
30+
final File config = new File(configFile);
31+
if (!config.exists())
32+
throw new Exception("Configuration file " + configFile + " does not exist.");
33+
34+
// read the configuration file
35+
final JsonObject root = new JsonParser().parse(new FileReader(config)).getAsJsonObject();
36+
for (final Entry<String, JsonElement> entry : root.getAsJsonObject("datasources").entrySet()) {
37+
final JsonObject dataSource = entry.getValue().getAsJsonObject();
38+
this.dataSources.put(entry.getKey(), dataSource.getAsJsonPrimitive("path").getAsString());
39+
}
40+
for (final Entry<String, JsonElement> entry : root.getAsJsonObject("prefixes").entrySet())
41+
this.prefixes.put(entry.getKey(), entry.getValue().getAsString());
42+
}
43+
44+
/**
45+
* Gets the data sources.
46+
* @return the data sources
47+
*/
48+
public Map<String, String> getDataSources() {
49+
return dataSources;
50+
}
51+
52+
/**
53+
* Gets the prefixes.
54+
* @return the prefixes
55+
*/
56+
public Map<String, String> getPrefixes() {
57+
return prefixes;
58+
}
59+
}

src/org/linkeddatafragments/servlets/BasicLdfServlet.java

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.linkeddatafragments.servlets;
22

3+
import java.util.HashMap;
4+
import java.util.Map.Entry;
35
import java.util.regex.Matcher;
46
import java.util.regex.Pattern;
57

@@ -9,6 +11,7 @@
911
import javax.servlet.http.HttpServletRequest;
1012
import javax.servlet.http.HttpServletResponse;
1113

14+
import org.linkeddatafragments.config.ConfigReader;
1215
import org.rdfhdt.hdt.hdt.HDT;
1316
import org.rdfhdt.hdt.hdt.HDTManager;
1417
import org.rdfhdt.hdtjena.HDTGraph;
@@ -29,15 +32,18 @@ public class BasicLdfServlet extends HttpServlet {
2932
private final static int TRIPLES_PER_PAGE = 100;
3033
private final static Pattern STRINGPATTERN = Pattern.compile("^\"(.*)\"(?:\\^\\^<(.*)>|@(.*))?$");
3134

32-
private Model model;
35+
private ConfigReader config;
36+
private HashMap<String, Model> dataSources = new HashMap<String, Model>();
3337

3438
@Override
35-
public void init(ServletConfig config) throws ServletException {
39+
public void init(ServletConfig servletConfig) throws ServletException {
3640
try {
37-
final String dataFile = config.getInitParameter("dataFile");
38-
final HDT hdt = HDTManager.mapIndexedHDT(dataFile, null);
39-
final HDTGraph graph = new HDTGraph(hdt);
40-
model = ModelFactory.createModelForGraph(graph);
41+
config = new ConfigReader(servletConfig.getInitParameter("configFile"));
42+
for (Entry<String, String> dataSource : config.getDataSources().entrySet()) {
43+
final HDT hdt = HDTManager.mapIndexedHDT(dataSource.getValue(), null);
44+
final Model model = ModelFactory.createModelForGraph(new HDTGraph(hdt));
45+
dataSources.put(dataSource.getKey(), model);
46+
}
4147
}
4248
catch (Exception e) {
4349
throw new ServletException(e);
@@ -47,16 +53,24 @@ public void init(ServletConfig config) throws ServletException {
4753
@Override
4854
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException {
4955
try {
56+
// find the data source
5057
final String path = request.getRequestURI().substring(request.getContextPath().length());
58+
final String dataSourceName = path.substring(1);
59+
final Model dataSource = dataSources.get(dataSourceName);
60+
if (dataSource == null)
61+
throw new Exception("data source not found");
5162

52-
// parse the subject, predicate, and object parameters
63+
// create the output model
5364
final Model output = ModelFactory.createDefaultModel();
65+
output.setNsPrefixes(config.getPrefixes());
66+
67+
// parse the subject, predicate, and object parameters
5468
final Resource subject = parseAsResource(request.getParameter("subject"), output);
5569
final Property predicate = parseAsProperty(request.getParameter("predicate"), output);
5670
final RDFNode object = parseAsNode(request.getParameter("object"), output);
5771

5872
// add all statements with the given parameters to the output model
59-
final StmtIterator statements = model.listStatements(subject, predicate, object);
73+
final StmtIterator statements = dataSource.listStatements(subject, predicate, object);
6074
for (int i = 0; i < TRIPLES_PER_PAGE && statements.hasNext(); i++)
6175
output.add(statements.next());
6276

0 commit comments

Comments
 (0)