javax.servlet.http.HttpServletResponse Scala Examples

The following examples show how to use javax.servlet.http.HttpServletResponse. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
Example 1
Source File: BuildSettingListPage.scala    From teamcity-slack   with MIT License 5 votes vote down vote up
package com.fpd.teamcity.slack.pages

import javax.servlet.http.{HttpServletRequest, HttpServletResponse}

import com.fpd.teamcity.slack.Helpers.Implicits._
import com.fpd.teamcity.slack.controllers.SlackController
import com.fpd.teamcity.slack.{ConfigManager, PermissionManager, Resources}
import jetbrains.buildServer.controllers.BaseController
import jetbrains.buildServer.serverSide.ProjectManager
import jetbrains.buildServer.web.openapi.{PluginDescriptor, WebControllerManager}
import org.springframework.web.servlet.ModelAndView

import scala.collection.JavaConverters._

class BuildSettingListPage(controllerManager: WebControllerManager,
                           descriptor: PluginDescriptor,
                           config: ConfigManager,
                           val permissionManager: PermissionManager,
                           projectManager: ProjectManager
                          ) extends BaseController with SlackController {
  controllerManager.registerController(Resources.buildSettingList.controllerUrl, this)

  override def handle(request: HttpServletRequest, response: HttpServletResponse): ModelAndView = {
    val view = descriptor.getPluginResourcesPath(Resources.buildSettingList.view)
    new ModelAndView(view, Map("list" → config.buildSettingList(request.param("buildTypeId").get).asJava).asJava)
  }

  override protected def checkPermission(request: HttpServletRequest): Boolean =
    request.param("buildTypeId").exists(id ⇒ permissionManager.buildAccessPermitted(request, id))
} 
Example 2
Source File: S3ConfigController.scala    From teamcity-s3-plugin   with Apache License 2.0 5 votes vote down vote up
package com.gu.teamcity

import jetbrains.buildServer.controllers.MultipartFormController
import jetbrains.buildServer.web.openapi.WebControllerManager
import org.jetbrains.annotations.NotNull
import org.springframework.web.servlet.ModelAndView
import org.springframework.web.servlet.view.RedirectView
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

class S3ConfigController(config: S3ConfigManager, webControllerManager: WebControllerManager) extends MultipartFormController {
  webControllerManager.registerController("/app/s3/**", this)

  protected def doPost(request: HttpServletRequest, response: HttpServletResponse): ModelAndView = {
    def param(name: String) = S3ConfigController.emptyAsNone(request.getParameter(name))

    config.updateAndPersist(S3Config(
      param("artifactBucket"), param("buildManifestBucket"), param("tagManifestBucket"),
      param("accessKey"), param("secretKey")
    ))

    new ModelAndView(new RedirectView("/admin/admin.html?item=S3"))
  }
}

object S3ConfigController {
  def emptyAsNone(s: String): Option[String] = Option(s).filterNot(_.trim.isEmpty)
} 
Example 3
Source File: YarnProxyRedirectFilter.scala    From Spark-2.3.1   with Apache License 2.0 5 votes vote down vote up
package org.apache.spark.deploy.yarn

import javax.servlet._
import javax.servlet.http.{HttpServletRequest, HttpServletResponse}

import org.apache.spark.internal.Logging


class YarnProxyRedirectFilter extends Filter with Logging {

  import YarnProxyRedirectFilter._

  override def destroy(): Unit = { }

  override def init(config: FilterConfig): Unit = { }

  override def doFilter(req: ServletRequest, res: ServletResponse, chain: FilterChain): Unit = {
    val hreq = req.asInstanceOf[HttpServletRequest]

    // The YARN proxy will send a request with the "proxy-user" cookie set to the YARN's client
    // user name. We don't expect any other clients to set this cookie, since the SHS does not
    // use cookies for anything.
    Option(hreq.getCookies()).flatMap(_.find(_.getName() == COOKIE_NAME)) match {
      case Some(_) =>
        doRedirect(hreq, res.asInstanceOf[HttpServletResponse])

      case _ =>
        chain.doFilter(req, res)
    }
  }

  private def doRedirect(req: HttpServletRequest, res: HttpServletResponse): Unit = {
    val redirect = req.getRequestURL().toString()

    // Need a client-side redirect instead of an HTTP one, otherwise the YARN proxy itself
    // will handle the redirect and get into an infinite loop.
    val content = s"""
      |<html xmlns="http://www.w3.org/1999/xhtml">
      |<head>
      |  <title>Spark History Server Redirect</title>
      |  <meta http-equiv="refresh" content="0;URL='$redirect'" />
      |</head>
      |<body>
      |  <p>The requested page can be found at: <a href="$redirect">$redirect</a>.</p>
      |</body>
      |</html>
      """.stripMargin

    logDebug(s"Redirecting YARN proxy request to $redirect.")
    res.setStatus(HttpServletResponse.SC_OK)
    res.setContentType("text/html")
    res.getWriter().write(content)
  }

}

