Revision 53932
Added by Alessia Bardi over 5 years ago
HttpConnectorTest.java | ||
---|---|---|
3 | 3 |
import java.io.IOException; |
4 | 4 |
import java.net.HttpURLConnection; |
5 | 5 |
import java.net.URL; |
6 |
import java.util.concurrent.TimeUnit; |
|
7 |
import javax.net.ssl.HttpsURLConnection; |
|
8 |
import javax.net.ssl.SSLProtocolException; |
|
9 | 6 |
|
10 | 7 |
import eu.dnetlib.data.collector.rmi.CollectorServiceException; |
11 | 8 |
import org.apache.commons.logging.Log; |
12 | 9 |
import org.apache.commons.logging.LogFactory; |
13 |
import org.apache.http.HttpResponse; |
|
14 |
import org.apache.http.client.methods.HttpGet; |
|
15 |
import org.apache.http.conn.ssl.SSLConnectionSocketFactory; |
|
16 |
import org.apache.http.impl.client.HttpClientBuilder; |
|
17 |
import org.apache.http.ssl.SSLContextBuilder; |
|
18 | 10 |
import org.junit.Before; |
19 | 11 |
import org.junit.Ignore; |
20 | 12 |
import org.junit.Test; |
21 |
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; |
|
13 |
|
|
22 | 14 |
@Ignore |
23 | 15 |
public class HttpConnectorTest { |
24 | 16 |
|
... | ... | |
29 | 21 |
private static final String URL_MISCONFIGURED_SERVER = "https://www.alexandria.unisg.ch/cgi/oai2?verb=Identify"; |
30 | 22 |
private static final String URL_GOODSNI_SERVER = "https://air.unimi.it/oai/openaire?verb=Identify"; |
31 | 23 |
|
32 |
private static final SSLContextBuilder sslContextBuilder = new SSLContextBuilder(); |
|
33 |
private static SSLConnectionSocketFactory sslSocketFactory; |
|
34 |
|
|
35 |
private static final HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory(HttpClientBuilder |
|
36 |
.create() |
|
37 |
.setConnectionTimeToLive(0, TimeUnit.MILLISECONDS) |
|
38 |
.setMaxConnPerRoute(1) |
|
39 |
.setMaxConnTotal(1) |
|
40 |
.disableAutomaticRetries() |
|
41 |
.disableConnectionState() |
|
42 |
.setSSLSocketFactory(sslSocketFactory) |
|
43 |
.build()); |
|
44 |
|
|
45 |
// static { |
|
46 |
// System.setProperty("javax.net.debug", "ssl,handshake"); |
|
47 |
// System.setProperty("jsse.enableSNIExtension", "true"); |
|
48 |
// try { |
|
49 |
// sslContextBuilder.loadTrustMaterial(null, (chain, authType) -> true); |
|
50 |
// SSLParameters sslParameters = new SSLParameters(); |
|
51 |
// List sniHostNames = new ArrayList(1); |
|
52 |
// //sniHostNames.add(new SNIHostName(url.getHost())); |
|
53 |
// sslParameters.setServerNames(sniHostNames); |
|
54 |
// sslSocketFactory = new SSLConnectionSocketFactory(sslContextBuilder.build().se, sslParameters); |
|
55 |
// |
|
56 |
// } catch (final NoSuchAlgorithmException | KeyStoreException | KeyManagementException e) { |
|
57 |
// log.error(e);; |
|
58 |
// } |
|
59 |
// } |
|
60 |
|
|
61 | 24 |
@Before |
62 | 25 |
public void setUp() { |
63 | 26 |
connector = new HttpConnector(); |
... | ... | |
75 | 38 |
System.out.println(connector.getInputSource(URL_MISCONFIGURED_SERVER)); |
76 | 39 |
} |
77 | 40 |
|
78 |
@Test |
|
79 |
@Ignore |
|
41 |
@Test(expected = javax.net.ssl.SSLProtocolException.class) |
|
80 | 42 |
public void testMisconfiguredServers2() throws IOException { |
81 | 43 |
HttpURLConnection urlConn = (HttpURLConnection) new URL(URL_MISCONFIGURED_SERVER).openConnection(); |
82 | 44 |
urlConn.getResponseMessage(); |
83 | 45 |
} |
84 | 46 |
|
85 | 47 |
@Test |
86 |
public void testDisablingSNI() throws IOException { |
|
87 |
HttpURLConnection urlConn = null; |
|
88 |
try { |
|
89 |
urlConn = (HttpURLConnection) new URL(URL_MISCONFIGURED_SERVER).openConnection(); |
|
90 |
urlConn.getResponseMessage(); |
|
91 |
} catch(SSLProtocolException sslExce) { |
|
92 |
if (sslExce.getMessage() != null && sslExce.getMessage().equals("handshake alert: unrecognized_name")) { |
|
93 |
System.out.println("Server has misconfigured SSL SNI (handshake alert: unrecognized_name). Trying to disable SNI"); |
|
94 |
if (urlConn instanceof HttpsURLConnection) { |
|
95 |
HttpResponse res = httpRequestFactory.getHttpClient().execute(new HttpGet(URL_MISCONFIGURED_SERVER)); |
|
96 |
System.out.println(res.getStatusLine()); |
|
97 |
// HttpsURLConnection httpsUrlConnection = (HttpsURLConnection) urlConn; |
|
98 |
// httpsUrlConnection.setSSLSocketFactory(sslSocketFactory); |
|
99 |
// httpsUrlConnection.setHostnameVerifier(new HostnameVerifier() { |
|
100 |
// public boolean verify( String s, SSLSession sess ) { |
|
101 |
// return true; |
|
102 |
// }}); |
|
103 |
// httpsUrlConnection.getResponseMessage(); |
|
104 |
} |
|
105 |
|
|
106 |
} |
|
107 |
} |
|
108 |
} |
|
109 |
|
|
110 |
|
|
111 |
|
|
112 |
|
|
113 |
@Test |
|
114 | 48 |
public void testGoodServers() throws CollectorServiceException { |
115 | 49 |
System.out.println(connector.getInputSource(URL_GOODSNI_SERVER)); |
116 | 50 |
} |
Also available in: Unified diff
Upgrade to commons.net:commons.net 3.6 (from 3.3) and commons-httpclient 4.5.6 (from 3.1).
using HttpConnector where needed.
httpConnector can now return an InputStream and not only a string.