Skip to content

Commit

Permalink
added a dynamic list of all resources when index is requested (and In…
Browse files Browse the repository at this point in the history
…dexResource is missing on the configuration)
  • Loading branch information
Sergio Fernandez committed Apr 3, 2012
1 parent 1591c67 commit 5070a4d
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 16 deletions.
16 changes: 15 additions & 1 deletion src/de/fuberlin/wiwiss/pubby/Configuration.java
Expand Up @@ -26,6 +26,8 @@
* @version $Id$
*/
public class Configuration {

public static final String INDEX = "index";
private final Model model;
private final Resource config;
private final PrefixMapping prefixes;
Expand All @@ -46,7 +48,7 @@ public Configuration(Model configurationModel) {
datasets = new ArrayList();
it = model.listStatements(config, CONF.dataset, (RDFNode) null);
while (it.hasNext()) {
datasets.add(new Dataset(it.nextStatement().getResource()));
datasets.add(new Dataset(it.nextStatement().getResource(), this.getWebApplicationBaseURI()));
}
labelProperties = new ArrayList();
it = model.listStatements(config, CONF.labelProperty, (RDFNode) null);
Expand Down Expand Up @@ -158,4 +160,16 @@ public String getProjectName() {
public String getWebApplicationBaseURI() {
return config.getProperty(CONF.webBase).getResource().getURI();
}

public String buildIndexResource() {
String webApplicationBaseURI = this.getWebApplicationBaseURI();
Statement dataset = this.config.getProperty(CONF.dataset);
String webResourcePrefix = dataset.getProperty(CONF.webResourcePrefix).getString();
return buildIndexResource(webApplicationBaseURI, webResourcePrefix);
}

public static String buildIndexResource(String webApplicationBaseURI, String webResourcePrefix) {
return webApplicationBaseURI + webResourcePrefix + INDEX;
}

}
14 changes: 8 additions & 6 deletions src/de/fuberlin/wiwiss/pubby/Dataset.java
Expand Up @@ -42,9 +42,10 @@ public class Dataset {
private final static String metadataPlaceholderURIPrefix = "about:metadata:";
private Calendar currentTime;
private Resource currentDocRepr;
private String webBase;

public Dataset(Resource config) {
model = config.getModel();
public Dataset(Resource config, String webBase) {
this.model = config.getModel();
this.config = config;
if (config.hasProperty(CONF.datasetURIPattern)) {
datasetURIPattern = Pattern.compile(
Expand Down Expand Up @@ -73,10 +74,10 @@ public Dataset(Resource config) {
}
if (config.hasProperty(CONF.sparqlEndpoint)) {
String endpointURL = config.getProperty(CONF.sparqlEndpoint).getResource().getURI();
String defaultGraph = config.hasProperty(CONF.sparqlDefaultGraph)
? config.getProperty(CONF.sparqlDefaultGraph).getResource().getURI()
: null;
dataSource = new RemoteSPARQLDataSource(endpointURL, defaultGraph);
String graphName = config.hasProperty(CONF.sparqlDefaultGraph)
? config.getProperty(CONF.sparqlDefaultGraph).getResource().getURI()
: null;
dataSource = new RemoteSPARQLDataSource(endpointURL, graphName, webBase, this.getWebResourcePrefix());
} else {
Model data = ModelFactory.createDefaultModel();
StmtIterator it = config.listProperties(CONF.loadRDF);
Expand Down Expand Up @@ -356,4 +357,5 @@ private String escapeURIDelimiters(String uri) {
private String unescapeURIDelimiters(String uri) {
return uri.replaceAll("%23", "#").replaceAll("%3F", "?");
}

}
53 changes: 48 additions & 5 deletions src/de/fuberlin/wiwiss/pubby/RemoteSPARQLDataSource.java
Expand Up @@ -6,23 +6,36 @@

import com.hp.hpl.jena.rdf.model.Model;
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;
import com.hp.hpl.jena.sparql.engine.http.QueryEngineHTTP;

import de.fuberlin.wiwiss.pubby.vocab.CONF;
import de.fuberlin.wiwiss.pubby.vocab.RDF;

/**
* A data source backed by a SPARQL endpoint accessed through
* the SPARQL protocol.
*
* @author Richard Cyganiak (richard@cyganiak.de)
* @author Sergio Fernández (sergio.fernandez@fundacionctic.org)
* @version $Id$
*/
public class RemoteSPARQLDataSource implements DataSource {

private Resource config;
private String endpointURL;
private String defaultGraphName;
private String webResourcePrefix;
private String webBase;
private String previousDescribeQuery;

public RemoteSPARQLDataSource(String endpointURL, String defaultGraphName) {
public RemoteSPARQLDataSource(String endpointURL, String graphName, String webBase, String webResourcePrefix) {
this.endpointURL = endpointURL;
this.defaultGraphName = defaultGraphName;
this.defaultGraphName = graphName;
this.webBase = webBase;
this.webResourcePrefix = webResourcePrefix;
}

public String getEndpointURL() {
Expand All @@ -40,16 +53,25 @@ public String getResourceDescriptionURL(String resourceURI) {
result.append("&");
}
result.append("query=");
result.append(URLEncoder.encode("DESCRIBE <" + resourceURI + ">", "utf-8"));
result.append(URLEncoder.encode(buildDescribeQuery(resourceURI), "utf-8"));
return result.toString();
} catch (UnsupportedEncodingException ex) {
// can't happen, utf-8 is always supported
throw new RuntimeException(ex);
}
}

private String buildDescribeQuery(String resourceURI) {
String index = Configuration.buildIndexResource(this.webBase, this.webResourcePrefix);
if (index.equals(resourceURI)) {
return "CONSTRUCT { ?s <" + RDF.type.getURI() + "> ?o } WHERE { ?s <" + RDF.type.getURI() + "> ?o }";
} else {
return "DESCRIBE <" + resourceURI + ">";
}
}

public Model getResourceDescription(String resourceURI) {
return execDescribeQuery("DESCRIBE <" + resourceURI + ">");
return execDescribeQuery(buildDescribeQuery(resourceURI));
}

public Model getAnonymousPropertyValues(String resourceURI, Property property, boolean isInverse) {
Expand All @@ -71,6 +93,27 @@ private Model execDescribeQuery(String query) {
if (defaultGraphName != null) {
endpoint.setDefaultGraphURIs(Collections.singletonList(defaultGraphName));
}
return endpoint.execDescribe();

//FIXME: more elegant way to discriminate query execution
// (lateral effect from introducing CONSTRUCT query for listing all resources on index)
if (query.startsWith("CONSTRUCT")) {
Model model = endpoint.execConstruct();
Property containerOf = model.createProperty("http://rdfs.org/sioc/ns#container_of");
ResIterator iter = model.listSubjects();
Resource index = model.createResource(Configuration.buildIndexResource(this.webBase, this.webResourcePrefix));
while (iter.hasNext()) {
Resource o = iter.next();
if (!index.equals(o)) {
Statement statement = model.createStatement(index, containerOf, o);
model.add(statement);
}
}
model.addLiteral(index,
model.createProperty("http://www.w3.org/2000/01/rdf-schema#label"),
model.createLiteral("Synthetic container for listing by default all resources on pubby"));
return model;
} else {
return endpoint.execDescribe();
}
}
}
18 changes: 14 additions & 4 deletions src/de/fuberlin/wiwiss/pubby/servlets/RootServlet.java
Expand Up @@ -12,10 +12,13 @@
* A catch-all servlet managing the URI space of the web application.
*
* @author Richard Cyganiak (richard@cyganiak.de)
* @author Sergio Fernández (sergio.fernandez@fundacionctic.org)
* @version $Id$
*/
public class RootServlet extends BaseServlet {

private static final long serialVersionUID = -4812044304620174504L;

protected boolean doGet(String relativeURI,
HttpServletRequest request, HttpServletResponse response,
Configuration config)
Expand All @@ -27,10 +30,17 @@ protected boolean doGet(String relativeURI,
return true;
}

// If index resource is defined, redirect requests for the index page to it
if ("".equals(relativeURI) && config.getIndexResource() != null) {
response.sendRedirect(config.getIndexResource().getWebURI());
return true;
if ("".equals(relativeURI)) {
// If index resource is defined
// redirect requests for the index page to it
// if not, list all resources
if (config.getIndexResource() != null) {
response.sendRedirect(config.getIndexResource().getWebURI());
return true;
} else {
response.sendRedirect(config.buildIndexResource());
return true;
}
}

// Assume it's a resource URI -- will produce 404 if not
Expand Down

0 comments on commit 5070a4d

Please sign in to comment.