private[spark] object YarnProxyRedirectFilter {
  val COOKIE_NAME = "proxy-user"
} 
Example 4
Source File: StubServer.scala    From graphcool-framework   with Apache License 2.0 5 votes vote down vote up
package cool.graph.stub

import javax.servlet.http.{HttpServletRequest, HttpServletResponse}

import cool.graph.stub.StubMatching.MatchResult
import org.eclipse.jetty.server.handler.AbstractHandler
import org.eclipse.jetty.server.{Server, Request => JettyRequest}

import scala.collection.mutable

case class StubServer(stubs: List[Stub], port: Int, stubNotFoundStatusCode: Int) {
  org.eclipse.jetty.util.log.Log.setLog(new JustWarningsLogger)
  val server        = new Server(port)
  def createHandler = StubServerHandler(stubs, stubNotFoundStatusCode)

  def start: Unit = server.start
  def stop: Unit  = server.stop

  def requests                      = handler.requests
  def lastRequest                   = handler.requests.head
  def lastRequest(path: String)     = handler.requests.filter(_.path == path).head
  def requestCount(stub: Stub): Int = handler.requestCount(stub)
  val handler                       = createHandler
  server.setHandler(handler)
}

case class StubServerHandler(stubs: List[Stub], stubNotFoundStatusCode: Int) extends AbstractHandler {
  var requests: List[StubRequest] = List()

  def handle(target: String, baseRequest: JettyRequest, servletRequest: HttpServletRequest, response: HttpServletResponse): Unit = {
    val stubResponse = try {
      val stubRequest = StubRequest.fromHttpRequest(servletRequest)
      requests = stubRequest :: requests
      stubResponseForRequest(stubRequest)
    } catch {
      case e: Throwable => failedResponse(e)
    }
    response.setContentType("application/json")
    response.setStatus(stubResponse.status)
    stubResponse.headers.foreach(kv => response.setHeader(kv._1, kv._2))
    response.getWriter.print(stubResponse.body)
    baseRequest.setHandled(true)
  }

  def stubResponseForRequest(stubRequest: StubRequest): StaticStubResponse = {
    val matches      = StubMatching.matchStubs(stubRequest, stubs)
    val topCandidate = matches.find(_.isMatch)
    topCandidate match {
      case Some(result) =>
        recordStubHit(result.stub)
        result.stub.stubbedResponse.getStaticStubResponse(stubRequest)
      case None =>
        noMatchResponse(stubRequest, matches)
    }
  }

  def failedResponse(e: Throwable) = {
    e.printStackTrace()
    StaticStubResponse(stubNotFoundStatusCode, s"Stub Matching failed with the following exception: ${e.toString}")
  }

  def noMatchResponse(request: StubRequest, notMatches: List[MatchResult]) = {
    val queryString = request.queryMap.map { case (k, v) => s"$k=$v" }.foldLeft("?") { case (acc, x) => s"$acc&$x" }
    val noMatchReasons = if (stubs.isEmpty) {
      """ "There are no registered stubs in the server!" """
    } else {
      notMatches.map(x => s""" "${x.noMatchMessage}" """).mkString(",\n")
    }
    val responseJson = {
      s"""{
         | "message": "No stub found for request [URL: ${request.path}$queryString] [METHOD: ${request.httpMethod}}] [BODY: ${request.body}]",
         | "noMatchReasons" : [
         |  $noMatchReasons
         | ]
         |}""".stripMargin
    }
    StaticStubResponse(stubNotFoundStatusCode, responseJson)
  }

  def requestCount(stub: Stub): Int = requestCountMap.getOrElse(stub, 0)

  private def recordStubHit(stub: Stub): Unit = {
    val numberOfRequests = requestCountMap.getOrElse(stub, 0)
    requestCountMap.update(stub, numberOfRequests + 1)
  }
  private val requestCountMap: mutable.Map[Stub, Int] = mutable.Map.empty
} 
Example 5
Source File: SlackController.scala    From teamcity-slack   with MIT License 5 votes vote down vote up
package com.fpd.teamcity.slack.controllers

import javax.servlet.http.{HttpServletRequest, HttpServletResponse}

