Skip to content
This repository has been archived by the owner on Jan 3, 2021. It is now read-only.

Commit

Permalink
metadata/VoID support second part, now with on/off switch and
Browse files Browse the repository at this point in the history
user-specified dataset metadata
  • Loading branch information
hannes committed May 4, 2012
1 parent 61b2b5e commit 3d16138
Show file tree
Hide file tree
Showing 10 changed files with 257 additions and 110 deletions.
12 changes: 12 additions & 0 deletions doc/terms/config.ttl
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@ d2r:metadataTemplate a rdf:Property;
rdfs:domain d2r:Server;
rdfs:isDefinedBy <>;
.
d2r:datasetMetadataTemplate a rdf:Property;
rdfs:label "dataset metadata template"@en;
rdfs:comment "A RDF dataset metadata template."@en;
rdfs:domain d2r:Server;
rdfs:isDefinedBy <>;
.
d2r:disableMetadata a rdf:Property;
rdfs:label "disable all metadata generation"@en;
rdfs:comment "Whether to disable all metadata generation."@en;
rdfs:domain d2r:Server;
rdfs:isDefinedBy <>;
.
d2r:limitPerClassMap a rdf:Property;
rdfs:label "Limit per class map"@en;
rdfs:comment "Maximum number of values for each class map that will be displayed in the web interface when browsing resources. A value of false means no limit."@en;
Expand Down
130 changes: 102 additions & 28 deletions src/de/fuberlin/wiwiss/d2rq/server/ConfigLoader.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package de.fuberlin.wiwiss.d2rq.server;

import java.io.File;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;

