View Javadoc

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