Skip to content

Commit

Permalink
refactor adding one method to UIUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
mgaido91 committed Jun 27, 2018
1 parent 8b26a53 commit 9934d47
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ import org.eclipse.jetty.server.handler.ContextHandler
import org.eclipse.jetty.servlet.{ServletContextHandler, ServletHolder}
import org.glassfish.jersey.server.ServerProperties
import org.glassfish.jersey.servlet.ServletContainer

import org.apache.spark.SecurityManager
import org.apache.spark.ui.SparkUI
import org.apache.spark.ui.{SparkUI, UIUtils}

/**
* Main entry point for serving spark application metrics as json, using JAX-RS.
Expand Down Expand Up @@ -148,34 +147,19 @@ private[v1] trait BaseAppResource extends ApiRequestContext {
}

private[v1] class ForbiddenException(msg: String) extends WebApplicationException(
Response.status(Response.Status.FORBIDDEN).entity(msg).`type`(MediaType.TEXT_PLAIN).build())
UIUtils.buildErrorResponse(Response.Status.FORBIDDEN, msg))

private[v1] class NotFoundException(msg: String) extends WebApplicationException(
new NoSuchElementException(msg),
Response
.status(Response.Status.NOT_FOUND)
.entity(msg)
.`type`(MediaType.TEXT_PLAIN)
.build()
)
new NoSuchElementException(msg),
UIUtils.buildErrorResponse(Response.Status.NOT_FOUND, msg))

private[v1] class ServiceUnavailable(msg: String) extends WebApplicationException(
new ServiceUnavailableException(msg),
Response
.status(Response.Status.SERVICE_UNAVAILABLE)
.entity(msg)
.`type`(MediaType.TEXT_PLAIN)
.build()
)
new ServiceUnavailableException(msg),
UIUtils.buildErrorResponse(Response.Status.SERVICE_UNAVAILABLE, msg))

private[v1] class BadParameterException(msg: String) extends WebApplicationException(
new IllegalArgumentException(msg),
Response
.status(Response.Status.BAD_REQUEST)
.entity(msg)
.`type`(MediaType.TEXT_PLAIN)
.build()
) {
new IllegalArgumentException(msg),
UIUtils.buildErrorResponse(Response.Status.BAD_REQUEST, msg)) {
def this(param: String, exp: String, actual: String) = {
this(raw"""Bad value for parameter "$param". Expected a $exp, got "$actual"""")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,8 @@ private[v1] class AbstractApplicationResource extends BaseAppResource {
.build()
} catch {
case NonFatal(_) =>
Response.serverError()
.entity(s"Event logs are not available for app: $appId.")
.`type`(MediaType.TEXT_PLAIN)
.status(Response.Status.SERVICE_UNAVAILABLE)
.build()
UIUtils.buildErrorResponse(Response.Status.SERVICE_UNAVAILABLE,
s"Event logs are not available for app: $appId.")
}
}

Expand Down
5 changes: 5 additions & 0 deletions core/src/main/scala/org/apache/spark/ui/UIUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import java.net.URLDecoder
import java.text.SimpleDateFormat
import java.util.{Date, Locale, TimeZone}
import javax.servlet.http.HttpServletRequest
import javax.ws.rs.core.{MediaType, Response}

import scala.util.control.NonFatal
import scala.xml._
Expand Down Expand Up @@ -566,4 +567,8 @@ private[spark] object UIUtils extends Logging {
NEWLINE_AND_SINGLE_QUOTE_REGEX.replaceAllIn(requestParameter, ""))
}
}

def buildErrorResponse(status: Response.Status, msg: String): Response = {
Response.status(Response.Status.FORBIDDEN).entity(msg).`type`(MediaType.TEXT_PLAIN).build()
}
}

0 comments on commit 9934d47

Please sign in to comment.