1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.tck.util; |
8 | |
|
9 | |
import org.mule.tck.junit4.AbstractMuleContextTestCase; |
10 | |
import org.mule.util.IOUtils; |
11 | |
import org.mule.util.StringUtils; |
12 | |
|
13 | |
import java.io.BufferedReader; |
14 | |
import java.io.InputStream; |
15 | |
import java.io.InputStreamReader; |
16 | |
import java.net.URL; |
17 | |
import java.net.URLConnection; |
18 | |
|
19 | |
import org.apache.commons.logging.Log; |
20 | |
import org.apache.commons.logging.LogFactory; |
21 | |
|
22 | 0 | public class WebServiceOnlineCheck |
23 | |
{ |
24 | |
public static final String TEST_URL = "http://www.webservicex.net/stockquote.asmx/GetQuote?symbol=CSCO"; |
25 | 0 | private static final Log logger = LogFactory.getLog(WebServiceOnlineCheck.class); |
26 | |
|
27 | |
public static boolean isWebServiceOnline() |
28 | |
{ |
29 | 0 | logger.debug("Verifying that the web service is on-line..."); |
30 | |
|
31 | 0 | BufferedReader input = null; |
32 | |
try |
33 | |
{ |
34 | 0 | URLConnection conn = new URL(TEST_URL).openConnection(); |
35 | |
|
36 | |
|
37 | 0 | conn.setConnectTimeout(AbstractMuleContextTestCase.RECEIVE_TIMEOUT); |
38 | 0 | conn.setReadTimeout(AbstractMuleContextTestCase.RECEIVE_TIMEOUT); |
39 | 0 | InputStream in = conn.getInputStream(); |
40 | |
|
41 | 0 | input = new BufferedReader(new InputStreamReader(in)); |
42 | |
|
43 | 0 | String response = ""; |
44 | |
String line; |
45 | 0 | while ((line = input.readLine()) != null) |
46 | |
{ |
47 | 0 | response += line; |
48 | |
} |
49 | |
|
50 | 0 | if (StringUtils.containsIgnoreCase(response, "Cisco")) |
51 | |
{ |
52 | 0 | return true; |
53 | |
} |
54 | |
else |
55 | |
{ |
56 | 0 | logger.warn("Unexpected response, web service does not seem to be on-line: \n" + response); |
57 | 0 | return false; |
58 | |
} |
59 | |
} |
60 | 0 | catch (Exception e) |
61 | |
{ |
62 | 0 | logger.warn("Exception occurred, web service does not seem to be on-line: " + e); |
63 | 0 | return false; |
64 | |
} |
65 | |
finally |
66 | |
{ |
67 | 0 | IOUtils.closeQuietly(input); |
68 | |
} |
69 | |
} |
70 | |
} |
71 | |
|
72 | |
|