Skip to main content
summaryrefslogtreecommitdiffstats
blob: 57bbf2b675f29838d9c7dd527d6ad82369fbaaac (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
/*******************************************************************************
 * Copyright (c) 2008, 2014 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.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import org.eclipse.help.internal.base.remote.HttpsUtility;

public class TestConnectionUtility {

	//This class provides a utility for testing if a connection
	//can be made to a given URL
	private static final String PATH_TOC = "/toc"; //$NON-NLS-1$
	private static final String PROTOCOL = "http"; //$NON-NLS-1$
	private static final String PROTOCOL_HTTPS = "https"; //$NON-NLS-1$

	private final static int SOCKET_TIMEOUT = 5000; //milliseconds

	public static boolean testConnection(String thisHost, String thisPort,
			String thisPath, String thisProtocol) {

		boolean validConnection = true;
		String urlConnection = ""; //$NON-NLS-1$

		// Build connection string
		if (thisPort.equals("80")) //$NON-NLS-1$
			urlConnection = thisProtocol + "://" + thisHost + thisPath; //$NON-NLS-1$
		else
			urlConnection = thisProtocol + "://" + thisHost + ":" + thisPort + thisPath; //$NON-NLS-1$ //$NON-NLS-2$

		if(thisProtocol.equalsIgnoreCase(PROTOCOL))
		{
			// Test Connection. If exception thrown, invalid connection
			try {
				// Validate Toc connection...
				URL testTocURL = new URL(urlConnection + PATH_TOC);
				validConnection = isValidToc(testTocURL);
			} catch (MalformedURLException e) {
				validConnection = false;
			}
		}
		else if(thisProtocol.equalsIgnoreCase(PROTOCOL_HTTPS))
		{
			// Validate Toc connection...
			validConnection = HttpsUtility.canConnectToHttpsURL(urlConnection + PATH_TOC);
		}
		return validConnection;
	}

	private static boolean isValidToc(URL url)
	{
		InputStream in = null;
		try{
			URLConnection connection = ProxyUtil.getConnection(url);
			setTimeout(connection, SOCKET_TIMEOUT);
			connection.connect();
			in = connection.getInputStream();
			if (in!=null)
			{
				BufferedReader reader = new BufferedReader(new InputStreamReader(in));
				String line;
				while (( line = reader.readLine())!=null){
					if (line.indexOf("<tocContributions>")>-1)  { //$NON-NLS-1$
						reader.close();
						return true;
					}
				}
				reader.close();
			}
		}catch (Exception ex){}
		finally{
			try {
				if (in!=null)
					in.close();
			} catch (IOException e) {}
		}
		return false;
	}

	private static void setTimeout(URLConnection conn, int milliseconds) {
		conn.setConnectTimeout(milliseconds);
	}
}

Back to the top