com.google.common.io.BaseEncoding Scala Examples

The following examples show how to use com.google.common.io.BaseEncoding. 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: MarathonDriver.scala    From sparta   with Apache License 2.0 5 votes vote down vote up
package com.stratio.sparta.driver

import akka.actor.{ActorSystem, Props}
import com.google.common.io.BaseEncoding
import com.stratio.sparta.driver.actor.MarathonAppActor
import com.stratio.sparta.driver.actor.MarathonAppActor.StartApp
import com.stratio.sparta.driver.exception.DriverException
import com.stratio.sparta.serving.core.config.SpartaConfig
import com.stratio.sparta.serving.core.constants.AkkaConstant
import com.stratio.sparta.serving.core.curator.CuratorFactoryHolder
import com.stratio.sparta.serving.core.utils.PluginsFilesUtils
import com.typesafe.config.ConfigFactory

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

object MarathonDriver extends PluginsFilesUtils {

  val NumberOfArguments = 3
  val PolicyIdIndex = 0
  val ZookeeperConfigurationIndex = 1
  val DetailConfigurationIndex = 2

  def main(args: Array[String]): Unit = {
    assert(args.length == NumberOfArguments,
      s"Invalid number of arguments: ${args.length}, args: $args, expected: $NumberOfArguments")
    Try {
      val policyId = args(PolicyIdIndex)
      val zookeeperConf = new String(BaseEncoding.base64().decode(args(ZookeeperConfigurationIndex)))
      val detailConf = new String(BaseEncoding.base64().decode(args(DetailConfigurationIndex)))
      initSpartaConfig(zookeeperConf, detailConf)
      val curatorInstance = CuratorFactoryHolder.getInstance()
      val system = ActorSystem(policyId)
      val marathonAppActor =
        system.actorOf(Props(new MarathonAppActor(curatorInstance)), AkkaConstant.MarathonAppActorName)

      marathonAppActor ! StartApp(policyId)
    } match {
      case Success(_) =>
        log.info("Initiated Marathon App environment")
      case Failure(driverException: DriverException) =>
        log.error(driverException.msg, driverException.getCause)
        throw driverException
      case Failure(exception) =>
        log.error(s"Error initiating Marathon App environment: ${exception.getLocalizedMessage}", exception)
        throw exception
    }
  }

  def initSpartaConfig(zKConfig: String, detailConf: String): Unit = {
    val configStr = s"${detailConf.stripPrefix("{").stripSuffix("}")}" +
      s"\n${zKConfig.stripPrefix("{").stripSuffix("}")}"
    log.info(s"Parsed config: sparta { $configStr }")
    val composedStr = s" sparta { $configStr } "
    SpartaConfig.initMainWithFallbackConfig(ConfigFactory.parseString(composedStr))
  }
} 
Example 2
Source File: BigQueryTypeSpec.scala    From shapeless-datatype   with Apache License 2.0 5 votes vote down vote up
package shapeless.datatype.bigquery

import java.net.URI

import com.fasterxml.jackson.databind.{ObjectMapper, SerializationFeature}
import com.google.api.services.bigquery.model.TableRow
import com.google.common.io.BaseEncoding
import com.google.protobuf.ByteString
import org.joda.time.{Instant, LocalDate, LocalDateTime, LocalTime}
import org.scalacheck.Prop.forAll
import org.scalacheck.ScalacheckShapeless._
import org.scalacheck._
import shapeless._
import shapeless.datatype.record._

import scala.reflect.runtime.universe._

object BigQueryTypeSpec extends Properties("BigQueryType") {
  import shapeless.datatype.test.Records._
  import shapeless.datatype.test.SerializableUtils._

  val mapper = new ObjectMapper().disable(SerializationFeature.FAIL_ON_EMPTY_BEANS)

  implicit def compareByteArrays(x: Array[Byte], y: Array[Byte]) = java.util.Arrays.equals(x, y)
  implicit def compareIntArrays(x: Array[Int], y: Array[Int]) = java.util.Arrays.equals(x, y)

  def roundTrip[A: TypeTag, L <: HList](m: A)(implicit
    gen: LabelledGeneric.Aux[A, L],
    fromL: FromTableRow[L],
    toL: ToTableRow[L],
    mr: MatchRecord[L]
  ): Boolean = {
    BigQuerySchema[A] // FIXME: verify the generated schema
    val t = ensureSerializable(BigQueryType[A])
    val f1: SerializableFunction[A, TableRow] =
      new SerializableFunction[A, TableRow] {
        override def apply(m: A): TableRow = t.toTableRow(m)
      }
    val f2: SerializableFunction[TableRow, Option[A]] =
      new SerializableFunction[TableRow, Option[A]] {
        override def apply(m: TableRow): Option[A] = t.fromTableRow(m)
      }
    val toFn = ensureSerializable(f1)
    val fromFn = ensureSerializable(f2)
    val copy = fromFn(mapper.readValue(mapper.writeValueAsString(toFn(m)), classOf[TableRow]))
    val rm = RecordMatcher[A]
    copy.exists(rm(_, m))
  }

