2019-02-16 11:04:41 +01:00
|
|
|
package eu.dnetlib.repo.manager.utils;
|
|
|
|
|
2023-01-11 17:50:31 +01:00
|
|
|
import org.slf4j.Logger;
|
2019-02-16 11:04:41 +01:00
|
|
|
import org.dom4j.io.DOMWriter;
|
2023-01-11 17:50:31 +01:00
|
|
|
import org.slf4j.LoggerFactory;
|
2019-02-16 11:04:41 +01:00
|
|
|
import org.w3c.dom.Document;
|
|
|
|
import se.kb.oai.pmh.*;
|
|
|
|
|
|
|
|
import javax.net.ssl.*;
|
|
|
|
import javax.xml.namespace.NamespaceContext;
|
|
|
|
import javax.xml.xpath.XPath;
|
|
|
|
import javax.xml.xpath.XPathExpressionException;
|
|
|
|
import javax.xml.xpath.XPathFactory;
|
|
|
|
import java.security.KeyManagementException;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
import java.security.cert.X509Certificate;
|
2019-12-16 11:54:06 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
2019-02-16 11:04:41 +01:00
|
|
|
|
|
|
|
public class OaiTools {
|
|
|
|
|
|
|
|
{
|
|
|
|
disableSslVerification();
|
|
|
|
}
|
|
|
|
|
2023-01-11 17:50:31 +01:00
|
|
|
private static Logger logger = LoggerFactory.getLogger(OaiTools.class);
|
2019-02-16 11:04:41 +01:00
|
|
|
|
|
|
|
public static List<String> getSetsOfRepo(String baseUrl) throws Exception {
|
|
|
|
try {
|
2022-12-14 15:33:12 +01:00
|
|
|
logger.debug("Getting sets of repository " + baseUrl);
|
2019-02-16 11:04:41 +01:00
|
|
|
OaiPmhServer harvester = new OaiPmhServer(baseUrl);
|
|
|
|
SetsList setList = harvester.listSets();
|
|
|
|
ResumptionToken token = setList.getResumptionToken();
|
2022-12-07 12:37:55 +01:00
|
|
|
List<Set> sets = new ArrayList<>(setList.asList());
|
2019-02-16 11:04:41 +01:00
|
|
|
while (token != null) {
|
|
|
|
setList = harvester.listSets(token);
|
|
|
|
token = setList.getResumptionToken();
|
|
|
|
sets.addAll(setList.asList());
|
|
|
|
}
|
|
|
|
|
|
|
|
List<String> ret = new ArrayList<String>();
|
|
|
|
for (Set set : sets) {
|
|
|
|
ret.add(set.getSpec().trim());
|
|
|
|
}
|
|
|
|
if (ret.size() > 0 )
|
|
|
|
Collections.sort(ret);
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
2022-12-14 15:33:12 +01:00
|
|
|
logger.error("Error getting sets of repository " + baseUrl, e);
|
2019-02-16 11:04:41 +01:00
|
|
|
return new ArrayList<String>();
|
|
|
|
//throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean identifyRepository(String baseUrl) throws Exception {
|
2022-12-14 15:33:12 +01:00
|
|
|
logger.debug("sending identify request to repo " + baseUrl);
|
2019-02-16 11:04:41 +01:00
|
|
|
|
|
|
|
OaiPmhServer harvester = new OaiPmhServer(baseUrl);
|
|
|
|
|
|
|
|
if (baseUrl.trim().isEmpty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
Identification identification = harvester.identify();
|
|
|
|
DOMWriter d4Writer = new DOMWriter();
|
|
|
|
Document d = d4Writer.write(identification.getResponse());
|
|
|
|
|
|
|
|
return verifyIdentify(d);
|
|
|
|
} catch (Exception e) {
|
2022-12-14 15:33:12 +01:00
|
|
|
logger.debug("Error verifying identify response", e);
|
2019-02-16 11:04:41 +01:00
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static boolean verifyIdentify(Document doc) throws XPathExpressionException {
|
|
|
|
NamespaceContext ctx = new NamespaceContext() {
|
|
|
|
public String getNamespaceURI(String prefix) {
|
|
|
|
String uri;
|
|
|
|
if (prefix.equals("oai"))
|
|
|
|
uri = "http://www.openarchives.org/OAI/2.0/";
|
|
|
|
else
|
|
|
|
uri = null;
|
|
|
|
return uri;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dummy implementation - not used!
|
|
|
|
public Iterator<String> getPrefixes(String val) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dummy implemenation - not used!
|
|
|
|
public String getPrefix(String uri) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Now the XPath expression
|
|
|
|
|
|
|
|
String xpathStr = "//oai:OAI-PMH/oai:Identify";
|
|
|
|
XPathFactory xpathFact = XPathFactory.newInstance();
|
|
|
|
XPath xpath = xpathFact.newXPath();
|
|
|
|
xpath.setNamespaceContext(ctx);
|
|
|
|
String result = xpath.evaluate(xpathStr, doc);
|
|
|
|
|
|
|
|
return (result != null && !result.equals(""));
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void disableSslVerification() {
|
|
|
|
try
|
|
|
|
{
|
2022-12-14 15:33:12 +01:00
|
|
|
logger.debug("disabling ssl verification");
|
2019-02-16 11:04:41 +01:00
|
|
|
// Create a trust manager that does not validate certificate chains
|
|
|
|
TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() {
|
|
|
|
public X509Certificate[] getAcceptedIssuers() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
public void checkClientTrusted(X509Certificate[] certs, String authType) {
|
|
|
|
}
|
|
|
|
public void checkServerTrusted(X509Certificate[] certs, String authType) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Install the all-trusting trust manager
|
|
|
|
SSLContext sc = SSLContext.getInstance("SSL");
|
|
|
|
sc.init(null, trustAllCerts, new java.security.SecureRandom());
|
|
|
|
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
|
|
|
|
|
|
|
|
// Create all-trusting host name verifier
|
|
|
|
HostnameVerifier allHostsValid = new HostnameVerifier() {
|
|
|
|
public boolean verify(String hostname, SSLSession session) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Install the all-trusting host verifier
|
|
|
|
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
2022-12-14 15:33:12 +01:00
|
|
|
logger.error("disabling ssl verification", e);
|
2019-02-16 11:04:41 +01:00
|
|
|
} catch (KeyManagementException e) {
|
2022-12-14 15:33:12 +01:00
|
|
|
logger.error("error while disabling ssl verification", e);
|
2019-02-16 11:04:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|