Page 129 - JOURNAL OF LIBRARY SCIENCE IN CHINA 2018 Vol. 42
P. 129
128 Journal of Library Science in China, Vol. 8, 2016
@Path("termservices")
@Consumes("text/plain")
@Produces("application/json")
public class TermServices {
/*TS1: Query the concepts that match the input keyword*/
@Path("searchConceptByKeyword")
@GET
public Response searchConceptByKeyword(@QueryParam("searchString") String searchString,
@QueryParam("format") String format, @QueryParam("searchMode") String searchMode) {
String query="…"; //a SPARQL query
query=String.format(query,searchString,searchString);
Response rp= null;
String service="http://localhost:3030/data/query"; //the address of the server that provides SPARQL query
service
QueryExecution qex=QueryExecutionFactory.sparqlService(service, query); //execute the query
ResultSet resultSet=qex.execSelect(); //query result set
Map<String,String> m=new HashMap<>();
QuerySolution qso;
RDFNode lt;
while(resultSet.hasNext()) { … } //read each result one by one from the result set
String str+="<skos:Concept rdf:about=\"http://www.example.com/CT/Concept/prefLabel\">\n";
Iterator<Map.Entry<String,String>> iter = m.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry<String,String> entry = iter.next();
str+="<skos:altLabel xml:lang=\"zh\">"+entry.getValue()+"</skos:altLabel>\n";
}
str+="</skos:Concept>";
rp=Response.ok(str).header("Content-type","application/rdf+xml").header("Access-Control-Allow-Origin",
"*").build(); //build the data that will be returned to the client
return rp; //return the respond of the server to the client
}
}
Figure 2. Core programming code to implement RESTful terminology services.
The HEADER’s Access-Control-Allow-Origin field controls the accessible domain lists. If
setting its value as “*”, it allows all the domains to call the RESTful services. The HEADER’s
Content-type field controls the returned content type. If setting its value as “application/rdf+xml”,
the query results can be returned in RDF/XML. Finally, the instance of Response is created using
the build method, and return the data including all the query results to the client.
2.3 Terminology service clients
We developed two types of terminology service clients: one is a Web-based client which directly
provides the parameter input and result display of terminology services; another is a built-in