com.fasterxml.jackson.databind.JsonNode Scala Examples

The following examples show how to use com.fasterxml.jackson.databind.JsonNode. 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: ServiceAdapter.scala    From ez-framework   with Apache License 2.0 5 votes vote down vote up
package com.ecfront.ez.framework.service.auth

import com.ecfront.common.Resp
import com.ecfront.ez.framework.core.EZServiceAdapter
import com.ecfront.ez.framework.core.rpc.AutoBuildingProcessor
import com.ecfront.ez.framework.core.rpc.apidoc.APIDocProcessor
import com.fasterxml.jackson.databind.JsonNode

import scala.collection.mutable

object ServiceAdapter extends EZServiceAdapter[JsonNode] {

  val EB_ORG_ADD_FLAG = "/ez/auth/rbac/organization/add/"
  val EB_ORG_REMOVE_FLAG = "/ez/auth/rbac/organization/remove/"
  val EB_RESOURCE_ADD_FLAG = "/ez/auth/rbac/resource/add/"
  val EB_RESOURCE_REMOVE_FLAG = "/ez/auth/rbac/resource/remove/"
  val EB_ROLE_ADD_FLAG = "/ez/auth/rbac/role/add/"
  val EB_ROLE_REMOVE_FLAG = "/ez/auth/rbac/role/remove/"

  val EB_ORG_INIT_FLAG = "/ez/auth/organizationInit/"
  val EB_LOGIN_SUCCESS_FLAG = "/ez/auth/loginSuccess/"
  val EB_LOGOUT_FLAG = "/ez/auth/logout/"

  val EB_FLUSH_FLAG = "/ez/gateway/auth/flush/"

  var customLogin: Boolean = _
  var defaultOrganizationCode: String = _
  var loginKeepSeconds: Int = _
  var loginLimit_showCaptcha: Int = _
  var encrypt_algorithm: String = _
  var encrypt_salt: String = _

  override def init(parameter: JsonNode): Resp[String] = {
    customLogin = parameter.path("customLogin").asBoolean(false)
    if (parameter.has("loginLimit")) {
      val loginLimit = parameter.get("loginLimit")
      if (loginLimit.has("showCaptcha")) {
        loginLimit_showCaptcha = loginLimit.get("showCaptcha").asInt()
      }
    } else {
      loginLimit_showCaptcha = Int.MaxValue
    }
    defaultOrganizationCode = parameter.path("defaultOrganizationCode").asText("")
    loginKeepSeconds = parameter.path("loginKeepSeconds").asInt(0)
    encrypt_algorithm =
      if (parameter.has("encrypt") && parameter.get("encrypt").has("algorithm")) {
        parameter.get("encrypt").get("algorithm").asText()
      } else {
        "SHA-256"
      }
    encrypt_salt =
      if (parameter.has("encrypt") && parameter.get("encrypt").has("salt")) {
        parameter.get("encrypt").get("salt").asText()
      } else {
        ""
      }
    Resp.success("")
  }

  override def initPost(): Unit = {
    AutoBuildingProcessor.autoBuilding("com.ecfront.ez.framework.service.auth")
    Initiator.init()
    super.initPost()
  }

  override def destroy(parameter: JsonNode): Resp[String] = {
    Resp.success("")
  }

  override lazy val dependents: mutable.Set[String] =
    mutable.Set(com.ecfront.ez.framework.service.jdbc.ServiceAdapter.serviceName)

  override var serviceName: String = "auth"

} 
Example 2
Source File: UpgradeConfiguration.scala    From RTran   with Apache License 2.0 5 votes vote down vote up
package com.ebay.rtran.core

import com.fasterxml.jackson.databind.JsonNode
import com.typesafe.scalalogging.LazyLogging
import org.json4s.JsonAST.JValue
import org.json4s.jackson.JsonMethods._
import com.ebay.rtran.api.{IModel, IRule, IRuleConfigFactory}
import org.json4s.DefaultFormats

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


trait RuleProducer {
  val ruleInstances: List[_ <: IRule[_ <: IModel]]
}

trait UpgradeConfiguration extends RuleProducer {
  val ruleConfigs: List[JsonRuleConfiguration]
}

case class JsonRuleConfiguration(name: String, metadata: Option[JValue] = None, config: Option[JValue] = None)

case class JsonUpgradeConfiguration(ruleConfigs: List[JsonRuleConfiguration])
  extends UpgradeConfiguration with JsonRuleProducer

trait JsonRuleProducer extends RuleProducer with LazyLogging {self: UpgradeConfiguration =>

  lazy val ruleInstances = ruleConfigs map {
    case JsonRuleConfiguration(name, metadata, configOpt) =>
      logger.info("Creating instance for {} with config {}", name, configOpt)
      implicit val formats = DefaultFormats

      //copy settings from metadata to Rule Registry
      RuleRegistry.findRuleDefinition(name) flatMap { case (ruleClass, rule) =>
        val properties = metadata.map(json => json.extract[Map[String, Any]])
        val configFactory = (rule.configFactory getOrElse DefaultJsonRuleConfigFactory)
          .asInstanceOf[IRuleConfigFactory[JsonNode]]
        configOpt map { config =>
          Try(JsonConfigurableRuleFactory.createRuleWithConfig(ruleClass, configFactory, asJsonNode(config)))
        } getOrElse Try(JsonConfigurableRuleFactory.createRule(ruleClass)) match {
          case Success(instance) =>
            properties.map(m => m.mapValues(_.toString)).map(m => RuleRegistry.saveRuleMetadata(instance, m))
            Some(instance)
          case Failure(e) =>
            logger.warn(e.getMessage)
            None
        }
      }
  } collect {
    case Some(instance) => instance
  }

} 
Example 3
Source File: JsonConfigurableRuleFactory.scala    From RTran   with Apache License 2.0 5 votes vote down vote up
package com.ebay.rtran.core

import com.fasterxml.jackson.databind.JsonNode
import com.ebay.rtran.api._


object JsonConfigurableRuleFactory extends IRuleFactory[JsonNode] {

  override def createRuleWithConfig[Rule <: IRule[_ <: IModel]](ruleClass: Class[Rule],
                                                                configFactory: IRuleConfigFactory[JsonNode],
                                                                configData: JsonNode): Rule = {
    ruleClass.getDeclaredConstructors find {c =>
      c.getParameterCount == 1 && classOf[IRuleConfig].isAssignableFrom(c.getParameterTypes.head)
    } match {
      case Some(constructor) =>
        val config = configFactory.createRuleConfig(
          constructor.getParameterTypes()(0).asSubclass(classOf[IRuleConfig]),
          configData
        )
        constructor.newInstance(config).asInstanceOf[Rule]
      case None => throw new IllegalStateException(s"Cannot find constructor to accept config for class $ruleClass")
    }
  }
} 
Example 4
Source File: JsonUtil.scala    From marvin-engine-executor   with Apache License 2.0 5 votes vote down vote up
package org.marvin.util

import com.fasterxml.jackson.databind.{DeserializationFeature, JsonNode, ObjectMapper}
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import com.github.fge.jsonschema.core.exceptions.ProcessingException
import com.github.fge.jsonschema.core.report.ProcessingMessage
import com.github.fge.jsonschema.main.JsonSchemaFactory
import grizzled.slf4j.Logging
import org.json4s.jackson.JsonMethods.{asJsonNode, parse}
import spray.json._

import scala.io.Source
import scala.reflect.{ClassTag, _}

object JsonUtil extends Logging {
  val jacksonMapper = new ObjectMapper()
  jacksonMapper.registerModule(DefaultScalaModule)
  jacksonMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)

  def toJson(value: Map[Symbol, Any]): String = {
    toJson(value map { case (k,v) => k.name -> v})
  }

  def toJson(value: Any): String = {
    jacksonMapper.writeValueAsString(value)
  }

  def toMap(jsonString: String): Map[String, Any] = {
    JsonUtil.fromJson[Map[String, List[Map[String, String]]]](jsonString)
  }

  def fromJson[T: ClassTag](jsonString: String, validate: Boolean = false): T = {

    if (validate) validateJson[T](jsonString)

    jacksonMapper.readValue[T](jsonString, classTag[T].runtimeClass.asInstanceOf[Class[T]])
  }

  def validateJson[T: ClassTag](jsonString: String): Unit = {
    val className = classTag[T].runtimeClass.getSimpleName
    val schemaName = className.toString + "Schema.json"

    var jsonSchema: String = null

    try{
      jsonSchema = Source.fromResource(schemaName).mkString
      validateJson(jsonString, jsonSchema)
    } catch {
      case e: NullPointerException => info(s"File ${schemaName} not found, check your schema file")
        throw e
    }
  }

  
  def validateJson(jsonString: String, jsonSchema: String): Unit = {

    val schema: JsonNode = asJsonNode(parse(jsonSchema))
    val jsonToValidate: JsonNode = asJsonNode(parse(jsonString))
    val validator = JsonSchemaFactory.byDefault().getValidator

    val processingReport = validator.validate(schema, jsonToValidate)
    if (!processingReport.isSuccess) {
      val sb = new StringBuilder()
      processingReport.forEach {
        message: ProcessingMessage => {
          warn(message.asJson())
          sb.append(message.getMessage)
        }
      }
      throw new ProcessingException(sb.toString)
    }
  }

  def format(jsonString: String): String ={
    return jsonString.parseJson.prettyPrint
  }

  def format(jsonMap: Map[String, Any]): String ={
    return this.format(this.toJson(jsonMap))
  }
} 
Example 5
Source File: JsonLifter.scala    From diffy   with GNU Affero General Public License v3.0 5 votes vote down vote up
package ai.diffy.lifter

import com.fasterxml.jackson.core.{JsonGenerator, JsonToken}
import com.fasterxml.jackson.databind.annotation.JsonSerialize
import com.fasterxml.jackson.databind.ser.std.StdSerializer
import com.fasterxml.jackson.databind.{JsonNode, ObjectMapper, SerializerProvider}
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import com.fasterxml.jackson.module.scala.experimental.ScalaObjectMapper
import com.twitter.util.Try

import scala.collection.JavaConversions._
import scala.language.postfixOps
import scala.reflect.runtime.universe.runtimeMirror
import scala.tools.reflect.ToolBox
import scala.util.control.NoStackTrace

object JsonLifter {
  @JsonSerialize(using = classOf[JsonNullSerializer])
  object JsonNull
  object JsonParseError extends Exception with NoStackTrace

  val toolbox = runtimeMirror(getClass.getClassLoader).mkToolBox()

  val Mapper = new ObjectMapper with ScalaObjectMapper
  Mapper.registerModule(DefaultScalaModule)
  def apply(obj: Any): JsonNode = Mapper.valueToTree(obj)

  def lift(node: JsonNode): Any = node.asToken match {
    case JsonToken.START_ARRAY        =>
      node.elements.toSeq.map {
        element => lift(element)
      }
    case JsonToken.START_OBJECT       => {
      val fields = node.fieldNames.toSet
      if (fields.exists{ field => Try(toolbox.parse(s"object ${field}123")).isThrow}) {
        node.fields map {field => (field.getKey -> lift(field.getValue))} toMap
      } else {
        FieldMap(
          node.fields map {field => (field.getKey -> lift(field.getValue))} toMap
        )
      }
    }
    case JsonToken.VALUE_FALSE        => false
    case JsonToken.VALUE_NULL         => JsonNull
    case JsonToken.VALUE_NUMBER_FLOAT => node.asDouble
    case JsonToken.VALUE_NUMBER_INT   => node.asLong
    case JsonToken.VALUE_TRUE         => true
    case JsonToken.VALUE_STRING       => node.textValue
    case _                            => throw JsonParseError
  }

  def decode(json: String): JsonNode = Mapper.readTree(json)
  def encode(item: Any): String = Mapper.writer.writeValueAsString(item)
}

class JsonNullSerializer(clazz: Class[Any]) extends StdSerializer[Any](clazz) {
  def this() {
    this(null)
  }

  override def serialize(t: Any, jsonGenerator: JsonGenerator, serializerProvider: SerializerProvider): Unit = {
    jsonGenerator.writeNull()
  }
} 
Example 6
Source File: PgJacksonJsonSupport.scala    From fusion-data   with Apache License 2.0 5 votes vote down vote up
package mass.db.slick

import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.node.NullNode
import com.github.tminglei.slickpg.json.PgJsonExtensions
import com.github.tminglei.slickpg.utils.{ PgCommonJdbcTypes, SimpleArrayUtils }
import com.github.tminglei.slickpg.{ ArraySupport, ExPostgresProfile }
import fusion.json.jackson.ScalaObjectMapper
import helloscala.common.data.NameValue
import mass.model.job.{ JobItem, JobTrigger }
import slick.jdbc._

