1 |
49145
|
alessia.ba
|
package eu.dnetlib.parthenos.registry;
|
2 |
|
|
|
3 |
|
|
import java.util.Map;
|
4 |
|
|
|
5 |
|
|
import com.google.common.collect.Maps;
|
6 |
|
|
import eu.dnetlib.parthenos.publisher.ParthenosPublisherException;
|
7 |
|
|
import eu.dnetlib.parthenos.publisher.SaxonHelper;
|
8 |
|
|
import net.sf.saxon.s9api.SaxonApiException;
|
9 |
|
|
import net.sf.saxon.s9api.Serializer;
|
10 |
|
|
import net.sf.saxon.s9api.XPathSelector;
|
11 |
|
|
import org.apache.commons.lang3.StringUtils;
|
12 |
|
|
import org.apache.commons.logging.Log;
|
13 |
|
|
import org.apache.commons.logging.LogFactory;
|
14 |
|
|
import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClient;
|
15 |
|
|
import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClientFactory;
|
16 |
|
|
import org.gcube.informationsystem.resourceregistry.publisher.ResourceRegistryPublisher;
|
17 |
|
|
import org.gcube.informationsystem.resourceregistry.publisher.ResourceRegistryPublisherFactory;
|
18 |
|
|
|
19 |
|
|
/**
|
20 |
|
|
* Created by Alessia Bardi on 26/09/2017.
|
21 |
|
|
*
|
22 |
|
|
* @author Alessia Bardi
|
23 |
|
|
*/
|
24 |
|
|
public class RegistryClient {
|
25 |
|
|
|
26 |
|
|
private static final Log log = LogFactory.getLog(RegistryClient.class);
|
27 |
|
|
|
28 |
|
|
private static final String OAI_NAMESPACE_URI = "http://www.openarchives.org/OAI/2.0/";
|
29 |
|
|
private static final String DRI_NAMESPACE_URI = "http://www.driver-repository.eu/namespace/dri";
|
30 |
|
|
private static final String RDF_NAMESPACE_URI = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
|
31 |
|
|
|
32 |
49324
|
alessia.ba
|
private GCubeResourceRegistrator gCubeResourceRegistrator;
|
33 |
49145
|
alessia.ba
|
private SaxonHelper saxonHelper;
|
34 |
|
|
private XPathSelector xpathSelectorObjIdentifier;
|
35 |
|
|
private XPathSelector xpathSelectorRDF;
|
36 |
|
|
|
37 |
|
|
private ResourceRegistryPublisher resourceRegistryPublisher;
|
38 |
|
|
private ResourceRegistryClient resourceRegistryClient;
|
39 |
|
|
private String defaultBaseURI;
|
40 |
|
|
|
41 |
49396
|
alessia.ba
|
protected RegistryClient(final SaxonHelper saxonHelper,
|
42 |
49324
|
alessia.ba
|
final String defaultBaseURI,
|
43 |
|
|
final GCubeResourceRegistrator gCubeResourceRegistrator)
|
44 |
49145
|
alessia.ba
|
throws ParthenosPublisherException {
|
45 |
|
|
this.saxonHelper = saxonHelper;
|
46 |
|
|
this.defaultBaseURI = defaultBaseURI;
|
47 |
|
|
this.resourceRegistryPublisher = ResourceRegistryPublisherFactory.create();
|
48 |
|
|
this.resourceRegistryClient = ResourceRegistryClientFactory.create();
|
49 |
49324
|
alessia.ba
|
this.gCubeResourceRegistrator = gCubeResourceRegistrator;
|
50 |
49145
|
alessia.ba
|
try {
|
51 |
|
|
prepareXpathSelectors();
|
52 |
49324
|
alessia.ba
|
} catch (SaxonApiException e) {
|
53 |
49145
|
alessia.ba
|
throw new ParthenosPublisherException(e);
|
54 |
|
|
}
|
55 |
|
|
}
|
56 |
|
|
|
57 |
49324
|
alessia.ba
|
public int unregister(final String datasourceInterface) {
|
58 |
|
|
return gCubeResourceRegistrator.unregister(datasourceInterface);
|
59 |
|
|
}
|
60 |
|
|
|
61 |
|
|
public void register(final String record) throws ParthenosPublisherException {
|
62 |
49145
|
alessia.ba
|
try {
|
63 |
|
|
if (StringUtils.isBlank(record)) {
|
64 |
|
|
log.warn("Got empty record");
|
65 |
|
|
}
|
66 |
|
|
String objIdentifier = extractFromRecord(record, xpathSelectorObjIdentifier);
|
67 |
|
|
if (StringUtils.isBlank(objIdentifier)) {
|
68 |
|
|
log.warn("Got record with no objIdentifier -- skipping");
|
69 |
|
|
}
|
70 |
49324
|
alessia.ba
|
String recordRDF = extractFromRecord(record, xpathSelectorRDF);
|
71 |
|
|
gCubeResourceRegistrator.register(recordRDF, objIdentifier);
|
72 |
|
|
} catch (Throwable e) {
|
73 |
49145
|
alessia.ba
|
log.error(e.getMessage());
|
74 |
|
|
throw new ParthenosPublisherException(e);
|
75 |
|
|
}
|
76 |
|
|
}
|
77 |
|
|
|
78 |
|
|
private void prepareXpathSelectors() throws SaxonApiException {
|
79 |
|
|
Map<String, String> namespaces = Maps.newHashMap();
|
80 |
|
|
namespaces.put("oai", OAI_NAMESPACE_URI);
|
81 |
|
|
namespaces.put("dri", DRI_NAMESPACE_URI);
|
82 |
|
|
namespaces.put("rdf", RDF_NAMESPACE_URI);
|
83 |
|
|
xpathSelectorObjIdentifier = this.saxonHelper.help().prepareXPathSelector("//oai:header/dri:objIdentifier/text()", namespaces);
|
84 |
|
|
xpathSelectorRDF = this.saxonHelper.help().prepareXPathSelector("//oai:metadata/rdf:RDF", namespaces);
|
85 |
|
|
}
|
86 |
|
|
|
87 |
|
|
private String extractFromRecord(final String record, final XPathSelector xPathSelector) {
|
88 |
|
|
try {
|
89 |
|
|
return this.saxonHelper.help().setSerializerProperty(Serializer.Property.OMIT_XML_DECLARATION, "yes").evaluateSingleAsString(record, xPathSelector);
|
90 |
|
|
} catch (SaxonApiException e) {
|
91 |
|
|
throw new RuntimeException("Cannot extract content ", e);
|
92 |
|
|
}
|
93 |
|
|
}
|
94 |
|
|
|
95 |
|
|
public void setSaxonHelper(final SaxonHelper saxonHelper) {
|
96 |
|
|
this.saxonHelper = saxonHelper;
|
97 |
|
|
}
|
98 |
|
|
|
99 |
|
|
|
100 |
|
|
public void setDefaultBaseURI(final String defaultBaseURI) {
|
101 |
|
|
this.defaultBaseURI = defaultBaseURI;
|
102 |
|
|
}
|
103 |
|
|
|
104 |
|
|
public SaxonHelper getSaxonHelper() {
|
105 |
|
|
return saxonHelper;
|
106 |
|
|
}
|
107 |
|
|
|
108 |
|
|
public String getDefaultBaseURI() {
|
109 |
|
|
return defaultBaseURI;
|
110 |
|
|
}
|
111 |
|
|
|
112 |
49324
|
alessia.ba
|
public GCubeResourceRegistrator getgCubeResourceRegistrator() {
|
113 |
|
|
return gCubeResourceRegistrator;
|
114 |
49145
|
alessia.ba
|
}
|
115 |
|
|
|
116 |
49324
|
alessia.ba
|
public void setgCubeResourceRegistrator(final GCubeResourceRegistrator gCubeResourceRegistrator) {
|
117 |
|
|
this.gCubeResourceRegistrator = gCubeResourceRegistrator;
|
118 |
49145
|
alessia.ba
|
}
|
119 |
|
|
|
120 |
|
|
public ResourceRegistryPublisher getResourceRegistryPublisher() {
|
121 |
|
|
return resourceRegistryPublisher;
|
122 |
|
|
}
|
123 |
|
|
|
124 |
|
|
public void setResourceRegistryPublisher(final ResourceRegistryPublisher resourceRegistryPublisher) {
|
125 |
|
|
this.resourceRegistryPublisher = resourceRegistryPublisher;
|
126 |
|
|
}
|
127 |
|
|
|
128 |
|
|
public ResourceRegistryClient getResourceRegistryClient() {
|
129 |
|
|
return resourceRegistryClient;
|
130 |
|
|
}
|
131 |
|
|
|
132 |
|
|
public void setResourceRegistryClient(final ResourceRegistryClient resourceRegistryClient) {
|
133 |
|
|
this.resourceRegistryClient = resourceRegistryClient;
|
134 |
|
|
}
|
135 |
|
|
}
|