test WS on internal server in the case of proxying
authorSasha Sherstnev <a.sherstnev@dundee.ac.uk>
Mon, 30 Sep 2013 13:28:19 +0000 (14:28 +0100)
committerSasha Sherstnev <a.sherstnev@dundee.ac.uk>
Mon, 30 Sep 2013 13:28:19 +0000 (14:28 +0100)
statpages/ServicesStatus.jsp
webservices/compbio/stat/servlet/ServiceStatus.java

index 67255a3..98d0ab6 100644 (file)
@@ -20,11 +20,16 @@ TODO refactor
 <c:set var="host" value="${pageContext.request.scheme}://${pageContext.request.serverName}:${pageContext.request.serverPort}${pageContext.request.contextPath}" />\r
 \r
 <ul style="font-weight:bold;">\r
-       <li>All the services are tested while this page is being loaded</li>\r
-       <li>If you want to test the services again, just reload this page.</li>\r
+       <li>\r
+               This servlet is tesing whether the web services are healthy on the tomcat instance on which JABAWS is deployed. <br/>\r
+               If this tomcat instance is proxing through another web server, the test is not checking availability of these <br/>\r
+               web services at the endpoints of this external server.</li>\r
+       <li>All the web services are tested while this page is being loaded</li>\r
+       <li>If you want to test the services again, reload this page.</li>\r
        <li>Click on the service status to see the results of the testing.</li>\r
        <li>Server tested: <a href="${host}">${host}</a> </li>\r
        <li>Time of execusion: ${timeexec} msec</li>\r
+       <li> Your IP is ${pageContext.request.remoteAddr}</li>\r
 </ul>\r
 \r
 <br/>\r
index a0f5516..93df9f5 100644 (file)
@@ -1,22 +1,52 @@
+/* Copyright (c) 2013 Alexander Sherstnev\r
+ * Copyright (c) 2011 Peter Troshin\r
+ *  \r
+ *  JAva Bioinformatics Analysis Web Services (JABAWS) @version: 2.0     \r
+ * \r
+ *  This library is free software; you can redistribute it and/or modify it under the terms of the\r
+ *  Apache License version 2 as published by the Apache Software Foundation\r
+ * \r
+ *  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without\r
+ *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache \r
+ *  License for more details.\r
+ * \r
+ *  A copy of the license is in apache_license.txt. It is also available here:\r
+ * @see: http://www.apache.org/licenses/LICENSE-2.0.txt\r
+ * \r
+ * Any republication or derived work distributed in source code form\r
+ * must include this copyright and license notice.\r
+ */\r
+\r
 package compbio.stat.servlet;\r
 \r
 import java.io.IOException;\r
 import java.io.PrintWriter;\r
 import java.io.StringWriter;\r
+import java.lang.management.ManagementFactory;\r
+import java.net.InetAddress;\r
+import java.net.UnknownHostException;\r
 import java.util.ArrayList;\r
+import java.util.Iterator;\r
 import java.util.List;\r
+import java.util.Set;\r
 import java.util.Collections;\r
 \r
+import javax.management.AttributeNotFoundException;\r
+import javax.management.InstanceNotFoundException;\r
+import javax.management.MBeanException;\r
+import javax.management.MBeanServer;\r
+import javax.management.MalformedObjectNameException;\r
+import javax.management.ObjectName;\r
+import javax.management.Query;\r
+import javax.management.ReflectionException;\r
 import javax.servlet.RequestDispatcher;\r
 import javax.servlet.ServletException;\r
 import javax.servlet.http.HttpServlet;\r
 import javax.servlet.http.HttpServletRequest;\r
 import javax.servlet.http.HttpServletResponse;\r
-import javax.servlet.ServletContext;\r
 \r
 import org.apache.log4j.Logger;\r
 \r
-import compbio.stat.servlet.util.Totals;\r
 import compbio.ws.client.Services;\r
 import compbio.ws.client.WSTester;\r
 \r
@@ -29,66 +59,73 @@ import compbio.ws.client.WSTester;
  * @author pvtroshin\r
  * \r
  */\r
