new rest method to import data into graphDB from url

This commit is contained in:
Enrico Ottonello 2020-05-30 17:21:03 +02:00
parent 30b3fa2140
commit 9814069fb2
3 changed files with 69 additions and 18 deletions

View File

@ -1,7 +1,10 @@
package eu.dnetlib.ariadneplus.graphdb;
import java.io.File;
import java.net.URL;
import java.time.LocalDateTime;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
@ -281,4 +284,38 @@ public class GraphDBClient {
throw new AriadnePlusPublisherException(e);
}
}
public String feedFromURL(final String dataUrl, final String context) throws AriadnePlusPublisherException{
try {
String result = new String("");
log.debug("init connection to graphDBServerUrl " + this.graphDBServerUrl);
RemoteRepositoryManager manager = new RemoteRepositoryManager(this.graphDBServerUrl);
manager.init();
manager.setUsernameAndPassword(getWriterUser(), getWriterPwd());
log.debug("manager init");
Repository repository = manager.getRepository(getRepository());
ValueFactory factory = repository.getValueFactory();
try (RepositoryConnection con = repository.getConnection()) {
log.debug("connection established");
con.begin();
String baseUri = null;
IRI contextIRI = factory.createIRI(getGraphDBBaseURI(), context);
con.add(new URL(dataUrl), baseUri, RDFFormat.TURTLE, contextIRI);
result.concat("data added from url: "+dataUrl+" into graph "+context);
con.commit();
log.debug("add data from Url executed");
con.close();
}
catch (RDF4JException e) {
log.error("error executing query ...", e);
}
repository.shutDown();
manager.shutDown();
log.debug("manager shutDown");
return result;
}catch(Throwable e){
log.error(e);
throw new AriadnePlusPublisherException(e);
}
}
}

View File

@ -28,32 +28,23 @@ public class AriadnePlusPublisherController {
}
@RequestMapping(value = "/publish", method = RequestMethod.POST)
public void publish(@RequestParam final String record, @RequestParam(required = false) String ariadneplusTarget) throws AriadnePlusPublisherException {
if (ariadneplusTarget==null) {
ariadneplusTarget = DEFAULT_TARGET_ENDPOINT;
}
getAriadnePlusPublisherHelper().publish(record, getTarget(ariadneplusTarget));
public void publish(@RequestParam final String record) throws AriadnePlusPublisherException {
getAriadnePlusPublisherHelper().publish(record, getTarget(DEFAULT_TARGET_ENDPOINT));
}
@RequestMapping(value = "/feedProvenance", method = RequestMethod.POST)
public void feedProvenance(@RequestParam final String datasource, @RequestParam final String datasourceApi, @RequestParam(required = false) String ariadneplusTarget) throws AriadnePlusPublisherException {
if (ariadneplusTarget==null) {
ariadneplusTarget = DEFAULT_TARGET_ENDPOINT;
}
getAriadnePlusPublisherHelper().feedProvenance(datasource, datasourceApi, getTarget(ariadneplusTarget));
public void feedProvenance(@RequestParam final String datasource, @RequestParam final String datasourceApi) throws AriadnePlusPublisherException {
getAriadnePlusPublisherHelper().feedProvenance(datasource, datasourceApi, getTarget(DEFAULT_TARGET_ENDPOINT));
}
@RequestMapping(value = "/dropDatasourceApiGraph", method = RequestMethod.POST)
public void dropDatasourceApisPartitionInfo(@RequestParam final String datasourceApi, @RequestParam(required = false) String ariadneplusTarget) throws AriadnePlusPublisherException {
if (ariadneplusTarget==null) {
ariadneplusTarget = DEFAULT_TARGET_ENDPOINT;
}
getAriadnePlusPublisherHelper().dropDatasourceApiGraph(datasourceApi, getTarget(ariadneplusTarget));
public void dropDatasourceApisPartitionInfo(@RequestParam final String datasourceApi) throws AriadnePlusPublisherException {
getAriadnePlusPublisherHelper().dropDatasourceApiGraph(datasourceApi, getTarget(DEFAULT_TARGET_ENDPOINT));
}
@RequestMapping(value = "/unpublish", method = RequestMethod.GET)
public void unpublish(@RequestParam final String datasourceApi, @RequestParam(required = false) String ariadneplusTarget) throws AriadnePlusPublisherException {
getAriadnePlusPublisherHelper().unpublish(datasourceApi, getTarget(ariadneplusTarget));
public void unpublish(@RequestParam final String datasourceApi) throws AriadnePlusPublisherException {
getAriadnePlusPublisherHelper().unpublish(datasourceApi, getTarget(DEFAULT_TARGET_ENDPOINT));
}
private AriadnePlusTargets getTarget(String value) {
@ -72,4 +63,10 @@ public class AriadnePlusPublisherController {
public String updateSparql(@RequestBody final String queryValue) throws AriadnePlusPublisherException {
return getAriadnePlusPublisherHelper().updateSparql(queryValue, getTarget(DEFAULT_TARGET_ENDPOINT));
}
@RequestMapping(value = "/feedFromURL", method = RequestMethod.POST)
public String feedFromURL(@RequestParam final String dataUrl, @RequestParam final String context) throws AriadnePlusPublisherException {
return getAriadnePlusPublisherHelper().feedFromURL(dataUrl, context, getTarget(DEFAULT_TARGET_ENDPOINT));
}
}

View File

@ -77,6 +77,17 @@ public class AriadnePlusPublisherHelper {
return res;
}
public String feedFromURL(final String dataUrl, final String context, final AriadnePlusTargets target) throws AriadnePlusPublisherException {
String res;
switch(target){
case GRAPHDB:
res = feedFromURL(dataUrl, context);
break;
default: throw new AriadnePlusPublisherException("Target "+target+" not supported yet");
}
return res;
}
private void publishGraphDB(final String record) throws AriadnePlusPublisherException {
log.debug("Publishing on graphdb");
GraphDBClient graphDBClient = this.graphdbClientFactory.getGraphDBClient();
@ -107,4 +118,10 @@ public class AriadnePlusPublisherHelper {
GraphDBClient graphDBClient = this.graphdbClientFactory.getGraphDBClient();
return graphDBClient.updateSparql(queryValue);
}
private String feedFromURL(final String dataUrl, final String context) throws AriadnePlusPublisherException {
log.info("feedFromURL "+dataUrl + " " + context);
GraphDBClient graphDBClient = this.graphdbClientFactory.getGraphDBClient();
return graphDBClient.feedFromURL(dataUrl, context);
}
}