View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
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  public class WebServiceOnlineCheck
23  {
24      public static final String TEST_URL = "http://www.webservicex.net/stockquote.asmx/GetQuote?symbol=CSCO";
25      private static final Log logger = LogFactory.getLog(WebServiceOnlineCheck.class);
26  
27      public static boolean isWebServiceOnline()
28      {
29          logger.debug("Verifying that the web service is on-line...");
30          
31          BufferedReader input = null;
32          try 
33          {
34              URLConnection conn = new URL(TEST_URL).openConnection();
35              // setting these timeouts ensures the client does not deadlock indefinitely
36              // when the server has problems.
37              conn.setConnectTimeout(AbstractMuleContextTestCase.RECEIVE_TIMEOUT);
38              conn.setReadTimeout(AbstractMuleContextTestCase.RECEIVE_TIMEOUT);
39              InputStream in = conn.getInputStream();
40  
41              input = new BufferedReader(new InputStreamReader(in));            
42  
43              String response = "";
44              String line;
45              while ((line = input.readLine()) != null) 
46              {
47                  response += line;
48              }
49  
50              if (StringUtils.containsIgnoreCase(response, "Cisco"))
51              {
52                  return true;
53              }
54              else
55              {
56                  logger.warn("Unexpected response, web service does not seem to be on-line: \n" + response);
57                  return false;
58              }
59          } 
60          catch (Exception e) 
61          {
62              logger.warn("Exception occurred, web service does not seem to be on-line: " + e);
63              return false;
64          } 
65          finally
66          {
67              IOUtils.closeQuietly(input);
68          }
69      }
70  }
71  
72