Project

General

Profile

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

    
3
import java.io.IOException;
4
import java.io.InputStream;
5
import java.net.HttpURLConnection;
6
import java.net.URL;
7
import java.util.concurrent.TimeUnit;
8
import javax.net.ssl.HttpsURLConnection;
9
import javax.net.ssl.SSLProtocolException;
10

    
11
import com.google.common.base.Joiner;
12
import eu.dnetlib.data.collector.rmi.CollectorServiceException;
13
import org.apache.commons.io.IOUtils;
14
import org.apache.commons.logging.Log;
15
import org.apache.commons.logging.LogFactory;
16
import org.apache.http.HttpResponse;
17
import org.apache.http.client.methods.CloseableHttpResponse;
18
import org.apache.http.client.methods.HttpGet;
19
import org.apache.http.client.methods.HttpUriRequest;
20
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
21
import org.apache.http.impl.client.HttpClientBuilder;
22
import org.apache.http.impl.client.HttpClients;
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
@Ignore
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 testConnectionRelease() throws IOException, InterruptedException {
75

    
76
		InputStream in;
77

    
78
		final HttpGet get = new HttpGet("http://www.google.com");
79
		try(CloseableHttpResponse rsp = HttpClients.createDefault().execute(get)) {
80

    
81
			in = rsp.getEntity().getContent();
82
		}
83

    
84
		log.info("going to sleep ... ");
85
		Thread.sleep(1000);
86

    
87
		log.info("wake up!");
88

    
89
		System.out.println(IOUtils.toString(in));
90
	}
91

    
92
	@Test
93
	@Ignore
94
	public void testGetInputSource() throws CollectorServiceException {
95
		System.out.println(connector.getInputSource(URL));
96
	}
97

    
98
	@Test
99
	@Ignore
100
	public void testMisconfiguredServers() throws CollectorServiceException {
101
		System.out.println(connector.getInputSource(URL_MISCONFIGURED_SERVER));
102
	}
103

    
104
	@Test
105
	@Ignore
106
	public void testMisconfiguredServers2() throws IOException {
107
		HttpURLConnection urlConn = (HttpURLConnection) new URL(URL_MISCONFIGURED_SERVER).openConnection();
108
		urlConn.getResponseMessage();
109
	}
110

    
111
	@Test
112
	public void testDisablingSNI() throws IOException {
113
		HttpURLConnection urlConn = null;
114
		try {
115
			urlConn = (HttpURLConnection) new URL(URL_MISCONFIGURED_SERVER).openConnection();
116
			urlConn.getResponseMessage();
117
		 } catch(SSLProtocolException sslExce) {
118
			if (sslExce.getMessage() != null && sslExce.getMessage().equals("handshake alert:  unrecognized_name")) {
119
				System.out.println("Server has misconfigured SSL SNI (handshake alert:  unrecognized_name). Trying to disable SNI");
120
				if (urlConn instanceof HttpsURLConnection) {
121
					HttpResponse res = httpRequestFactory.getHttpClient().execute(new HttpGet(URL_MISCONFIGURED_SERVER));
122
					System.out.println(res.getStatusLine());
123
//					HttpsURLConnection httpsUrlConnection = (HttpsURLConnection) urlConn;
124
//					httpsUrlConnection.setSSLSocketFactory(sslSocketFactory);
125
//					httpsUrlConnection.setHostnameVerifier(new HostnameVerifier() {
126
//						public boolean verify( String s, SSLSession sess ) {
127
//							return true;
128
//						}});
129
//					httpsUrlConnection.getResponseMessage();
130
				}
131

    
132
			}
133
		}
134
	}
135

    
136

    
137

    
138

    
139
	@Test
140
	public void testGoodServers() throws CollectorServiceException {
141
		System.out.println(connector.getInputSource(URL_GOODSNI_SERVER));
142
	}
143

    
144

    
145
}
(2-2/2)