Browse Source

LIVY-357. Web UI: Updated staticResourceServlet to return 404s and not throw WARNs

PR moved from old repo https://github.com/cloudera/livy/pull/339

[LIVY-357](https://issues.cloudera.org/browse/LIVY-357)

Since https://github.com/cloudera/livy/pull/319 added the UI whenever a static resource is accessed a `MimeException` is thrown as a `WARN`. This was caused because a `InputStream` must be wrapped in a `BufferedInputStream` for a full implementation.

I updated `staticResourceServlet` with the wrapper as well as adding a `404 File not found` return when a file does not exist (currently an empty file is returned in such a case)

Tested manually

Author: Alex Bozarth <ajbozart@us.ibm.com>

Closes #2 from ajbozarth/ui-error.
Alex Bozarth 7 years ago
parent
commit
257ccdfd69
1 changed files with 14 additions and 2 deletions
  1. 14 2
      server/src/main/scala/com/cloudera/livy/server/LivyServer.scala

+ 14 - 2
server/src/main/scala/com/cloudera/livy/server/LivyServer.scala

@@ -18,6 +18,7 @@
 
 package com.cloudera.livy.server
 
+import java.io.{BufferedInputStream, InputStream}
 import java.util.concurrent._
 import java.util.EnumSet
 import javax.servlet._
@@ -28,9 +29,9 @@ import scala.concurrent.Future
 import org.apache.hadoop.security.{SecurityUtil, UserGroupInformation}
 import org.apache.hadoop.security.authentication.server._
 import org.eclipse.jetty.servlet.FilterHolder
+import org.scalatra.{NotFound, ScalatraServlet}
 import org.scalatra.metrics.MetricsBootstrap
 import org.scalatra.metrics.MetricsSupportExtensions._
-import org.scalatra.ScalatraServlet
 import org.scalatra.servlet.{MultipartConfig, ServletApiImplicits}
 
 import com.cloudera.livy._
@@ -146,9 +147,20 @@ class LivyServer extends Logging {
 
     // Servlet for hosting static files such as html, css, and js
     // Necessary since Jetty cannot set it's resource base inside a jar
+    // Returns 404 if the file does not exist
     val staticResourceServlet = new ScalatraServlet {
       get("/*") {
-        getClass.getResourceAsStream("ui/static/" + params("splat"))
+        val fileName = params("splat")
+        val notFoundMsg = "File not found"
+
+        if (!fileName.isEmpty) {
+          getClass.getResourceAsStream(s"ui/static/$fileName") match {
+            case is: InputStream => new BufferedInputStream(is)
+            case null => NotFound(notFoundMsg)
+          }
+        } else {
+          NotFound(notFoundMsg)
+        }
       }
     }