import scala.language.implicitConversions
import scala.reflect.{ ClassTag, classTag }
import scala.util.Try

trait PgJacksonJsonSupport extends PgJsonExtensions with PgCommonJdbcTypes {
  driver: PostgresProfile with ArraySupport =>
  import driver.api._

  def pgjson: String
  def objectMapper: ScalaObjectMapper

  trait JacksonCodeGenSupport {
    driver match {
      case profile: ExPostgresProfile =>
        profile.bindPgTypeToScala("json", classTag[JsonNode])
        profile.bindPgTypeToScala("jsonb", classTag[JsonNode])
      case _ =>
    }
  }

  trait JsonImplicits extends JacksonImplicits

  trait JacksonImplicits extends JacksonCodeGenSupport {
    implicit val jacksonJsonTypeMapper: JdbcType[JsonNode] = new GenericJdbcType[JsonNode](
      pgjson,
      v => Try(objectMapper.readTree(v)).getOrElse(NullNode.instance),
      v => objectMapper.stringify(v))

    implicit val jacksonArrayTypeMapper: AdvancedArrayJdbcType[JsonNode] =
      new AdvancedArrayJdbcType[JsonNode](
        pgjson,
        s => SimpleArrayUtils.fromString[JsonNode](jstr => objectMapper.readTree(jstr))(s).orNull,
        v => SimpleArrayUtils.mkString[JsonNode](jnode => objectMapper.stringify(jnode))(v))

    implicit val passportColumnType: JdbcType[NameValue] = mkJsonColumnType[NameValue]
    implicit val triggerConfTypeMapper: JdbcType[JobTrigger] = mkJsonColumnType[JobTrigger]
    implicit val jobItemTypeMapper: JdbcType[JobItem] = mkJsonColumnType[JobItem]

    def mkJsonColumnType[T](implicit ev1: ClassTag[T]): JdbcType[T] =
      MappedColumnType
        .base[T, JsonNode](objectMapper.valueToTree, objectMapper.treeToValue(_, ev1.runtimeClass).asInstanceOf[T])

    implicit def jacksonJsonColumnExtensionMethods(c: Rep[JsonNode]): JsonColumnExtensionMethods[JsonNode, JsonNode] =
      new JsonColumnExtensionMethods[JsonNode, JsonNode](c)

    implicit def jacksonJsonOptionColumnExtensionMethods(
        c: Rep[Option[JsonNode]]): JsonColumnExtensionMethods[JsonNode, Option[JsonNode]] =
      new JsonColumnExtensionMethods[JsonNode, Option[JsonNode]](c)
  }

  trait JacksonJsonPlainImplicits extends JacksonCodeGenSupport {
    import com.github.tminglei.slickpg.utils.PlainSQLUtils._
    implicit class PgJacksonJsonPositionResult(r: PositionedResult) {
      def nextJson(): JsonNode = nextJsonOption().getOrElse(NullNode.instance)
      def nextJsonOption(): Option[JsonNode] =
        r.nextStringOption().map(s => Try(objectMapper.readTree(s)).getOrElse(NullNode.instance))
    }

    implicit val getJacksonJson: GetResult[JsonNode] = mkGetResult(_.nextJson())
    implicit val getJacksonJsonOption: GetResult[Option[JsonNode]] = mkGetResult(_.nextJsonOption())
    implicit val setJacksonJson: SetParameter[JsonNode] = mkSetParameter(pgjson, jnode => objectMapper.stringify(jnode))
    implicit val setJacksonJsonOption: SetParameter[Option[JsonNode]] =
      mkOptionSetParameter[JsonNode](pgjson, jnode => objectMapper.stringify(jnode))
  }
} 
Example 7
Source File: SinkRecordParser.scala    From stream-reactor   with Apache License 2.0 5 votes vote down vote up
package com.datamountaineer.streamreactor.connect.influx.converters

import com.datamountaineer.streamreactor.connect.influx.helpers.Util
import com.datamountaineer.streamreactor.connect.influx.writers.KcqlDetails.Path
import com.datamountaineer.streamreactor.connect.influx.writers.ValuesExtractor
import com.fasterxml.jackson.databind.JsonNode
import com.landoop.json.sql.JacksonJson
import org.apache.kafka.connect.data.{Schema, Struct}
import org.apache.kafka.connect.sink.SinkRecord

import scala.util.Try

object SinkRecordParser {
  type Field = String

  trait ParsedSinkRecord {
    def valueFields(ignored: Set[Path]): Seq[(String, Any)]

    def field(path: Path): Option[Any]
  }

  trait ParsedKeyValueSinkRecord extends ParsedSinkRecord {
    def keyFields(ignored: Set[Path]): Seq[(String, Any)]
  }

  private case class JsonSinkRecord(json: JsonNode) extends ParsedSinkRecord {
    override def valueFields(ignored: Set[Path]): Seq[(String, Any)] = ValuesExtractor.extractAllFields(json, ignored.map(_.value.last))

    override def field(path: Path): Option[Any] = Option(ValuesExtractor.extract(json, path.value))
  }

  private case class StructSinkRecord(struct: Struct) extends ParsedSinkRecord {
    override def valueFields(ignored: Set[Path]): Seq[(String, Any)] = ValuesExtractor.extractAllFields(struct, ignored.map(_.value.last))

    override def field(path: Path): Option[Any] = Option(ValuesExtractor.extract(struct, path.value))
  }

  private case class MapSinkRecord(map: java.util.Map[String, Any]) extends ParsedSinkRecord {
    override def valueFields(ignored: Set[Path]): Seq[(String, Any)] = ValuesExtractor.extractAllFields(map, ignored.map(_.value.last))

    override def field(path: Path): Option[Any] = Option(ValuesExtractor.extract(map, path.value))
  }

  private case class KeyValueRecord(key: ParsedSinkRecord, value: ParsedSinkRecord) extends ParsedKeyValueSinkRecord {
    override def valueFields(ignored: Set[Path]): Seq[(String, Any)] = value.valueFields(ignored)

    override def field(path: Path): Option[Any] = path.value.headOption match {
      case Some(fieldName) if Util.caseInsensitiveComparison(fieldName, Util.KEY_CONSTANT) => key.field(Path(path.value.tail))
      case Some(_) => value.field(path)
      case None => throw new IllegalArgumentException("Unreachable situation detected. Path should never be empty")
    }

    override def keyFields(ignored: Set[Path]): Seq[(String, Any)] = key.valueFields(ignored)
  }

  def build(record: SinkRecord): Try[ParsedKeyValueSinkRecord] = {

    val key = Option(record.keySchema()).map(_.`type`()) match {
      case Some(Schema.Type.STRING) => Try(JsonSinkRecord(JacksonJson.asJson(record.key().asInstanceOf[String])))
      case Some(Schema.Type.STRUCT) => Try(StructSinkRecord(record.key().asInstanceOf[Struct]))
      case None => Try(MapSinkRecord(record.key().asInstanceOf[java.util.Map[String, Any]]))
    }

    val value = Option(record.valueSchema()).map(_.`type`()) match {
      case Some(Schema.Type.STRING) =>
        Try(require(record.value() != null && record.value().getClass == classOf[String], "The SinkRecord payload should be of type String")).flatMap(_ => Try(JsonSinkRecord(JacksonJson.asJson(record.value().asInstanceOf[String]))))
      case Some(Schema.Type.STRUCT) =>
        Try(require(record.value() != null && record.value().getClass == classOf[Struct], "The SinkRecord payload should be of type Struct")).flatMap(_ => Try(StructSinkRecord(record.value().asInstanceOf[Struct])))
      case None =>
        Try(require(record.value() != null && record.value().isInstanceOf[java.util.Map[_, _]], "The SinkRecord payload should be of type java.util.Map[String, Any]")).flatMap(_ => Try(MapSinkRecord(record.value().asInstanceOf[java.util.Map[String, Any]])))
    }

    key
      .flatMap(key => value.map(key -> _))
      .map { case (k, v) => KeyValueRecord(k, v) }
  }
} 
Example 8
Source File: ElasticJsonWriter.scala    From stream-reactor   with Apache License 2.0 5 votes vote down vote up
package com.datamountaineer.streamreactor.connect.elastic6

import java.util

import com.datamountaineer.kcql.{Kcql, WriteModeEnum}
import com.datamountaineer.streamreactor.connect.converters.FieldConverter
import com.datamountaineer.streamreactor.connect.elastic6.config.ElasticSettings
import com.datamountaineer.streamreactor.connect.elastic6.indexname.CreateIndex
import com.datamountaineer.streamreactor.connect.errors.ErrorHandler
import com.datamountaineer.streamreactor.connect.schemas.ConverterUtil
import com.fasterxml.jackson.databind.JsonNode
import com.landoop.sql.Field
import com.sksamuel.elastic4s.Indexable
import com.sksamuel.elastic4s.http.ElasticDsl._
import com.typesafe.scalalogging.StrictLogging
import org.apache.kafka.connect.sink.SinkRecord

import scala.collection.JavaConverters._
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._
import scala.concurrent.{Await, Future}
import scala.util.Try

class ElasticJsonWriter(client: KElasticClient, settings: ElasticSettings)
  extends ErrorHandler with StrictLogging with ConverterUtil {

  logger.info("Initialising Elastic Json writer")

  //initialize error tracker
  initialize(settings.taskRetries, settings.errorPolicy)

  //create the index automatically if it was set to do so
  settings.kcqls.filter(_.isAutoCreate).foreach(client.index)

  private val topicKcqlMap = settings.kcqls.groupBy(_.getSource)

  private val kcqlMap = new util.IdentityHashMap[Kcql, KcqlValues]()
  settings.kcqls.foreach { kcql =>
    kcqlMap.put(kcql,
      KcqlValues(
        kcql.getFields.asScala.map(FieldConverter.apply),
        kcql.getIgnoredFields.asScala.map(FieldConverter.apply),
        kcql.getPrimaryKeys.asScala.map { pk =>
          val path = Option(pk.getParentFields).map(_.asScala.toVector).getOrElse(Vector.empty)
          path :+ pk.getName
        }
      ))

  }


  implicit object SinkRecordIndexable extends Indexable[SinkRecord] {
    override def json(t: SinkRecord): String = convertValueToJson(t).toString
  }

  
  def autoGenId(record: SinkRecord): String = {
    val pks = Seq(record.topic(), record.kafkaPartition(), record.kafkaOffset())
    pks.mkString(settings.pkJoinerSeparator)
  }

  private case class KcqlValues(fields: Seq[Field],
                                ignoredFields: Seq[Field],
                                primaryKeysPath: Seq[Vector[String]])

}


case object IndexableJsonNode extends Indexable[JsonNode] {
  override def json(t: JsonNode): String = t.toString
} 
Example 9
Source File: ServiceAdapter.scala    From ez-framework   with Apache License 2.0 5 votes vote down vote up
package com.ecfront.ez.framework.service.email

import com.ecfront.common.Resp
import com.ecfront.ez.framework.core.EZServiceAdapter
import com.fasterxml.jackson.databind.JsonNode

object ServiceAdapter extends EZServiceAdapter[JsonNode] {

  override def init(parameter: JsonNode): Resp[String] = {
    EmailProcessor.init(
      parameter.path("host").asText(),
      parameter.path("port").asInt(),
      parameter.path("userName").asText(),
      parameter.path("password").asText(),
      parameter.path("protocol").asText(),
      parameter.path("poolSize").asInt(-1),
      parameter.path("defaultSender").asText(),
      parameter.path("defaultSendAddress").asText()
    )
  }

  override def destroy(parameter: JsonNode): Resp[String] = {
    Resp.success("")
  }

  override var serviceName: String = "email"

} 
Example 10
Source File: ServiceAdapter.scala    From ez-framework   with Apache License 2.0 5 votes vote down vote up
package com.ecfront.ez.framework.service.jdbc

import com.ecfront.common.Resp
import com.ecfront.ez.framework.core.EZServiceAdapter
import com.fasterxml.jackson.databind.JsonNode

object ServiceAdapter extends EZServiceAdapter[JsonNode] {

  var createTable:Boolean=false

  override def init(parameter: JsonNode): Resp[String] = {
    createTable=parameter.path("createTable").asBoolean(false)
    val processor = JDBCProcessor(
      parameter.path("url").asText(),
      parameter.path("userName").asText(),
      parameter.path("password").asText()
    )
    if (parameter.has("initialSize")) {
      processor.setInitialSize(parameter.path("initialSize").asInt())
    }
    if (parameter.has("minSize")) {
      processor.setMinSize(parameter.path("minSize").asInt())
    }
    if (parameter.has("maxSize")) {
      processor.setMaxSize(parameter.path("maxSize").asInt())
    }
    if (parameter.has("maxIdleTime")) {
      processor.setMaxIdleTime(parameter.path("maxIdleTime").asInt())
    } else {
      processor.setMaxIdleTime(18000)
    }
    val result = JDBCProcessor.initDS(processor)
    if (parameter.has("package")) {
      EntityContainer.autoBuilding(parameter.get("package").asText())
    }
    result
  }