import com.fpd.teamcity.slack.{PermissionManager, Resources}
import jetbrains.buildServer.controllers.{BaseController, SimpleView}
import jetbrains.buildServer.web.openapi.PluginDescriptor
import org.springframework.web.servlet.ModelAndView

trait SlackController extends BaseController {
  protected val permissionManager: PermissionManager

  def handle(request: HttpServletRequest, response: HttpServletResponse): ModelAndView

  override def doHandle(request: HttpServletRequest, response: HttpServletResponse): ModelAndView =
    if (checkPermission(request))
      handle(request, response)
    else
      SimpleView.createTextView("Access denied")

  def ajaxView(message: String)(implicit descriptor: PluginDescriptor): ModelAndView = {
    val modelAndView = new ModelAndView(descriptor.getPluginResourcesPath(Resources.ajaxView.view))
    modelAndView.getModel.put("message", message)
    modelAndView
  }

  protected def checkPermission(request: HttpServletRequest): Boolean = permissionManager.accessPermitted(request)
} 
Example 6
Source File: BuildSettingsSave.scala    From teamcity-slack   with MIT License 5 votes vote down vote up
package com.fpd.teamcity.slack.controllers

import javax.servlet.http.{HttpServletRequest, HttpServletResponse}

import com.fpd.teamcity.slack.ConfigManager.{BuildSetting, BuildSettingFlag}
import com.fpd.teamcity.slack.Helpers.Implicits._
import com.fpd.teamcity.slack.Strings.BuildSettingsController._
import com.fpd.teamcity.slack.{ConfigManager, PermissionManager, Resources, SlackGateway}
import jetbrains.buildServer.web.openapi.{PluginDescriptor, WebControllerManager}
import org.springframework.web.servlet.ModelAndView

import scala.util.{Failure, Success, Try}

class BuildSettingsSave(val configManager: ConfigManager,
                        controllerManager: WebControllerManager,
                        slackGateway: SlackGateway,
                        val permissionManager: PermissionManager,
                        implicit val descriptor: PluginDescriptor
                       )
  extends SlackController {

  controllerManager.registerController(Resources.buildSettingSave.controllerUrl, this)

  override def handle(request: HttpServletRequest, response: HttpServletResponse): ModelAndView =
    ajaxView(handleSave(request))

  def handleSave(request: HttpServletRequest): String = {
    def flags = {
      val keyToFlag = Map(
        "success" → BuildSettingFlag.success,
        "failureToSuccess" → BuildSettingFlag.failureToSuccess,
        "fail" → BuildSettingFlag.failure,
        "successToFailure" → BuildSettingFlag.successToFailure,
        "started" → BuildSettingFlag.started,
        "canceled" → BuildSettingFlag.canceled,
        "queued" → BuildSettingFlag.queued
      )
      val keys = keyToFlag.keys.filter(key ⇒ request.param(key).isDefined)
      keys.map(keyToFlag).toSet
    }

    val result = for {
    // preparing params
      branch ← request.param("branchMask")
      buildId ← request.param("buildTypeId")
      message ← request.param("messageTemplate")

      config ← configManager.config
    } yield {
      lazy val artifactsMask = request.param("artifactsMask")
      val channel = request.param("slackChannel")
      val notifyCommitter = request.param("notifyCommitter").isDefined
      val maxVcsChanges = request.param("maxVcsChanges").getOrElse(BuildSetting.defaultMaxVCSChanges.toString).toInt

      // store build setting
      def updateConfig() = configManager.updateBuildSetting(
        BuildSetting(buildId, branch, channel.getOrElse(""), message, flags, artifactsMask.getOrElse(""), request.param("deepLookup").isDefined, notifyCommitter, maxVcsChanges),
        request.param("key")
      )

      // check channel availability
      if (!channel.exists(_.nonEmpty) && !notifyCommitter) {
        channelOrNotifyCommitterError
      } else if (Try(branch.r).isFailure) {
        compileBranchMaskError
      } else if (artifactsMask.exists(s ⇒ Try(s.r).isFailure)) {
        compileArtifactsMaskError
      } else {
        slackGateway.sessionByConfig(config) match {
          case Success(session) ⇒
            if (channel.exists(s ⇒ null == session.findChannelByName(s))) {
              channelNotFoundError(channel.get)
            } else {
              updateConfig() match {
                case Some(_) ⇒ ""
                case _ ⇒ emptyConfigError
              }
            }
          case Failure(e) ⇒
            sessionByConfigError(e.getMessage)
        }
      }
    }

    result getOrElse requirementsError
  }

  override protected def checkPermission(request: HttpServletRequest): Boolean =
    request.param("buildTypeId").exists(permissionManager.buildAccessPermitted(request, _))
} 
Example 7
Source File: BuildSettingsTry.scala    From teamcity-slack   with MIT License 5 votes vote down vote up
package com.fpd.teamcity.slack.controllers

