Project

General

Profile

1
package eu.dnetlib.data.collector.plugins;
2

    
3
import java.io.IOException;
4
import java.net.HttpURLConnection;
5
import java.net.URL;
6
import java.security.KeyManagementException;
7
import java.security.KeyStoreException;
8
import java.security.NoSuchAlgorithmException;
9
import java.util.ArrayList;
10
import java.util.List;
11
import java.util.concurrent.TimeUnit;
12
import javax.net.ssl.HttpsURLConnection;
13
import javax.net.ssl.SSLParameters;
14
import javax.net.ssl.SSLProtocolException;
15

    
16
import eu.dnetlib.data.collector.rmi.CollectorServiceException;
17
import org.apache.commons.logging.Log;
18
import org.apache.commons.logging.LogFactory;
19
import org.apache.http.HttpResponse;
20
import org.apache.http.client.methods.HttpGet;
21
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
22
import org.apache.http.impl.client.HttpClientBuilder;
23
import org.apache.http.ssl.SSLContextBuilder;
24
import org.junit.Before;
25
import org.junit.Ignore;
26
import org.junit.Test;
27
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
28

    
29
public class HttpConnectorTest {
30

    
31
	private static final Log log = LogFactory.getLog(HttpConnectorTest.class);
32
	private HttpConnector connector;
33

    
34
	private static final String URL = "https://researchdata.ands.org.au/registry/services/oai?verb=Identify";
35
	private static final String URL_MISCONFIGURED_SERVER = "https://www.alexandria.unisg.ch/cgi/oai2?verb=Identify";
36
	private static final String URL_GOODSNI_SERVER = "https://air.unimi.it/oai/openaire?verb=Identify";
37

    
38
	private static final SSLContextBuilder sslContextBuilder = new SSLContextBuilder();
39
	private static SSLConnectionSocketFactory sslSocketFactory;
40

    
41
	private static final HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory(HttpClientBuilder
42
			.create()
43
			.setConnectionTimeToLive(0, TimeUnit.MILLISECONDS)
44
			.setMaxConnPerRoute(1)
45
			.setMaxConnTotal(1)
46
			.disableAutomaticRetries()
47
			.disableConnectionState()
48
			.setSSLSocketFactory(sslSocketFactory)
49
			.build());
50

    
51
	static {
52
		System.setProperty("javax.net.debug", "ssl,handshake");
53
		System.setProperty("jsse.enableSNIExtension", "true");
54
		try {
55
			sslContextBuilder.loadTrustMaterial(null, (chain, authType) -> true);
56
			SSLParameters sslParameters = new SSLParameters();
57
			List sniHostNames = new ArrayList(1);
58
			//sniHostNames.add(new SNIHostName(url.getHost()));
59
			sslParameters.setServerNames(sniHostNames);
60
			sslSocketFactory = new SSLConnectionSocketFactory(sslContextBuilder.build().se, sslParameters);
61

    
62
		} catch (final NoSuchAlgorithmException | KeyStoreException | KeyManagementException e) {
63
			log.error(e);;
64
		}
65
	}
66

    
67
	@Before
68
	public void setUp() {
69
		connector = new HttpConnector();
70
	}
71

    
72
	@Test
73
	@Ignore
74
	public void testGetInputSource() throws CollectorServiceException {
75
		System.out.println(connector.getInputSource(URL));
76
	}
77

    
78
	@Test
79
	@Ignore
80
	public void testMisconfiguredServers() throws CollectorServiceException {
81
		System.out.println(connector.getInputSource(URL_MISCONFIGURED_SERVER));
82
	}
83

    
84
	@Test
85
	@Ignore
86
	public void testMisconfiguredServers2() throws IOException {
87
		HttpURLConnection urlConn = (HttpURLConnection) new URL(URL_MISCONFIGURED_SERVER).openConnection();
88
		urlConn.getResponseMessage();
89
	}
90

    
91
	@Test
92
	public void testDisablingSNI() throws IOException {
93
		HttpURLConnection urlConn = null;
94
		try {
95
			urlConn = (HttpURLConnection) new URL(URL_MISCONFIGURED_SERVER).openConnection();
96
			urlConn.getResponseMessage();
97
		 } catch(SSLProtocolException sslExce) {
98
			if (sslExce.getMessage() != null && sslExce.getMessage().equals("handshake alert:  unrecognized_name")) {
99
				System.out.println("Server has misconfigured SSL SNI (handshake alert:  unrecognized_name). Trying to disable SNI");
100
				if (urlConn instanceof HttpsURLConnection) {
101
					HttpResponse res = httpRequestFactory.getHttpClient().execute(new HttpGet(URL_MISCONFIGURED_SERVER));
102
					System.out.println(res.getStatusLine());
103
//					HttpsURLConnection httpsUrlConnection = (HttpsURLConnection) urlConn;
104
//					httpsUrlConnection.setSSLSocketFactory(sslSocketFactory);
105
//					httpsUrlConnection.setHostnameVerifier(new HostnameVerifier() {
106
//						public boolean verify( String s, SSLSession sess ) {
107
//							return true;
108
//						}});
109
//					httpsUrlConnection.getResponseMessage();
110
				}
111

    
112
			}
113
		}
114
	}
115

    
116

    
117

    
118

    
119
	@Test
120
	public void testGoodServers() throws CollectorServiceException {
121
		System.out.println(connector.getInputSource(URL_GOODSNI_SERVER));
122
	}
123

    
124

    
125
}
(2-2/2)