Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Dynamic index resources #7

Merged
merged 7 commits into from

2 participants

@wikier

patch for dynamically generate a list of all available resources when index is requested (and IndexResource is missing in the configuration)

Sergio Ferna... added some commits
@wikier wikier closed this
@wikier wikier reopened this
@wikier

Useful patch for us on some of our private deployments

@cygri
Owner

Nice! This is much better than the current default behaviour when no index resource is set. A couple of points though:

  • The FIXME should indeed be fixed. I think it could be done simply by checking the URI against the index address in getResourceDescription() rather than in execDescribe().
  • A note in the section on conf:indexResource in doc/index.html would be nice.
  • RDF.java is unnecessary, a file with the same constants already ships with Jena.
  • The icing on the cake would be paging of the resource list. Read a ?page=xxx value from the request params and use it to set a LIMIT/OFFSET value? Probably means there should be ORDER BY ?s too.
@wikier

I pushed new commits solving the first three comments.

For the last one I'd need more time for diving deeper in the code, and find where/how should be the right way to add pagination only for the index resource without affect the main functionality and performance.

@wikier

BTW, acknowledgement to the ADAPTA research project, partially funded by the INNPACTO 2011 programme (IPT-2011-0949-430000) of the Spanish Ministry of Science and Innovation and the European Regional Development Fund (FEDER).

@cygri
Owner

Looks great @wikier! For pagination, the cleanest thing would probably be to add a separate IndexServlet that has the pagination, and forward from the RootServlet to the IndexServlet only if no conf:indexResource is set.

@wikier

Right, maybe we should use to a separate servlet. The current implementation reuse the default rendering mechanism (both HTML and RDF) creating a fake resource resource/Index, which is of questionable accuracy.

I'll try to take a look on this issue during the week.

@cygri cygri merged commit fd3a300 into cygri:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 3, 2012
  1. added a dynamic list of all resources when index is requested (and In…

    Sergio Fernandez authored
    …dexResource is missing on the configuration)
  2. added missing class

    Sergio Fernandez authored
  3. removed unused stuff

    Sergio Fernandez authored
  4. documented the new feature

    Sergio Fernandez authored
  5. refactorized index generation

    Sergio Fernandez authored
This page is out of date. Refresh to see the latest.
View
3  doc/index.html
@@ -218,7 +218,8 @@ <h2 id="news">News</h2>
<dt><tt>conf:indexResource &lt;<em>dataset_uri</em>&gt;;</tt></dt>
<dd><p>The URI of a resource whose description will be displayed as the home page of the Pubby installation.
- Note that you have to specify a <em>dataset URI</em>, not a mapped web URI.</p></dd>
+ Note that you have to specify a <em>dataset URI</em>, not a mapped web URI. When this directive is missing
+ on the configuration, a list of all available resources is generated.</p></dd>
<dt><tt>conf:dataset [ ... ];</tt></dt>
<dd><p><strong>Required.</strong> Introduces a dataset configuration section.
View
16 src/de/fuberlin/wiwiss/pubby/Configuration.java
@@ -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;
@@ -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);
@@ -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;
+ }
+
}
View
13 src/de/fuberlin/wiwiss/pubby/Dataset.java
@@ -43,8 +43,8 @@
private Calendar currentTime;
private Resource currentDocRepr;
- 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(
@@ -73,10 +73,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);
@@ -356,4 +356,5 @@ private String escapeURIDelimiters(String uri) {
private String unescapeURIDelimiters(String uri) {
return uri.replaceAll("%23", "#").replaceAll("%3F", "?");
}
+
}
View
53 src/de/fuberlin/wiwiss/pubby/RemoteSPARQLDataSource.java
@@ -6,23 +6,33 @@
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 com.hp.hpl.jena.vocabulary.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 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() {
@@ -40,16 +50,29 @@ 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) {
+ return "DESCRIBE <" + resourceURI + ">";
+ }
+
+ private String buildConstructQuery(String resourceURI) {
+ return "CONSTRUCT { ?s <" + RDF.type.getURI() + "> ?o } WHERE { ?s <" + RDF.type.getURI() + "> ?o } LIMIT 1000";
+ }
public Model getResourceDescription(String resourceURI) {
- return execDescribeQuery("DESCRIBE <" + resourceURI + ">");
+ String index = Configuration.buildIndexResource(this.webBase, this.webResourcePrefix);
+ if (index.equals(resourceURI)) {
+ return execConstructQuery(buildConstructQuery(resourceURI));
+ } else {
+ return execDescribeQuery(buildDescribeQuery(resourceURI));
+ }
}
public Model getAnonymousPropertyValues(String resourceURI, Property property, boolean isInverse) {
@@ -73,4 +96,26 @@ private Model execDescribeQuery(String query) {
}
return endpoint.execDescribe();
}
+
+ private Model execConstructQuery(String query) {
+ QueryEngineHTTP endpoint = new QueryEngineHTTP(endpointURL, query);
+ if (defaultGraphName != null) {
+ endpoint.setDefaultGraphURIs(Collections.singletonList(defaultGraphName));
+ }
+ Model model = endpoint.execConstruct();
+ Property siocContainerOf = 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.nextResource();
+ if (!index.equals(o)) {
+ Statement statement = model.createStatement(index, siocContainerOf, o);
+ model.add(statement);
+ }
+ }
+ model.addLiteral(index,
+ model.createProperty("http://www.w3.org/2000/01/rdf-schema#label"),
+ model.createLiteral("Synthetic container for listing all resources available on this dataset"));
+ return model;
+ }
}
View
18 src/de/fuberlin/wiwiss/pubby/servlets/RootServlet.java
@@ -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)
@@ -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
Something went wrong with that request. Please try again.