Github user andrewor14 commented on a diff in the pull request:

    https://github.com/apache/spark/pull/4435#discussion_r24285568
  
    --- Diff: 
core/src/main/scala/org/apache/spark/status/StatusJsonHandler.scala ---
    @@ -0,0 +1,168 @@
    +/*
    + * 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
    + *
    + *    http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * 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.spark.status
    +
    +import javax.servlet.http.{HttpServletResponse, HttpServlet, 
HttpServletRequest}
    +
    +import com.fasterxml.jackson.annotation.JsonInclude
    +import com.fasterxml.jackson.databind.{SerializationFeature, ObjectMapper}
    +import org.apache.spark.status.api.ApplicationInfo
    +import org.apache.spark.ui.SparkUI
    +import org.apache.spark.ui.exec.ExecutorsJsonRoute
    +import org.apache.spark.ui.jobs.{AllJobsJsonRoute, OneStageJsonRoute, 
AllStagesJsonRoute}
    +import org.apache.spark.ui.storage.{AllRDDJsonRoute, RDDJsonRoute}
    +import org.eclipse.jetty.servlet.{ServletHolder, ServletContextHandler}
    +
    +import scala.util.matching.Regex
    +
    +import org.apache.spark.{Logging, SecurityManager}
    +import org.apache.spark.deploy.history.{OneApplicationJsonRoute, 
AllApplicationsJsonRoute}
    +
    +
    +/**
    + * get the response for one endpoint in the json status api.
    + *
    + * Implementations only need to return the objects that are to be 
converted to json -- the framework
    + * will convert to json via jackson
    + */
    +private[spark] trait StatusJsonRoute[T] {
    +  def renderJson(request: HttpServletRequest): T
    +}
    +
    +private[spark] class JsonRequestHandler(uiRoot: UIRoot, securityManager: 
SecurityManager) extends Logging {
    +  def route(req: HttpServletRequest) : Option[StatusJsonRoute[_]] = {
    +    specs.collectFirst { case (pattern, route) if 
pattern.pattern.matcher(req.getPathInfo()).matches() =>
    +      route
    +    }
    +  }
    +
    +  private val noSlash = """[^/]"""
    +
    +  private val specs: IndexedSeq[(Regex, StatusJsonRoute[_])] = IndexedSeq(
    +    "/applications/?".r -> new AllApplicationsJsonRoute(uiRoot),
    +    s"/applications/$noSlash+/?".r -> new OneApplicationJsonRoute(uiRoot),
    +    s"/applications/$noSlash+/jobs/?".r -> new AllJobsJsonRoute(this),
    +    s"/applications/$noSlash+/executors/?".r -> new 
ExecutorsJsonRoute(this),
    +    s"/applications/$noSlash+/stages/?".r -> new AllStagesJsonRoute(this),
    +    s"/applications/$noSlash+/stages/$noSlash+/?".r -> new 
OneStageJsonRoute(this),
    +    s"/applications/$noSlash+/storage/rdd/?".r -> new 
AllRDDJsonRoute(this),
    +    s"/applications/$noSlash+/storage/rdd/$noSlash+/?".r -> new 
RDDJsonRoute(this)
    +  )
    +
    +  private val jsonMapper = {
    +    val t = new ObjectMapper()
    +    t.registerModule(com.fasterxml.jackson.module.scala.DefaultScalaModule)
    +    t.enable(SerializationFeature.INDENT_OUTPUT)
    +    t.setSerializationInclusion(JsonInclude.Include.NON_NULL)
    +    t
    +  }
    +
    +  val jsonContextHandler = {
    +
    +    //TODO throw out all the JettyUtils stuff, so I can set the response 
status code, etc.
    +    val servlet = new HttpServlet {
    +      override def doGet(request: HttpServletRequest, response: 
HttpServletResponse) {
    +        if (securityManager.checkUIViewPermissions(request.getRemoteUser)) 
{
    +          response.setContentType("text/json;charset=utf-8")
    +          route(request) match {
    +            case Some(jsonRoute) =>
    +              response.setHeader("Cache-Control", "no-cache, no-store, 
must-revalidate")
    +              try {
    +                val responseObj = jsonRoute.renderJson(request)
    +                val result = jsonMapper.writeValueAsString(responseObj)
    +                response.setStatus(HttpServletResponse.SC_OK)
    +                response.getWriter.println(result)
    +              } catch {
    +                case iae: IllegalArgumentException =>
    +                  response.setStatus(HttpServletResponse.SC_BAD_REQUEST)
    +                  response.getOutputStream.print(iae.getMessage())
    +              }
    +            case None =>
    +              println("no match for path: " + request.getPathInfo)
    +              response.setStatus(HttpServletResponse.SC_NOT_FOUND)
    +          }
    +        } else {
    +          response.setStatus(HttpServletResponse.SC_UNAUTHORIZED)
    +          response.setHeader("Cache-Control", "no-cache, no-store, 
must-revalidate")
    +          response.sendError(HttpServletResponse.SC_UNAUTHORIZED,
    +            "User is not authorized to access this page.")
    +        }
    +      }
    +    }
    +    val path = "/json/v1"
    +    val contextHandler = new ServletContextHandler
    +    val holder = new ServletHolder(servlet)
    +    contextHandler.setContextPath(path)
    +    contextHandler.addServlet(holder, "/*")
    +    contextHandler
    +  }
    --- End diff --
    
    would be a little easier to read if you put this into a class on its own


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to