  implicit val byteStringBigQueryMappableType = BigQueryType.at[ByteString]("BYTES")(
    x => ByteString.copyFrom(BaseEncoding.base64().decode(x.toString)),
    x => BaseEncoding.base64().encode(x.toByteArray)
  )
  property("required") = forAll { m: Required => roundTrip(m) }
  property("optional") = forAll { m: Optional => roundTrip(m) }
  property("repeated") = forAll { m: Repeated => roundTrip(m) }
  property("mixed") = forAll { m: Mixed => roundTrip(m) }
  property("nested") = forAll { m: Nested => roundTrip(m) }
  property("seqs") = forAll { m: Seqs => roundTrip(m) }

  implicit val arbDate = Arbitrary(arbInstant.arbitrary.map(i => new LocalDate(i.getMillis)))
  implicit val arbTime = Arbitrary(arbInstant.arbitrary.map(i => new LocalTime(i.getMillis)))
  implicit val arbDateTime = Arbitrary(
    arbInstant.arbitrary.map(i => new LocalDateTime(i.getMillis))
  )

  case class DateTimeTypes(
    instant: Instant,
    date: LocalDate,
    time: LocalTime,
    dateTime: LocalDateTime
  )
  property("date time types") = forAll { m: DateTimeTypes => roundTrip(m) }

  implicit val uriBigQueryType =
    BigQueryType.at[URI]("STRING")(v => URI.create(v.toString), _.toASCIIString)
  property("custom") = forAll { m: Custom => roundTrip(m) }
} 
Example 3
Source File: LambdaTransport.scala    From shield   with MIT License 5 votes vote down vote up
package shield.transports

import com.amazonaws.services.lambda._
import spray.http.HttpHeaders.RawHeader
import spray.http.{HttpEntity, _}
import spray.json._
import spray.json.JsonParser
import com.amazonaws.handlers.AsyncHandler
import com.amazonaws.services.lambda.model._
import com.google.common.io.BaseEncoding
import spray.http.parser.HttpParser

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.{Future, Promise}
import scala.util.Try

object LambdaTransport {
  type SendReceive = spray.client.pipelining.SendReceive
  val base64 = BaseEncoding.base64()
  class LambdaAsyncHandler(promise: Promise[InvokeResult]) extends AsyncHandler[InvokeRequest, InvokeResult] {
    def onError(exception: Exception) = promise.failure(exception)

    def onSuccess(request: InvokeRequest, result: InvokeResult) = promise.success(result)
  }

  def lambdaTransport(arn: String): SendReceive = {
    val client = AWSLambdaAsyncClientBuilder.defaultClient()
    def executeCall(request: HttpRequest): Future[HttpResponse] = {
      val req = new InvokeRequest()
        .withFunctionName(arn)
        .withPayload(LambdaRequest.translate(request).toJson.compactPrint)
      val p = Promise[InvokeResult]
      client.invokeAsync(req, new LambdaAsyncHandler(p))
      p.future.map(req => {
        JsonParser(ParserInput(req.getPayload.array)).convertTo[LambdaResponse].toResponse
      })
    }
    executeCall
  }

  case class LambdaRequest(method: String, headers: List[(String, String)], uri: String, body: Option[String])

  object LambdaRequest extends DefaultJsonProtocol {
    def translate(request: HttpRequest): LambdaRequest = {
      val processedHeaders = request.entity.toOption match {
        // No body - leave the content-type header (probably a HEAD request)
        case None => request.headers
        // Some body - set the content-type based off what spray associated with the entity
        // * content-type has probably been filtered by HttpProxyLogic before reaching here, so this likely redundant
        // * Adding-content-type from the entity matches Spray's HttpRequest rendering logic
        case Some(entity) => HttpHeaders.`Content-Type`(entity.contentType) :: request.headers.filterNot(_.lowercaseName == "content-type")
      }
      val headers =  processedHeaders.map(header => (header.lowercaseName, header.value))
      val body = request.entity.toOption.map(e => base64.encode(e.data.toByteArray))
      LambdaRequest(request.method.value, headers, request.uri.toString, body)
    }
    implicit val requestFormat : JsonFormat[LambdaRequest] = jsonFormat4(LambdaRequest.apply)
  }

