Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter unavailable variable substitutions in Sparql query data getter. #459

Merged
merged 2 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
Expand All @@ -27,7 +28,7 @@
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.rdf.model.ResourceFactory;
import org.apache.jena.shared.Lock;

import org.apache.jena.sparql.ARQException;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.dao.DisplayVocabulary;
import edu.cornell.mannlib.vitro.webapp.dao.jena.QueryUtils;
Expand Down Expand Up @@ -169,6 +170,8 @@ private void addTypedParameter(String name, Set<String> set, QuerySolution soln)
public Map<String, Object> getData(Map<String, Object> pageData) {
Map<String, String> merged = mergeParameters(vreq.getParameterMap(), pageData);

merged = filterUnavailableParameters(merged);

String boundQueryText = bindParameters(queryText, merged);

if (modelURI != null) {
Expand All @@ -179,6 +182,14 @@ public Map<String, Object> getData(Map<String, Object> pageData) {
}
}

protected Map<String, String> filterUnavailableParameters(Map<String, String> merged) {
return merged
.entrySet()
.stream()
.filter(entry -> queryText.contains("?" + entry.getKey()))
.collect(Collectors.toMap(map -> map.getKey(), map -> map.getValue()));
}

/** Merge the pageData with the request parameters. PageData overrides. */
private Map<String, String> mergeParameters(
Map<String, String[]> parameterMap, Map<String, Object> pageData) {
Expand Down Expand Up @@ -239,7 +250,14 @@ private void substitute(Map<String, String> parameters, Set<String> keys, Parame
for (String key : keys) {
String value = parameters.get(key);
if (value != null) {
substitution.apply(pss, key, value);
try {
substitution.apply(pss, key, value);
} catch(ARQException arcException) {
log.error(String.format(
"Exception happend while trying to substitute value %s of variable %s in query\n%s",
value, key, pss.toString()));
throw arcException;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/* $This file is distributed under the terms of the license in LICENSE$ */
package edu.cornell.mannlib.vitro.webapp.utils.dataGetter;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
Expand Down Expand Up @@ -30,6 +33,7 @@

public class SparqlQueryDataGetterTest extends AbstractTestClass {

private static final String TO_FILTER = "toFilter";
private static final PropertyImpl HAS_ID = new PropertyImpl("test:has-id");
private static final String VAR_PARAM = "param";
private static final String PERSON_TYPE = "http://xmlns.com/foaf/0.1/Person";
Expand Down Expand Up @@ -153,6 +157,17 @@ public void testDataGetterWithBooleanParam() throws Exception {
checkData(data);
}

@Test
public void testFilterUnavailableParameters() throws Exception {
SparqlQueryDataGetter sdg = getDataGetter("dataGetterStringParam");
Map<String, String> unfilteredParameters = new HashMap<String, String>();
unfilteredParameters.put(VAR_PARAM, "");
unfilteredParameters.put(TO_FILTER, "");
Map<String, String> filteredParameters = sdg.filterUnavailableParameters(unfilteredParameters);
assertFalse(filteredParameters.containsKey(TO_FILTER));
assertTrue(filteredParameters.containsKey(VAR_PARAM));
}

private SparqlQueryDataGetter getDataGetter(String dataGetterName)
throws InstantiationException, IllegalAccessException, ClassNotFoundException, InvocationTargetException {
DataGetter dg = DataGetterUtils.dataGetterForURI(vreq, displayModel, PREFIX + dataGetterName);
Expand Down