+\r
 public class ServiceStatus extends HttpServlet {\r
 \r
        private final static Logger log = Logger.getLogger(ServiceStatus.class);\r
 \r
-\r
-       private static String getURL(HttpServletRequest req) {\r
-               String scheme = req.getScheme();             // http\r
-               String serverName = req.getServerName();     // hostname.com\r
-               int serverPort = req.getServerPort();        // 80\r
-               String contextPath = req.getContextPath();   // /mywebapp\r
-               String servletPath = req.getServletPath();   // /servlet/MyServlet\r
-               String pathInfo = req.getPathInfo();         // /a/b;c=123\r
-               String queryString = req.getQueryString();          // d=789\r
-\r
-               // Reconstruct original requesting URL\r
-               StringBuffer url =  new StringBuffer();\r
-               url.append(scheme).append("://").append(serverName);\r
-\r
-               if ((serverPort != 80) && (serverPort != 443)) {\r
-                       url.append(":").append(serverPort);\r
+       private List<String> getEndPoints() throws MalformedObjectNameException, NullPointerException, UnknownHostException,\r
+                       AttributeNotFoundException, InstanceNotFoundException, MBeanException, ReflectionException {\r
+               MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();\r
+               Set<ObjectName> objs = mbs.queryNames(new ObjectName("*:type=Connector,*"),\r
+                               Query.match(Query.attr("protocol"), Query.value("HTTP/1.1")));\r
+               List<String> endPoints = new ArrayList<String>();\r
+               for (Iterator<ObjectName> i = objs.iterator(); i.hasNext();) {\r
+                       ObjectName obj = i.next();\r
+                       String scheme = mbs.getAttribute(obj, "scheme").toString();\r
+                       String port = obj.getKeyProperty("port");\r
+                       String hostname = InetAddress.getLocalHost().getHostName();\r
+                       endPoints.add(scheme + "://" + hostname + ":" + port);\r
                }\r
+               return endPoints;\r
+       }\r
 \r
-               url.append(contextPath).append(servletPath);\r
-\r
-               if (pathInfo != null) {\r
-                       url.append(pathInfo);\r
+       @Override\r
+       protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {\r
+               /**\r
+                * // PROBLEM: the code tries to test not WS endpoints on an internal\r
+                * Tomcat, but on an external // endpoints with wrong info on the\r
+                * proxing StringBuffer jabawspath = req.getRequestURL(); jabawspath =\r
+                * jabawspath.delete(jabawspath.lastIndexOf("/"), jabawspath.length());\r
+                * String serverPath = jabawspath.toString();\r
+                */\r
+               List<String> eps = new ArrayList<String>();\r
+               try {\r
+                       eps = getEndPoints();\r
+               } catch (MalformedObjectNameException e) {\r
+                       // TODO Auto-generated catch block\r
+                       e.printStackTrace();\r
+               } catch (AttributeNotFoundException e) {\r
+                       // TODO Auto-generated catch block\r
+                       e.printStackTrace();\r
+               } catch (InstanceNotFoundException e) {\r
+                       // TODO Auto-generated catch block\r
+                       e.printStackTrace();\r
+               } catch (NullPointerException e) {\r
+                       // TODO Auto-generated catch block\r
+                       e.printStackTrace();\r
+               } catch (MBeanException e) {\r
+                       // TODO Auto-generated catch block\r
+                       e.printStackTrace();\r
+               } catch (ReflectionException e) {\r
+                       // TODO Auto-generated catch block\r
+                       e.printStackTrace();\r
                }\r
-               if (queryString != null) {\r
-                       url.append("?").append(queryString);\r
+               if (1 != eps.size()) {\r
+                       log.info(eps.size() + "EndPoints found");\r
+                       //System.out.println("      " + eps.size() + " EndPoints found");\r
+                       for (String endpoint : eps) {\r
+                               log.info(eps.size() + "EndPoint is " + endpoint);\r
+                               //System.out.println("   EndPoint is " + endpoint);\r
+                       }\r
                }\r
-               return url.toString();\r
-       }\r
 \r
-       @Override\r
-       protected void doGet(HttpServletRequest req, HttpServletResponse resp)\r
-                       throws ServletException, IOException {\r
-               StringBuffer jabawspath = req.getRequestURL();\r
-               jabawspath = jabawspath.delete(jabawspath.lastIndexOf("/"), jabawspath.length());\r
-               String serverPath = jabawspath.toString();\r
-/**\r
-               String host = java.net.InetAddress.getLocalHost().getCanonicalHostName();\r
-               System.out.println("Testing services at " + req.getRequestURL()); \r
-               System.out.println("Really: Testing services at " + serverPath); \r
-               System.out.println("      serverPath2 is " + getURL(req)); \r
-               System.out.println("      Context Path is " + req.getContextPath()); \r
-               System.out.println("      Host is " + host); \r
-               System.out.println("      Request URI is " + req.getRequestURI());\r
-               System.out.println("      Servlet Path is " + req.getServletPath());\r
-               System.out.println("      Path Info is " + req.getPathInfo());\r
-               System.out.println("      Local Port is " + req.getLocalPort());\r
-               System.out.println("      Remote Port is " + req.getRemotePort());\r
-               System.out.println("      Local address is " + req.getLocalAddr());\r
-               System.out.println("      Remote address is " + req.getRemoteAddr());\r
-               @SuppressWarnings("unchecked")\r
-               List<String> list = Collections.list(req.getHeaderNames());\r
-               System.out.println("      HTTP header names are " + list.toString());\r
-               for (String hdr : list) {\r
-                       System.out.println("      " + hdr + " header is " + req.getHeader(hdr));\r
+               String serverPath = new String();\r
+               for (String endpoint : eps) {\r
+                       serverPath = endpoint + req.getContextPath();\r
                }\r
-*/\r
+               //System.out.println("Testing services at " + serverPath);\r
+\r
                List<ServiceTestResult> testResults = new ArrayList<ServiceTestResult>();\r
                long startTime = System.nanoTime();\r
                for (Services service : Services.values()) {\r