import javax.servlet.http.{HttpServletRequest, HttpServletResponse}

import com.fpd.teamcity.slack.ConfigManager.BuildSetting
import com.fpd.teamcity.slack.Helpers.Implicits._
import com.fpd.teamcity.slack.SlackGateway.{Destination, SlackChannel, SlackUser, attachmentToSlackMessage}
import com.fpd.teamcity.slack._
import jetbrains.buildServer.serverSide.{BuildHistory, SFinishedBuild}
import jetbrains.buildServer.users.SUser
import jetbrains.buildServer.web.openapi.{PluginDescriptor, WebControllerManager}
import jetbrains.buildServer.web.util.SessionUser
import org.springframework.web.servlet.ModelAndView

import scala.collection.JavaConverters._
import scala.concurrent.Await
import scala.concurrent.duration._
import scala.language.postfixOps
import scala.util.{Failure, Try}

class BuildSettingsTry(buildHistory: BuildHistory,
                       configManager: ConfigManager,
                       gateway: SlackGateway,
                       controllerManager: WebControllerManager,
                       val permissionManager: PermissionManager,
                       messageBuilderFactory: MessageBuilderFactory,
                       implicit val descriptor: PluginDescriptor
                      )
  extends SlackController {

  import BuildSettingsTry._
  import Strings.BuildSettingsTry._

  controllerManager.registerController(Resources.buildSettingTry.controllerUrl, this)

  override def handle(request: HttpServletRequest, response: HttpServletResponse): ModelAndView = Try {
    val id = request.param("id")
      .getOrElse(throw HandlerException(emptyIdParam))
    val setting = configManager.buildSetting(id)
      .getOrElse(throw HandlerException(buildSettingNotFound))
    val build = findPreviousBuild(buildHistory, setting)
      .getOrElse(throw HandlerException(previousBuildNotFound))

    detectDestination(setting, SessionUser.getUser(request)) match {
      case Some(dest) ⇒
        val future = gateway.sendMessage(dest,
          attachmentToSlackMessage(
            messageBuilderFactory.createForBuild(build).compile(setting.messageTemplate, setting),
            configManager.sendAsAttachment.exists(x ⇒ x)
          ))
        Await.result(future, 10 seconds) match {
          case Failure(error) ⇒ throw HandlerException(error.getMessage)
          case _ ⇒ messageSent(dest.toString)
        }
      case _ ⇒
        throw HandlerException(unknownDestination)
    }
  } recover { case x: HandlerException ⇒ s"Error: ${x.getMessage}" } map {
    ajaxView
  } get

  override protected def checkPermission(request: HttpServletRequest): Boolean =
    request.param("id").exists(id ⇒ permissionManager.settingAccessPermitted(request, id))
}

object BuildSettingsTry {
  def findPreviousBuild(buildHistory: BuildHistory, setting: BuildSetting): Option[SFinishedBuild] =
    buildHistory.getEntries(false).asScala.find(b ⇒ !b.isPersonal && b.matchBranch(setting.branchMask) && b.getBuildTypeId == setting.buildTypeId)

  def detectDestination(setting: BuildSetting, user: ⇒ SUser): Option[Destination] = setting.slackChannel.isEmpty match {
    case true if setting.notifyCommitter ⇒
      Some(SlackUser(user.getEmail))
    case false ⇒
      Some(SlackChannel(setting.slackChannel))
    case _ ⇒
      None
  }

  case class HandlerException(message: String) extends Exception(message)
} 
Example 8
Source File: BuildSettingsDelete.scala    From teamcity-slack   with MIT License 5 votes vote down vote up
package com.fpd.teamcity.slack.controllers

import javax.servlet.http.{HttpServletRequest, HttpServletResponse}

import com.fpd.teamcity.slack.Helpers.Implicits._
import com.fpd.teamcity.slack.{ConfigManager, PermissionManager, Resources}
import jetbrains.buildServer.web.openapi.{PluginDescriptor, WebControllerManager}
import org.springframework.web.servlet.ModelAndView

