1717
1818package org .apache .spark .status
1919
20- import javax .servlet .http .HttpServletRequest
20+ import javax .servlet .http .{ HttpServletResponse , HttpServlet , HttpServletRequest }
2121
2222import com .fasterxml .jackson .annotation .JsonInclude
2323import com .fasterxml .jackson .databind .{SerializationFeature , ObjectMapper }
@@ -31,8 +31,7 @@ import org.eclipse.jetty.servlet.{ServletHolder, ServletContextHandler}
3131import scala .util .matching .Regex
3232
3333import org .apache .spark .{Logging , SecurityManager }
34- import org .apache .spark .deploy .history .{OneApplicationJsonRoute , HistoryServer , AllApplicationsJsonRoute }
35- import org .apache .spark .ui .JettyUtils ._
34+ import org .apache .spark .deploy .history .{OneApplicationJsonRoute , AllApplicationsJsonRoute }
3635
3736
3837/**
@@ -60,9 +59,9 @@ private[spark] class JsonRequestHandler(uiRoot: UIRoot, securityManager: Securit
6059 s " /applications/ $noSlash+/jobs/? " .r -> new AllJobsJsonRoute (this ),
6160 s " /applications/ $noSlash+/executors/? " .r -> new ExecutorsJsonRoute (this ),
6261 s " /applications/ $noSlash+/stages/? " .r -> new AllStagesJsonRoute (this ),
63- s " /applications/ $noSlash+/stages/ \\ d +/?" .r -> new OneStageJsonRoute (this ),
62+ s " /applications/ $noSlash+/stages/ $noSlash +/? " .r -> new OneStageJsonRoute (this ),
6463 s " /applications/ $noSlash+/storage/rdd/? " .r -> new AllRDDJsonRoute (this ),
65- s " /applications/ $noSlash+/storage/rdd/ \\ d +/?" .r -> new RDDJsonRoute (this )
64+ s " /applications/ $noSlash+/storage/rdd/ $noSlash +/? " .r -> new RDDJsonRoute (this )
6665 )
6766
6867 private val jsonMapper = {
@@ -76,17 +75,36 @@ private[spark] class JsonRequestHandler(uiRoot: UIRoot, securityManager: Securit
7675 val jsonContextHandler = {
7776
7877 // TODO throw out all the JettyUtils stuff, so I can set the response status code, etc.
79- val params = new ServletParams (
80- {
81- (request : HttpServletRequest ) =>
82- route(request).map{jsonRoute =>
83- logInfo(" handling route: " + request.getPathInfo)
84- val responseObj = jsonRoute.renderJson(request)
85- jsonMapper.writeValueAsString(responseObj)
86- }.getOrElse(throw new IllegalArgumentException (" unmatched route" ))
87- }, " text/json" )
78+ val servlet = new HttpServlet {
79+ override def doGet (request : HttpServletRequest , response : HttpServletResponse ) {
80+ if (securityManager.checkUIViewPermissions(request.getRemoteUser)) {
81+ response.setContentType(" text/json;charset=utf-8" )
82+ route(request) match {
83+ case Some (jsonRoute) =>
84+ response.setHeader(" Cache-Control" , " no-cache, no-store, must-revalidate" )
85+ try {
86+ val responseObj = jsonRoute.renderJson(request)
87+ val result = jsonMapper.writeValueAsString(responseObj)
88+ response.setStatus(HttpServletResponse .SC_OK )
89+ response.getWriter.println(result)
90+ } catch {
91+ case iae : IllegalArgumentException =>
92+ response.setStatus(HttpServletResponse .SC_BAD_REQUEST )
93+ response.getOutputStream.print(iae.getMessage())
94+ }
95+ case None =>
96+ println(" no match for path: " + request.getPathInfo)
97+ response.setStatus(HttpServletResponse .SC_NOT_FOUND )
98+ }
99+ } else {
100+ response.setStatus(HttpServletResponse .SC_UNAUTHORIZED )
101+ response.setHeader(" Cache-Control" , " no-cache, no-store, must-revalidate" )
102+ response.sendError(HttpServletResponse .SC_UNAUTHORIZED ,
103+ " User is not authorized to access this page." )
104+ }
105+ }
106+ }
88107 val path = " /json/v1"
89- val servlet = createServlet(params, securityManager)
90108 val contextHandler = new ServletContextHandler
91109 val holder = new ServletHolder (servlet)
92110 contextHandler.setContextPath(path)
@@ -148,4 +166,3 @@ object RouteUtils {
148166 }
149167 }
150168}
151-
0 commit comments