Skip to main content
summaryrefslogtreecommitdiffstats
blob: 4e1df585cfecb3c42767ada6e4304797ba6fd4f9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*******************************************************************************
 * Copyright (c) 2010, 2015 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.help.internal.base.remote;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;


public class HttpsUtility {

	private static final String PATH_TOC = "/toc"; //$NON-NLS-1$
	private static final String PARAM_LANG = "lang"; //$NON-NLS-1$

	private final static int SOCKET_TIMEOUT = 5000; //milliseconds

	public static HttpsURLConnection getConnection(URL httpsURL)
	{
		try
		{
			SSLContext sc = SSLContext.getInstance("TLS"); //$NON-NLS-1$
			sc.init( null, null, new java.security.SecureRandom() );
			HttpsURLConnection con = (HttpsURLConnection)httpsURL.openConnection();
			con.setSSLSocketFactory(sc.getSocketFactory());
			return con;
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}
	public static InputStream getHttpsStream(URL httpsURL)
	{
		try {
			HttpsURLConnection con = getConnection(httpsURL);
			return con==null ? null : con.getInputStream();
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}

	public static InputStream getHttpsInputStream(String thisProtocol,String thisHost, String thisPort, String thisPath, String locale)
	{
		try {
			URL url = new URL(thisProtocol, thisHost, Integer.valueOf(thisPort) .intValue(),
					thisPath + PATH_TOC + '?' + PARAM_LANG + '=' + locale);
	        return getHttpsStream(url);
		} catch (Exception e) {
			e.printStackTrace();
	        return null;
		}
	}

	public static URL getHttpsURL(String thisProtocol,String thisHost, int thisPort, String thisPath)
	{
		try {
			return new URL(thisProtocol, thisHost, Integer.valueOf(thisPort) .intValue(),
					thisPath + PATH_TOC);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static URL getHttpsURL(String urlPath)
	{
		try {
			return new URL(urlPath);
		} catch (MalformedURLException e) {
			e.printStackTrace();
			return null;
		}
	}

	public static URL getHttpsURL(String thisProtocol,String thisHost, String thisPort, String thisPath)
	{
		return getHttpsURL(thisProtocol,thisHost,Integer.parseInt(thisPort),thisPath);
	}

	public static boolean canConnectToHttpsURL(String urlConnection)
	{
		try
		{
			HttpsURLConnection testConnection = getConnection(new URL(urlConnection));
			testConnection.setConnectTimeout(SOCKET_TIMEOUT);
			testConnection.connect();
		}
		catch (Exception e) {
			return false;
		}
		return true;
	}
}

Back to the top