Skip to content

Commit

Permalink
respond with proper JSON-LD structure for concept search results
Browse files Browse the repository at this point in the history
  • Loading branch information
twagoo committed Nov 10, 2023
1 parent 15f968d commit 5c12475
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.github.jsonldjava.utils.JsonUtils;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
Expand Down Expand Up @@ -43,12 +42,13 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se
final String keywords = req.getParameter("keywords");
logger.debug("CCR request: keywords = {}", keywords);

final List<Object> concepts = getSkosmosService().searchConcepts(keywordToQuery(keywords));
//final List<Object> concepts = getSkosmosService().searchConcepts(keywordToQuery(keywords));
final Object concepts = getSkosmosService().searchConceptsJsonLd(keywordToQuery(keywords));

if (concepts == null) {
resp.sendError(500, "No response from CCR");
} else {
logger.debug("CCR response: {} concepts", concepts.size());
//logger.debug("CCR response: {} concepts", concepts.size());

resp.setStatus(HttpServletResponse.SC_OK);
resp.setHeader("Content-Type", CONTENT_TYPE_HEADER_VALUE_JSON);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,29 @@ public class SkosmosService {
private Set<String> includedVocabs = Collections.emptySet();

/**
* JSON-LD contexts that we always want to set
* JSON-LD contexts that we always want to setfdefinition
*/
private static final ImmutableMap CONTEXTS_TO_SET = ImmutableMap.builder()
private static final ImmutableMap JSON_LD_CONTEXT = ImmutableMap.builder()
.put("uri", "@id")
.put("type", "@type")
.put("rdfs", "http://www.w3.org/2000/01/rdf-schema#")
.put("skos", "http://www.w3.org/2004/02/skos/core#")
.put("onki", "http://schema.onki.fi/onki#")
.put("isothes", "http://purl.org/iso25964/skos-thes#")
.put("prefLabel", "skos:prefLabel")
.put("altLabel", "skos:altLabel")
.put("hiddenLabel", "skos:hiddenLabel")
.put("definition", "skos:definition")
.put("notation", "skos:notation")
.put("results",
ImmutableMap.builder()
.put("@id", "onki:results")
.put("@container", "@list")
.build())
.build();

private static final String CONCEPT_SEARCH_INCLUDED_FIELDS = "definition";
private static final String CONCEPT_SEARCH_INCLUDED_FIELDS = "definition notation prefLabel";

private final WebResource service;

// Caches
Expand Down Expand Up @@ -337,6 +349,18 @@ private Map retrieveConceptSchemeInfo(String schemeUri) {
return Collections.emptyMap();
}

public Object searchConceptsJsonLd(String query) throws IOException {
final List<Object> results = searchConcepts(query);

final ImmutableMap<Object, Object> resultMap = ImmutableMap.builder()
.put("@context", JSON_LD_CONTEXT)
.put("uri", "")
.put("results", results)
.build();

return JsonLdProcessor.compact(resultMap, JSON_LD_CONTEXT, new JsonLdOptions());
}

public List<Object> searchConcepts(String query) {
if (includedVocabs.isEmpty()) {
// search across all vocabularies
Expand Down Expand Up @@ -410,7 +434,7 @@ private Stream<Object> searchConcepts(String query, Optional<String> scheme, Opt
private void setContexts(final Object resultsObject) {
if (resultsObject instanceof Map resultsMap) {
if (resultsMap.get("@context") instanceof Map contextMap) {
contextMap.putAll(CONTEXTS_TO_SET);
contextMap.putAll(JSON_LD_CONTEXT);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
*/
package clarin.cmdi.componentregistry.skosmos;

import com.github.jsonldjava.utils.JsonUtils;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Multimap;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;
import java.util.concurrent.ExecutionException;
import javax.ws.rs.core.UriBuilder;
Expand All @@ -41,12 +44,12 @@ public class SkosmosServiceRunner {
private final static String SERVICE_URI = "https://clarin-skosmos.sd.di.huc.knaw.nl/rest/v1";
private final static String CONCEPT_SCHEME_METADATA = "http://hdl.handle.net/11459/CCR_P-Metadata_6f3f84d1-6f06-6291-4e20-4cd361cca128";

public final static void main(String[] args) throws InterruptedException, ExecutionException {
public final static void main(String[] args) throws InterruptedException, ExecutionException, IOException {
mainConcepts();
// mainVocabs();
}

public final static void mainConcepts() throws InterruptedException, ExecutionException {
public final static void mainConcepts() throws InterruptedException, ExecutionException, IOException {
logger.info("......Testing concept retrieval.....");

final String query = "*test*";
Expand All @@ -55,9 +58,10 @@ public final static void mainConcepts() throws InterruptedException, ExecutionEx
logger.info("Searching for \"{}\" in scheme \"{}\"", query, scheme);
final SkosmosService service = new SkosmosService(UriBuilder.fromUri(SERVICE_URI).build());
service.setIncludedSchemes(ImmutableSet.of(scheme));
final List<Object> results = service.searchConcepts(query);
logger.info("Received {} results", results.size());
results.forEach(o -> logger.info("Result: {}", o));
final Object results = service.searchConceptsJsonLd(query);
try (OutputStreamWriter outWriter = new OutputStreamWriter(System.out)) {
JsonUtils.writePrettyPrint(outWriter, results);
}
}

public final static void mainVocabs() throws InterruptedException, ExecutionException {
Expand Down

0 comments on commit 5c12475

Please sign in to comment.