Project

General

Profile

1
package eu.dnetlib.repo.manager.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

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

    
21
public class OaiTools {
22

    
23
	{
24
		disableSslVerification();
25
	}
26

    
27
	private static Logger LOGGER = Logger.getLogger(OaiTools.class);
28

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

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

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

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

    
61
		OaiPmhServer harvester = new OaiPmhServer(baseUrl);
62

    
63
		if (baseUrl.trim().isEmpty()) {
64
			return false;
65
		}
66

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

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

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

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

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

    
101
		// Now the XPath expression
102

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

    
109
		return (result != null && !result.equals(""));
110
	}
111

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

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

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

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