Project

General

Profile

1
package eu.dnetlib.repo.manager.service.utils;
2

    
3
import org.apache.log4j.Logger;
4
import org.dom4j.io.DOMWriter;
5
import org.w3c.dom.Document;
6
import se.kb.oai.pmh.*;
7
import se.kb.oai.pmh.Set;
8

    
9
import javax.net.ssl.*;
10
import javax.xml.namespace.NamespaceContext;
11
import javax.xml.xpath.XPath;
12
import javax.xml.xpath.XPathExpressionException;
13
import javax.xml.xpath.XPathFactory;
14
import java.security.KeyManagementException;
15
import java.security.NoSuchAlgorithmException;
16
import java.security.cert.X509Certificate;
17
import java.util.*;
18

    
19
public class OaiTools {
20

    
21
	{
22
		disableSslVerification();
23
	}
24

    
25
	private static Logger LOGGER = Logger.getLogger(OaiTools.class);
26

    
27
	public static List<String> getSetsOfRepo(String baseUrl) throws Exception {
28
		try {
29
			LOGGER.debug("Getting sets of repository " + baseUrl);
30
			OaiPmhServer harvester = new OaiPmhServer(baseUrl);
31
			SetsList setList = harvester.listSets();
32
			ResumptionToken token = setList.getResumptionToken();
33
			List<Set> sets = new ArrayList<Set>();
34
			sets.addAll(setList.asList());
35
			while (token != null) {
36
				setList = harvester.listSets(token);
37
				token = setList.getResumptionToken();
38
				sets.addAll(setList.asList());
39
			}
40

    
41
			List<String> ret = new ArrayList<String>();
42
			for (Set set : sets) {
43
				ret.add(set.getSpec().trim());
44
			}
45
			if (ret.size() > 0 )
46
				Collections.sort(ret);
47
			return ret;
48

    
49
		} catch (Exception e) {
50
			LOGGER.error("Error getting sets of repository " + baseUrl, e);
51
			return new ArrayList<String>();
52
			//throw e;
53
		}
54
	}
55

    
56
	public static boolean identifyRepository(String baseUrl) throws Exception {
57
		LOGGER.debug("sending identify request to repo " + baseUrl);
58

    
59
		OaiPmhServer harvester = new OaiPmhServer(baseUrl);
60

    
61
		if (baseUrl.trim().isEmpty()) {
62
			return false;
63
		}
64

    
65
		try {
66
			Identification identification = harvester.identify();
67
			DOMWriter d4Writer = new DOMWriter();
68
			Document d = d4Writer.write(identification.getResponse());
69

    
70
			return verifyIdentify(d);
71
		} catch (Exception e) {
72
			LOGGER.debug("Error verifying identify response", e);
73
			throw e;
74
		}
75
	}
76

    
77
	private static boolean verifyIdentify(Document doc) throws XPathExpressionException {
78
		NamespaceContext ctx = new NamespaceContext() {
79
			public String getNamespaceURI(String prefix) {
80
				String uri;
81
				if (prefix.equals("oai"))
82
					uri = "http://www.openarchives.org/OAI/2.0/";
83
				else
84
					uri = null;
85
				return uri;
86
			}
87

    
88
			// Dummy implementation - not used!
89
			public Iterator<String> getPrefixes(String val) {
90
				return null;
91
			}
92

    
93
			// Dummy implemenation - not used!
94
			public String getPrefix(String uri) {
95
				return null;
96
			}
97
		};
98

    
99
		// Now the XPath expression
100

    
101
		String xpathStr = "//oai:OAI-PMH/oai:Identify";
102
		XPathFactory xpathFact = XPathFactory.newInstance();
103
		XPath xpath = xpathFact.newXPath();
104
		xpath.setNamespaceContext(ctx);
105
		String result = xpath.evaluate(xpathStr, doc);
106

    
107
		return (result != null && !result.equals(""));
108
	}
109

    
110
	private static void disableSslVerification() {
111
		try
112
		{
113
			LOGGER.debug("disabling ssl verification");
114
			// Create a trust manager that does not validate certificate chains
115
			TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() {
116
				public X509Certificate[] getAcceptedIssuers() {
117
					return null;
118
				}
119
				public void checkClientTrusted(X509Certificate[] certs, String authType) {
120
				}
121
				public void checkServerTrusted(X509Certificate[] certs, String authType) {
122
				}
123
			}
124
			};
125

    
126
			// Install the all-trusting trust manager
127
			SSLContext sc = SSLContext.getInstance("SSL");
128
			sc.init(null, trustAllCerts, new java.security.SecureRandom());
129
			HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
130

    
131
			// Create all-trusting host name verifier
132
			HostnameVerifier allHostsValid = new HostnameVerifier() {
133
				public boolean verify(String hostname, SSLSession session) {
134
					return true;
135
				}
136
			};
137

    
138
			// Install the all-trusting host verifier
139
			HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
140
		} catch (NoSuchAlgorithmException e) {
141
			LOGGER.error("disabling ssl verification", e);
142
		} catch (KeyManagementException e) {
143
			LOGGER.error("error while disabling ssl verification", e);
144
		}
145
	}
146
}
(2-2/2)