  override def destroy(parameter: JsonNode): Resp[String] = {
    JDBCProcessor.close()
    Resp.success("")
  }

  override var serviceName: String = "jdbc"

} 
Example 11
Source File: TPSITestService.scala    From ez-framework   with Apache License 2.0 5 votes vote down vote up
package com.ecfront.ez.framework.service.tpsi

import java.util.concurrent.CountDownLatch

import com.ecfront.common.Resp
import com.ecfront.ez.framework.core.rpc.{REPLY, RESP, RPC, SUB}
import com.fasterxml.jackson.databind.JsonNode

import scala.beans.BeanProperty

@RPC("/tpsi/","","")
object TPSITestService extends TPSIService {

  val counter = new CountDownLatch(3)

  @REPLY("reply/","","","","")
  def reply(parameter: Map[String, String], body: TPSITestObj): Resp[TPSITestObj] = {
    assert(parameter("id") == "1")
    assert(body.t == "测试")
    assert(body.d == 2.2)
    exec(parameter("id"), "reply", body)
  }

  @SUB("sub/","","","","")
  def sub(parameter: Map[String, String], body: TPSITestObj): Resp[TPSITestObj] = {
    assert(parameter("id") == "1")
    assert(body.t == "测试")
    assert(body.d == 2.2)
    exec(parameter("id"), "sub", body)
  }

  @RESP("resp/","","","","")
  def resp(parameter: Map[String, String], body: TPSITestObj): Resp[TPSITestObj] = {
    assert(parameter("id") == "1")
    assert(body.t == "测试")
    assert(body.d == 2.2)
    exec(parameter("id"), "resp", body)
  }

  override protected def init(args: JsonNode): Unit = {
    assert(args.get("tt").asText() == "字段")
  }

  def exec(id: String, funName: String, body: TPSITestObj): Resp[TPSITestObj] = {
    execute(id, funName, {
      Thread.sleep(100)
      body
    }, {
      body =>
        counter.countDown()
        Resp.success(body.asInstanceOf[TPSITestObj])
    })
  }


}

class TPSITestObj {
  @BeanProperty
  var t: String = _
  @BeanProperty
  var d: BigDecimal = _
}

object TPSITestObj {
  def apply(t: String, d: BigDecimal): TPSITestObj = {
    val obj = new TPSITestObj()
    obj.t = t
    obj.d = d
    obj
  }
} 
Example 12
Source File: RedisClusterManage.scala    From ez-framework   with Apache License 2.0 5 votes vote down vote up
package com.ecfront.ez.framework.cluster.redis

import com.ecfront.common.Resp
import com.ecfront.ez.framework.core.EZManager
import com.ecfront.ez.framework.core.cluster.ClusterManage
import com.fasterxml.jackson.databind.JsonNode
import redis.clients.jedis._

import scala.collection.JavaConversions._

object RedisClusterManage extends ClusterManage {

  private var redisCluster: JedisCluster = _
  private var redisPool: JedisPool = _

  override def init(config: JsonNode): Resp[Void] = {
    val address = config.get("address").asText().split(";")
    val db = config.path("db").asInt(0)
    val auth = config.path("auth").asText("")

    if (address.size == 1) {
      val Array(host, port) = address.head.split(":")
      redisPool = new JedisPool(
        new JedisPoolConfig(), host, port.toInt,
        Protocol.DEFAULT_TIMEOUT, if (auth == null || auth.isEmpty) null else auth, db)
    } else {
      val node = address.map {
        addr =>
          val Array(host, port) = addr.split(":")
          new HostAndPort(host, port.toInt)
      }.toSet
      redisCluster = new JedisCluster(node)
      // TODO select db & pwd
    }
    Resp.success(null)
  }


  def client(): JedisCommands = {
    if (redisPool != null) {
      if(!EZManager.isClose) {
        redisPool.getResource
      }else{
        null
      }
    } else {
      redisCluster
    }
  }

  override def close(): Unit = {
    if (redisCluster != null) {
      redisCluster.close()
    }
    if (redisPool != null) {
      redisPool.destroy()
      redisPool.close()
    }
  }

  private[redis] def execute[T](client: JedisCommands, fun: JedisCommands => T, method: String): T = {
    try {
      if (client != null) {
        fun(client)
      } else {
        logger.warn("Redis is closed.")
        null.asInstanceOf[T]
      }
    } catch {
      case e: Throwable =>
        logger.error(s"Redis $method error.", e)
        throw e
    } finally {
      if (RedisClusterManage.redisPool != null && client != null) {
        client.asInstanceOf[Jedis].close()
      }
    }
  }

  def flushdb(): Unit = {
    execute[Unit](RedisClusterManage.client(), {
      client =>
        if (redisPool != null) {
          client.asInstanceOf[Jedis].flushDB()
        }
    }, "flushdb")
  }

} 
Example 13
Source File: Partition.scala    From comet-data-pipeline   with Apache License 2.0 5 votes vote down vote up
package com.ebiznext.comet.schema.model

import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
import com.fasterxml.jackson.databind.node.ArrayNode
import com.fasterxml.jackson.databind.{DeserializationContext, JsonDeserializer, JsonNode}


@JsonDeserialize(using = classOf[PartitionDeserializer])
case class Partition(
  sampling: Option[Double],
  attributes: Option[List[String]]
) {
  def getAttributes(): List[String] = attributes.getOrElse(Nil)

  def getSampling() = sampling.getOrElse(0.0)

}

class PartitionDeserializer extends JsonDeserializer[Partition] {

  override def deserialize(jp: JsonParser, ctx: DeserializationContext): Partition = {
    val node: JsonNode = jp.getCodec().readTree[JsonNode](jp)
    deserialize(node)
  }

  def deserialize(node: JsonNode): Partition = {
    def isNull(field: String): Boolean =
      node.get(field) == null || node.get(field).isNull

    val sampling =
      if (isNull("sampling")) 0.0
      else
        node.get("sampling").asDouble()

    import scala.collection.JavaConverters._
    val attributes =
      if (isNull("attributes")) None
      else
        Some(
          node
            .get("attributes")
            .asInstanceOf[ArrayNode]
            .elements
            .asScala
            .toList
            .map(_.asText())
        )
    Partition(Some(sampling), attributes)
  }
} 
Example 14
Source File: RabbitmqClusterManage.scala    From ez-framework   with Apache License 2.0 5 votes vote down vote up
package com.ecfront.ez.framework.cluster.rabbitmq

import java.util.concurrent.CopyOnWriteArrayList

import com.ecfront.common.Resp
import com.ecfront.ez.framework.core.cluster.ClusterManage
import com.fasterxml.jackson.databind.JsonNode
import com.rabbitmq.client.{Channel, Connection, ConnectionFactory}

import scala.collection.JavaConversions._

object RabbitmqClusterManage extends ClusterManage {

  private var conn: Connection = _
  private var factory: ConnectionFactory = _
  private val channels: CopyOnWriteArrayList[Channel] = new CopyOnWriteArrayList[Channel]()

  private[rabbitmq] var defaultTopicExchangeName: String = _
  private[rabbitmq] var defaultRPCExchangeName: String = _
  private[rabbitmq] var defaultQueueExchangeName: String = _

  override def init(config: JsonNode): Resp[Void] = {
    factory = new ConnectionFactory()
    if (config.has("userName")) {
      factory.setUsername(config.get("userName").asText())
      factory.setPassword(config.get("password").asText())
    }
    if (config.has("virtualHost")) {
      factory.setVirtualHost(config.get("virtualHost").asText())
    }
    factory.setHost(config.get("host").asText())
    factory.setPort(config.get("port").asInt())
    if (config.has("defaultTopicExchangeName")) {
      defaultTopicExchangeName = config.get("defaultTopicExchangeName").asText()
    }
    if (config.has("defaultRPCExchangeName")) {
      defaultRPCExchangeName = config.get("defaultRPCExchangeName").asText()
    }
    if (config.has("defaultQueueExchangeName")) {
      defaultQueueExchangeName = config.get("defaultQueueExchangeName").asText()
    }
    conn = factory.newConnection()
    Resp.success(null)
  }

  override def close(): Unit = {
    closeChannel()
    conn.close()
  }

  private[rabbitmq] def getChannel(): Channel = {
    val channel = conn.createChannel()
    channels += channel
    channel
  }

  private[rabbitmq] def closeChannel(): Unit = {
    channels.foreach {
      channel =>
        if (channel.isOpen) {
          channel.close()
        }
    }
  }

} 
Example 15
Source File: ExchangeProcessor.scala    From ez-framework   with Apache License 2.0 5 votes vote down vote up
package com.ecfront.ez.framework.service.gateway

import com.ecfront.common.Resp
import com.ecfront.ez.framework.core.EZ
import com.ecfront.ez.framework.core.logger.Logging
import com.ecfront.ez.framework.core.rpc._
import com.fasterxml.jackson.databind.JsonNode

import scala.collection.JavaConversions._

@RPC("/ez/auth/", "网关组件内部处理", "")
object ExchangeProcessor extends Logging {

  @SUB("/ez/gateway/address/add/","接收发布的资源地址","","","")
  def subscribeAddAddress(args: Map[String, String], apiDTO: APIDTO): Resp[Void] = {
    LocalCacheContainer.addRouter(apiDTO.method, apiDTO.path)
  }

  @SUB("/ez/gateway/auth/flush/","刷新权限数据","","","")
  def flushCache(args: Map[String, String], body: String): Resp[Void] = {
    LocalCacheContainer.flushAuth()
  }

  @SUB("rbac/organization/add/","添加组织信息","",
    """
      |code|string|组织编码|true
    ""","")
  def subscribeAddOrganization(args: Map[String, String], org: Map[String, String]): Resp[Void] = {
    LocalCacheContainer.addOrganization(org("code"))
  }

  @SUB("rbac/organization/remove/","删除组织信息","",
    """
      |code|string|组织编码|true
    ""","")
  def subscribeRemoveOrganization(args: Map[String, String], org: Map[String, String]): Resp[Void] = {
    LocalCacheContainer.removeOrganization(org("code"))
  }

  @SUB("rbac/resource/add/","添加资源信息","",
    """
      |method|string|资源方法|true
      |uri|string|资源路径|true
    ""","")
  def subscribeAddResource(args: Map[String, String], res: Map[String, String]): Resp[Void] = {
    LocalCacheContainer.addResource(res("method"), res("uri"))
  }

  @SUB("rbac/resource/remove/","删除资源信息","",
    """
      |code|string|资源编码|true
    ""","")
  def subscribeRemoveResource(args: Map[String, String], res: Map[String, String]): Resp[Void] = {
    val code = res("code")
    val Array(method, uri) = code.split(EZ.eb.ADDRESS_SPLIT_FLAG)
    LocalCacheContainer.removeResource(method, uri)
  }

  @SUB("rbac/role/add/","添加角色信息","",
    """
      |code|string|角色编码|true
      |resource_codes|array|此角色对应的资源|true
    ""","")
  def subscribeAddRole(args: Map[String, String], res: JsonNode): Resp[Void] = {
    LocalCacheContainer.addRole(res.get("code").asText(), res.get("resource_codes").map(_.asText()).toSet)
  }

  @SUB("rbac/role/remove/","删除角色信息","",
    """
      |code|string|角色编码|true
    ""","")
  def subscribeRemoveRole(args: Map[String, String], res: JsonNode): Resp[Void] = {
    LocalCacheContainer.removeRole(res.get("code").asText())
  }

} 
Example 16
Source File: RespHttpClientProcessor.scala    From ez-framework   with Apache License 2.0 5 votes vote down vote up
package com.ecfront.ez.framework.core.rpc

