Project

General

Profile

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

    
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.ArrayList;
18
import java.util.Collections;
19
import java.util.Iterator;
20
import java.util.List;
21

    
22

    
23

    
24
public class OaiTools {
25

    
26
	{
27
		disableSslVerification();
28
	}
29

    
30
	private static Logger LOGGER = Logger.getLogger(OaiTools.class);
31

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

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

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

    
61
	public static boolean identifyRepository(String baseUrl) throws Exception {
62
		LOGGER.debug("sending identify request to repo " + baseUrl);
63

    
64
		OaiPmhServer harvester = new OaiPmhServer(baseUrl);
65

    
66
		if (baseUrl.trim().isEmpty()) {
67
			return false;
68
		}
69

    
70
		try {
71
			Identification identification = harvester.identify();
72
			DOMWriter d4Writer = new DOMWriter();
73
			Document d = d4Writer.write(identification.getResponse());
74

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

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

    
93
			// Dummy implementation - not used!
94
			public Iterator<String> getPrefixes(String val) {
95
				return null;
96
			}
97

    
98
			// Dummy implemenation - not used!
99
			public String getPrefix(String uri) {
100
				return null;
101
			}
102
		};
103

    
104
		// Now the XPath expression
105

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

    
112
		return (result != null && !result.equals(""));
113
	}
114

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

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

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

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