Skip to content

Commit

Permalink
Filter parameters not available in the sparql query data getter
Browse files Browse the repository at this point in the history
  • Loading branch information
litvinovg committed Apr 4, 2024
1 parent b40d379 commit 1e92f83
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
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 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
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

0 comments on commit 1e92f83

Please sign in to comment.