class BuildSettingsDelete(configManager: ConfigManager,
                          controllerManager: WebControllerManager,
                          val permissionManager: PermissionManager,
                          implicit val descriptor: PluginDescriptor
                         )
  extends SlackController {

  controllerManager.registerController(Resources.buildSettingDelete.controllerUrl, this)

  override def handle(request: HttpServletRequest, response: HttpServletResponse): ModelAndView = {
    val result = for {
      id ← request.param("id")
      result ← configManager.removeBuildSetting(id)
    } yield result

    ajaxView(result.filter(_ == true).map(_ ⇒ "") getOrElse "Something went wrong")
  }

  override protected def checkPermission(request: HttpServletRequest): Boolean =
    request.param("id").exists(id ⇒ permissionManager.settingAccessPermitted(request, id))
} 
Example 9
Source File: ConfigController.scala    From teamcity-slack   with MIT License 5 votes vote down vote up
package com.fpd.teamcity.slack.controllers

import java.net.URLEncoder
import javax.servlet.http.{HttpServletRequest, HttpServletResponse}

import com.fpd.teamcity.slack._
import com.fpd.teamcity.slack.Strings.ConfigController._
import jetbrains.buildServer.web.openapi.WebControllerManager
import org.springframework.web.servlet.ModelAndView

import scala.util.{Failure, Success}

class ConfigController(
                        configManager: ConfigManager,
                        controllerManager: WebControllerManager,
                        val permissionManager: PermissionManager,
                        slackGateway: SlackGateway
                      )
  extends SlackController {
  import ConfigController._
  import Helpers.Implicits._

  controllerManager.registerController(Resources.configPage.controllerUrl, this)

  override def handle(request: HttpServletRequest, response: HttpServletResponse): ModelAndView = {
    val result = for {
      oauthKey ← request.param("oauthKey")
    } yield {
      val newConfig = ConfigManager.Config(oauthKey)
      val publicUrl = request.param("publicUrl").getOrElse("")
      val senderName = request.param("senderName").getOrElse("")

      slackGateway.sessionByConfig(newConfig).map { _ ⇒
        configManager.update(
          oauthKey,
          publicUrl,
          request.param("personalEnabled").isDefined,
          request.param("enabled").isDefined,
          senderName,
          request.param("sendAsAttachment").isDefined
        )
      } match {
        case Success(true) ⇒ Left(true)
        case Success(_) ⇒ Right(oauthTokenUpdateFailed)
        case Failure(e) ⇒ Right(sessionByConfigError(e.getMessage))
      }
    }

    val either = result.getOrElse(Right(oauthKeyParamMissing))

    redirectTo(createRedirect(either, request.getContextPath), response)
  }
}

object ConfigController {
  private def createRedirect(either: Either[Boolean, String], context: String): String = either match {
    case Left(_) ⇒ s"$context/admin/admin.html?item=${Strings.tabId}"
    case Right(error) ⇒ s"$context/admin/admin.html?item=${Strings.tabId}&error=${URLEncoder.encode(error, "UTF-8")}"
  }
} 
Example 10
Source File: BuildSettingEditPage.scala    From teamcity-slack   with MIT License 5 votes vote down vote up
package com.fpd.teamcity.slack.pages

import javax.servlet.http.{HttpServletRequest, HttpServletResponse}

import com.fpd.teamcity.slack.Helpers.Implicits._
import com.fpd.teamcity.slack.controllers.SlackController
import com.fpd.teamcity.slack.{ConfigManager, SBuildMessageBuilder, PermissionManager, Resources}
import jetbrains.buildServer.web.openapi.{PluginDescriptor, WebControllerManager}
import org.springframework.web.servlet.ModelAndView

import scala.collection.JavaConverters._

class BuildSettingEditPage(controllerManager: WebControllerManager,
                           descriptor: PluginDescriptor,
                           val permissionManager: PermissionManager,
                           config: ConfigManager
                          ) extends SlackController {
  controllerManager.registerController(Resources.buildSettingEdit.controllerUrl, this)

  override def handle(request: HttpServletRequest, response: HttpServletResponse): ModelAndView = {
    import com.fpd.teamcity.slack.Helpers.Implicits._

    val view = descriptor.getPluginResourcesPath(Resources.buildSettingEdit.view)

    val result = for {
      key ← request.param("id")
      model ← config.buildSetting(key)
    } yield {
      new ModelAndView(view, Map("model" → model, "key" → key).asJava)
    }

    result getOrElse new ModelAndView(view, Map("defaultMessage" → SBuildMessageBuilder.defaultMessage).asJava)
  }

  override protected def checkPermission(request: HttpServletRequest): Boolean =
    request.param("buildTypeId").exists(id ⇒ permissionManager.buildAccessPermitted(request, id))
} 
Example 11
Source File: SecurityFilter.scala    From meteorite-core   with Apache License 2.0 5 votes vote down vote up
package bi.meteorite.core.security

import javax.inject.{Inject, Named, Singleton}
import javax.servlet.http.{HttpServletRequest, HttpServletResponse}
import javax.servlet.{Filter, FilterChain, FilterConfig, ServletRequest, ServletResponse}

import bi.meteorite.core.api.security.AdminLoginService
import bi.meteorite.core.api.security.exceptions.TokenProviderException
import bi.meteorite.core.api.security.tokenprovider.TokenProvider
import org.ops4j.pax.cdi.api.OsgiService


@Singleton
@Named("securityFilter")
class SecurityFilter extends Filter {

  @Inject
  @OsgiService
  private var tokenProvider: TokenProvider = _

  @Inject
  @OsgiService
  private var adminLoginService: AdminLoginService = _

  override def doFilter(request: ServletRequest, response: ServletResponse, chain: FilterChain) {
    val servletRequest = request.asInstanceOf[HttpServletRequest]
    val servletResponse = response.asInstanceOf[HttpServletResponse]
    var s = servletRequest.getPathInfo
    val s2 = servletRequest.getServletPath
    if (s != null && s2 != null) {
      s = s2 + s
    } else if (s == null) {
      s = s2
    }
    if (s.startsWith("/admin/ui/") || s == "/rest/core/auth/login") {
      chain.doFilter(request, response)
    } else if (servletRequest.getPathInfo.startsWith("/rest/core/admin")) {
      val token = tokenProvider.getTokenFromRequest(servletRequest)
      var isAdmin = false
      try {
        val userDetails = tokenProvider.verifyToken(token)

        if (adminLoginService.getUsername ==
          userDetails.get(TokenProvider.USERNAME).get) {
          isAdmin = true
        }
      } catch {
        case e: TokenProviderException =>
      }
      if (isAdmin) {
        chain.doFilter(request, response)
      } else {
        servletResponse.sendRedirect("/admin/ui/index.html")
      }
    } else {
      chain.doFilter(request, response)
    }
  }

  override def init(arg0: FilterConfig) {
  }

  override def destroy() {
  }

  def setTokenProvider(tokenProvider: TokenProvider) = this.tokenProvider = tokenProvider

  def setAdminLoginService(adminLoginService: AdminLoginService) = this.adminLoginService = adminLoginService
} 
Example 12
Source File: CsrfFilter.scala    From incubator-livy   with Apache License 2.0 5 votes vote down vote up
package org.apache.livy.server

import javax.servlet._
import javax.servlet.http.{HttpServletRequest, HttpServletResponse}

class CsrfFilter extends Filter {

  val METHODS_TO_IGNORE = Set("GET", "OPTIONS", "HEAD");

  val HEADER_NAME = "X-Requested-By";

  override def init(filterConfig: FilterConfig): Unit = {}

  override def doFilter(request: ServletRequest,
                        response: ServletResponse,
                        chain: FilterChain): Unit = {
    val httpRequest = request.asInstanceOf[HttpServletRequest]

    if (!METHODS_TO_IGNORE.contains(httpRequest.getMethod)
      && httpRequest.getHeader(HEADER_NAME) == null) {
      response.asInstanceOf[HttpServletResponse].sendError(HttpServletResponse.SC_BAD_REQUEST,
        "Missing Required Header for CSRF protection.")
    } else {
      chain.doFilter(request, response)
    }
  }

  override def destroy(): Unit = {}
} 
Example 13
Source File: AccessFilter.scala    From incubator-livy   with Apache License 2.0 5 votes vote down vote up
package org.apache.livy.server

import javax.servlet._
import javax.servlet.http.{HttpServletRequest, HttpServletResponse}

private[livy] class AccessFilter(accessManager: AccessManager) extends Filter {

  override def init(filterConfig: FilterConfig): Unit = {}

  override def doFilter(request: ServletRequest,
                        response: ServletResponse,
                        chain: FilterChain): Unit = {
    val httpRequest = request.asInstanceOf[HttpServletRequest]
    val remoteUser = httpRequest.getRemoteUser
    if (accessManager.isUserAllowed(remoteUser)) {
      chain.doFilter(request, response)
    } else {
      val httpServletResponse = response.asInstanceOf[HttpServletResponse]
      httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN,
        "User not authorised to use Livy.")
    }
  }

  override def destroy(): Unit = {}
} 
Example 14
Source File: Spark2JobApiIT.scala    From incubator-livy   with Apache License 2.0 5 votes vote down vote up
package org.apache.livy.test

import java.io.File
import java.net.URI
import java.util.concurrent.{TimeUnit, Future => JFuture}
import javax.servlet.http.HttpServletResponse

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import org.scalatest.BeforeAndAfterAll

import org.apache.http.client.methods.HttpGet

import org.apache.livy._
import org.apache.livy.client.common.HttpMessages._
import org.apache.livy.sessions.SessionKindModule
import org.apache.livy.test.framework.BaseIntegrationTestSuite
import org.apache.livy.test.jobs.spark2._

class Spark2JobApiIT extends BaseIntegrationTestSuite with BeforeAndAfterAll with Logging {

  private var client: LivyClient = _
  private var sessionId: Int = _
  private val mapper = new ObjectMapper()
    .registerModule(DefaultScalaModule)
    .registerModule(new SessionKindModule())

  override def afterAll(): Unit = {
    super.afterAll()

    if (client != null) {
      client.stop(true)
    }

    livyClient.connectSession(sessionId).stop()
  }

  test("create a new session and upload test jar") {
    val prevSessionCount = sessionList().total
    val tempClient = createClient(livyEndpoint)

    try {
      // Figure out the session ID by poking at the REST endpoint. We should probably expose this
      // in the Java API.
      val list = sessionList()
      assert(list.total === prevSessionCount + 1)
      val tempSessionId = list.sessions(0).id

      livyClient.connectSession(tempSessionId).verifySessionIdle()
      waitFor(tempClient.uploadJar(new File(testLib)))

      client = tempClient
      sessionId = tempSessionId
    } finally {
      if (client == null) {
        try {
          if (tempClient != null) {
            tempClient.stop(true)
          }
        } catch {
          case e: Exception => warn("Error stopping client.", e)
        }
      }
    }
  }

  test("run spark2 job") {
    assume(client != null, "Client not active.")
    val result = waitFor(client.submit(new SparkSessionTest()))
    assert(result === 3)
  }

  test("run spark2 dataset job") {
    assume(client != null, "Client not active.")
    val result = waitFor(client.submit(new DatasetTest()))
    assert(result === 2)
  }

  private def waitFor[T](future: JFuture[T]): T = {
    future.get(60, TimeUnit.SECONDS)
  }

  private def sessionList(): SessionList = {
    val httpGet = new HttpGet(s"$livyEndpoint/sessions/")
    val r = livyClient.httpClient.execute(httpGet)
    val statusCode = r.getStatusLine().getStatusCode()
    val responseBody = r.getEntity().getContent
    val sessionList = mapper.readValue(responseBody, classOf[SessionList])
    r.close()

    assert(statusCode ==  HttpServletResponse.SC_OK)
    sessionList
  }

  private def createClient(uri: String): LivyClient = {
    new LivyClientBuilder().setURI(new URI(uri)).build()
  }
} 
Example 15
Source File: HttpStreamServer.scala    From spark-http-stream   with BSD 2-Clause "Simplified" License 5 votes vote down vote up
package org.apache.spark.sql.execution.streaming.http

import org.apache.spark.internal.Logging
import org.eclipse.jetty.server.Server
import org.eclipse.jetty.servlet.ServletContextHandler

import javax.servlet.ServletConfig
import javax.servlet.http.HttpServlet
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import org.eclipse.jetty.servlet.ServletHolder
import scala.collection.JavaConversions

class MissingRequiredRequestParameterException(paramName: String)
		extends RuntimeException(s"missing required request parameter: $paramName") {
}

class UnsupportedActionException(action: String)
		extends RuntimeException(s"unsupported action in HTTP request header: $action") {
}

object HttpStreamServer {
	def start(httpServletPath: String, httpPort: Int) = {
		val server = new HttpStreamServer(httpServletPath, httpPort);
		server.start;
		server;
	}
}


	def withActionsHandler[T <: ActionsHandler](actionsHandler: T): T = {
		this.actionsHandler = actionsHandler;
		actionsHandler;
	}

	def withBuffer(): MemoryBufferAsReceiver = {
		withActionsHandler(new MemoryBufferAsReceiver());
	}

	def withKafka(bootstrapServers: String): KafkaAsReceiver = {
		withActionsHandler(new KafkaAsReceiver(bootstrapServers));
	}

	def stop() = {
		httpStreamServlet.destroy();
		if (server != null)
			server.stop();
	}
} 
Example 16
Source File: OpenApiServlet.scala    From udash-core   with Apache License 2.0 5 votes vote down vote up
package io.udash
package rest.openapi

