1
|
package eu.dnetlib.validator.service.impls.providers;
|
2
|
|
3
|
|
4
|
import java.util.Iterator;
|
5
|
|
6
|
import javax.xml.namespace.NamespaceContext;
|
7
|
import javax.xml.xpath.XPath;
|
8
|
import javax.xml.xpath.XPathExpressionException;
|
9
|
import javax.xml.xpath.XPathFactory;
|
10
|
|
11
|
import org.apache.log4j.Logger;
|
12
|
import org.dom4j.DocumentException;
|
13
|
import org.dom4j.io.DOMWriter;
|
14
|
import org.w3c.dom.Document;
|
15
|
|
16
|
import se.kb.oai.OAIException;
|
17
|
import se.kb.oai.pmh.Identification;
|
18
|
import se.kb.oai.pmh.OaiPmhServer;
|
19
|
|
20
|
public class RepositoryChecks {
|
21
|
|
22
|
private static Logger logger = Logger.getLogger(RepositoryChecks.class);
|
23
|
|
24
|
public static boolean identifyRepository(String baseUrl) {
|
25
|
logger.debug("sending identify request to repo " + baseUrl);
|
26
|
|
27
|
OaiPmhServer harvester = new OaiPmhServer(baseUrl);
|
28
|
|
29
|
if (baseUrl.trim().isEmpty()) {
|
30
|
return false;
|
31
|
}
|
32
|
|
33
|
try {
|
34
|
Identification identification = harvester.identify();
|
35
|
DOMWriter d4Writer = new DOMWriter();
|
36
|
Document d = d4Writer.write(identification.getResponse());
|
37
|
|
38
|
return verifyIdentify(d);
|
39
|
} catch (OAIException e) {
|
40
|
logger.debug("Could not get response form " + baseUrl);
|
41
|
return false;
|
42
|
} catch (XPathExpressionException e) {
|
43
|
logger.debug("Error verifying identify response", e);
|
44
|
return false;
|
45
|
} catch (DocumentException e) {
|
46
|
logger.debug("Error verifying identify response", e);
|
47
|
return false;
|
48
|
}
|
49
|
}
|
50
|
|
51
|
private static boolean verifyIdentify(Document doc) throws XPathExpressionException {
|
52
|
NamespaceContext ctx = new NamespaceContext() {
|
53
|
public String getNamespaceURI(String prefix) {
|
54
|
String uri;
|
55
|
if (prefix.equals("oai"))
|
56
|
uri = "http://www.openarchives.org/OAI/2.0/";
|
57
|
else
|
58
|
uri = null;
|
59
|
return uri;
|
60
|
}
|
61
|
|
62
|
// Dummy implementation - not used!
|
63
|
public Iterator<String> getPrefixes(String val) {
|
64
|
return null;
|
65
|
}
|
66
|
|
67
|
// Dummy implemenation - not used!
|
68
|
public String getPrefix(String uri) {
|
69
|
return null;
|
70
|
}
|
71
|
};
|
72
|
|
73
|
// Now the XPath expression
|
74
|
|
75
|
String xpathStr = "//oai:OAI-PMH/oai:Identify";
|
76
|
XPathFactory xpathFact = XPathFactory.newInstance();
|
77
|
XPath xpath = xpathFact.newXPath();
|
78
|
xpath.setNamespaceContext(ctx);
|
79
|
String result = xpath.evaluate(xpathStr, doc);
|
80
|
|
81
|
return (result != null && !result.equals(""));
|
82
|
}
|
83
|
}
|