本文整理汇总了Scala中com.google.common.io.BaseEncoding类的典型用法代码示例。如果您正苦于以下问题:Scala BaseEncoding类的具体用法?Scala BaseEncoding怎么用?Scala BaseEncoding使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了BaseEncoding类的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: Files
//设置package包名称以及导入依赖的类
package controllers
import java.io.FileInputStream
import java.util.UUID
import play.api.Play
import play.api.mvc.{Action, Controller, ResponseHeader, Result}
import com.google.common.io.BaseEncoding
import exceptions.GoogleAuthTokenExpired
import models.File
import play.api.libs.iteratee.Enumerator
object Files extends Controller {
lazy val oauth2 = new utils.OAuth2(Play.current)
def fileUpload = Action(parse.multipartFormData) { request =>
request.body.file("file").map { picture =>
val fileName = picture.filename
val contentType = picture.contentType.getOrElse("text/html")
val fstream = new FileInputStream(picture.ref.file)
val ftext = BaseEncoding.base64.encode(Stream.continually(fstream.read).takeWhile(_ != -1).map(_.toByte).toArray)
try {
val user = oauth2.getUser(request.session)
// Persist
File.create(new File(0, user.id, fileName, contentType, ftext))
// TODO(delyan): this should return file ID perhaps?
Ok(fileName)
} catch {
case expired: GoogleAuthTokenExpired =>
val state = UUID.randomUUID().toString
Ok(views.html.index(None, Some(oauth2.getLoginURL(state)), List()))
.withSession("oauth-state" -> state)
}
}.getOrElse {
Redirect(routes.Users.index)
.flashing("error" -> "Missing file")
}
}
def file = Action {
// TODO(delyan): File.get...
Ok("{}").as("application/json")
}
def downloadFile(fileName: String) = Action { request =>
val user = oauth2.getUser(request.session)
val file = File.getByName(user.id, fileName)
// TODO(delyan): File.get...
val fileContent: Enumerator[Array[Byte]] = Enumerator(BaseEncoding.base64.decode(file.file))
Result(
header = ResponseHeader(200),
body = fileContent
).as(file.content_type)
}
}
示例2: basic644
//设置package包名称以及导入依赖的类
package uk.gov.hmrc.fileupload.support
import com.google.common.base.Charsets
import com.google.common.io.BaseEncoding
import org.scalatest.Suite
import play.api.http.HeaderNames
import play.api.libs.ws.WSResponse
import play.utils.UriEncoding
import uk.gov.hmrc.fileupload.{EnvelopeId, FileId, FileRefId}
trait FileActions extends ActionsSupport {
this: Suite =>
def basic644(s:String): String = {
BaseEncoding.base64().encode(s.getBytes(Charsets.UTF_8))
}
def urlEncode(fileId: FileId) = UriEncoding.encodePathSegment(fileId.value, "UTF-8")
def upload(data: Array[Byte], envelopeId: EnvelopeId, fileId: FileId, fileRefId: FileRefId): WSResponse =
client
.url(s"$url/envelopes/$envelopeId/files/$fileId/$fileRefId")
.withHeaders("Content-Type" -> "application/octet-stream")
.put(data)
.futureValue
def delete(envelopeId: EnvelopeId, fileId: FileId): WSResponse =
client
.url(s"$url/envelopes/$envelopeId/files/${urlEncode(fileId)}")
.delete()
.futureValue
def download(envelopeId: EnvelopeId, fileId: FileId): WSResponse =
client
.url(s"$url/envelopes/$envelopeId/files/${urlEncode(fileId)}/content").withHeaders(HeaderNames.AUTHORIZATION -> ("Basic " + basic644("yuan:yaunspassword")))
.get()
.futureValue
def getFileMetadataFor(envelopeId: EnvelopeId, fileId: FileId): WSResponse =
client
.url(s"$url/envelopes/$envelopeId/files/${urlEncode(fileId)}/metadata")
.get()
.futureValue
def downloadEnvelope(envelopeId: EnvelopeId): WSResponse =
client
.url(s"$fileTransferUrl/envelopes/$envelopeId")
.get()
.futureValue
}
示例3: jsonToAny
//设置package包名称以及导入依赖的类
package com.seancheatham.akka
import akka.serialization.Serialization
import com.google.common.io.BaseEncoding
import play.api.libs.json._
package object persistence {
def jsonToAny(v: JsValue)(implicit serialization: Serialization): Any =
serializedToAny((v \ "vt").as[String], (v \ "v").get)
protected[persistence] def serializedToAny(typeName: String, json: JsValue)(implicit serialization: Serialization): Any = {
typeName match {
case "int" => json.as[Int]
case "long" => json.as[Long]
case "float" => json.as[Float]
case "double" => json.as[Double]
case "boolean" => json.as[Boolean]
case "string" => json.as[String]
case t =>
val decoded = BaseEncoding.base64().decode(json.as[String])
serialization.deserialize(decoded, t.toInt, None).get
}
}
}
示例4: AuthenticatedRequest
//设置package包名称以及导入依赖的类
package controllers
import com.google.common.io.BaseEncoding
import scala.concurrent.Future
import play.api.libs.concurrent.Execution.Implicits._
import play.api.mvc._
import play.api.mvc.Results._
import play.api.libs.json._
import models.User
import play.api.Logger
import play.mvc.Http.HeaderNames._
import com.auth0.jwt.JWTVerifier
class AuthenticatedRequest[A](val user: User, val request: Request[A]) extends WrappedRequest[A](request)
class AuthenticatedAsyncRequest[A](user: User, request: Request[A]) extends WrappedRequest[A](request)
trait Secured {
def SecuredAction = AuthenticatedAction
}
object AuthenticatedAction extends ActionBuilder[AuthenticatedRequest] {
def invokeBlock[A](request: Request[A], block: AuthenticatedRequest[A] => Future[Result]) ={
val auth = request.headers.get(AUTHORIZATION)
if(auth.isDefined) {
val token = auth.get.substring(6).trim
val secret = BaseEncoding.base64Url.omitPadding.decode("password")
val verifier: JWTVerifier = new JWTVerifier(secret, "", "");
val claims = verifier.verify(token)
val user = User(claims.get("name").toString,
claims.get("nickname").toString,
claims.get("family_name").toString,
claims.get("user_id").toString,
claims.get("email").toString,
claims.get("email_verified").toString,
claims.get("user_metadata").toString
)
Logger.info("user" + user)
block(new AuthenticatedRequest[A](user, request))
}
else{
block(new AuthenticatedRequest[A](User("Anonymous","", "", "", "", "", ""), request))
}
}
}
示例5: basic644
//设置package包名称以及导入依赖的类
package uk.gov.hmrc.fileupload.support
import com.google.common.base.Charsets
import com.google.common.io.BaseEncoding
import org.scalatest.Suite
import play.api.http.HeaderNames
import play.api.libs.ws.WSResponse
import uk.gov.hmrc.fileupload.{EnvelopeId, FileId, FileRefId}
trait FileActions extends ActionsSupport {
this: Suite =>
def basic644(s:String): String = {
BaseEncoding.base64().encode(s.getBytes(Charsets.UTF_8))
}
def upload(data: Array[Byte], envelopeId: EnvelopeId, fileId: FileId, fileRefId: FileRefId): WSResponse =
client
.url(s"$url/envelopes/$envelopeId/files/$fileId/$fileRefId")
.withHeaders("Content-Type" -> "application/octet-stream")
.put(data)
.futureValue
def delete(envelopeId: EnvelopeId, fileId: FileId): WSResponse =
client
.url(s"$url/envelopes/$envelopeId/files/$fileId")
.delete()
.futureValue
def download(envelopeId: EnvelopeId, fileId: FileId): WSResponse =
client
.url(s"$url/envelopes/$envelopeId/files/$fileId/content").withHeaders(HeaderNames.AUTHORIZATION -> ("Basic " + basic644("yuan:yaunspassword")))
.get()
.futureValue
def updateFileMetadata(data: String, envelopeId: EnvelopeId, fileId: FileId): WSResponse =
client
.url(s"$url/envelopes/$envelopeId/files/$fileId/metadata" )
.withHeaders("Content-Type" -> "application/json")
.put(data.getBytes)
.futureValue
def getFileMetadataFor(envelopeId: EnvelopeId, fileId: FileId): WSResponse =
client
.url(s"$url/envelopes/$envelopeId/files/$fileId/metadata")
.get()
.futureValue
def downloadEnvelope(envelopeId: EnvelopeId): WSResponse =
client
.url(s"$fileTransferUrl/envelopes/$envelopeId")
.get()
.futureValue
}