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