import com.ecfront.common.{JsonHelper, Resp, StandardCode}
import com.ecfront.ez.framework.core.logger.Logging
import com.ecfront.ez.framework.core.rpc.Method.Method
import com.fasterxml.jackson.databind.JsonNode


  def delete[E: Manifest](url: String, contentType: String = "application/json; charset=utf-8"): Resp[E] = {
    request[E](Method.DELETE, url, null, contentType)
  }

  private def request[E](method: Method, url: String, body: Any, contentType: String)(implicit m: Manifest[E]): Resp[E] = {
    val resp = HttpClientProcessor.request(method, url, body, contentType, Map())
    val json = JsonHelper.toJson(resp)
    val code = json.get("code").asText()
    if (code == StandardCode.SUCCESS) {
      val jsonBody = json.get("body")
      if (jsonBody == null) {
        Resp[E](StandardCode.SUCCESS, "")
      } else {
        val body = jsonBody match {
          case obj: JsonNode =>
            JsonHelper.toObject[E](obj)
          case _ =>
            jsonBody.asInstanceOf[E]
        }
        Resp.success[E](body)
      }
    } else {
      Resp[E](code, json.get("message").asText())
    }
  }

} 
Example 17
Source File: EZConfig.scala    From ez-framework   with Apache License 2.0 5 votes vote down vote up
package com.ecfront.ez.framework.core.config

import com.fasterxml.jackson.databind.JsonNode


private[core] case class EZConfig(ez: EZInfo, var args: JsonNode)

// EZ服务配置项
private[core] case class EZInfo(
                                 // APP名称
                                 var app: String,
                                 // 模块名称
                                 var module: String,
                                 // 实例名称
                                 var instance: String,
                                 // 集群信息
                                 var cluster: JsonNode,
                                 // RPC信息
                                 var rpc: Map[String, Any],
                                 // 时区
                                 var timezone: String,
                                 // 语言
                                 var language: String,
                                 var isDebug: Boolean,
                                 // 性能设置
                                 var perf: collection.mutable.Map[String, Any],
                                 // 服务配置项
                                 var services: Map[String, Any]
                               ) 
Example 18
Source File: NatsClusterManage.scala    From ez-framework   with Apache License 2.0 5 votes vote down vote up
package com.ecfront.ez.framework.cluster.nats

import com.ecfront.common.Resp
import com.ecfront.ez.framework.core.cluster.ClusterManage
import com.fasterxml.jackson.databind.JsonNode
import io.nats.client.{Connection, ConnectionFactory}

import scala.collection.JavaConversions._

object NatsClusterManage extends ClusterManage {

  private var connection: Connection = _
  private[nats] var timeout: Long = _

  override def init(config: JsonNode): Resp[Void] = {
    val address = config.get("address").map("nats://"+_.asText()).toArray
    timeout = config.path("timeout").asLong(-1)

    val cf = new ConnectionFactory()
    cf.setServers(address)
    connection = cf.createConnection()
    Resp.success(null)
  }

  private[nats] def getConnection = connection

  override def close(): Unit = {
    connection.close()
  }

} 
Example 19
Source File: SchemaValidation.scala    From iep-apps   with Apache License 2.0 5 votes vote down vote up
package com.netflix.iep.lwc.fwd.admin

import com.fasterxml.jackson.databind.JsonNode
import com.github.fge.jsonschema.main.JsonSchema
import com.github.fge.jsonschema.main.JsonSchemaFactory
import com.netflix.atlas.json.Json
import com.typesafe.scalalogging.StrictLogging

import scala.io.Source
import scala.jdk.CollectionConverters._

class SchemaValidation extends StrictLogging {

  val schema: JsonSchema = {
    val reader = Source.fromResource("cw-fwding-cfg-schema.json").reader()
    try {
      JsonSchemaFactory
        .byDefault()
        .getJsonSchema(Json.decode[SchemaCfg](reader).schema)
    } finally {
      reader.close()
    }
  }

  def validate(key: String, json: JsonNode): Unit = {
    val pr = schema.validate(json)
    if (!pr.isSuccess) {
      throw new IllegalArgumentException(
        pr.asScala.map(_.getMessage).mkString("\n")
      )
    }
  }

}

case class SchemaCfg(schema: JsonNode, validationHook: String) 
Example 20
Source File: Api.scala    From iep-apps   with Apache License 2.0 5 votes vote down vote up
package com.netflix.iep.lwc.fwd.admin

import akka.actor.ActorSystem
import akka.http.scaladsl.model._
import akka.http.scaladsl.server.Directives.entity
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.Route
import akka.http.scaladsl.unmarshalling.Unmarshaller._
import com.fasterxml.jackson.databind.JsonNode
import com.netflix.atlas.akka.CustomDirectives._
import com.netflix.atlas.akka.WebApi
import com.netflix.atlas.json.Json
import com.netflix.iep.lwc.fwd.cw.ExpressionId
import com.netflix.iep.lwc.fwd.cw.Report
import com.netflix.spectator.api.Registry
import com.typesafe.scalalogging.StrictLogging

import scala.concurrent.Future

class Api(
  registry: Registry,
  schemaValidation: SchemaValidation,
  cwExprValidations: CwExprValidations,
  markerService: MarkerService,
  purger: Purger,
  exprDetailsDao: ExpressionDetailsDao,
  system: ActorSystem
) extends WebApi
    with StrictLogging {

  private implicit val configUnmarshaller =
    byteArrayUnmarshaller.map(Json.decode[JsonNode](_))

  private implicit val blockingDispatcher = system.dispatchers.lookup("blocking-dispatcher")

  def routes: Route = {

    endpointPath("api" / "v1" / "cw" / "check", Remaining) { key =>
      post {
        entity(as[JsonNode]) { json =>
          complete {
            schemaValidation.validate(key, json)
            cwExprValidations.validate(key, json)

            HttpResponse(StatusCodes.OK)
          }
        }
      }
    } ~
    endpointPath("api" / "v1" / "cw" / "report") {
      post {
        entity(as[JsonNode]) { json =>
          complete {
            Json
              .decode[List[Report]](json)
              .foreach { report =>
                val enqueued = markerService.queue.offer(report)
                if (!enqueued) {
                  logger.warn(s"Unable to queue report $report")
                }
              }
            HttpResponse(StatusCodes.OK)
          }
        }
      }
    } ~
    endpointPath("api" / "v1" / "cw" / "expr" / "purgeEligible") {
      get {
        parameter("events".as(CsvSeq[String])) { events =>
          complete {
            Future {
              val body = Json.encode(
                exprDetailsDao.queryPurgeEligible(
                  System.currentTimeMillis(),
                  events.toList
                )
              )

              HttpResponse(
                StatusCodes.OK,
                entity = HttpEntity(MediaTypes.`application/json`, body)
              )
            }
          }
        }
      }
    } ~
    endpointPath("api" / "v1" / "cw" / "expr" / "purge") {
      delete {
        entity(as[JsonNode]) { json =>
          complete {
            purger.purge(Json.decode[List[ExpressionId]](json))
          }
        }
      }
    }
  }

} 
Example 21
Source File: JacksonCompat.scala    From circe-jackson   with Apache License 2.0 5 votes vote down vote up
package io.circe.jackson

import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.{ DeserializationContext, JsonNode, ObjectMapper, ObjectWriter }
import com.fasterxml.jackson.databind.node.ObjectNode

private[jackson] trait JacksonCompat {
  protected def makeWriter(mapper: ObjectMapper): ObjectWriter = mapper.writerWithDefaultPrettyPrinter[ObjectWriter]()

  protected def handleUnexpectedToken(context: DeserializationContext)(
    klass: Class[_],
    parser: JsonParser
  ): Unit =
    throw context.mappingException(klass)

  protected def objectNodeSetAll(node: ObjectNode, fields: java.util.Map[String, JsonNode]): JsonNode =
    node.setAll(fields)
} 
Example 22
Source File: JacksonCompat.scala    From circe-jackson   with Apache License 2.0 5 votes vote down vote up
package io.circe.jackson

import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.{ DeserializationContext, JsonNode, ObjectMapper, ObjectWriter }
import com.fasterxml.jackson.databind.node.ObjectNode

private[jackson] trait JacksonCompat {
  protected def makeWriter(mapper: ObjectMapper): ObjectWriter = mapper.writerWithDefaultPrettyPrinter()

  protected def handleUnexpectedToken(context: DeserializationContext)(
    klass: Class[_],
    parser: JsonParser
  ): Unit =
    context.handleUnexpectedToken(klass, parser)

  protected def objectNodeSetAll(node: ObjectNode, fields: java.util.Map[String, JsonNode]): JsonNode =
    node.setAll(fields)
} 
Example 23
Source File: JacksonCompat.scala    From circe-jackson   with Apache License 2.0 5 votes vote down vote up
package io.circe.jackson

import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.{ DeserializationContext, JsonNode, ObjectMapper, ObjectWriter }
import com.fasterxml.jackson.databind.node.ObjectNode

private[jackson] trait JacksonCompat {
  protected def makeWriter(mapper: ObjectMapper): ObjectWriter = mapper.writerWithDefaultPrettyPrinter()

  protected def handleUnexpectedToken(context: DeserializationContext)(
    klass: Class[_],
    parser: JsonParser
  ): Unit =
    throw context.mappingException(klass)

  protected def objectNodeSetAll(node: ObjectNode, fields: java.util.Map[String, JsonNode]): JsonNode =
    node.setAll(fields)
} 
Example 24
Source File: JacksonCompat.scala    From circe-jackson   with Apache License 2.0 5 votes vote down vote up
package io.circe.jackson

import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.{ DeserializationContext, JsonNode, ObjectMapper, ObjectWriter }
import com.fasterxml.jackson.databind.node.ObjectNode

private[jackson] trait JacksonCompat {
  protected def makeWriter(mapper: ObjectMapper): ObjectWriter = mapper.writerWithDefaultPrettyPrinter()

  protected def handleUnexpectedToken(context: DeserializationContext)(
    klass: Class[_],
    parser: JsonParser
  ): Unit =
    context.handleUnexpectedToken(klass, parser)

  protected def objectNodeSetAll(node: ObjectNode, fields: java.util.Map[String, JsonNode]): JsonNode =
    node.setAll[JsonNode](fields)
} 
Example 25
Source File: DefinitionBuilder.scala    From swagger-check   with MIT License 5 votes vote down vote up
package de.leanovate.swaggercheck.schema.jackson

import com.fasterxml.jackson.annotation.{JsonCreator, JsonProperty}
import com.fasterxml.jackson.databind.{JsonNode, ObjectMapper}
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import de.leanovate.swaggercheck.schema.model._

@JsonDeserialize(builder = classOf[DefinitionBuilder])
trait DefinitionMixin {

}

class DefinitionBuilder @JsonCreator()(
                                        @JsonProperty("type") schemaType: Option[String] = None,
                                        @JsonProperty("allOf") allOf: Option[Seq[Definition]] = None,
                                        @JsonProperty("enum") enum: Option[List[String]] = None,
                                        @JsonProperty("exclusiveMinimum") exclusiveMinimum: Option[Boolean] = None,
                                        @JsonProperty("exclusiveMaximum") exclusiveMaximum: Option[Boolean] = None,
                                        @JsonProperty("format") format: Option[String] = None,
                                        @JsonProperty("items") items: Option[Definition] = None,
                                        @JsonProperty("minItems") minItems: Option[Int] = None,
                                        @JsonProperty("maxItems") maxItems: Option[Int] = None,
                                        @JsonProperty("minimum") minimum: Option[BigDecimal] = None,
                                        @JsonProperty("maximum") maximum: Option[BigDecimal] = None,
                                        @JsonProperty("minLength") minLength: Option[Int] = None,
                                        @JsonProperty("maxLength") maxLength: Option[Int] = None,
                                        @JsonProperty("oneOf") oneOf: Option[Seq[Definition]] = None,
                                        @JsonProperty("pattern") pattern: Option[String] = None,
                                        @JsonProperty("properties") properties: Option[Map[String, Definition]] = None,
                                        @JsonProperty("additionalProperties") additionalPropertiesNode: Option[JsonNode] = None,
                                        @JsonProperty("required") required: Option[Set[String]] = None,
                                        @JsonProperty("$ref") ref: Option[String] = None,
                                        @JsonProperty("uniqueItems") uniqueItems: Option[Boolean] = None) {
  val additionalProperties = additionalPropertiesNode.map {
    case node if node.isBoolean => Left(node.isBinary)
    case node => Right(DefinitionBuilder.mapper.treeToValue(node, classOf[Definition]))
  }

  def build(): Definition = {
    Definition.build(
      schemaType,
      allOf,
      enum,
      exclusiveMinimum,
      exclusiveMaximum,
      format,
      items,
      minItems,
      maxItems,
      minimum,
      maximum,
      minLength,
      maxLength,
      oneOf,
      pattern,
      properties,
      additionalProperties,
      required,
      ref,
      uniqueItems
    )
  }
}

object DefinitionBuilder {
  val mapper = new ObjectMapper().registerModule(DefaultScalaModule).registerModule(JsonSchemaModule)
} 
Example 26
Source File: JSONHelper.scala    From daf-semantics   with Apache License 2.0 5 votes vote down vote up
package utilities

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.JsonNode
import com.typesafe.config.Config

object JSONHelper {

