Skip to content

Commit

Permalink
[ issue #120 ] available datasets service
Browse files Browse the repository at this point in the history
  • Loading branch information
agazzarini committed Dec 11, 2015
1 parent c5ddeb0 commit 092067c
Show file tree
Hide file tree
Showing 11 changed files with 374 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.gazzax.labs.solrdf.handler.search.handler;
package org.gazzax.labs.solrdf.handler.search;
import static org.gazzax.labs.solrdf.F.readCommandFromIncomingStream;

import java.security.Principal;
Expand Down Expand Up @@ -53,10 +53,10 @@ public class Sparql11SearchHandler extends RequestHandlerBase {
static final String INVALID_HTTP_METHOD = "SPARQL Protocol violation: request method must be GET or POST.";

static final String SEARCH_HANDLER_PARAMETER_NAME = "s";
static final String DEFAULT_SEARCH_HANDLER_NAME = "/sparql-query";
public static final String DEFAULT_SPARQL_QUERY_HANDLER_NAME = "/sparql-query";

static final String UPDATE_HANDLER_PARAMETER_NAME = "u";
static final String DEFAULT_UPDATE_HANDLER_NAME = "/sparql-update";
static final String DEFAULT_SPARQL_UPDATE_HANDLER_NAME = "/sparql-update";

@Override
public void handleRequestBody(final SolrQueryRequest request, final SolrQueryResponse response) throws Exception {
Expand All @@ -65,7 +65,7 @@ public void handleRequestBody(final SolrQueryRequest request, final SolrQueryRes
if (containsQueryParameter(parameters)) {
requestHandler(
request,
parameters.get(SEARCH_HANDLER_PARAMETER_NAME, DEFAULT_SEARCH_HANDLER_NAME))
parameters.get(SEARCH_HANDLER_PARAMETER_NAME, DEFAULT_SPARQL_QUERY_HANDLER_NAME))
.handleRequest(request, response);
} else {
throw new SolrException(
Expand All @@ -77,12 +77,12 @@ public void handleRequestBody(final SolrQueryRequest request, final SolrQueryRes
if (containsUpdateParameter(parameters)) {
requestHandler(
request,
parameters.get(UPDATE_HANDLER_PARAMETER_NAME, DEFAULT_UPDATE_HANDLER_NAME))
parameters.get(UPDATE_HANDLER_PARAMETER_NAME, DEFAULT_SPARQL_UPDATE_HANDLER_NAME))
.handleRequest(new SparqlUpdateSolrQueryRequest(request), response);
} else if (containsQueryParameter(parameters)) {
requestHandler(
request,
parameters.get(SEARCH_HANDLER_PARAMETER_NAME, DEFAULT_SEARCH_HANDLER_NAME))
parameters.get(SEARCH_HANDLER_PARAMETER_NAME, DEFAULT_SPARQL_QUERY_HANDLER_NAME))
.handleRequest(request, response);
} else {
throw new SolrException(
Expand All @@ -91,10 +91,13 @@ public void handleRequestBody(final SolrQueryRequest request, final SolrQueryRes
}
} else if (isSparqlQueryContentType(request)) {
if (isBodyNotEmpty(request)) {
request.setParams(new ModifiableSolrParams(parameters).set(Names.QUERY, readCommandFromIncomingStream(request.getContentStreams().iterator().next())));
request.setParams(
new ModifiableSolrParams(parameters)
.set(Names.QUERY, readCommandFromIncomingStream(
request.getContentStreams().iterator().next())));
requestHandler(
request,
parameters.get(SEARCH_HANDLER_PARAMETER_NAME, DEFAULT_SEARCH_HANDLER_NAME))
parameters.get(SEARCH_HANDLER_PARAMETER_NAME, DEFAULT_SPARQL_QUERY_HANDLER_NAME))
.handleRequest(new SparqlQuerySolrQueryRequest(request), response);
} else {
throw new SolrException(
Expand All @@ -105,7 +108,7 @@ public void handleRequestBody(final SolrQueryRequest request, final SolrQueryRes
if (isBodyNotEmpty(request)) {
requestHandler(
request,
parameters.get(UPDATE_HANDLER_PARAMETER_NAME, DEFAULT_UPDATE_HANDLER_NAME))
parameters.get(UPDATE_HANDLER_PARAMETER_NAME, DEFAULT_SPARQL_UPDATE_HANDLER_NAME))
.handleRequest(request, response);
} else {
throw new SolrException(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.gazzax.labs.solrdf.handler.search.ldf;

import org.apache.solr.common.params.ModifiableSolrParams;
import org.apache.solr.handler.RequestHandlerBase;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.request.SolrRequestHandler;
import org.apache.solr.response.SolrQueryResponse;
import org.gazzax.labs.solrdf.Names;
import org.gazzax.labs.solrdf.handler.search.Sparql11SearchHandler;

public class AvailableDatasetsSearchHandler extends RequestHandlerBase {
final static String SELECT_ALL_DATASETS =
"CONSTRUCT { ?s ?p ?o } "
+ "WHERE { "
+ " ?s <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://rdfs.org/ns/void#Dataset> . "
+ " ?s ?p ?o "
+ "}";

@Override
public void handleRequestBody(final SolrQueryRequest request, final SolrQueryResponse response) throws Exception {
request.setParams(
new ModifiableSolrParams(request.getParams())
.set(Names.QUERY, SELECT_ALL_DATASETS));
requestHandler(request).handleRequest(request, response);
}

@Override
public String getDescription() {
return "Returns the available datasets within this SolRDF instance";
}

@Override
public String getSource() {
return "https://github.com/agazzarini/SolRDF";
}

/**
* Returns the {@link SolrRequestHandler} associated with the given name.
*
* @param request the current Solr request.
* @param name the {@link SolrRequestHandler} name.
* @return the {@link SolrRequestHandler} associated with the given name.
*/
SolrRequestHandler requestHandler(final SolrQueryRequest request) {
return request.getCore().getRequestHandler(Sparql11SearchHandler.DEFAULT_SPARQL_QUERY_HANDLER_NAME);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.gazzax.labs.solrdf.handler.search.ldf;

import org.apache.solr.handler.RequestHandlerBase;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.response.SolrQueryResponse;


public class TriplePatternSearchHandler extends RequestHandlerBase {
@Override
public void handleRequestBody(final SolrQueryRequest request, final SolrQueryResponse response) throws Exception {

}

@Override
public String getDescription() {
// TODO Auto-generated method stub
return null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Top level package for SolRDF Linked Data Fragments search request handlers.
*
* @see http://linkeddatafragments.org
* @see http://data.linkeddatafragments.org
* @see http://www.hydra-cg.com/spec/latest/triple-pattern-fragments
* @see http://www.hydra-cg.com/spec/latest/linked-data-fragments
*/
package org.gazzax.labs.solrdf.handler.search.ldf;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
* Top level package for SolRDF search request handlers.
*/
package org.gazzax.labs.solrdf.handler.search.handler;
package org.gazzax.labs.solrdf.handler.search;
12 changes: 10 additions & 2 deletions solrdf/solrdf-core/src/solr-home/store/conf/solrconfig.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
<!--
RequestHandler acting as SPARQL endpoint / facade
-->
<requestHandler name="/sparql" class="org.gazzax.labs.solrdf.handler.search.handler.Sparql11SearchHandler">
<requestHandler name="/sparql" class="org.gazzax.labs.solrdf.handler.search.Sparql11SearchHandler">
<lst name="invariants">
<str name="s">/sparql-query</str>
<str name="u">/sparql-update</str>
Expand Down Expand Up @@ -130,7 +130,15 @@
<str name="wt">json</str>
</lst>
</requestHandler>


<!-- Linked Data Fragments Handlers: Get all available datasets -->
<requestHandler name="/ldf/datasets" class="org.gazzax.labs.solrdf.handler.search.ldf.AvailableDatasetsSearchHandler">
<lst name="invariants">
<str name="s">/sparql-query</str>
<str name="u">/sparql-update</str>
</lst>
</requestHandler>

<!-- Classic Solr UpdateRequestHandler -->
<requestHandler name="/update" class="org.apache.solr.handler.UpdateRequestHandler"/>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.gazzax.labs.solrdf.handler.search.handler;
package org.gazzax.labs.solrdf.handler.search;

import static org.gazzax.labs.solrdf.TestUtility.randomString;
import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -31,6 +31,7 @@
import org.apache.solr.response.SolrQueryResponse;
import org.gazzax.labs.solrdf.Names;
import org.gazzax.labs.solrdf.Strings;
import org.gazzax.labs.solrdf.handler.search.Sparql11SearchHandler;
import org.junit.Before;
import org.junit.Test;

Expand Down Expand Up @@ -76,13 +77,13 @@ public void queryViaGET() throws Exception {
when(request.getParams()).thenReturn(parameters.set(Names.QUERY, randomString()));
cut.handleRequestBody(request, response);

verify(cut).requestHandler(request, Sparql11SearchHandler.DEFAULT_SEARCH_HANDLER_NAME);
verify(cut).requestHandler(request, Sparql11SearchHandler.DEFAULT_SPARQL_QUERY_HANDLER_NAME);

reset(cut);

when(request.getParams()).thenReturn(parameters.set(CommonParams.Q, randomString()));
cut.handleRequestBody(request, response);
verify(cut).requestHandler(request, Sparql11SearchHandler.DEFAULT_SEARCH_HANDLER_NAME);
verify(cut).requestHandler(request, Sparql11SearchHandler.DEFAULT_SPARQL_QUERY_HANDLER_NAME);
}

@Test
Expand Down Expand Up @@ -125,7 +126,7 @@ public void updateUsingPOSTWithURLEncodedParameters() throws Exception {

cut.handleRequestBody(request, response);

verify(cut).requestHandler(request, Sparql11SearchHandler.DEFAULT_UPDATE_HANDLER_NAME);
verify(cut).requestHandler(request, Sparql11SearchHandler.DEFAULT_SPARQL_UPDATE_HANDLER_NAME);
}

@Test
Expand Down Expand Up @@ -155,7 +156,7 @@ public void queryUsingPOSTWithURLEncodedParameters_I() throws Exception {

cut.handleRequestBody(request, response);

verify(cut).requestHandler(request, Sparql11SearchHandler.DEFAULT_SEARCH_HANDLER_NAME);
verify(cut).requestHandler(request, Sparql11SearchHandler.DEFAULT_SPARQL_QUERY_HANDLER_NAME);
}

@Test
Expand All @@ -169,7 +170,7 @@ public void queryUsingPOSTWithURLEncodedParameters_II() throws Exception {

cut.handleRequestBody(request, response);

verify(cut).requestHandler(request, Sparql11SearchHandler.DEFAULT_SEARCH_HANDLER_NAME);
verify(cut).requestHandler(request, Sparql11SearchHandler.DEFAULT_SPARQL_QUERY_HANDLER_NAME);
}

@Test
Expand Down Expand Up @@ -205,7 +206,7 @@ public String getContentType() {

cut.handleRequestBody(request, response);

verify(cut).requestHandler(request, Sparql11SearchHandler.DEFAULT_UPDATE_HANDLER_NAME);
verify(cut).requestHandler(request, Sparql11SearchHandler.DEFAULT_SPARQL_UPDATE_HANDLER_NAME);
}

@Test
Expand Down Expand Up @@ -254,7 +255,7 @@ public String getContentType() {

cut.handleRequestBody(request, response);

verify(cut).requestHandler(request, Sparql11SearchHandler.DEFAULT_SEARCH_HANDLER_NAME);
verify(cut).requestHandler(request, Sparql11SearchHandler.DEFAULT_SPARQL_QUERY_HANDLER_NAME);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* This Test case makes use of some examples from
*
* "Learning SPARQL - Querying and Updating with SPARQL 1.1" by Bob DuCharme
*
* Publisher: O'Reilly
* Author: Bob DuCharme
* ISBN: 978-1-449-37143-2
* http://www.learningsparql.com/
* http://shop.oreilly.com/product/0636920030829.do
*
* We warmly appreciate and thank the author and O'Reilly for such permission.
*
*/
package org.gazzax.labs.solrdf.integration.ldf;

import static org.gazzax.labs.solrdf.MisteryGuest.misteryGuest;

import java.io.File;

import org.gazzax.labs.solrdf.integration.IntegrationTestSupertypeLayer;
import org.junit.Test;

import com.hp.hpl.jena.graph.NodeFactory;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;

/**
* Linked Data Fragments interface integration tests.
*
* Note: the SolRDF client is not yet ready from a full understanding of the LDF protocol perspective.
*
* @author Andrea Gazzarini
* @since 1.0
*/
public class LinkedDataFragments_ITCase extends IntegrationTestSupertypeLayer {
@Test
public void availableDatasets() throws Exception {
load(misteryGuest(null, "sample-datasets.nt"));
Thread.sleep(10000000000000000L);
final Model response = SOLRDF_CLIENT.ldf().availableDatasets();
final Model expectedResponse = ModelFactory.createDefaultModel().read(
new File(examplesDirectory(),
"available-datasets-expected-response.nt").toURI().toString());

assertTrue(response.isIsomorphicWith(expectedResponse));
}

@Override
protected String examplesDirectory() {
return "src/test/resources/ldf";
}
}
Loading

0 comments on commit 092067c

Please sign in to comment.