1
|
package eu.dnetlib.repo.manager.server.utils;
|
2
|
|
3
|
import org.apache.log4j.Logger;
|
4
|
import org.dom4j.io.DOMWriter;
|
5
|
import org.springframework.beans.factory.annotation.Configurable;
|
6
|
import org.w3c.dom.Document;
|
7
|
import se.kb.oai.OAIException;
|
8
|
import se.kb.oai.pmh.*;
|
9
|
|
10
|
import javax.net.ssl.*;
|
11
|
import javax.xml.namespace.NamespaceContext;
|
12
|
import javax.xml.xpath.XPath;
|
13
|
import javax.xml.xpath.XPathExpressionException;
|
14
|
import javax.xml.xpath.XPathFactory;
|
15
|
import java.security.KeyManagementException;
|
16
|
import java.security.NoSuchAlgorithmException;
|
17
|
import java.security.cert.X509Certificate;
|
18
|
import java.util.ArrayList;
|
19
|
import java.util.Collections;
|
20
|
import java.util.Iterator;
|
21
|
import java.util.List;
|
22
|
|
23
|
public class OaiTools {
|
24
|
|
25
|
{
|
26
|
disableSslVerification();
|
27
|
}
|
28
|
|
29
|
private static Logger LOGGER = Logger.getLogger(OaiTools.class);
|
30
|
|
31
|
public static List<String> getSetsOfRepo(String baseUrl) throws Exception {
|
32
|
try {
|
33
|
LOGGER.debug("Getting sets of repository " + baseUrl);
|
34
|
OaiPmhServer harvester = new OaiPmhServer(baseUrl);
|
35
|
SetsList setList = harvester.listSets();
|
36
|
ResumptionToken token = setList.getResumptionToken();
|
37
|
List<Set> sets = new ArrayList<Set>();
|
38
|
sets.addAll(setList.asList());
|
39
|
while (token != null) {
|
40
|
setList = harvester.listSets(token);
|
41
|
token = setList.getResumptionToken();
|
42
|
sets.addAll(setList.asList());
|
43
|
}
|
44
|
|
45
|
List<String> ret = new ArrayList<String>();
|
46
|
for (Set set : sets) {
|
47
|
ret.add(set.getSpec().trim());
|
48
|
}
|
49
|
if (ret.size() > 0 )
|
50
|
Collections.sort(ret);
|
51
|
return ret;
|
52
|
|
53
|
} catch (Exception e) {
|
54
|
LOGGER.error("Error getting sets of repository " + baseUrl, e);
|
55
|
return new ArrayList<String>();
|
56
|
//throw e;
|
57
|
}
|
58
|
}
|
59
|
|
60
|
public static boolean identifyRepository(String baseUrl) throws Exception {
|
61
|
LOGGER.debug("sending identify request to repo " + baseUrl);
|
62
|
|
63
|
OaiPmhServer harvester = new OaiPmhServer(baseUrl);
|
64
|
|
65
|
if (baseUrl.trim().isEmpty()) {
|
66
|
return false;
|
67
|
}
|
68
|
|
69
|
try {
|
70
|
Identification identification = harvester.identify();
|
71
|
DOMWriter d4Writer = new DOMWriter();
|
72
|
Document d = d4Writer.write(identification.getResponse());
|
73
|
|
74
|
return verifyIdentify(d);
|
75
|
} catch (Exception e) {
|
76
|
LOGGER.debug("Error verifying identify response", e);
|
77
|
throw e;
|
78
|
}
|
79
|
}
|
80
|
|
81
|
private static boolean verifyIdentify(Document doc) throws XPathExpressionException {
|
82
|
NamespaceContext ctx = new NamespaceContext() {
|
83
|
public String getNamespaceURI(String prefix) {
|
84
|
String uri;
|
85
|
if (prefix.equals("oai"))
|
86
|
uri = "http://www.openarchives.org/OAI/2.0/";
|
87
|
else
|
88
|
uri = null;
|
89
|
return uri;
|
90
|
}
|
91
|
|
92
|
// Dummy implementation - not used!
|
93
|
public Iterator<String> getPrefixes(String val) {
|
94
|
return null;
|
95
|
}
|
96
|
|
97
|
// Dummy implemenation - not used!
|
98
|
public String getPrefix(String uri) {
|
99
|
return null;
|
100
|
}
|
101
|
};
|
102
|
|
103
|
// Now the XPath expression
|
104
|
|
105
|
String xpathStr = "//oai:OAI-PMH/oai:Identify";
|
106
|
XPathFactory xpathFact = XPathFactory.newInstance();
|
107
|
XPath xpath = xpathFact.newXPath();
|
108
|
xpath.setNamespaceContext(ctx);
|
109
|
String result = xpath.evaluate(xpathStr, doc);
|
110
|
|
111
|
return (result != null && !result.equals(""));
|
112
|
}
|
113
|
|
114
|
private static void disableSslVerification() {
|
115
|
try
|
116
|
{
|
117
|
LOGGER.debug("disabling ssl verification");
|
118
|
// Create a trust manager that does not validate certificate chains
|
119
|
TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() {
|
120
|
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
|
121
|
return null;
|
122
|
}
|
123
|
public void checkClientTrusted(X509Certificate[] certs, String authType) {
|
124
|
}
|
125
|
public void checkServerTrusted(X509Certificate[] certs, String authType) {
|
126
|
}
|
127
|
}
|
128
|
};
|
129
|
|
130
|
// Install the all-trusting trust manager
|
131
|
SSLContext sc = SSLContext.getInstance("SSL");
|
132
|
sc.init(null, trustAllCerts, new java.security.SecureRandom());
|
133
|
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
|
134
|
|
135
|
// Create all-trusting host name verifier
|
136
|
HostnameVerifier allHostsValid = new HostnameVerifier() {
|
137
|
public boolean verify(String hostname, SSLSession session) {
|
138
|
return true;
|
139
|
}
|
140
|
};
|
141
|
|
142
|
// Install the all-trusting host verifier
|
143
|
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
|
144
|
} catch (NoSuchAlgorithmException e) {
|
145
|
LOGGER.error("disabling ssl verification", e);
|
146
|
} catch (KeyManagementException e) {
|
147
|
LOGGER.error("error while disabling ssl verification", e);
|
148
|
}
|
149
|
}
|
150
|
}
|