Revision dfc366ca
Added by Andreas Müller about 13 years ago
cdmlib-commons/src/main/java/eu/etaxonomy/cdm/common/UriUtils.java | ||
---|---|---|
14 | 14 |
import java.io.FileInputStream; |
15 | 15 |
import java.io.IOException; |
16 | 16 |
import java.io.InputStream; |
17 |
import java.net.InetAddress; |
|
17 | 18 |
import java.net.URI; |
18 | 19 |
import java.net.URISyntaxException; |
19 | 20 |
import java.net.URL; |
21 |
import java.net.UnknownHostException; |
|
20 | 22 |
import java.util.ArrayList; |
21 | 23 |
import java.util.HashMap; |
22 | 24 |
import java.util.List; |
... | ... | |
30 | 32 |
import org.apache.http.client.ClientProtocolException; |
31 | 33 |
import org.apache.http.client.HttpClient; |
32 | 34 |
import org.apache.http.client.methods.HttpGet; |
35 |
import org.apache.http.client.methods.HttpHead; |
|
36 |
import org.apache.http.client.methods.HttpUriRequest; |
|
33 | 37 |
import org.apache.http.client.utils.URIUtils; |
34 | 38 |
import org.apache.http.client.utils.URLEncodedUtils; |
35 | 39 |
import org.apache.http.impl.client.DefaultHttpClient; |
... | ... | |
153 | 157 |
if(qparams == null){ |
154 | 158 |
qparams = new ArrayList<NameValuePair>(0); |
155 | 159 |
} |
156 |
|
|
160 |
String query = null; |
|
161 |
if(! qparams.isEmpty()){ |
|
162 |
query = URLEncodedUtils.format(qparams, "UTF-8"); |
|
163 |
} |
|
164 |
|
|
157 | 165 |
URI uri = URIUtils.createURI(baseUrl.getProtocol(), |
158 |
baseUrl.getHost(), baseUrl.getPort(), path, URLEncodedUtils.format(qparams, "UTF-8"), fragment);
|
|
166 |
baseUrl.getHost(), baseUrl.getPort(), path, query, fragment);
|
|
159 | 167 |
|
160 | 168 |
return uri; |
161 | 169 |
} |
170 |
|
|
171 |
/** |
|
172 |
* Tests internet connectivity by testing HEAD request for 4 known URL's.<BR> |
|
173 |
* If non of them is available <code>false</code> is returned. Otherwise true.<BR> |
|
174 |
* @param firstUriToTest if not <code>null</code> this URI is tested before testing the standard URLs. |
|
175 |
* @return true if internetconnectivity is given. |
|
176 |
*/ |
|
177 |
public static boolean isInternetAvailable(URI firstUriToTest){ |
|
178 |
boolean result = false; |
|
179 |
if (firstUriToTest != null && isServiceAvailable(firstUriToTest)){ |
|
180 |
return true; |
|
181 |
} |
|
182 |
|
|
183 |
URI uri = URI.create("http://www.cnn.com/"); |
|
184 |
if (isServiceAvailable(uri)){ |
|
185 |
return true; |
|
186 |
} |
|
187 |
uri = URI.create("http://www.bahn.de/"); |
|
188 |
if (isServiceAvailable(uri)){ |
|
189 |
return true; |
|
190 |
} |
|
191 |
uri = URI.create("http://www.google.com/"); |
|
192 |
if (isServiceAvailable(uri)){ |
|
193 |
return true; |
|
194 |
} |
|
195 |
uri = URI.create("http://www.facebook.com/"); |
|
196 |
if (isServiceAvailable(uri)){ |
|
197 |
return true; |
|
198 |
} |
|
199 |
|
|
200 |
return result; |
|
201 |
} |
|
202 |
|
|
203 |
/** |
|
204 |
* Performs HEAD request for the given URI.<BR> |
|
205 |
* If any exception occurs <code>false</code> is returned. Otherwise true. <BR> |
|
206 |
* @param serviceUri the URI to test. |
|
207 |
* @return true if service is available. |
|
208 |
*/ |
|
209 |
public static boolean isServiceAvailable(URI serviceUri){ |
|
210 |
boolean result = false; |
|
211 |
|
|
212 |
//Http |
|
213 |
HttpClient client = new DefaultHttpClient(); |
|
214 |
HttpUriRequest request = new HttpHead(serviceUri); |
|
215 |
|
|
216 |
try { |
|
217 |
// Execute the request |
|
218 |
HttpResponse response = client.execute(request); |
|
219 |
// Examine the response status |
|
220 |
if (logger.isDebugEnabled()){ |
|
221 |
logger.debug(response.getStatusLine()); |
|
222 |
} |
|
223 |
result = true; |
|
224 |
|
|
225 |
} catch (UnknownHostException e1) { |
|
226 |
logger.warn("Unknwon Host: " +e1.getMessage()); |
|
227 |
} catch (ClientProtocolException e2) { |
|
228 |
logger.warn("ClientProtocolException: " + e2.getMessage()); |
|
229 |
} catch (IOException e3) { |
|
230 |
logger.warn("IOException: " + e3.getMessage()); |
|
231 |
} |
|
232 |
|
|
233 |
// When HttpClient instance is no longer needed, |
|
234 |
// shut down the connection manager to ensure |
|
235 |
// immediate deallocation of all system resources |
|
236 |
//needed ? |
|
237 |
// client.getConnectionManager().shutdown(); |
|
238 |
|
|
239 |
return result; |
|
240 |
} |
|
241 |
|
|
242 |
/** |
|
243 |
* Tests reachability of a root server by trying to resolve a host name. |
|
244 |
* @param hostNameToResolve the host name to resolve. If <code>null</code> |
|
245 |
* a default host name is tested. |
|
246 |
* @return |
|
247 |
*/ |
|
248 |
public static boolean isRootServerAvailable(String hostNameToResolve){ |
|
249 |
try { |
|
250 |
if (hostNameToResolve == null){ |
|
251 |
hostNameToResolve = "cnn.com"; |
|
252 |
} |
|
253 |
InetAddress inetHost = InetAddress.getByName(hostNameToResolve); |
|
254 |
System.out.println("The hosts IP address is: " + inetHost.getHostAddress()); |
|
255 |
return true; |
|
256 |
} catch(UnknownHostException ex) { |
|
257 |
System.out.println("Unrecognized host"); |
|
258 |
return false; |
|
259 |
} |
|
260 |
} |
|
261 |
|
|
162 | 262 |
} |
Also available in: Unified diff
implemented internet connectivity test #2259