  private val json_mapper = new ObjectMapper
  private val json_writer = json_mapper.writerWithDefaultPrettyPrinter()
  private val json_reader = json_mapper.reader()

  def read(json: String): JsonNode = {
    val content = json.replaceAll("(,)\\s+]", "]") // hack for removing trailing commas (invalid JSON)
    json_reader.readTree(content)
  }

  def write(json_tree: JsonNode): String = {
    json_writer.writeValueAsString(json_tree)
  }

  def pretty(json: String): String = {
    val tree = read(json)
    write(tree)
  }

} 
Example 27
Source File: NominatimLookup.scala    From daf-semantics   with Apache License 2.0 5 votes vote down vote up
package examples.nominatim

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Await
import scala.concurrent.duration.Duration
import play.api.libs.ws.ahc.AhcWSClient
import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.JsonNode
import clients.HTTPClient

// SEE: Prefix.cc Lookup - http://prefix.cc/foaf.file.json

class NominatimLookup {

  val http = HTTPClient

  def start() {
    http.start()
  }

  def stop() {
    http.stop()
  }

  def nominatim(address: String) = {

    val url = "http://nominatim.openstreetmap.org/search"

    val parameters = Map(
      "q" -> address,
      "addressdetails" -> "1",
      "format" -> "json",
      "limit" -> "4",
      "addressdetails" -> "1",
      "dedupe" -> "1",
      "extratags" -> "1",
      "namedetails" -> "1").toList

    val ret = http.ws.url(url)
      .withQueryString(parameters: _*)
      .get()
      .map { response =>
        response.status match {
          case 200 => response.body
          case _   => "{}"
        }

      }

    ret

  }

}

object MainNominatimLookup extends App {

  import scala.collection.JavaConversions._
  import scala.collection.JavaConverters._

  val nominatim = new NominatimLookup
  nominatim.start()

  val json_mapper = new ObjectMapper
  val json_reader = json_mapper.reader()

  val result = Await.ready(nominatim.nominatim("135 pilkington avenue, birmingham"), Duration.Inf)
    .value.get.get

  val json_list: List[JsonNode] = json_reader.readTree(result).elements().toList

  // simulazione di output...
  if (json_list.size > 0) {
    println(s"RESULTS [${json_list.size}]")
    json_list
      .zipWithIndex
      .foreach {
        case (node, i) =>
          println(s"result ${i + 1}")
          println(node.get("place_id"))
          println(node.get("address").get("road").asText() + ", " + node.get("address").get("house_number").asText())
      }
  } else {
    println("cannot find results...")
  }

  nominatim.stop()

} 
Example 28
Source File: RegularBoxScalaTest.scala    From Sidechains-SDK   with MIT License 5 votes vote down vote up
package com.horizen.box

import com.fasterxml.jackson.databind.JsonNode
import com.horizen.fixtures.BoxFixture
import com.horizen.proposition.PublicKey25519Proposition
import com.horizen.serialization.ApplicationJsonSerializer
import com.horizen.utils.{BytesUtils, Ed25519}
import org.junit.Assert._
import org.junit.Test
import org.scalatest.junit.JUnitSuite
import scorex.core.utils.ScorexEncoder

class RegularBoxScalaTest
  extends JUnitSuite with BoxFixture
{

  @Test
  def testToJson(): Unit = {
    val seed = "12345".getBytes
    val keyPair = Ed25519.createKeyPair(seed)
    val privateKey = keyPair.getKey
    val publicKey = keyPair.getValue

    val proposition = new PublicKey25519Proposition(publicKey)
    val nonce = 12345
    val value = 10
    val box = getRegularBox(proposition, nonce, value)

    val serializer = ApplicationJsonSerializer.getInstance()
    serializer.setDefaultConfiguration()

    val jsonStr = serializer.serialize(box)

    val node: JsonNode = serializer.getObjectMapper().readTree(jsonStr)

    assertEquals("Json must contain only 1 proposition.",
      1, node.findValues("proposition").size())
    assertEquals("Proposition json content must be the same.",
      BytesUtils.toHexString(proposition.pubKeyBytes()),
      node.path("proposition").path("publicKey").asText())

    assertEquals("Json must contain only 1 id.",
      1, node.findValues("id").size())
    assertTrue("Id json value must be the same.",
      box.id().sameElements(ScorexEncoder.default.decode(node.path("id").asText()).get))

    assertEquals("Json must contain only 1 nonce.",
      1, node.findValues("nonce").size())
    assertEquals("Nonce json value must be the same.",
      box.nonce(), node.path("nonce").asLong())

    assertEquals("Json must contain only 1 value.",
      1, node.findValues("value").size())
    assertEquals("Value json value must be the same.",
      box.value(), node.path("value").asLong())

  }
} 
Example 29
Source File: CertifierRightBoxScalaTest.scala    From Sidechains-SDK   with MIT License 5 votes vote down vote up
package com.horizen.box

import com.fasterxml.jackson.databind.JsonNode
import com.horizen.fixtures.BoxFixture
import com.horizen.proposition.PublicKey25519Proposition
import com.horizen.serialization.ApplicationJsonSerializer
import com.horizen.utils.{BytesUtils, Ed25519}
import org.junit.Assert._
import org.junit.Test
import org.scalatest.junit.JUnitSuite
import scorex.core.utils.ScorexEncoder

class CertifierRightBoxScalaTest extends JUnitSuite with BoxFixture
{

  @Test
  def testToJson(): Unit = {
    val seed = "12345".getBytes
    val keyPair = Ed25519.createKeyPair(seed)
    val privateKey = keyPair.getKey
    val publicKey = keyPair.getValue

    val proposition = new PublicKey25519Proposition(publicKey)
    val nonce = 12345
    val value = 10
    val minimumWithdrawalEpoch = 5
    val box = getCertifierRightBox(proposition, nonce, value, minimumWithdrawalEpoch)

    val serializer = ApplicationJsonSerializer.getInstance()
    serializer.setDefaultConfiguration()

    val jsonStr = serializer.serialize(box)

    val node: JsonNode = serializer.getObjectMapper().readTree(jsonStr)

    assertEquals("Json must contain only 1 proposition.",
      1, node.findValues("proposition").size())
    assertEquals("Proposition json content must be the same.",
      BytesUtils.toHexString(proposition.pubKeyBytes()),
      node.path("proposition").path("publicKey").asText())

    assertEquals("Json must contain only 1 id.",
      1, node.findValues("id").size())
    assertTrue("Id json value must be the same.",
      box.id().sameElements(ScorexEncoder.default.decode(node.path("id").asText()).get))

    assertEquals("Json must contain only 1 nonce.",
      1, node.findValues("nonce").size())
    assertEquals("Nonce json value must be the same.",
      box.nonce(), node.path("nonce").asLong())

    assertEquals("Json must contain only 1 value.",
      1, node.findValues("value").size())
    assertEquals("Value json value must be the same.",
      box.value(), node.path("value").asLong())

    assertEquals("Json must contain only 1 activeFromWithdrawalEpoch.",
      1, node.findValues("activeFromWithdrawalEpoch").size())
    assertEquals("ActiveFromWithdrawalEpoch json value must be the same.",
      box.activeFromWithdrawalEpoch(), node.path("activeFromWithdrawalEpoch").asLong())
  }
} 
Example 30
Source File: PublicKey25519PropositionTest.scala    From Sidechains-SDK   with MIT License 5 votes vote down vote up
package com.horizen.proposition

import com.fasterxml.jackson.databind.JsonNode
import com.horizen.serialization.ApplicationJsonSerializer
import com.horizen.utils.Ed25519
import org.junit.Assert.assertEquals
import org.junit.Test
import org.scalatest.junit.JUnitSuite
import scorex.core.utils.ScorexEncoder

class PublicKey25519PropositionScalaTest
  extends JUnitSuite
{

  @Test
  def testToJson(): Unit = {
    val seed = "12345".getBytes
    val keyPair = Ed25519.createKeyPair(seed)
    val privateKey = keyPair.getKey
    val publicKey = keyPair.getValue

    val prop1 = new PublicKey25519Proposition(publicKey)

    val serializer = ApplicationJsonSerializer.getInstance()
    serializer.setDefaultConfiguration()

    val jsonStr = serializer.serialize(prop1)

    val node : JsonNode = serializer.getObjectMapper().readTree(jsonStr)

    assertEquals("Json must contain only 1 publicKey.",
      1, node.findValues("publicKey").size())
    assertEquals("PublicKey json value must be the same.",
      ScorexEncoder.default.encode(prop1.pubKeyBytes()), node.path("publicKey").asText())
  }
} 
Example 31
Source File: Signature25519ScalaTest.scala    From Sidechains-SDK   with MIT License 5 votes vote down vote up
package com.horizen.proof


import com.fasterxml.jackson.databind.JsonNode
import com.horizen.secret.PrivateKey25519Creator
import com.horizen.serialization.ApplicationJsonSerializer
import org.junit.Assert.assertEquals
import org.junit.Test
import org.scalatest.junit.JUnitSuite
import scorex.core.utils.ScorexEncoder

class Signature25519ScalaTest
  extends JUnitSuite
{

  @Test
  def testToJson(): Unit = {
    val testMessage: Array[Byte] = "Test string message to sign/verify.".getBytes
    val seed = "12345".getBytes
    val key = PrivateKey25519Creator.getInstance.generateSecret(seed)
    val prp = key.publicImage
    val pr = key.sign(testMessage)

    val serializer = ApplicationJsonSerializer.getInstance()
    serializer.setDefaultConfiguration()

    val jsonStr = serializer.serialize(pr)

    val node : JsonNode = serializer.getObjectMapper().readTree(jsonStr)

    assertEquals("Json must contain only 1 signature.",
      1, node.findValues("signature").size())
    assertEquals("",
      ScorexEncoder.default.encode(pr.signatureBytes), node.path("signature").asText())
  }
} 
Example 32
Source File: JsonRecord.scala    From hydra   with Apache License 2.0 5 votes vote down vote up
package hydra.kafka.producer

import com.fasterxml.jackson.databind.{JsonNode, ObjectMapper}
import hydra.core.transport.AckStrategy
import org.apache.commons.lang3.StringUtils


case class JsonRecord(
    destination: String,
    key: String,
    payload: JsonNode,
    ackStrategy: AckStrategy
) extends KafkaRecord[String, JsonNode]

object JsonRecord {
  val mapper = new ObjectMapper()

  def apply(
      topic: String,
      key: Option[String],
      obj: Any,
      ackStrategy: AckStrategy
  ): JsonRecord = {
    val payload = mapper.convertValue[JsonNode](obj, classOf[JsonNode])
    new JsonRecord(topic, key.orNull, payload, ackStrategy)
  }
} 
Example 33
Source File: JsonRecordFactory.scala    From hydra   with Apache License 2.0 5 votes vote down vote up
package hydra.kafka.producer

import com.fasterxml.jackson.databind.{JsonNode, ObjectMapper}
import hydra.core.ingest.HydraRequest

import scala.concurrent.{ExecutionContext, Future}


object JsonRecordFactory extends KafkaRecordFactory[String, JsonNode] {

  val mapper = new ObjectMapper()

  override def build(
      request: HydraRequest
  )(implicit ec: ExecutionContext): Future[KafkaRecord[String, JsonNode]] = {
    for {
      topic <- Future.fromTry(getTopic(request))
      payload <- parseJson(request.payload)
    } yield JsonRecord(
      topic,
      getKey(request, payload),
      payload,
      request.ackStrategy
    )

  }

  private def parseJson(json: String)(implicit ec: ExecutionContext) =
    Future(mapper.reader().readTree(json))

} 
Example 34
Source File: KafkaRecordFactory.scala    From hydra   with Apache License 2.0 5 votes vote down vote up
package hydra.kafka.producer

import com.fasterxml.jackson.databind.JsonNode
import hydra.avro.util.SchemaWrapper
import hydra.core.ingest.RequestParams._
import hydra.core.ingest.{HydraRequest, RequestParams}
import hydra.core.protocol.MissingMetadataException
import hydra.core.transport.RecordFactory
import hydra.kafka.producer.KafkaRecordFactory.RecordKeyExtractor
import org.apache.avro.generic.GenericRecord

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


  def getTopicAndSchemaSubject(request: HydraRequest): Try[(String, String)] = {
    val subject = request.metadataValue(RequestParams.HYDRA_SCHEMA_PARAM)
    request.metadataValue(HYDRA_KAFKA_TOPIC_PARAM) match {
      case Some(topic) => Success(topic -> subject.getOrElse(topic))
      case None =>
        Failure(
          MissingMetadataException(
            HYDRA_KAFKA_TOPIC_PARAM,
            "No kafka topic present in the request."
          )
        )
    }
  }
}

object KafkaRecordFactory {

  trait RecordKeyExtractor[K, V] {

    def extractKeyValue(request: HydraRequest, record: V): Option[K]
  }

  object RecordKeyExtractor {

    implicit object StringRecordKeyExtractor
        extends RecordKeyExtractor[String, String] {

      override def extractKeyValue(
          request: HydraRequest,
          record: String
      ): Option[String] = {
        request
          .metadataValue(HYDRA_RECORD_KEY_PARAM)
          .map(key => JsonPathKeys.getKey(key, record))
      }
    }

    implicit object JsonRecordKeyExtractor
        extends RecordKeyExtractor[String, JsonNode] {

      override def extractKeyValue(
          request: HydraRequest,
          record: JsonNode
      ): Option[String] = {
        request
          .metadataValue(HYDRA_RECORD_KEY_PARAM)
          .map(key => JsonPathKeys.getKey(key, record.toString))
      }
    }

    implicit object SchemaKeyExtractor
        extends RecordKeyExtractor[String, GenericRecord] {

      override def extractKeyValue(
          request: HydraRequest,
          payload: GenericRecord
      ): Option[String] = {
        request
          .metadataValue(HYDRA_RECORD_KEY_PARAM)
          .map { key => JsonPathKeys.getKey(key, request.payload) }
          .orElse {
            val schema = payload.getSchema
            val wrapper = SchemaWrapper.from(schema)
            wrapper
              .validate()
              .get //we're throwing the exception here so that the request ends with a 400
            wrapper.primaryKeys.map(payload.get) match {
              case Nil  => None
              case keys => Some(keys.mkString("|"))
            }
          }
      }
    }

  }

} 
Example 35
Source File: JsonBodyWritables.scala    From play-ws   with Apache License 2.0 5 votes vote down vote up
package play.api.libs.ws

import akka.util.ByteString
import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.ObjectMapper
import play.api.libs.json.JsValue
import play.api.libs.json.Json

trait JsonBodyWritables {

  
  implicit val writeableOf_JsValue: BodyWritable[JsValue] = {
    BodyWritable(a => InMemoryBody(ByteString.fromArrayUnsafe(Json.toBytes(a))), "application/json")
  }

  def body(objectMapper: ObjectMapper): BodyWritable[JsonNode] =
    BodyWritable(
      json => InMemoryBody(ByteString.fromArrayUnsafe(objectMapper.writer.writeValueAsBytes(json))),
      "application/json"
    )
}

object JsonBodyWritables extends JsonBodyWritables 
Example 36
Source File: SwaggerAPI.scala    From swagger-check   with MIT License 5 votes vote down vote up
package de.leanovate.swaggercheck.schema

import java.io.InputStream

import com.fasterxml.jackson.annotation.{JsonCreator, JsonProperty}
import com.fasterxml.jackson.core.JsonFactory
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
import com.fasterxml.jackson.databind.{DeserializationFeature, JsonNode, MappingJsonFactory, ObjectMapper}
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import de.leanovate.swaggercheck.schema.jackson.JsonSchemaModule
import de.leanovate.swaggercheck.schema.model.{Definition, Parameter}

import scala.collection.JavaConverters._
import scala.io.Source

@JsonDeserialize(builder = classOf[SwaggerAPIBuilder])
case class SwaggerAPI(
                       basePath: Option[String],
                       paths: Map[String, Map[String, Operation]],
                       definitions: Map[String, Definition]
                     )

object SwaggerAPI {
  val jsonMapper = objectMapper(new MappingJsonFactory())
  val yamlMapper = objectMapper(new YAMLFactory())

  def parse(jsonOrYaml: String): SwaggerAPI = {
    val mapper = if (jsonOrYaml.trim().startsWith("{")) jsonMapper else yamlMapper
    mapper.readValue(jsonOrYaml, classOf[SwaggerAPI])
  }

  def parse(swaggerInput: InputStream): SwaggerAPI = {
    parse(Source.fromInputStream(swaggerInput).mkString)
  }

  def objectMapper(jsonFactory: JsonFactory): ObjectMapper = {
    val mapper = new ObjectMapper(jsonFactory)
    mapper.registerModule(DefaultScalaModule)
    mapper.registerModule(JsonSchemaModule)
    mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
    mapper
  }
}

class SwaggerAPIBuilder @JsonCreator()(
                                        @JsonProperty("basePath") basePath: Option[String],
                                        @JsonProperty("consumes") consumes: Option[Seq[String]],
                                        @JsonProperty("produces") produces: Option[Seq[String]],
                                        @JsonProperty("paths") paths: Option[Map[String, JsonNode]],
                                        @JsonProperty("definitions") definitions: Option[Map[String, Definition]],
                                        @JsonProperty("parameters") globalParameters: Option[Map[String, Parameter]]
                                      ) {
  def build(): SwaggerAPI = {
    val defaultConsumes = consumes.map(_.toSet).getOrElse(Set.empty)
    val defaultProduces = produces.map(_.toSet).getOrElse(Set.empty)
    SwaggerAPI(basePath,
      paths.getOrElse(Map.empty).map {
        case (path, pathDefinition) =>
          val defaultParameters = Option(pathDefinition.get("parameters")).map {
             node =>
               node.iterator().asScala.map {
                 element => SwaggerAPI.jsonMapper.treeToValue(element, classOf[OperationParameter])
               }.toSeq
          }.getOrElse(Seq.empty)

          basePath.map(_ + path).getOrElse(path) -> pathDefinition.fields().asScala.filter(_.getKey != "parameters").map {
            entry =>
              val operation = SwaggerAPI.jsonMapper.treeToValue(entry.getValue, classOf[Operation])
              entry.getKey.toUpperCase -> operation.withDefaults(defaultParameters, defaultConsumes, defaultProduces).resolveGlobalParameters(globalParameters.getOrElse(Map()))
          }.toMap
      },
      definitions.getOrElse(Map.empty))
  }
} 
Example 37
Source File: HttpOrderBook.scala    From matcher   with MIT License 5 votes vote down vote up
package com.wavesplatform.dex.api.http.entities

import com.fasterxml.jackson.core.{JsonGenerator, JsonParser}
import com.fasterxml.jackson.databind.annotation.JsonSerialize
import com.fasterxml.jackson.databind.deser.std.StdDeserializer
import com.fasterxml.jackson.databind.module.SimpleModule
import com.fasterxml.jackson.databind.ser.std.StdSerializer
import com.fasterxml.jackson.databind.{DeserializationContext, JsonNode, ObjectMapper, SerializerProvider}
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import com.fasterxml.jackson.module.scala.experimental.ScalaObjectMapper
import com.wavesplatform.dex.domain.asset.Asset.{IssuedAsset, Waves}
import com.wavesplatform.dex.domain.asset.{Asset, AssetPair}
import com.wavesplatform.dex.domain.bytes.ByteStr
import com.wavesplatform.dex.domain.model.Denormalization
import com.wavesplatform.dex.model.LevelAgg

@JsonSerialize(using = classOf[HttpOrderBook.Serializer])
case class HttpOrderBook(timestamp: Long, pair: AssetPair, bids: Seq[LevelAgg], asks: Seq[LevelAgg], assetPairDecimals: Option[(Int, Int)] = None)

object HttpOrderBook {

  private val coreTypeSerializers = new SimpleModule()
  coreTypeSerializers.addDeserializer(classOf[AssetPair], new AssetPairDeserializer)

  private val mapper = new ObjectMapper() with ScalaObjectMapper
  mapper.registerModule(DefaultScalaModule)
  mapper.registerModule(coreTypeSerializers)

  private def serialize(value: Any): String = mapper.writeValueAsString(value)

  private class AssetPairDeserializer extends StdDeserializer[AssetPair](classOf[AssetPair]) {

    override def deserialize(p: JsonParser, ctxt: DeserializationContext): AssetPair = {
      val node = p.getCodec.readTree[JsonNode](p)

      def readAssetId(fieldName: String): Asset = {
        val x = node.get(fieldName).asText(Asset.WavesName)
        if (x == Asset.WavesName) Waves else IssuedAsset(ByteStr.decodeBase58(x).get)
      }

      AssetPair(readAssetId("amountAsset"), readAssetId("priceAsset"))
    }
  }

  private def formatValue(value: BigDecimal, decimals: Int): String = new java.text.DecimalFormat(s"0.${"0" * decimals}").format(value)

  private def denormalizeAndSerializeSide(side: Seq[LevelAgg], amountAssetDecimals: Int, priceAssetDecimals: Int, jg: JsonGenerator): Unit = {
    side.foreach { levelAgg =>
      val denormalizedPrice  = Denormalization.denormalizePrice(levelAgg.price, amountAssetDecimals, priceAssetDecimals)
      val denormalizedAmount = Denormalization.denormalizeAmountAndFee(levelAgg.amount, amountAssetDecimals)

      jg.writeStartArray(2)
      jg.writeString(formatValue(denormalizedPrice, priceAssetDecimals))
      jg.writeString(formatValue(denormalizedAmount, amountAssetDecimals))
      jg.writeEndArray()
    }
  }

  def toJson(x: HttpOrderBook): String = serialize(x)

  class Serializer extends StdSerializer[HttpOrderBook](classOf[HttpOrderBook]) {
    override def serialize(x: HttpOrderBook, j: JsonGenerator, serializerProvider: SerializerProvider): Unit = {
      j.writeStartObject()
      j.writeNumberField("timestamp", x.timestamp)

      x.assetPairDecimals.fold {

        j.writeFieldName("pair")
        j.writeStartObject()
        j.writeStringField("amountAsset", x.pair.amountAssetStr)
        j.writeStringField("priceAsset", x.pair.priceAssetStr)
        j.writeEndObject()

        j.writeArrayFieldStart("bids")
        x.bids.foreach(j.writeObject)
        j.writeEndArray()

        j.writeArrayFieldStart("asks")
        x.asks.foreach(j.writeObject)
        j.writeEndArray()

      } {
        case (amountAssetDecimals, priceAssetDecimals) =>
          j.writeArrayFieldStart("bids")
          denormalizeAndSerializeSide(x.bids, amountAssetDecimals, priceAssetDecimals, j)
          j.writeEndArray()

          j.writeArrayFieldStart("asks")
          denormalizeAndSerializeSide(x.asks, amountAssetDecimals, priceAssetDecimals, j)
          j.writeEndArray()
      }

      j.writeEndObject()
    }
  }
} 
Example 38
Source File: LoggedUser.scala    From sparta   with Apache License 2.0 5 votes vote down vote up
package com.stratio.sparta.serving.core.models.dto

import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.node.ArrayNode

import scala.collection.JavaConverters._
import scala.util.Try


object LoggedUser{

  implicit def jsonToDto(stringJson: String): Option[LoggedUser] = {
    if (stringJson.trim.isEmpty) None
    else {
      implicit val json = new ObjectMapper().readTree(stringJson)
      Some(LoggedUser(getValue(LoggedUserConstant.infoIdTag), getValue(LoggedUserConstant.infoNameTag),
        getValue(LoggedUserConstant.infoMailTag, Some(LoggedUserConstant.dummyMail)),
        getValue(LoggedUserConstant.infoGroupIDTag), getArrayValues(LoggedUserConstant.infoGroupsTag),
        getArrayValues(LoggedUserConstant.infoRolesTag)))
    }
  }

  private def getValue(tag: String, defaultElse: Option[String]= None)(implicit json: JsonNode) : String = {
    Option(json.findValue(tag)) match {
      case Some(jsonValue) =>
        defaultElse match{
          case Some(value) => Try(jsonValue.asText()).getOrElse(value)
          case None => Try(jsonValue.asText()).get
        }
      case None =>
        defaultElse match {
          case Some(value) => value
          case None => ""
        }
    }
  }

  private def getArrayValues(tag:String)(implicit jsonNode: JsonNode): Seq[String] = {
    Option(jsonNode.findValue(tag)) match {
      case Some(roles: ArrayNode) => roles.asScala.map(x => x.asText()).toSeq
      case None => Seq.empty[String]
    }
  }
}

case class LoggedUser(id: String, name: String, email: String, gid: String,
                      groups:Seq[String], roles: Seq[String]){

  def isAuthorized(securityEnabled: Boolean, allowedRoles: Seq[String] = LoggedUserConstant.allowedRoles): Boolean = {
    if(securityEnabled){
      roles.intersect(allowedRoles).nonEmpty
    } else true
  }

} 
Example 39
Source File: JSONMatcher.scala    From avrohugger   with Apache License 2.0 5 votes vote down vote up
package avrohugger
package matchers

import stores.SchemaStore

import scala.reflect.runtime.universe._
import scala.reflect.runtime._

import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.node._

object JsonMatcher {

  def toJsonNode(namespace: Option[Name], dv: Tree, schemaStore: SchemaStore) : JsonNode = {

    def getFullName(nme: String): String = {
      namespace match {
        case Some(ns) => ns + "+" + nme
        case None => nme
      }
    } 

    lazy val jsonNodeFactory = JsonNodeFactory.instance 

    dv match {
      // use of null here is for Java interop, builds Avro FieldConstructor w/o default value
      case EmptyTree                                   => null 
      case Literal(Constant(x: Unit))                  => jsonNodeFactory.nullNode
      case Literal(Constant(x: Boolean))               => jsonNodeFactory.booleanNode(x)
      case Literal(Constant(x: Int))                   => jsonNodeFactory.numberNode(x)
      case Literal(Constant(x: Long))                  => jsonNodeFactory.numberNode(x)
      case Literal(Constant(x: Float))                 => jsonNodeFactory.numberNode(x)
      case Literal(Constant(x: Double))                => jsonNodeFactory.numberNode(x)
      case Literal(Constant(x: String))                => jsonNodeFactory.textNode(x)
      case Literal(Constant(null))                     => jsonNodeFactory.nullNode
      case Ident(NameTag("None"))                      => jsonNodeFactory.nullNode
      case Apply(Ident(NameTag("Some")), List(x))      => toJsonNode(namespace, x, schemaStore)
      case Apply(Ident(NameTag("List")), xs)           => {
        val jsonArray = jsonNodeFactory.arrayNode
        xs.map(x => toJsonNode(namespace, x, schemaStore)).map(v => jsonArray.add(v))
        jsonArray
      }
      case Apply(Ident(NameTag("Map")), kvps)          => {
        val jsonObject = jsonNodeFactory.objectNode
        kvps.foreach(kvp => kvp match {
          case Apply(Select(Literal(Constant(key: String)), NameTag(tn)), List(x)) =>  {
            jsonObject.put(key, toJsonNode(namespace, x, schemaStore))
          }
        })
        jsonObject
      }
      // if the default value is another (i.e. nested) record/case class
      case Apply(Ident(NameTag(name)), xs) if schemaStore.schemas.contains(getFullName(name.toString)) => {
        val jsonObject = jsonNodeFactory.objectNode
        xs.zipWithIndex.map( x => {
          val value = x._1
          val index = x._2
          val nestedRecordField = schemaStore.schemas(getFullName(name.toString)).getFields.get(index)
          // values from the tree, field names from cross referencing tree's pos with schema field pos
          // (they always correspond since the schema is defined based on the fields in a class def)
          jsonObject.put(nestedRecordField.name, toJsonNode(namespace, value, schemaStore))
        })
        jsonObject
      }
      // enum
      case Select(Ident(NameTag(enum)), NameTag(enumValue)) => jsonNodeFactory.textNode(enumValue.toString())
      case x => sys.error("Could not extract default value. Found: " + x + ", " + showRaw(x))
    }
  } 
} 
Example 40
Source File: JacksonDefinitions.scala    From borer   with Mozilla Public License 2.0 5 votes vote down vote up
package io.bullet.borer.benchmarks

import com.fasterxml.jackson.core.`type`.TypeReference
import com.fasterxml.jackson.databind.{JsonNode, ObjectMapper}
import com.fasterxml.jackson.module.afterburner.AfterburnerModule
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import org.openjdk.jmh.annotations._

object JacksonCodecs {
  val mapper: ObjectMapper = new ObjectMapper().registerModule(DefaultScalaModule).registerModule(new AfterburnerModule)

  val foosTypeRef = new TypeReference[Map[String, Foo]] {}
  val intsTypeRef = new TypeReference[List[Int]] {}
}

import io.bullet.borer.benchmarks.JacksonCodecs._

class JacksonEncodingBenchmark extends EncodingBenchmark {

  @Benchmark
  def encodeFoos: Array[Byte] = mapper.writeValueAsBytes(foos)

  @Benchmark
  def encodeInts: Array[Byte] = mapper.writeValueAsBytes(ints)

  @Benchmark
  def encodeEmptyArray: Array[Byte] = mapper.writeValueAsBytes(List.empty[Int])
}

class JacksonDecodingBenchmark extends DecodingBenchmark {

  @Benchmark
  def decodeFoos: Map[String, Foo] = mapper.readValue(foosJson, foosTypeRef)

  @Benchmark
  def decodeInts: List[Int] = mapper.readValue(intsJson, intsTypeRef)

  @Benchmark
  def decodeEmptyArray: List[Int] = mapper.readValue(emptyArrayJson, intsTypeRef)
}

class JacksonDomBenchmark extends DomBenchmark {

  private var root: JsonNode = _
  def setup(): Unit          = root = mapper.readTree(fileBytes)

  @Benchmark
  def encodeDom: Array[Byte] = mapper.writeValueAsBytes(root)

  @Benchmark
  def decodeDom: JsonNode = mapper.readTree(fileBytes)
}

class JacksonModelBenchmark extends DomBenchmark {

  private var root: Product = _

  lazy val typeRef: TypeReference[Product] = {
    val c = fileName match {
      case "australia-abc.json"      => new TypeReference[Australia.RootInterface] {}
      case "bitcoin.json"            => new TypeReference[Bitcoin.RootInterface] {}
      case "doj-blog.json"           => new TypeReference[DojBlog.RootInterface] {}
      case "eu-lobby-country.json"   => new TypeReference[EuLobbyCountry.RootInterface] {}
      case "eu-lobby-financial.json" => new TypeReference[EuLobbyFinancial.RootInterface] {}
      case "eu-lobby-repr.json"      => new TypeReference[EuLobbyRepr.RootInterface] {}
      case "github-gists.json"       => new TypeReference[List[GithubGists.RootInterface]] {}
      case "json-generator.json"     => new TypeReference[List[JsonGenerator.RootInterface]] {}
      case "meteorites.json"         => new TypeReference[List[Meteorites.RootInterface]] {}
      case "movies.json"             => new TypeReference[List[Movies.RootInterface]] {}
      case "reddit-scala.json"       => new TypeReference[Reddit.RootInterface[Reddit.Data]] {}
      case "rick-morty.json"         => new TypeReference[RickMorty.RootInterface] {}
      case "temp-anomaly.json"       => new TypeReference[TempAnomaly.RootInterface] {}
      case "thai-cinemas.json"       => new TypeReference[ThaiCinemas.RootInterface] {}
      case "turkish.json"            => new TypeReference[Turkish.RootInterface] {}

      case "github-events.json" =>
        new TypeReference[
          List[GithubEvents.RootInterface[GithubEvents.Head[GithubEvents.Repo1], GithubEvents.Forkee]]] {}

      case "twitter_api_compact_response.json" | "twitter_api_response.json" =>
        new TypeReference[List[TwitterApiResponse.RootInterface]] {}
    }
    c.asInstanceOf[TypeReference[Product]]
  }

  def setup(): Unit =
    root =
      try mapper.readValue(fileBytes, typeRef)
      finally ()

  @Benchmark
  def encodeModel: Array[Byte] = mapper.writeValueAsBytes(root)

  @Benchmark
  def decodeModel: Product = mapper.readValue(fileBytes, typeRef)
} 
Example 41
Source File: ConfigFile.scala    From pizza-auth-3   with MIT License 5 votes vote down vote up
package moe.pizza.auth.config

import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.databind.JsonNode
import moe.pizza.auth.adapters.GraderChain
import moe.pizza.auth.adapters.PilotGraderLike.PilotGraderFactory
import moe.pizza.auth.interfaces.PilotGrader
import moe.pizza.auth.webapp.oauth.OAuthApplication

import scala.concurrent.ExecutionContext
import org.http4s.client.Client


object ConfigFile {
  case class PingBotConfig(
      host: String,
      password: String
  )
  case class EmbeddedLdapConfig(
      instancePath: String = "./ldap",
      port: Int = 389,
      basedn: String = "ou=pizza",
      host: String = "localhost",
      password: Option[String] = None
  )
  case class AuthGroupConfig(closed: List[String],
                             open: List[String],
                             public: List[String])

  case class AuthConfig(
      domain: String,
      corporation: String,
      alliance: String,
      groupName: String,
      groupShortName: String,
      groups: AuthGroupConfig,
      graders: List[JsonNode],
      pingbot: Option[PingBotConfig],
      restkeys: List[String],
      applications: List[OAuthApplication] = List()
  ) {
    def constructGraders(c: ConfigFile)(
        implicit client: Client): PilotGrader =
      new GraderChain(
        graders.map(g => PilotGraderFactory.fromYaml(g, c)).flatten.toList)

  }
  case class CrestConfig(
      @JsonProperty("login_url") loginUrl: String,
      @JsonProperty("crest_url") crestUrl: String,
      clientID: String,
      secretKey: String,
      redirectUrl: String
  )
  case class ConfigFile(
      crest: CrestConfig,
      auth: AuthConfig,
      embeddedldap: EmbeddedLdapConfig
  )

} 
Example 42
Source File: HttpUtil.scala    From CM-Well   with Apache License 2.0 5 votes vote down vote up
package cmwell.analytics.util

import com.fasterxml.jackson.databind.{JsonNode, ObjectMapper}
import org.apache.http.HttpResponse
import org.apache.http.client.methods.{HttpGet, HttpPost}
import org.apache.http.client.{ClientProtocolException, ResponseHandler}
import org.apache.http.entity.{ContentType, StringEntity}
import org.apache.http.impl.client.{CloseableHttpClient, HttpClients}
import org.apache.http.util.EntityUtils


object HttpUtil {

  private val mapper = new ObjectMapper()

  private val responseHandler = new ResponseHandler[String]() {

    override def handleResponse(response: HttpResponse): String = {
      val status = response.getStatusLine.getStatusCode
      if (status >= 200 && status < 300) {
        val entity = response.getEntity

        if (entity != null)
          EntityUtils.toString(entity)
        else
          throw new ClientProtocolException("Unexpected null response")
      }
      else {
        throw new ClientProtocolException("Unexpected response status: " + status)
      }
    }
  }

  def get(url: String, client: CloseableHttpClient): String = {
    val httpGet = new HttpGet(url)
    client.execute(httpGet, responseHandler)
  }

  def get(url: String): String = {

    val client =  HttpClients.createDefault
    try {
      get(url, client)
    }
    finally {
      client.close()
    }
  }

  def getJson(url: String): JsonNode = {
    val client =  HttpClients.createDefault
    try {
      getJson(url, client)
    }
    finally {
      client.close()
    }
  }

  def getJson(url: String, client: CloseableHttpClient): JsonNode = {
    mapper.readTree(get(url, client))
  }

  def getJson(url: String, content: String, contentType: String): JsonNode = {
    val httpPost = new HttpPost(url)
    val entity = new StringEntity(content, ContentType.create(contentType, "UTF-8"))
    httpPost.setEntity(entity)

    val client = HttpClients.createDefault
    try {
      mapper.readTree(client.execute(httpPost, responseHandler))
    }
    finally {
      client.close()
    }
  }
} 
Example 43
Source File: IndexWithKeyFields.scala    From CM-Well   with Apache License 2.0 5 votes vote down vote up
package cmwell.analytics.data

import com.fasterxml.jackson.databind.JsonNode
import com.typesafe.config.ConfigFactory
import org.apache.avro.{LogicalTypes, Schema, SchemaBuilder}
import org.apache.avro.generic.GenericRecord
import org.apache.log4j.LogManager
import org.joda.time.format.ISODateTimeFormat

import scala.util.control.NonFatal


case class IndexWithKeyFields(uuid: String,
                              lastModified: java.sql.Timestamp,
                              path: String) extends GenericRecord with CsvGenerator {

  override def put(key: String, v: scala.Any): Unit = ???

  override def get(key: String): AnyRef = key match {
    case "uuid" => uuid
    case "lastModified" => java.lang.Long.valueOf(lastModified.getTime)
    case "path" => path
  }

  override def put(i: Int, v: scala.Any): Unit = ???

  override def get(i: Int): AnyRef = i match {
    case 0 => uuid
    case 1 => java.lang.Long.valueOf(lastModified.getTime)
    case 2 => path
    case _ => throw new IllegalArgumentException
  }

  override def getSchema: Schema = IndexWithSystemFields.schema

  override def csv: String =
    (if (uuid == null) "" else uuid) + "," +
      (if (lastModified == null) "" else ISODateTimeFormat.dateTime.print(lastModified.getTime)) + "," +
      (if (path == null) "" else path)
}

object IndexWithKeyFields extends ObjectExtractor[IndexWithKeyFields] {

  private val logger = LogManager.getLogger(IndexWithSystemFields.getClass)

