Révision 2894

tmp/org.txm.rcp/src/main/java/org/txm/rcp/commands/OpenLocalizedWebPage.java (revision 2894)
28 28
package org.txm.rcp.commands;
29 29

  
30 30
import java.net.URL;
31
import java.security.GeneralSecurityException;
31 32
import java.util.Locale;
32 33

  
33 34
import javax.net.ssl.HttpsURLConnection;
35
import javax.net.ssl.SSLContext;
36
import javax.net.ssl.TrustManager;
37
import javax.net.ssl.X509TrustManager;
38
import javax.security.cert.X509Certificate;
34 39

  
35 40
import org.eclipse.core.commands.AbstractHandler;
36 41
import org.eclipse.core.commands.ExecutionEvent;
......
89 94
		return null;
90 95
	}
91 96

  
97
	/**
98
	 * try connecting to an URL if it fails, the alternative url is returned
99
	 *  
100
	 * @param urltoTest
101
	 * @param alternativeUrl
102
	 * @return
103
	 */
92 104
	static public String getValidURL(String urltoTest, String alternativeUrl) {
93 105
		try {
94 106
			URL url = new URL(urltoTest);
95 107
			if (url.getProtocol().equals("https")) {
108

  
109
				//ts.setCertificateEntry(UUID.randomUUID(), cert);
96 110
				HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
111
				if (SSLContext.getDefault() != null) {
112

  
113
					// Create a trust manager that does not validate certificate chains
114
					TrustManager[] trustAllCerts = new TrustManager[] { 
115
							new X509TrustManager() {     
116
								public java.security.cert.X509Certificate[] getAcceptedIssuers() { 
117
									return new java.security.cert.X509Certificate[0];
118
								} 
119
								public void checkClientTrusted( 
120
										java.security.cert.X509Certificate[] certs, String authType) {
121
								} 
122
								public void checkServerTrusted( 
123
										java.security.cert.X509Certificate[] certs, String authType) {
124
								}
125
							} 
126
					};
127

  
128
					// Install the all-trusting trust manager for this connection ONLY
129
					try {
130
						SSLContext sc = SSLContext.getInstance("SSL");
131
						sc.init(null, trustAllCerts, new java.security.SecureRandom()); 
132
						connection.setSSLSocketFactory(sc.getSocketFactory());
133
					} catch (GeneralSecurityException e) {
134
						Log.printStackTrace(e);
135
					} 
136
				}
137

  
97 138
				if (connection.getResponseCode() != HttpsURLConnection.HTTP_OK) {
98 139
					Log.fine(NLS.bind("HTTP Error while testing the \"{0}\" web page: {1}", urltoTest, connection.getResponseCode()));
99 140
					connection.disconnect();
......
124 165
		if (idx > 0) version = version.substring(0, idx); // remove the "qualifier" part
125 166

  
126 167
		String locale = Locale.getDefault().getLanguage();
127
		
168

  
128 169
		if (TEXTOMETRIE.equals(key)) {
129 170
			if ("fr".equals(locale)) { //$NON-NLS-1$
130 171
				return OpenBrowser.openfile(URL_TEXTOMETRIE); 
......
195 236
			return OpenBrowser.openfile(key); //$NON-NLS-1$
196 237
		}
197 238
	}
198
	
239

  
199 240
	public static void main(String args[]) {
200
		String s = getValidURL("https://stackoverflow.com/questionz", "https://stackoverflow.com/questions");
241
		String s = getValidURL("https://txm.gitpages.huma-num.fr/textometrie/files/software/TXM/0.8.1beta/", "https://txm.gitpages.huma-num.fr/textometrie/files/software/TXM/0.8.1beta");
201 242
		System.out.println(s);
202 243
	}
203 244
}

Formats disponibles : Unified diff