  case class LambdaResponse(status: Int, headers: List[(String, String)], body: Option[String]) {
    def parseHeaders : List[HttpHeader] = HttpParser.parseHeaders(headers.map(RawHeader.tupled))._2
    def toResponse: HttpResponse = {
      val parsedContentType = headers.find(_._1.toLowerCase() == "content-type").map(ct => HttpParser.parse(HttpParser.ContentTypeHeaderValue, ct._2))
      val entity = body.map(base64.decode)

      (parsedContentType, entity) match {
        case (None, None) => HttpResponse(status, headers = parseHeaders)
        case (None, Some(data)) => HttpResponse(status, HttpEntity(ContentTypes.`application/octet-stream`, data), parseHeaders)
        case (Some(Left(err)), _) => HttpResponse(StatusCodes.BadGateway, HttpEntity(s"Upstream supplied an invalid content-type header: ${err.detail}"))
        case (Some(Right(contentType)), None) => HttpResponse(status, headers = parseHeaders)
        case (Some(Right(contentType)), Some(data)) => HttpResponse(status, HttpEntity(contentType, data), parseHeaders)
      }
    }
  }

  object LambdaResponse extends DefaultJsonProtocol {
    implicit val responseFormat : JsonFormat[LambdaResponse] = jsonFormat3(LambdaResponse.apply)
  }
} 
Example 4
Source File: Util.scala    From ratatool   with Apache License 2.0 5 votes vote down vote up
package com.spotify.ratatool.samplers.util

import java.nio.charset.Charset

import com.google.common.hash.{Funnel, HashCode, Hasher}
import com.google.common.io.BaseEncoding

trait SampleDistribution
case object StratifiedDistribution extends SampleDistribution
case object UniformDistribution extends SampleDistribution

object SampleDistribution {
  def fromString(s: String): SampleDistribution = {
    if (s == "stratified") {
      StratifiedDistribution
    }
    else if (s == "uniform") {
      UniformDistribution
    }
    else {
      throw new IllegalArgumentException(s"Invalid distribution $s")
    }
  }
}

trait Determinism
case object NonDeterministic extends Determinism
case object Deterministic extends Determinism

object Determinism {
  def fromSeq(l: Seq[_]): Determinism = {
    if (l == Seq()) {
      NonDeterministic
    } else {
      Deterministic
    }
  }
}

trait Precision
case object Approximate extends Precision
case object Exact extends Precision

object Precision {
  def fromBoolean(exact: Boolean): Precision = {
    if (exact) {
      Exact
    } else {
      Approximate
    }
  }
}

trait ByteEncoding
case object RawEncoding extends ByteEncoding
case object HexEncoding extends ByteEncoding
case object Base64Encoding extends ByteEncoding

object ByteEncoding {
  def fromString(s: String): ByteEncoding = {
    if(s == "raw") {
      RawEncoding
    }
    else if(s == "hex") {
      HexEncoding
    }
    else if(s == "base64") {
      Base64Encoding
    }
    else {
      throw new IllegalArgumentException(s"Invalid byte encoding $s")
    }
  }
} 
Example 5
Source File: Key.scala    From Waves   with MIT License 5 votes vote down vote up
package com.wavesplatform.database

import com.google.common.base.CaseFormat
import com.google.common.io.BaseEncoding
import com.google.common.primitives.{Bytes, Shorts}

abstract class Key[V](prefix: Short, val name: String, val suffix: Array[Byte]) {
  val keyBytes: Array[Byte] = Bytes.concat(Shorts.toByteArray(prefix), suffix)
  def parse(bytes: Array[Byte]): V
  def encode(v: V): Array[Byte]

  override lazy val toString: String = s"$name($prefix,${BaseEncoding.base16().encode(suffix)})"

  override def equals(obj: Any): Boolean = obj match {
    case that: Key[V] => java.util.Arrays.equals(this.keyBytes, that.keyBytes)
    case _            => false
  }

  override def hashCode(): Int = java.util.Arrays.hashCode(keyBytes)
}

object Key {
  private[this] val converter = CaseFormat.UPPER_CAMEL.converterTo(CaseFormat.LOWER_HYPHEN)

  def apply[V](keyTag: KeyTags.KeyTag, keySuffix: Array[Byte], parser: Array[Byte] => V, encoder: V => Array[Byte]): Key[V] =
    new Key[V](keyTag.id.toShort, converter.convert(keyTag.toString), keySuffix) {
      override def parse(bytes: Array[Byte]): V = parser(bytes)
      override def encode(v: V): Array[Byte]    = encoder(v)
    }

  def opt[V](keyTag: KeyTags.KeyTag, keySuffix: Array[Byte], parser: Array[Byte] => V, encoder: V => Array[Byte]): Key[Option[V]] =
    apply[Option[V]](
      keyTag,
      keySuffix,
      Option(_).map(parser),
      _.fold[Array[Byte]](Array.emptyByteArray)(encoder)
    )
}