• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Scala Status类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Scala中play.api.mvc.Results.Status的典型用法代码示例。如果您正苦于以下问题:Scala Status类的具体用法?Scala Status怎么用?Scala Status使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了Status类的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Scala代码示例。

示例1: SecureRequest

//设置package包名称以及导入依赖的类
package io.skul.katebin.logic

import io.skul.katebin._
import io.skul.katebin.repo.Repo
import javax.inject.Inject
import play.api.mvc.Results.Status
import play.api.mvc.{ActionBuilder, Request, Result, WrappedRequest}
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global

class SecureRequest[A](val user: User, request: Request[A]) extends WrappedRequest[A](request)

class SecureAction @Inject() (repo: Repo) extends ActionBuilder[SecureRequest] {

  def invokeBlock[A](req: Request[A], block: (SecureRequest[A] => Future[Result])): Future[Result] = {
    req.cookies.get(Secure.cookie).map(c => repo.getUserByCookie(c.value)).getOrElse(Future.successful(None)).flatMap {
      case Some(user) =>
        val request = new SecureRequest(user, req)
        block(request)
      case _ =>
        Future.successful(Status(401)("Unauthorized"))
    }
  }
}

object Secure {
  val cookie = "ktb-session"
} 
开发者ID:Saverin-lab,项目名称:katebin-rest,代码行数:29,代码来源:SecurityLogic.scala


示例2: ErrorHandler

//设置package包名称以及导入依赖的类
import scala.concurrent._

import play.api.Logger
import play.api.http.HttpErrorHandler
import play.api.mvc.RequestHeader
import play.api.mvc.Results.{InternalServerError, Status}

class ErrorHandler extends HttpErrorHandler {

  def onClientError(request: RequestHeader, statusCode: Int, message: String) = {
    Logger.error(s"${request.method} ${request.uri} returned $statusCode - $message")
    Future.successful(
      Status(statusCode)(message)
    )
  }

  def onServerError(request: RequestHeader, exception: Throwable) = {
    Future.successful(
      InternalServerError(exception.getMessage)
    )
  }
} 
开发者ID:sebtomba,项目名称:wordcount-akka-streams,代码行数:23,代码来源:ErrorHandler.scala


示例3: hash

//设置package包名称以及导入依赖的类
package services

import java.security.MessageDigest
import java.util.UUID

import play.api.libs.json.{JsValue, Json}
import play.api.mvc.Results.Status
import play.api.mvc.{RequestHeader, Result}


trait ApiSecurity {
  val sessionId = "sessionId"
  def hash(s: String) = MessageDigest.getInstance("SHA-256").digest(s.getBytes("UTF-8")).map("%02x".format(_)).mkString("")

  implicit class ResultWithToken(result: Result)(implicit request: RequestHeader) {
    def withToken: Result = {
      request.session.get(sessionId) match {
        case Some(session) => result
        case None => result.withSession(sessionId -> hash(request.remoteAddress + UUID.randomUUID().toString))
      }
    }
  }

  def JsonAnswer(status: Int = 200, jsObject: JsValue)(implicit request: RequestHeader): Result =
    Status(status).apply(
      Json.obj(
        "result" -> Json.obj("status" -> status),
        "data" -> jsObject
      )
    ).withToken
} 
开发者ID:kormoglaz,项目名称:TestTask,代码行数:32,代码来源:ApiSecurity.scala


示例4: ServerGateway

//设置package包名称以及导入依赖的类
package services

import javax.inject.Inject

import play.api.Configuration
import play.api.libs.iteratee.Enumerator
import play.api.libs.ws.WSClient
import play.api.mvc.Results.Status
import play.api.mvc.{ResponseHeader, Result}

import scala.concurrent.Future

object ServerGateway

class ServerGateway @Inject()(configuration: Configuration) {

  implicit val context = play.api.libs.concurrent.Execution.Implicits.defaultContext

  def forwardRequestToServer(request: ProxyRequest, ws: WSClient): Future[Result] = {
    val wsRequest = ws.url(request.uri.toString)
      .withHeaders(request.simpleHeaderMap.toList: _*)
      .withMethod(request.method)
      .withBody(request.httpMessage.body.getOrElse(""))
      .withRequestTimeout(configuration.getLong("server.timeout").getOrElse(60000))

    wsRequest.execute().map { response =>
      def responseHeader = toSimpleHeaderMap(response.allHeaders)
      responseHeader.get("Transfer-Encoding") match {
        case Some("chunked") =>
          new Status(response.status)
            .chunked(Enumerator(response.bodyAsBytes))
            .withHeaders(responseHeader.toList: _*)
        case default =>
          val headers =
            responseHeader map {
              // fix the "Content-Lenght" header manually
              h => (h._1, if (h._1 == "Content-Length")
                response.body.toString
              else h._2.head)
            }
          Result(ResponseHeader(response.status, responseHeader), Enumerator(response.bodyAsBytes))

      }


    }

  }