  // AVRO-2065 - doesn't allow union over logical type, so we can't make timestamp column nullable.
  val timestampMilliType: Schema = LogicalTypes.timestampMillis.addToSchema(Schema.create(Schema.Type.LONG))

  val schema: Schema = SchemaBuilder
    .record("IndexWithSystemFields").namespace("cmwell.analytics")
    .fields
    .name("uuid").`type`.unionOf.stringType.and.nullType.endUnion.noDefault
    .name("lastModified").`type`(timestampMilliType).noDefault
    .name("path").`type`.unionOf.stringType.and.nullType.endUnion.noDefault
    .endRecord

  private val config = ConfigFactory.load
  val infotonSize: Int = config.getInt("extract-index-from-es.fetch-size-index-with-uuid-lastModified-path")

  def includeFields: String = {
    // Note that 'quad' is not included in this list
    val fields = "uuid,lastModified,path"
      .split(",")
      .map(name => s""""system.$name"""")
      .mkString(",")

    s""""_source": [$fields]"""
  }

  def extractFromJson(hit: JsonNode): IndexWithKeyFields = {

    val system = hit.findValue("_source").findValue("system")

    def extractString(name: String): String = system.findValue(name) match {
      case x: JsonNode => x.asText
      case _ => null
    }

    // Extracting date values as Long - as a java.sql.Date might be better
    def extractDate(name: String): java.sql.Timestamp = system.findValue(name) match {
      case x: JsonNode =>
        try {
          new java.sql.Timestamp(ISODateTimeFormat.dateTime.parseDateTime(x.asText).getMillis)
        }
        catch {
          case NonFatal(ex) =>
            logger.warn(s"Failed conversion of date value: $x", ex)
            throw ex
        }
      case _ => null
    }

    IndexWithKeyFields(
      uuid = extractString("uuid"),
      lastModified = extractDate("lastModified"),
      path = extractString("path"))
  }
} 
Example 44
Source File: IndexWithCompleteDocument.scala    From CM-Well   with Apache License 2.0 5 votes vote down vote up
package cmwell.analytics.data

import com.fasterxml.jackson.databind.JsonNode
import com.typesafe.config.ConfigFactory
import org.apache.avro.generic.GenericRecord
import org.apache.avro.{Schema, SchemaBuilder}

case class IndexWithCompleteDocument(uuid: String, document: String) extends GenericRecord with CsvGenerator {

  override def put(key: String, v: scala.Any): Unit = ???

  override def get(key: String): AnyRef = key match {
    case "uuid" => uuid
    case "document" => document
    case _ => throw new IllegalArgumentException
  }

  override def put(i: Int, v: scala.Any): Unit = ???

  override def get(i: Int): AnyRef = i match {
    case 0 => uuid
    case 1 => document
    case _ => throw new IllegalArgumentException
  }

  override def getSchema: Schema = IndexWithCompleteDocument.schema

  // Specifically don't implement CsvGenerator.csv since it is guaranteed to be invalid CSV - force use of Parquet.
}

object IndexWithCompleteDocument extends ObjectExtractor[IndexWithCompleteDocument] {

  val schema: Schema = SchemaBuilder
    .record("IndexWithCompleteDocument").namespace("cmwell.analytics")
    .fields
    .name("uuid").`type`.unionOf.stringType.and.nullType.endUnion.noDefault
    .name("document").`type`.unionOf.stringType.and.nullType.endUnion.noDefault
    .endRecord

  private val config = ConfigFactory.load
  val infotonSize: Int = config.getInt("extract-index-from-es.fetch-size-index-with-complete-document")

  def includeFields: String = s""""_source": "*""""

  def extractFromJson(hit: JsonNode): IndexWithCompleteDocument =
    IndexWithCompleteDocument(
      uuid = hit.findValue("_id").asText,
      document = hit.findValue("_source").toString)
} 
Example 45
Source File: HttpUtil.scala    From CM-Well   with Apache License 2.0 5 votes vote down vote up
package cmwell.analytics.util

import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.RequestEntityAcceptance.Tolerated
import akka.http.scaladsl.model.{HttpMethod, HttpRequest, HttpResponse}
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.Sink
import akka.util.ByteString
import com.fasterxml.jackson.databind.{JsonNode, ObjectMapper}
import com.typesafe.config.ConfigFactory

import scala.concurrent.duration.{MILLISECONDS, _}
import scala.concurrent.{Await, ExecutionContextExecutor, Future}

object HttpUtil {

  private val mapper = new ObjectMapper()

  private val config = ConfigFactory.load
  private val ReadTimeout = FiniteDuration(config.getDuration("extract-index-from-es.read-timeout").toMillis, MILLISECONDS)

  // Elasticsearch uses the POST verb in some places where the request is actually idempotent.
  // Requests that use POST, but are known to be idempotent can use this method.
  // The presence of any non-idempotent request in-flight causes Akka to not retry, and that will tend result in
  // entire downloads failing more often.
  val SAFE_POST = HttpMethod(
    value = "POST",
    isSafe = true,
    isIdempotent = true,
    requestEntityAcceptance = Tolerated)

  def resultAsync(request: HttpRequest,
                  action: String)
                 (implicit system: ActorSystem,
                  executionContext: ExecutionContextExecutor,
                  actorMaterializer: ActorMaterializer): Future[ByteString] =
    Http().singleRequest(request).map {

      case HttpResponse(status, _, entity, _) if status.isSuccess =>
        entity.dataBytes
          .fold(ByteString.empty)(_ ++ _)
          .runWith(Sink.head)

      case HttpResponse(status, _, entity, _) =>
        val message = Await.result(entity.toStrict(10.seconds).map(_.data), 10.seconds).utf8String
        throw new RuntimeException(s"HTTP request for $action failed. Status code: $status, message:$message")
    }
      .flatMap(identity)

  def result(request: HttpRequest,
             action: String,
             timeout: FiniteDuration = ReadTimeout)
            (implicit system: ActorSystem,
             executionContext: ExecutionContextExecutor,
             actorMaterializer: ActorMaterializer): ByteString =
    Await.result(resultAsync(request, action), timeout)

  def jsonResult(request: HttpRequest,
                 action: String,
                 timeout: FiniteDuration = ReadTimeout)
                (implicit system: ActorSystem,
                 executionContext: ExecutionContextExecutor,
                 actorMaterializer: ActorMaterializer): JsonNode =
    mapper.readTree(result(request, action, timeout).utf8String)

  def jsonResultAsync(request: HttpRequest,
                      action: String)
                     (implicit system: ActorSystem,
                      executionContext: ExecutionContextExecutor,
                      actorMaterializer: ActorMaterializer): Future[JsonNode] =
    resultAsync(request, action).map((bytes: ByteString) => mapper.readTree(bytes.utf8String))
} 
Example 46
Source File: DiscoverEsTopology.scala    From CM-Well   with Apache License 2.0 5 votes vote down vote up
package cmwell.analytics.util

import akka.actor.ActorSystem
import akka.http.scaladsl.model.HttpRequest
import akka.stream.ActorMaterializer
import com.fasterxml.jackson.databind.JsonNode

import scala.collection.JavaConverters._
import scala.collection.breakOut
import scala.concurrent.ExecutionContextExecutor

case class Shard(indexName: String, shard: Int) {

  // This field is not in the constructor argument list since it is not part of equality.
  private var _downloadAttempt: Int = 0

  def downloadAttempt: Int = _downloadAttempt

  def nextAttempt: Shard = {
    val copy = this.copy()
    copy._downloadAttempt = this._downloadAttempt + 1
    copy
  }
}


case class EsTopology(nodes: Map[String, String], // nodeId -> address
                      shards: Map[Shard, Seq[String]], // shard -> Seq[nodeId]
                      allIndexNames: Set[String])


object DiscoverEsTopology {

  def apply(esContactPoint: String,
            aliases: Seq[String] = Seq.empty)
           (implicit system: ActorSystem,
            executionContext: ExecutionContextExecutor,
            actorMaterializer: ActorMaterializer): EsTopology = {

    // Get a map from node name -> address

    val nodesJson = HttpUtil.jsonResult(HttpRequest(uri = s"http://$esContactPoint/_nodes"), "find es nodes")
    val extractAddress = "inet\\[/(.+)]".r // "inet[/10.204.146.152:9304]"
    val nodes: Map[String, String] = nodesJson.get("nodes").fields.asScala.map { entry =>

      val nodeId = entry.getKey
      val extractAddress(hostPort) = entry.getValue.get("http_address").asText

      nodeId -> hostPort
    }.toMap

    // Find all the shards for all indexes.

    val searchShardsJson = HttpUtil.jsonResult(HttpRequest(uri = s"http://$esContactPoint/_search_shards"), "search shards")

    val shards: Map[Shard, Seq[String]] = searchShardsJson.get("shards").elements.asScala.map { shardLocations: JsonNode =>

      // Sort the shard locations so that the primary is first - we will always try the primary first
      val locations = shardLocations.elements.asScala.toSeq.sortBy(_.findValue("primary").booleanValue).reverse

      assert(locations.nonEmpty)
      assert(locations.head.findValue("primary").booleanValue) // first one is primary node

      val indexName = locations.head.findValue("index").asText
      val shard = locations.head.findValue("shard").asInt
      val nodeIds: Vector[String] = locations.map(_.findValue("node").asText)(breakOut)

      Shard(indexName, shard) -> nodeIds
    }.toMap

    // Get a list of aliases that we want to read from.
    // This is used to filter the list of all shards down to the ones that we want to read from.

    def resolveAlias(alias: String): Set[String] = {
      val aliasesJson = HttpUtil.jsonResult(HttpRequest(uri = s"http://$esContactPoint/$alias/_alias"), s"shards for $alias")
      aliasesJson.fieldNames.asScala.toSet
    }

    val readIndexNames: Set[String] = if (aliases.isEmpty)
      resolveAlias("cm_well_all") // Default if no alias or index name specified.
    else
      (Set.empty[String] /: aliases) (_ ++ resolveAlias(_)) // resolve and combine all the index names

    // allIndexNames is useful for validation of parameters to ensure they are all valid index names.

    val allIndexNames: Set[String] = {
      val aliasesJson = HttpUtil.jsonResult(HttpRequest(uri = s"http://$esContactPoint/_all/_alias"), "Get all index names")
      aliasesJson.fieldNames.asScala.toSet
    }

    EsTopology(
      nodes = nodes,
      // Only read shards for indexes that are included in the given aliases or index names.
      shards = shards.filter { case (shard, _) => readIndexNames.contains(shard.indexName) },
      allIndexNames = allIndexNames)
  }
} 
Example 47
Source File: FindContactPoints.scala    From CM-Well   with Apache License 2.0 5 votes vote down vote up
package cmwell.analytics.util

import akka.actor.ActorSystem
import akka.http.scaladsl.model.HttpMethods.GET
import akka.http.scaladsl.model.{HttpRequest, Uri}
import akka.stream.ActorMaterializer
import com.fasterxml.jackson.databind.JsonNode

import scala.collection.JavaConverters._
import scala.concurrent.ExecutionContextExecutor
import scala.util.Try

// TODO: These s/b `Uri`s?
case class ContactPoints(cassandra: String,
                         es: String)

object FindContactPoints {

  
  def es(url: String)
        (implicit system: ActorSystem,
         executionContext: ExecutionContextExecutor,
         actorMaterializer: ActorMaterializer): String = {

    val uri = Uri(url)

    val request = HttpRequest(
      method = GET,
      uri = s"http://${uri.authority.host}:${uri.authority.port}/proc/health?format=json")

    val json: JsonNode = HttpUtil.jsonResult(request, "fetch /proc/health")

    val masterIpAddresses: Seq[String] = json.get("fields").findValue("masters").elements.asScala.map(_.textValue).toSeq

    if (masterIpAddresses.isEmpty)
      throw new RuntimeException("No master node addresses found.")

    // For Elasticsearch, the port is 9201 for a single node, and 9200 for clustered.
    val esPort = if (masterIpAddresses.lengthCompare(1) > 0) "9200" else "9201"

    // All the masters should be accessible, but verify that.
    // A better implementation would keep all the endpoints in the list, and we could fall back to the others
    // if the one we are using disappears.
    val firstAccessibleESEndpoint = masterIpAddresses.find { ipAddress =>
      val request = HttpRequest(
        method = GET,
        uri = s"http://$ipAddress:$esPort")

      Try(HttpUtil.result(request, "probe for accessible es endpoint")).isSuccess
    }

    if (firstAccessibleESEndpoint.isEmpty)
      throw new RuntimeException("No accessible ES endpoint was found.")

    s"${firstAccessibleESEndpoint.get}:$esPort"
  }
}