import javax.servlet.ServletContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.ResIterator;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
Expand All @@ -24,11 +31,12 @@ public class ConfigLoader {

public static final int DEFAULT_LIMIT_PER_CLASS_MAP = 50;
public static final int DEFAULT_LIMIT_PER_PROPERTY_BRIDGE = 50;

private static final Log log = LogFactory.getLog(ConfigLoader.class);

/**
* Accepts an absolute URI, relative file: URI, or plain
* file name (including names with spaces, Windows backslashes
* etc) and returns an equivalent full absolute URI.
* Accepts an absolute URI, relative file: URI, or plain file name
* (including names with spaces, Windows backslashes etc) and returns an
* equivalent full absolute URI.
*/
public static String toAbsoluteURI(String fileName) {
// Permit backslashes when using the file: URI scheme under Windows
Expand All @@ -37,11 +45,14 @@ public static String toAbsoluteURI(String fileName) {
fileName = fileName.replaceAll("\\\\", "/");
}
try {
// Check if it's an absolute URI already - but don't confuse Windows drive letters with URI schemes
if (fileName.matches("[a-zA-Z0-9]{2,}:.*") && new URI(fileName).isAbsolute()) {
// Check if it's an absolute URI already - but don't confuse Windows
// drive letters with URI schemes
if (fileName.matches("[a-zA-Z0-9]{2,}:.*")
&& new URI(fileName).isAbsolute()) {
return fileName;
}
return new File(fileName).getAbsoluteFile().toURI().normalize().toString();
return new File(fileName).getAbsoluteFile().toURI().normalize()
.toString();
} catch (URISyntaxException ex) {
throw new D2RQException(ex);
}
Expand All @@ -59,9 +70,10 @@ public static String toAbsoluteURI(String fileName) {
private boolean autoReloadMapping = true;
private int limitPerClassMap = DEFAULT_LIMIT_PER_CLASS_MAP;
private int limitPerPropertyBridge = DEFAULT_LIMIT_PER_PROPERTY_BRIDGE;

/**
* @param configURL Config file URL, or <code>null</code> for an empty config
* @param configURL
* Config file URL, or <code>null</code> for an empty config
*/
public ConfigLoader(String configURL) {
this.configURL = configURL;
Expand Down Expand Up @@ -101,8 +113,8 @@ public void load() {
try {
this.port = Integer.parseInt(value);
} catch (NumberFormatException ex) {
throw new D2RQException(
"Illegal integer value '" + value + "' for d2r:port");
throw new D2RQException("Illegal integer value '" + value
+ "' for d2r:port");
}
}
s = server.getProperty(RDFS.label);
Expand All @@ -116,7 +128,7 @@ public void load() {
s = server.getProperty(D2RConfig.vocabularyIncludeInstances);
if (s != null) {
this.vocabularyIncludeInstances = s.getBoolean();
}
}
s = server.getProperty(D2RConfig.autoReloadMapping);
if (s != null) {
this.autoReloadMapping = s.getBoolean();
Expand All @@ -142,55 +154,55 @@ public void load() {
}
}
}

public boolean isLocalMappingFile() {
return this.isLocalMappingFile;
}

public String getLocalMappingFilename() {
if (!this.isLocalMappingFile) {
return null;
}
return this.mappingFilename;
}

public int port() {
if (this.model == null) {
throw new IllegalStateException("Must load() first");
}
return this.port;
}

public String baseURI() {
if (this.model == null) {
throw new IllegalStateException("Must load() first");
}
return this.baseURI;
}

public String serverName() {
if (this.model == null) {
throw new IllegalStateException("Must load() first");
}
return this.serverName;
}

public boolean getVocabularyIncludeInstances() {
return this.vocabularyIncludeInstances;
}

public int getLimitPerClassMap() {
return limitPerClassMap;
}

public int getLimitPerPropertyBridge() {
return limitPerPropertyBridge;
}

public boolean getAutoReloadMapping() {
return this.autoReloadMapping;
}

public void addDocumentMetadata(Model document, Resource documentResource) {
if (this.documentMetadata == null) {
return;
Expand All @@ -201,31 +213,93 @@ public void addDocumentMetadata(Model document, Resource documentResource) {
StmtIterator it = this.documentMetadata.listProperties();
while (it.hasNext()) {
Statement stmt = it.nextStatement();
document.add(documentResource, stmt.getPredicate(), stmt.getObject());

document.add(documentResource, stmt.getPredicate(),
stmt.getObject());
}
it = this.model.listStatements(null, null, this.documentMetadata);
while (it.hasNext()) {
Statement stmt = it.nextStatement();
if (stmt.getPredicate().equals(D2RConfig.documentMetadata)) {
continue;
}
document.add(stmt.getSubject(), stmt.getPredicate(), documentResource);
document.add(stmt.getSubject(), stmt.getPredicate(),
documentResource);
}
}

protected Resource findServerResource() {
ResIterator it = this.model.listSubjectsWithProperty(RDF.type, D2RConfig.Server);
ResIterator it = this.model.listSubjectsWithProperty(RDF.type,
D2RConfig.Server);
if (!it.hasNext()) {
return null;
}
return it.nextResource();
}

protected Resource findDatabaseResource() {
ResIterator it = this.model.listSubjectsWithProperty(RDF.type, D2RQ.Database);
ResIterator it = this.model.listSubjectsWithProperty(RDF.type,
D2RQ.Database);
if (!it.hasNext()) {
return null;
}
return it.nextResource();
}

// cache resource metadata model, so we dont need to load the file on every
// request (!)
private Model resourceMetadataTemplate = null;

protected Model getResourceMetadataTemplate(D2RServer server,
ServletContext context) {
if (resourceMetadataTemplate == null) {
resourceMetadataTemplate = loadMetadataTemplate(server, context,
D2RConfig.metadataTemplate, "resource-metadata.ttl");
}
return resourceMetadataTemplate;
}

// cache dataset metadata model, so we dont need to load the file on every
// request (!)
private Model datasetMetadataTemplate = null;

protected Model getDatasetMetadataTemplate(D2RServer server,
ServletContext context) {
if (datasetMetadataTemplate == null) {
datasetMetadataTemplate = loadMetadataTemplate(server, context,
D2RConfig.datasetMetadataTemplate, "dataset-metadata.ttl");

}
return datasetMetadataTemplate;
}

private Model loadMetadataTemplate(D2RServer server,
ServletContext context, Property configurationFlag,
String defaultTemplateName) {

Model metadataTemplate;

File userTemplateFile = MetadataCreator.findTemplateFile(server,
configurationFlag);
Model userResourceTemplate = MetadataCreator
.loadTemplateFile(userTemplateFile);
if (userResourceTemplate != null && userResourceTemplate.size() > 0) {
metadataTemplate = userResourceTemplate;
log.info("Using user-specified metadata template at '"
+ userTemplateFile + "'");

} else {
// load default template
InputStream drtStream = context.getResourceAsStream("/WEB-INF/"
+ defaultTemplateName);
log.info("Using default metadata template.");
metadataTemplate = MetadataCreator.loadMetadataTemplate(drtStream);
}

return metadataTemplate;
}

protected boolean serveMetadata() {
return !findServerResource().hasProperty(D2RConfig.disableMetadata);
}
}
18 changes: 10 additions & 8 deletions src/de/fuberlin/wiwiss/d2rq/server/D2RServer.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package de.fuberlin.wiwiss.d2rq.server;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import javax.servlet.ServletContext;

import org.apache.commons.logging.Log;
Expand All @@ -12,17 +16,22 @@

import com.hp.hpl.jena.graph.BulkUpdateHandler;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.shared.JenaException;
import com.hp.hpl.jena.shared.PrefixMapping;
import com.hp.hpl.jena.sparql.core.describe.DescribeHandler;
import com.hp.hpl.jena.sparql.core.describe.DescribeHandlerFactory;
import com.hp.hpl.jena.sparql.core.describe.DescribeHandlerRegistry;
import com.hp.hpl.jena.sparql.util.Context;
import com.hp.hpl.jena.util.FileManager;

import de.fuberlin.wiwiss.d2rq.ResourceDescriber;
import de.fuberlin.wiwiss.d2rq.SystemLoader;
import de.fuberlin.wiwiss.d2rq.algebra.Relation;
import de.fuberlin.wiwiss.d2rq.map.Mapping;
import de.fuberlin.wiwiss.d2rq.vocab.D2RConfig;

/**
* A D2R Server instance. Sets up a service, loads the D2RQ model, and starts
Expand Down Expand Up @@ -194,8 +203,6 @@ public void start() {
log.info("Safe mode (launch using --fast to use all optimizations)");
}

this.metadataCreator = new MetadataCreator(this);

// Set up a custom DescribeHandler that calls out to
// {@link ResourceDescriber}
DescribeHandlerRegistry.get().clear();
Expand Down Expand Up @@ -256,10 +263,6 @@ public static SystemLoader retrieveSystemLoader(ServletContext context) {
return (SystemLoader) context.getAttribute(SYSTEM_LOADER);
}

public MetadataCreator getMetadataCreator() {
return metadataCreator;
}

private static String getUri(String base, String service) {
return base.endsWith("/") ? base + service : base + "/" + service;
}
Expand All @@ -271,7 +274,6 @@ public String getDatasetIri() {
}

public String getSparqlUrl() {
return getUri(getConfig().baseURI(),
D2RServer.getSparqlServiceName());
return getUri(getConfig().baseURI(), D2RServer.getSparqlServiceName());
}
}
26 changes: 22 additions & 4 deletions src/de/fuberlin/wiwiss/d2rq/server/DatasetDescriptionServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException {
D2RServer server = D2RServer.fromServletContext(getServletContext());

if (!server.getConfig().serveMetadata()) {
response.setStatus(404);
response.setContentType("text/plain");
response.getOutputStream().println(
"404 Not Found: Dataset description has been disabled.");
return;
}

Model dDesc = ModelFactory.createDefaultModel();
dDesc.setNsPrefix("void", VoID.NS);

Expand Down Expand Up @@ -126,9 +134,19 @@ protected void doGet(HttpServletRequest request,
dDesc.add(defaultDatasetDesc, SD.defaultGraph, datasetIRI);
dDesc.add(datasetIRI, RDF.type, SD.Graph);

// TODO: allow user to add his own triples here


// add user-specified dataset metadata, either from default or
// user-specified metadata template
Model datasetMetadataTemplate = server.getConfig()
.getDatasetMetadataTemplate(server, getServletContext());
MetadataCreator datasetMetadataCreator = new MetadataCreator(server,
datasetMetadataTemplate);
dDesc.add(datasetMetadataCreator.addMetadataFromTemplate(
server.getDatasetIri(), server.getDatasetIri(),
server.getDatasetIri()));
Map<String, String> dDescPrefixes = dDesc.getNsPrefixMap();
dDescPrefixes.putAll(datasetMetadataTemplate.getNsPrefixMap());
dDesc.setNsPrefixes(dDescPrefixes);

// decide whether to serve RDF or HTML
ContentTypeNegotiator negotiator = PubbyNegotiator.getPubbyNegotiator();
MediaRangeSpec bestMatch = negotiator.getBestMatch(
Expand All @@ -154,7 +172,7 @@ protected void doGet(HttpServletRequest request,
// add prefixes to context
Map<String, String> nsSet = dDesc.getNsPrefixMap();
context.put("prefixes", nsSet.entrySet());

context.put("dataset_iri", datasetIRI);

// add a empty map for keeping track of blank nodes aliases
Expand Down

0 comments on commit 3d16138

Please sign in to comment.