  private def toSimpleHeaderMap(headers: Map[String, Seq[String]]): Map[String, String] = {
    headers.map {
      case (k, v) => (k, v.mkString(","))
    }
  }

} 
开发者ID:innoq,项目名称:docster,代码行数:57,代码来源:ServerGateway.scala


示例5: ErrorHandler

//设置package包名称以及导入依赖的类
package sc.ript.cgmlms.fw.handlers

import javax.inject.{Inject, Singleton}

import com.google.inject.Provider
import com.typesafe.scalalogging.LazyLogging
import play.api.http.DefaultHttpErrorHandler
import play.api.mvc.Results.{InternalServerError, Status}
import play.api.mvc.{RequestHeader, Result}
import play.api.routing.Router
import play.api.{Configuration, Environment, OptionalSourceMapper}

import scala.concurrent.Future

@Singleton
private class ErrorHandler @Inject()(env: Environment, config: Configuration, sourceMapper: OptionalSourceMapper, router: Provider[Router]) extends DefaultHttpErrorHandler(env, config, sourceMapper, router) with LazyLogging {

  override def onClientError(request: RequestHeader, statusCode: Int, message: String): Future[Result] = {
    logger.info("statusCode = {}, message = {}", statusCode.toString, message)
    Future.successful(Status(statusCode)("A client error occurred: " + message))
  }

  override def onServerError(request: RequestHeader, exception: Throwable): Future[Result] = {
    logger.error(s"message = ${exception.getMessage}", exception)
    Future.successful(InternalServerError("A server error occurred: " + exception.getMessage))
  }

} 
开发者ID:brightgenerous,项目名称:cgm-lms,代码行数:29,代码来源:ErrorHandler.scala


示例6: SlackFail

//设置package包名称以及导入依赖的类
package com.kifi.slack.models

import play.api.http.Status
import play.api.http.Status._
import play.api.libs.json._

// Failures on our end that deal with slack
sealed abstract class SlackFail(val status: Int, val code: String, val extra: JsValue = JsNull) extends Exception(s"$code: ${Json.stringify(extra)}") {
  import play.api.mvc.Results.Status
  def asResponse = Status(status)(Json.obj("error" -> code, "extra" -> extra))
}
object SlackFail {
  case object NoAppCredentials extends SlackFail(UNAUTHORIZED, "no_app_credentials")
  case object NoAuthCode extends SlackFail(BAD_REQUEST, "no_auth_code")
  case object InvalidAuthState extends SlackFail(BAD_REQUEST, "invalid_auth_state")
  case class MalformedPayload(payload: JsValue) extends SlackFail(BAD_REQUEST, "malformed_payload", payload)
  case class MalformedState(state: SlackAuthState) extends SlackFail(BAD_REQUEST, "malformed_state", JsString(state.state))
  case class SlackResponse(response: SlackAPIErrorResponse) extends SlackFail(response.status, response.error, response.payload)
}

// Failures that Slack can give
case class SlackAPIErrorResponse(status: Int, error: String, payload: JsValue) extends Exception(s"$status response: $error ($payload)")
object SlackAPIErrorResponse {
  def ApiError(status: Int, payload: JsValue) = SlackAPIErrorResponse(status, (payload \ "error").asOpt[String] getOrElse "api_error", payload)
  val TokenRevoked = SlackAPIErrorResponse(Status.NOT_FOUND, SlackErrorCode.TOKEN_REVOKED, JsNull)
  val WebhookRevoked = SlackAPIErrorResponse(Status.NOT_FOUND, SlackErrorCode.WEBHOOK_REVOKED, JsNull)
}
object SlackErrorCode {
  def unapply(fail: SlackAPIErrorResponse) = Some(fail.error)
  val ACCOUNT_INACTIVE = "account_inactive"
  val ALREADY_REACTED = "already_reacted"
  val CANT_UPDATE_MESSAGE = "cant_update_message"
  val CHANNEL_NOT_FOUND = "channel_not_found"
  val EDIT_WINDOW_CLOSED = "edit_window_closed"
  val INVALID_AUTH = "invalid_auth"
  val IS_ARCHIVED = "is_archived"
  val MESSAGE_NOT_FOUND = "message_not_found"
  val NOT_IN_CHANNEL = "not_in_channel"
  val RESTRICTED_ACTION = "restricted_action"
  val TOKEN_REVOKED = "token_revoked"
  val WEBHOOK_REVOKED = "webhook_revoked"
} 
开发者ID:kifi,项目名称:slack-client,代码行数:43,代码来源:SlackFail.scala



注:本文中的play.api.mvc.Results.Status类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Scala BitVector类代码示例发布时间:2022-05-23
下一篇:
Scala ScriptEngine类代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap