Project

General

Profile

« Previous | Next » 

Revision 52731

committed partial test for Michele, my hero :-)

View differences:

modules/dnet-modular-collector-service/trunk/src/test/java/eu/dnetlib/data/collector/plugins/HttpConnectorTest.java
3 3
import java.io.IOException;
4 4
import java.net.HttpURLConnection;
5 5
import java.net.URL;
6
import javax.net.ssl.HostnameVerifier;
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;
7 12
import javax.net.ssl.HttpsURLConnection;
13
import javax.net.ssl.SSLParameters;
8 14
import javax.net.ssl.SSLProtocolException;
9
import javax.net.ssl.SSLSession;
10 15

  
11 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;
12 24
import org.junit.Before;
13 25
import org.junit.Ignore;
14 26
import org.junit.Test;
27
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
15 28

  
16 29
public class HttpConnectorTest {
17 30

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

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

  
38
	private static final SSLContextBuilder sslContextBuilder = new SSLContextBuilder();
39
	private static SSLConnectionSocketFactory sslSocketFactory;
24 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

  
25 51
	static {
26 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
		}
27 65
	}
28 66

  
29 67
	@Before
30 68
	public void setUp() {
31 69
		connector = new HttpConnector();
32
		System.setProperty("jsse.enableSNIExtension", "true");
33 70
	}
34 71

  
35 72
	@Test
......
61 98
			if (sslExce.getMessage() != null && sslExce.getMessage().equals("handshake alert:  unrecognized_name")) {
62 99
				System.out.println("Server has misconfigured SSL SNI (handshake alert:  unrecognized_name). Trying to disable SNI");
63 100
				if (urlConn instanceof HttpsURLConnection) {
64
					HttpsURLConnection httpsUrlConnection = (HttpsURLConnection) urlConn;
65
					httpsUrlConnection.setHostnameVerifier(new HostnameVerifier() {
66
						public boolean verify( String s, SSLSession sess ) {
67
							return true;
68
						}});
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
				}
69 111

  
70
				}
71
				urlConn.getResponseMessage();
72 112
			}
73 113
		}
74 114
	}
75 115

  
116

  
117

  
118

  
76 119
	@Test
77 120
	public void testGoodServers() throws CollectorServiceException {
78 121
		System.out.println(connector.getInputSource(URL_GOODSNI_SERVER));

Also available in: Unified diff