瀏覽代碼

AMBARI-16957. Zeppelin views : UI broken when service is not running (Renjith Kamath via pallavkul)

Pallav Kulshreshtha 9 年之前
父節點
當前提交
7be6dc4d9d

+ 12 - 0
contrib/views/zeppelin/pom.xml

@@ -86,6 +86,18 @@
       <artifactId>ambari-views-utils</artifactId>
       <version>2.4.0.0.0</version>
     </dependency>
+    <dependency>
+      <groupId>org.json</groupId>
+      <artifactId>json</artifactId>
+      <version>20160212</version>
+    </dependency>
+
+    <dependency>
+      <groupId>org.apache.httpcomponents</groupId>
+      <artifactId>httpclient</artifactId>
+      <version>4.3.6</version>
+    </dependency>
+
 
   </dependencies>
 

+ 64 - 0
contrib/views/zeppelin/src/main/java/org/apache/ambari/view/zeppelin/ZeppelinServiceCheck.java

@@ -0,0 +1,64 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ambari.view.zeppelin;
+
+import org.apache.http.HttpResponse;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.impl.client.HttpClientBuilder;
+import org.json.JSONObject;
+
+import javax.servlet.ServletConfig;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.io.PrintWriter;
+
+
+public class ZeppelinServiceCheck extends HttpServlet {
+    @Override
+    public void init(ServletConfig config) throws ServletException {
+        super.init(config);
+    }
+
+    @Override
+    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
+        String url = "http:" + request.getParameter("url");
+        PrintWriter printwriter = response.getWriter();
+        JSONObject json = new JSONObject();
+        response.setContentType("application/json");
+        response.setStatus(HttpServletResponse.SC_OK);
+
+        try {
+            HttpClient httpclient = HttpClientBuilder.create().build();
+            HttpGet httpget = new HttpGet(url);
+            HttpResponse httpresponse = httpclient.execute(httpget);
+            if (httpresponse.getStatusLine().getStatusCode() == 200) {
+                json.put("status", "SUCCESS");
+                json.put("message", "Zeppelin is running");
+            }
+        } catch (Exception e) {
+            json.put("status", "ERROR");
+            json.put("message", "Zeppelin is not running");
+        }
+        printwriter.print(json.toString());
+    }
+}

+ 33 - 5
contrib/views/zeppelin/src/main/resources/WEB-INF/index.jsp

@@ -19,16 +19,29 @@
 <html lang="en">
 <head>
     <meta charset="utf-8"/>
+    <link rel="stylesheet" href="/stylesheets/vendor.css">
 </head>
 <body>
 
+<div class="container-fluid" id="messageContainer" style="display:none;">
+    <h1>Welcome to the Zeppelin View</h1>
+    <h3>Service check failed</h3>
+
+    <table class="table">
+        <tbody>
+        <tr>
+            <td>zeppelin service is not running</td>
+        </tr>
+        </tbody>
+    </table>
+
+</div>
+
 <iframe id='zeppelinIFrame' width="100%" seamless="seamless" style="border: 0px;"></iframe>
 <script>
     var $ = jQuery = parent.jQuery;
     var iframe = document.querySelector('#zeppelinIFrame');
     var port = "${port}";
-    var publicName = "${publicname}";
-
 
     $.getJSON('/api/v1/clusters', function (data) {
         $.getJSON('/api/v1/clusters/' +
@@ -39,8 +52,7 @@
                         for (var j = 0; j < data['items'][i]['host_components'].length; j++) {
                             if (data['items'][i]['host_components'][j]['HostRoles']['component_name'] == 'ZEPPELIN_MASTER') {
                                 var url = '//' + data['items'][i]['host_components'][j]['HostRoles']['host_name'] + ':' + port;
-                                iframe.src = url;
-                                iframe.height = window.innerHeight;
+                                validateAndLoadZeppelinUI(iframe, url);
                                 return;
                             }
                         }
@@ -51,6 +63,22 @@
     $(window).resize(function () {
         iframe.height = window.innerHeight;
     });
+
+    function validateAndLoadZeppelinUI(iframe, url) {
+        $.get(location.href + "zeppelin-service-check?url=" + url, function (response) {
+            if (response.status === "SUCCESS") {
+                messageContainer.style.display = "none";
+                iframe.style.display = "block";
+                iframe.src = url;
+                iframe.height = window.innerHeight;
+            } else {
+                messageContainer.style.display = "block";
+                iframe.style.display = "none";
+            }
+        });
+
+
+    }
 </script>
 </body>
-</html>
+</html>

+ 12 - 1
contrib/views/zeppelin/src/main/resources/WEB-INF/web.xml

@@ -26,6 +26,16 @@ limitations under the License. Kerberos, LDAP, Custom. Binary/Htt
   <description>
     This is the Zeppelin view servlet application.
   </description>
+  <servlet>
+    <servlet-name>ZeppelinServiceCheck</servlet-name>
+    <servlet-class>org.apache.ambari.view.zeppelin.ZeppelinServiceCheck</servlet-class>
+  </servlet>
+
+  <servlet-mapping>
+    <servlet-name>ZeppelinServiceCheck</servlet-name>
+    <url-pattern>/zeppelin-service-check</url-pattern>
+  </servlet-mapping>
+
   <servlet>
     <servlet-name>ZeppelinServlet</servlet-name>
     <servlet-class>org.apache.ambari.view.zeppelin.ZeppelinServlet</servlet-class>
@@ -35,4 +45,5 @@ limitations under the License. Kerberos, LDAP, Custom. Binary/Htt
     <servlet-name>ZeppelinServlet</servlet-name>
     <url-pattern>/</url-pattern>
   </servlet-mapping>
-</web-app>
+
+</web-app>