import com.avsystem.commons.OptArg
import com.avsystem.commons.annotation.explicitGenerics
import com.avsystem.commons.serialization.json.JsonStringOutput
import javax.servlet.http.{HttpServlet, HttpServletRequest, HttpServletResponse}

object OpenApiServlet {
  @explicitGenerics def apply[RestApi: OpenApiMetadata](
    info: Info,
    components: Components = Components(),
    servers: List[Server] = Nil,
    security: List[SecurityRequirement] = Nil,
    tags: List[Tag] = Nil,
    externalDocs: OptArg[ExternalDocumentation] = OptArg.Empty
  ): OpenApiServlet = new OpenApiServlet {
    protected def render(request: HttpServletRequest): OpenApi =
      implicitly[OpenApiMetadata[RestApi]].openapi(info, components, servers, security, tags, externalDocs)
  }
}

abstract class OpenApiServlet extends HttpServlet {
  protected def render(request: HttpServletRequest): OpenApi

  override def doGet(req: HttpServletRequest, resp: HttpServletResponse): Unit = {
    resp.setContentType("application/json;charset=utf-8")
    resp.getWriter.write(JsonStringOutput.writePretty(render(req)))
  }
} 
Example 17
Source File: FileUploadServlet.scala    From udash-core   with Apache License 2.0 5 votes vote down vote up
package io.udash.rpc.utils

import java.io.InputStream
import java.nio.file.Paths
import javax.servlet.http.{HttpServlet, HttpServletRequest, HttpServletResponse}
import com.avsystem.commons._


  protected def handleFile(name: String, content: InputStream): Unit

  override protected def doPost(request: HttpServletRequest, response: HttpServletResponse): Unit = {
    request.getParts.asScala
      .filter(part => fileFields.contains(part.getName))
      .foreach(filePart => {
        val fileName = Paths.get(filePart.getSubmittedFileName).getFileName.toString
        val fileContent = filePart.getInputStream
        handleFile(fileName, fileContent)
        fileContent.close()
      })
  }
} 
Example 18
Source File: FileDownloadServlet.scala    From udash-core   with Apache License 2.0 5 votes vote down vote up
package io.udash.rpc.utils

import java.io.File
import java.nio.file.Files
import javax.servlet.http.{HttpServlet, HttpServletRequest, HttpServletResponse}


  protected def resolveFileMimeType(file: File): String =
    Option(getServletContext.getMimeType(file.getAbsolutePath)).getOrElse("application/octet-stream")

  override def doGet(request: HttpServletRequest, response: HttpServletResponse): Unit = {
    val file = resolveFile(request)

    if (!file.exists()) response.sendError(404, "File not found!")
    else {
      // MIME type
      response.setContentType(resolveFileMimeType(file))
      // content length
      response.setContentLengthLong(file.length)
      // file name
      response.setHeader("Content-Disposition", s"""attachment; filename="${presentedFileName(file.getName)}"""")

      val outStream = response.getOutputStream
      Files.copy(file.toPath, outStream)
      outStream.close()
    }
  }
} 
Example 19
Source File: CustomHttpAuthenticationFaultHandler.scala    From meteorite-core   with Apache License 2.0 5 votes vote down vote up
package bi.meteorite.core.security.authentication

import java.io.IOException
import javax.servlet.http.HttpServletResponse

import org.apache.cxf.interceptor.security.AuthenticationException
import org.apache.cxf.transport.http.{AbstractHTTPDestination, HttpAuthenticationFaultHandler}
import org.apache.cxf.message.Message


class CustomHttpAuthenticationFaultHandler extends HttpAuthenticationFaultHandler{

  val authenticationType1 = "Basic"
  val realm1 = "CXF service"

  override def handleFault(message: Message) {
    val ex: Exception = message.getContent(classOf[Exception])
    if (ex.isInstanceOf[AuthenticationException]) {
      val resp: HttpServletResponse = message.getExchange.getInMessage.get(AbstractHTTPDestination.HTTP_RESPONSE).asInstanceOf[HttpServletResponse]
      resp.setStatus(HttpServletResponse.SC_UNAUTHORIZED)
      resp.setHeader("WWW-Authenticate", authenticationType1 + " realm=\"" + realm1 + "\"")
      resp.setHeader("Access-Control-Allow-Origin", "*")
      resp.setContentType("text/plain")

      try {
        resp.getWriter.write(ex.getMessage)
        resp.getWriter.flush
        message.getInterceptorChain.abort
      }
      catch {
        case e: IOException => {
        }
      }
    }
  }

}