java.lang.reflect.Type Scala Examples

The following examples show how to use java.lang.reflect.Type. 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: JacksonMessageWriter.scala    From drizzle-spark   with Apache License 2.0 6 votes vote down vote up
package org.apache.spark.status.api.v1

import java.io.OutputStream
import java.lang.annotation.Annotation
import java.lang.reflect.Type
import java.nio.charset.StandardCharsets
import java.text.SimpleDateFormat
import java.util.{Calendar, SimpleTimeZone}
import javax.ws.rs.Produces
import javax.ws.rs.core.{MediaType, MultivaluedMap}
import javax.ws.rs.ext.{MessageBodyWriter, Provider}

import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.databind.{ObjectMapper, SerializationFeature}


@Provider
@Produces(Array(MediaType.APPLICATION_JSON))
private[v1] class JacksonMessageWriter extends MessageBodyWriter[Object]{

  val mapper = new ObjectMapper() {
    override def writeValueAsString(t: Any): String = {
      super.writeValueAsString(t)
    }
  }
  mapper.registerModule(com.fasterxml.jackson.module.scala.DefaultScalaModule)
  mapper.enable(SerializationFeature.INDENT_OUTPUT)
  mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL)
  mapper.setDateFormat(JacksonMessageWriter.makeISODateFormat)

  override def isWriteable(
      aClass: Class[_],
      `type`: Type,
      annotations: Array[Annotation],
      mediaType: MediaType): Boolean = {
      true
  }

  override def writeTo(
      t: Object,
      aClass: Class[_],
      `type`: Type,
      annotations: Array[Annotation],
      mediaType: MediaType,
      multivaluedMap: MultivaluedMap[String, AnyRef],
      outputStream: OutputStream): Unit = {
    t match {
      case ErrorWrapper(err) => outputStream.write(err.getBytes(StandardCharsets.UTF_8))
      case _ => mapper.writeValue(outputStream, t)
    }
  }

  override def getSize(
      t: Object,
      aClass: Class[_],
      `type`: Type,
      annotations: Array[Annotation],
      mediaType: MediaType): Long = {
    -1L
  }
}

private[spark] object JacksonMessageWriter {
  def makeISODateFormat: SimpleDateFormat = {
    val iso8601 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'GMT'")
    val cal = Calendar.getInstance(new SimpleTimeZone(0, "GMT"))
    iso8601.setCalendar(cal)
    iso8601
  }
} 
Example 2
Source File: JacksonMessageWriter.scala    From iolap   with Apache License 2.0 5 votes vote down vote up
package org.apache.spark.status.api.v1

import java.io.OutputStream
import java.lang.annotation.Annotation
import java.lang.reflect.Type
import java.text.SimpleDateFormat
import java.util.{Calendar, SimpleTimeZone}
import javax.ws.rs.Produces
import javax.ws.rs.core.{MediaType, MultivaluedMap}
import javax.ws.rs.ext.{MessageBodyWriter, Provider}

import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.databind.{ObjectMapper, SerializationFeature}


@Provider
@Produces(Array(MediaType.APPLICATION_JSON))
private[v1] class JacksonMessageWriter extends MessageBodyWriter[Object]{

  val mapper = new ObjectMapper() {
    override def writeValueAsString(t: Any): String = {
      super.writeValueAsString(t)
    }
  }
  mapper.registerModule(com.fasterxml.jackson.module.scala.DefaultScalaModule)
  mapper.enable(SerializationFeature.INDENT_OUTPUT)
  mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL)
  mapper.setDateFormat(JacksonMessageWriter.makeISODateFormat)

  override def isWriteable(
      aClass: Class[_],
      `type`: Type,
      annotations: Array[Annotation],
      mediaType: MediaType): Boolean = {
      true
  }

  override def writeTo(
      t: Object,
      aClass: Class[_],
      `type`: Type,
      annotations: Array[Annotation],
      mediaType: MediaType,
      multivaluedMap: MultivaluedMap[String, AnyRef],
      outputStream: OutputStream): Unit = {
    t match {
      case ErrorWrapper(err) => outputStream.write(err.getBytes("utf-8"))
      case _ => mapper.writeValue(outputStream, t)
    }
  }

  override def getSize(
      t: Object,
      aClass: Class[_],
      `type`: Type,
      annotations: Array[Annotation],
      mediaType: MediaType): Long = {
    -1L
  }
}

private[spark] object JacksonMessageWriter {
  def makeISODateFormat: SimpleDateFormat = {
    val iso8601 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'GMT'")
    val cal = Calendar.getInstance(new SimpleTimeZone(0, "GMT"))
    iso8601.setCalendar(cal)
    iso8601
  }
} 
Example 3
Source File: DatasourceListJsonProtocol.scala    From seahorse-workflow-executor   with Apache License 2.0 5 votes vote down vote up
package io.deepsense.commons.json.datasources

import java.lang.reflect.Type

import com.google.gson._
import com.google.gson.reflect.TypeToken

import io.deepsense.api.datasourcemanager.model.Datasource
import io.deepsense.commons.rest.client.datasources.DatasourceTypes.DatasourceList
import org.joda

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

object DatasourceListJsonProtocol {
  private val t = new TypeToken[java.util.LinkedList[Datasource]](){}.getType

  private val DateTimeType = new TypeToken[joda.time.DateTime](){}.getType

  private val gson = new GsonBuilder()
    .serializeNulls()
    .registerTypeAdapter(DateTimeType, new DatetimeJsonProtocol)
    .create()


  def fromString(json: String): DatasourceList = {
    val ds: java.util.List[Datasource] = gson.fromJson(json, t)
    ds.toList
  }

  def toString(datasources: DatasourceList): String = {
    gson.toJson(datasources.asJava)
  }
}

class DatetimeJsonProtocol
  extends JsonDeserializer[joda.time.DateTime]
  with JsonSerializer[joda.time.DateTime] {


  override def deserialize(
      json: JsonElement,
      typeOfT: Type,
      context: JsonDeserializationContext): joda.time.DateTime = {
    val fmt = joda.time.format.ISODateTimeFormat.dateTimeParser()
    fmt.parseDateTime(json.getAsString)
  }

  override def serialize(
      src: joda.time.DateTime,
      typeOfSrc: Type,
      context: JsonSerializationContext): JsonElement = {
    val fmt = joda.time.format.ISODateTimeFormat.dateTime()
    new JsonPrimitive(fmt.print(src))
  }
} 
Example 4
Source File: MesosAgentClient.scala    From marathon-vault-plugin   with MIT License 5 votes vote down vote up
package com.avast.marathon.plugin.vault

import java.lang.reflect.Type
import java.time.{Duration, Instant}

import feign.codec.{Decoder, StringDecoder}
import feign.{Feign, Param, RequestLine, Response}
import feign.gson.GsonDecoder

import scala.collection.JavaConverters._

object MesosAgentClient {
  private val contentTypeHeader = "Content-Type"
  private val gson = new GsonDecoder
  private val string = new StringDecoder

  def apply(agentUrl: String): MesosAgentClient = {
    Feign.builder()
      .decoder(new Decoder {
        override def decode(response: Response, `type`: Type): AnyRef = {
          if (response.headers().containsKey(contentTypeHeader)) {
            val value = response.headers().get(contentTypeHeader).asScala.head
            if (value.contains("json")) return gson.decode(response, `type`)
          }
          string.decode(response, `type`)
        }
      })
      .target(classOf[MesosAgentClient], agentUrl)
  }

  implicit class MesosAgentClientEx(agentClient: MesosAgentClient) {
    def waitForStdOutContentsMatch(envVarName: String, executor: MesosExecutor, fn: String => Option[String], timeout: Duration): String = {
      val stdOutPath = s"${executor.directory}/stdout"
      var matchOption: Option[String] = None
      var stdOut: String = null
      val maxTime = Instant.now().plus(timeout)
      do {
        if (Instant.now().compareTo(maxTime) > 1) {
          throw new RuntimeException("Timed out when waiting for task stdout to match.")
        }
        stdOut = agentClient.download(stdOutPath)
        matchOption = EnvAppCmd.extractEnvValue(envVarName, stdOut)
      } while (matchOption.isEmpty)
      matchOption.get
    }
  }
}

trait MesosAgentClient {
  @RequestLine("GET /state")
  def fetchState(): MesosAgentState
  @RequestLine("GET /files/download?path={path}")
  def download(@Param("path") path: String): String
}

case class MesosFramework(id: String, executors: Array[MesosExecutor])
case class MesosExecutor(id: String, name: String, directory: String)
case class MesosAgentState(frameworks: Array[MesosFramework]) 
Example 5
Source File: JSONHTTP.scala    From orders-aws   with Apache License 2.0 5 votes vote down vote up
package works.weave.socks.aws.orders.dataaccess.web

import com.amazonaws.util.IOUtils
import com.fasterxml.jackson.core.`type`.TypeReference
import java.lang.reflect.ParameterizedType
import java.lang.reflect.Type
import java.net.URI
import org.apache.http.client.methods.HttpGet
import org.apache.http.impl.client.HttpClientBuilder
import org.slf4j.LoggerFactory
import works.weave.socks.aws.orders.ProjectDefaultJacksonMapper

object JSONHTTP {

  val Log = LoggerFactory.getLogger(getClass)

  val objectMapper = ProjectDefaultJacksonMapper.build() // after (_.bla)

  def get[T : Manifest : NotNothing](uri : URI) : T = {
    val client = HttpClientBuilder.create.build
    val get = new HttpGet(uri)
    get.addHeader("Accept", "application/json")
    val response = client.execute(get)
    val responseString = IOUtils.toString(response.getEntity.getContent)
    Log.info(s"Got status ${response.getStatusLine.getStatusCode}")
    require(response.getStatusLine.getStatusCode == 200)
    Log.info(s"Got response from URI $uri: $responseString")
    objectMapper.readValue(responseString, typeReference[T])
  }

  def typeReference[T : Manifest] = new TypeReference[T] {
    override def getType = typeFromManifest(manifest[T])
  }

  def typeFromManifest(m : Manifest[_]) : Type = {
    if (m.typeArguments.isEmpty) { m.runtimeClass }
    else new ParameterizedType {
      def getRawType = m.runtimeClass
      def getActualTypeArguments = m.typeArguments.map(typeFromManifest).toArray
      def getOwnerType = null
    }
  }

  // Trick to disable nothign for type param
  sealed trait NotNothing[-T]

  object NotNothing {
    implicit object notNothing extends NotNothing[Any]
    implicit object `The error is because the missing type parameter was resolved to Nothing` extends NotNothing[Nothing]
  }

} 
Example 6
Source File: JacksonMessageWriter.scala    From BigDatalog   with Apache License 2.0 5 votes vote down vote up
package org.apache.spark.status.api.v1

import java.io.OutputStream
import java.lang.annotation.Annotation
import java.lang.reflect.Type
import java.text.SimpleDateFormat
import java.util.{Calendar, SimpleTimeZone}
import javax.ws.rs.Produces
import javax.ws.rs.core.{MediaType, MultivaluedMap}
import javax.ws.rs.ext.{MessageBodyWriter, Provider}

import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.databind.{ObjectMapper, SerializationFeature}


@Provider
@Produces(Array(MediaType.APPLICATION_JSON))
private[v1] class JacksonMessageWriter extends MessageBodyWriter[Object]{

  val mapper = new ObjectMapper() {
    override def writeValueAsString(t: Any): String = {
      super.writeValueAsString(t)
    }
  }
  mapper.registerModule(com.fasterxml.jackson.module.scala.DefaultScalaModule)
  mapper.enable(SerializationFeature.INDENT_OUTPUT)
  mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL)
  mapper.setDateFormat(JacksonMessageWriter.makeISODateFormat)

  override def isWriteable(
      aClass: Class[_],
      `type`: Type,
      annotations: Array[Annotation],
      mediaType: MediaType): Boolean = {
      true
  }

  override def writeTo(
      t: Object,
      aClass: Class[_],
      `type`: Type,
      annotations: Array[Annotation],
      mediaType: MediaType,
      multivaluedMap: MultivaluedMap[String, AnyRef],
      outputStream: OutputStream): Unit = {
    t match {
      case ErrorWrapper(err) => outputStream.write(err.getBytes("utf-8"))
      case _ => mapper.writeValue(outputStream, t)
    }
  }

  override def getSize(
      t: Object,
      aClass: Class[_],
      `type`: Type,
      annotations: Array[Annotation],
      mediaType: MediaType): Long = {
    -1L
  }
}

private[spark] object JacksonMessageWriter {
  def makeISODateFormat: SimpleDateFormat = {
    val iso8601 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'GMT'")
    val cal = Calendar.getInstance(new SimpleTimeZone(0, "GMT"))
    iso8601.setCalendar(cal)
    iso8601
  }
} 
Example 7
Source File: ValueJsonConversion.scala    From ingraph   with Eclipse Public License 1.0 5 votes vote down vote up
package ingraph.compiler.sql.driver

import java.lang.reflect.Type

import com.google.gson._
import org.neo4j.driver.internal.InternalPath.SelfContainedSegment
import org.neo4j.driver.internal.value._
import org.neo4j.driver.internal.{InternalNode, InternalPath, InternalRelationship}
import org.neo4j.driver.v1.Value
import org.neo4j.driver.v1.types.Path.Segment
import org.neo4j.driver.v1.types.{Node, Path, Relationship}

import scala.reflect.ClassTag

object ValueJsonConversion {

  private implicit class GsonBuilderExtension(val gsonBuilder: GsonBuilder) extends AnyVal {
    private def registerTypedAdapterInternal[T](clazz: Class[T], allowedClasses: Class[_ <: T]*): GsonBuilder = {
      gsonBuilder.registerTypeAdapter(clazz, new TypedJsonAdapter[T](allowedClasses: _*))
    }

    def registerTypedAdapter[T](allowedClasses: Class[_ <: T]*)(implicit classTag: ClassTag[T]): GsonBuilder = {
      registerTypedAdapterInternal(classTag.runtimeClass.asInstanceOf[Class[Any]], allowedClasses: _*)
    }

    def registerTypedAdapter[T, ConcreteType]()(implicit classTagT: ClassTag[T], classTagConcreteType: ClassTag[ConcreteType]): GsonBuilder = {
      registerTypedAdapterInternal(classTagT.runtimeClass.asInstanceOf[Class[Any]], classTagConcreteType.runtimeClass)
    }

    def registerSingletonValueAdapter[T <: Value](singletonValue: T): GsonBuilder = {
      gsonBuilder.registerTypeAdapter(singletonValue.getClass, new JsonSerializer[T] with JsonDeserializer[Value] {
        override def serialize(src: T, typeOfSrc: Type, context: JsonSerializationContext): JsonElement =
          new JsonObject

        override def deserialize(json: JsonElement, typeOfT: Type, context: JsonDeserializationContext): Value =
          singletonValue
      })
    }
  }

  val gson: Gson = (new GsonBuilder)
    .registerSingletonValueAdapter(NullValue.NULL)
    .registerSingletonValueAdapter(BooleanValue.FALSE)
    .registerSingletonValueAdapter(BooleanValue.TRUE)

    .registerTypedAdapter[Value](
    classOf[StringValue],
    classOf[NodeValue],
    classOf[RelationshipValue],
    classOf[NullValue],
    classOf[IntegerValue],
    classOf[MapValue],
    classOf[BytesValue],
    classOf[ListValue],
    classOf[PathValue],
    classOf[FloatValue],
    BooleanValue.FALSE.getClass,
    BooleanValue.TRUE.getClass
  )

    .registerTypedAdapter[Node, InternalNode]
    .registerTypedAdapter[Relationship, InternalRelationship]
    .registerTypedAdapter[Path, InternalPath]
    .registerTypedAdapter[Segment, SelfContainedSegment]

    .create()
} 
Example 8
Source File: TypedJsonAdapter.scala    From ingraph   with Eclipse Public License 1.0 5 votes vote down vote up
package ingraph.compiler.sql.driver

import java.lang.reflect.Type

import com.google.gson._

// https://stackoverflow.com/a/8683689
class TypedJsonAdapter[T](val allowedClasses: Class[_ <: T]*) extends JsonSerializer[T] with JsonDeserializer[T] {
  val typeKey = "type"
  val valueKey = "value"

  // Whitelist allowed classes to prevent unsafe reflection
  val classMap: Map[String, Class[_ <: T]] = allowedClasses
    .map(clazz => clazz.getSimpleName -> clazz)
    .toMap

  override def serialize(src: T, typeOfSrc: Type, context: JsonSerializationContext): JsonElement = {
    val clazz = src.getClass
    assert(allowedClasses.contains(clazz), clazz + " is not allowed for dynamic deserialization. Please check the whitelist.")
    val className = clazz.getSimpleName

    val valueJson = context.serialize(src)

    val retValue = new JsonObject
    retValue.addProperty(typeKey, className)
    retValue.add(valueKey, valueJson)

    retValue
  }

  override def deserialize(json: JsonElement, typeOfT: Type, context: JsonDeserializationContext): T = {
    val jsonObject = json.getAsJsonObject
    val className = jsonObject.getAsJsonPrimitive(typeKey).getAsString

    val clazz = classMap(className)

    context.deserialize(jsonObject.get(valueKey), clazz)
  }
} 
Example 9
Source File: JacksonMessageWriter.scala    From Spark-2.3.1   with Apache License 2.0 5 votes vote down vote up
package org.apache.spark.status.api.v1

import java.io.OutputStream
import java.lang.annotation.Annotation
import java.lang.reflect.Type
import java.nio.charset.StandardCharsets
import java.text.SimpleDateFormat
import java.util.{Calendar, Locale, SimpleTimeZone}
import javax.ws.rs.Produces
import javax.ws.rs.core.{MediaType, MultivaluedMap}
import javax.ws.rs.ext.{MessageBodyWriter, Provider}

import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.databind.{ObjectMapper, SerializationFeature}


@Provider
@Produces(Array(MediaType.APPLICATION_JSON))
private[v1] class JacksonMessageWriter extends MessageBodyWriter[Object]{

  val mapper = new ObjectMapper() {
    override def writeValueAsString(t: Any): String = {
      super.writeValueAsString(t)
    }
  }
  mapper.registerModule(com.fasterxml.jackson.module.scala.DefaultScalaModule)
  mapper.enable(SerializationFeature.INDENT_OUTPUT)
  mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL)
  mapper.setDateFormat(JacksonMessageWriter.makeISODateFormat)

  override def isWriteable(
      aClass: Class[_],
      `type`: Type,
      annotations: Array[Annotation],
      mediaType: MediaType): Boolean = {
      true
  }

  override def writeTo(
      t: Object,
      aClass: Class[_],
      `type`: Type,
      annotations: Array[Annotation],
      mediaType: MediaType,
      multivaluedMap: MultivaluedMap[String, AnyRef],
      outputStream: OutputStream): Unit = {
    t match {
      case ErrorWrapper(err) => outputStream.write(err.getBytes(StandardCharsets.UTF_8))
      case _ => mapper.writeValue(outputStream, t)
    }
  }

  override def getSize(
      t: Object,
      aClass: Class[_],
      `type`: Type,
      annotations: Array[Annotation],
      mediaType: MediaType): Long = {
    -1L
  }
}

private[spark] object JacksonMessageWriter {
  def makeISODateFormat: SimpleDateFormat = {
    val iso8601 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'GMT'", Locale.US)
    val cal = Calendar.getInstance(new SimpleTimeZone(0, "GMT"))
    iso8601.setCalendar(cal)
    iso8601
  }
} 
Example 10
Source File: JacksonMessageWriter.scala    From spark1.52   with Apache License 2.0 5 votes vote down vote up
package org.apache.spark.status.api.v1

import java.io.OutputStream
import java.lang.annotation.Annotation
import java.lang.reflect.Type
import java.text.SimpleDateFormat
import java.util.{Calendar, SimpleTimeZone}
import javax.ws.rs.Produces
import javax.ws.rs.core.{MediaType, MultivaluedMap}
import javax.ws.rs.ext.{MessageBodyWriter, Provider}

import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.databind.{ObjectMapper, SerializationFeature}


@Provider
@Produces(Array(MediaType.APPLICATION_JSON))
private[v1] class JacksonMessageWriter extends MessageBodyWriter[Object]{

  val mapper = new ObjectMapper() {
    override def writeValueAsString(t: Any): String = {
      super.writeValueAsString(t)
    }
  }
  mapper.registerModule(com.fasterxml.jackson.module.scala.DefaultScalaModule)
  mapper.enable(SerializationFeature.INDENT_OUTPUT)
  mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL)
  mapper.setDateFormat(JacksonMessageWriter.makeISODateFormat)

  override def isWriteable(
      aClass: Class[_],
      `type`: Type,
      annotations: Array[Annotation],
      mediaType: MediaType): Boolean = {
      true
  }

  override def writeTo(
      t: Object,
      aClass: Class[_],
      `type`: Type,
      annotations: Array[Annotation],
      mediaType: MediaType,
      multivaluedMap: MultivaluedMap[String, AnyRef],
      outputStream: OutputStream): Unit = {
    t match {
      case ErrorWrapper(err) => outputStream.write(err.getBytes("utf-8"))
      case _ => mapper.writeValue(outputStream, t)
    }
  }

  override def getSize(
      t: Object,
      aClass: Class[_],
      `type`: Type,
      annotations: Array[Annotation],
      mediaType: MediaType): Long = {
    -1L
  }
}

private[spark] object JacksonMessageWriter {
  def makeISODateFormat: SimpleDateFormat = {
    val iso8601 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'GMT'")
    val cal = Calendar.getInstance(new SimpleTimeZone(0, "GMT"))
    iso8601.setCalendar(cal)
    iso8601
  }
} 
Example 11
Source File: DatasourceListJsonProtocol.scala    From seahorse   with Apache License 2.0 5 votes vote down vote up
package ai.deepsense.commons.json.datasources

import java.lang.reflect.Type

import com.google.gson._
import com.google.gson.reflect.TypeToken

import ai.deepsense.api.datasourcemanager.model.Datasource
import ai.deepsense.commons.rest.client.datasources.DatasourceTypes.DatasourceList
import org.joda

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

object DatasourceListJsonProtocol {
  private val t = new TypeToken[java.util.LinkedList[Datasource]](){}.getType

  private val DateTimeType = new TypeToken[joda.time.DateTime](){}.getType

  private val gson = new GsonBuilder()
    .serializeNulls()
    .registerTypeAdapter(DateTimeType, new DatetimeJsonProtocol)
    .create()


  def fromString(json: String): DatasourceList = {
    val ds: java.util.List[Datasource] = gson.fromJson(json, t)
    ds.toList
  }

  def toString(datasources: DatasourceList): String = {
    gson.toJson(datasources.asJava)
  }
}

class DatetimeJsonProtocol
  extends JsonDeserializer[joda.time.DateTime]
  with JsonSerializer[joda.time.DateTime] {


  override def deserialize(
      json: JsonElement,
      typeOfT: Type,
      context: JsonDeserializationContext): joda.time.DateTime = {
    val fmt = joda.time.format.ISODateTimeFormat.dateTimeParser()
    fmt.parseDateTime(json.getAsString)
  }

  override def serialize(
      src: joda.time.DateTime,
      typeOfSrc: Type,
      context: JsonSerializationContext): JsonElement = {
    val fmt = joda.time.format.ISODateTimeFormat.dateTime()
    new JsonPrimitive(fmt.print(src))
  }
} 
Example 12
Source File: JacksonMessageWriter.scala    From multi-tenancy-spark   with Apache License 2.0 5 votes vote down vote up
package org.apache.spark.status.api.v1

import java.io.OutputStream
import java.lang.annotation.Annotation
import java.lang.reflect.Type
import java.nio.charset.StandardCharsets
import java.text.SimpleDateFormat
import java.util.{Calendar, Locale, SimpleTimeZone}
import javax.ws.rs.Produces
import javax.ws.rs.core.{MediaType, MultivaluedMap}
import javax.ws.rs.ext.{MessageBodyWriter, Provider}

import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.databind.{ObjectMapper, SerializationFeature}


@Provider
@Produces(Array(MediaType.APPLICATION_JSON))
private[v1] class JacksonMessageWriter extends MessageBodyWriter[Object]{

  val mapper = new ObjectMapper() {
    override def writeValueAsString(t: Any): String = {
      super.writeValueAsString(t)
    }
  }
  mapper.registerModule(com.fasterxml.jackson.module.scala.DefaultScalaModule)
  mapper.enable(SerializationFeature.INDENT_OUTPUT)
  mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL)
  mapper.setDateFormat(JacksonMessageWriter.makeISODateFormat)

  override def isWriteable(
      aClass: Class[_],
      `type`: Type,
      annotations: Array[Annotation],
      mediaType: MediaType): Boolean = {
      true
  }

  override def writeTo(
      t: Object,
      aClass: Class[_],
      `type`: Type,
      annotations: Array[Annotation],
      mediaType: MediaType,
      multivaluedMap: MultivaluedMap[String, AnyRef],
      outputStream: OutputStream): Unit = {
    t match {
      case ErrorWrapper(err) => outputStream.write(err.getBytes(StandardCharsets.UTF_8))
      case _ => mapper.writeValue(outputStream, t)
    }
  }

  override def getSize(
      t: Object,
      aClass: Class[_],
      `type`: Type,
      annotations: Array[Annotation],
      mediaType: MediaType): Long = {
    -1L
  }
}

private[spark] object JacksonMessageWriter {
  def makeISODateFormat: SimpleDateFormat = {
    val iso8601 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'GMT'", Locale.US)
    val cal = Calendar.getInstance(new SimpleTimeZone(0, "GMT"))
    iso8601.setCalendar(cal)
    iso8601
  }
} 
Example 13
Source File: PlayServiceConfiguration.scala    From play-soap   with Apache License 2.0 5 votes vote down vote up
package play.soap

import java.lang.reflect.Method
import java.lang.reflect.ParameterizedType
import java.lang.reflect.Type

import org.apache.cxf.wsdl.service.factory.AbstractServiceConfiguration
import java.util.concurrent.CompletionStage

import scala.concurrent.Future

import java.lang.Boolean.FALSE

private[soap] class PlayServiceConfiguration extends AbstractServiceConfiguration {

  
  override def hasOutMessage(m: Method) = {
    m.getGenericReturnType match {
      case future: ParameterizedType
          if future.getRawType == classOf[Future[_]] ||
            future.getRawType == classOf[CompletionStage[_]] =>
        future.getActualTypeArguments.headOption match {
          case Some(unit) if unit == classOf[Unit] || unit == classOf[Void] => FALSE
          case _                                                            => null
        }
      case _ => null
    }
  }
} 
Example 14
Source File: ReflectionUtils.scala    From DynaML   with Apache License 2.0 5 votes vote down vote up
package io.github.mandar2812.dynaml.utils.sumac

import annotation.tailrec
import java.lang.reflect.{Type, ParameterizedType, Field}

import scala.reflect.ClassTag


object ReflectionUtils {

  @tailrec
  def getAllDeclaredFields(cls: Class[_], acc: Seq[Field] = Seq.empty): Seq[Field] = {
    val fields = acc ++ cls.getDeclaredFields
    Option(cls.getSuperclass) match {
      case Some(clazz) => getAllDeclaredFields(clazz, fields)
      case _ => fields
    }
  }

  def findGenericInterface(cls: Class[_], interface: Class[_]) : Option[ParameterizedType] = {
    val interfaces = cls.getGenericInterfaces
    //first check if this directly implements it
    findParameterizedType(interfaces, interface).orElse{
      //then check if its inherited by any of those interfaces
      interfaces.flatMap{tpe =>
        findGenericInterface(getRawClass(tpe), interface)
      }.headOption
    }
  }

  def getRawClass(tpe: Type) : Class[_] = {
    tpe match {
      case x: Class[_] => x
      case p: ParameterizedType => p.getRawType.asInstanceOf[Class[_]]
    }
  }

  def findParameterizedType(types: Array[Type], interface: Class[_]) : Option[ParameterizedType] = {
    types.find { tpe =>
      tpe match {
        case ptpe: ParameterizedType => ParseHelper.checkType(ptpe, classOf[Argable[_]])
        case _ => false
      }
    }.map{_.asInstanceOf[ParameterizedType]}
  }

  def construct[T](clz: Class[T]): T = {
    val ctors = clz.getDeclaredConstructors
    ctors.find(_.getGenericParameterTypes.length == 0) match {
      case Some(ctor) =>
        ctor.setAccessible(true)
        ctor.newInstance().asInstanceOf[T]
      case None => throw new AssertionError("No zero-arg constructor found")
    }
  }

  def construct[T](tag: ClassTag[T]): T = {
    construct(tag.runtimeClass.asInstanceOf[Class[T]])
  }
} 
Example 15
Source File: JacksonMessageWriter.scala    From sparkoscope   with Apache License 2.0 5 votes vote down vote up
package org.apache.spark.status.api.v1

import java.io.OutputStream
import java.lang.annotation.Annotation
import java.lang.reflect.Type
import java.nio.charset.StandardCharsets
import java.text.SimpleDateFormat
import java.util.{Calendar, Locale, SimpleTimeZone}
import javax.ws.rs.Produces
import javax.ws.rs.core.{MediaType, MultivaluedMap}
import javax.ws.rs.ext.{MessageBodyWriter, Provider}

import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.databind.{ObjectMapper, SerializationFeature}


@Provider
@Produces(Array(MediaType.APPLICATION_JSON))
private[v1] class JacksonMessageWriter extends MessageBodyWriter[Object]{

  val mapper = new ObjectMapper() {
    override def writeValueAsString(t: Any): String = {
      super.writeValueAsString(t)
    }
  }
  mapper.registerModule(com.fasterxml.jackson.module.scala.DefaultScalaModule)
  mapper.enable(SerializationFeature.INDENT_OUTPUT)
  mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL)
  mapper.setDateFormat(JacksonMessageWriter.makeISODateFormat)

  override def isWriteable(
      aClass: Class[_],
      `type`: Type,
      annotations: Array[Annotation],
      mediaType: MediaType): Boolean = {
      true
  }

  override def writeTo(
      t: Object,
      aClass: Class[_],
      `type`: Type,
      annotations: Array[Annotation],
      mediaType: MediaType,
      multivaluedMap: MultivaluedMap[String, AnyRef],
      outputStream: OutputStream): Unit = {
    t match {
      case ErrorWrapper(err) => outputStream.write(err.getBytes(StandardCharsets.UTF_8))
      case _ => mapper.writeValue(outputStream, t)
    }
  }

  override def getSize(
      t: Object,
      aClass: Class[_],
      `type`: Type,
      annotations: Array[Annotation],
      mediaType: MediaType): Long = {
    -1L
  }
}

private[spark] object JacksonMessageWriter {
  def makeISODateFormat: SimpleDateFormat = {
    val iso8601 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'GMT'", Locale.US)
    val cal = Calendar.getInstance(new SimpleTimeZone(0, "GMT"))
    iso8601.setCalendar(cal)
    iso8601
  }
} 
Example 16
Source File: JsonJacksonMarshaller.scala    From wix-http-testkit   with MIT License 5 votes vote down vote up
package com.wix.e2e.http.json

import java.lang.reflect.{ParameterizedType, Type}

import com.fasterxml.jackson.core.`type`.TypeReference
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_AS_TIMESTAMPS
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module
import com.fasterxml.jackson.datatype.joda.JodaModule
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import com.wix.e2e.http.api.Marshaller

class JsonJacksonMarshaller extends Marshaller {

  def unmarshall[T : Manifest](jsonStr: String): T = objectMapper.readValue(jsonStr, typeReference[T])
  def marshall[T](t: T): String = objectMapper.writeValueAsString(t)

  def configure: ObjectMapper = objectMapper

  private val objectMapper = new ObjectMapper()
                                    .registerModule(new Jdk8Module().configureAbsentsAsNulls(true))
                                    .registerModules(new JodaModule, new ParameterNamesModule, new JavaTimeModule) // time modules
                                    .registerModule(new DefaultScalaModule)
                                    .disable( WRITE_DATES_AS_TIMESTAMPS )

  private def typeReference[T: Manifest] = new TypeReference[T] {
    override def getType = typeFromManifest(manifest[T])
  }

  private def typeFromManifest(m: Manifest[_]): Type = {
    if (m.typeArguments.isEmpty) { m.runtimeClass }
    else new ParameterizedType {
      def getRawType = m.runtimeClass
      def getActualTypeArguments = m.typeArguments.map(typeFromManifest).toArray
      def getOwnerType = null
    }
  }
} 
Example 17
Source File: TSDBUpdater.scala    From sprue   with Apache License 2.0 5 votes vote down vote up
package com.cloudera.sprue

import java.io._
import org.apache.commons._
import org.apache.http._
import org.apache.http.client._
import org.apache.http.client.methods.HttpPost
import java.util.ArrayList
import org.apache.http.client.entity.UrlEncodedFormEntity
import com.google.gson.Gson
import java.util.HashMap
import java.lang.reflect.Type
import com.google.gson.reflect.TypeToken
import org.apache.http.entity.StringEntity
import org.apache.http.impl.client.DefaultHttpClient
import org.apache.spark.sql.Row

 
case class MetricsTags(state: String)
case class OpenTSDBMessageElement(metric: String, timestamp: Long, value: Long, tags: MetricsTags)

object TSDBUpdater {
    val client = new DefaultHttpClient()
    // val client = HttpClientBuilder.create.build
}


class TSDBUpdater (url : String) extends Serializable {
       
  def loadPatientStats (row : Row) {
      
     val metricList = new ArrayList[OpenTSDBMessageElement]()
     val jmap = new MetricsTags(row.getString(0))
     val evalTimestamp = row.getLong(1)
     
     val sirsMetric = new OpenTSDBMessageElement("sirs", evalTimestamp, row.getLong(2), jmap)
     metricList.add(sirsMetric)
     
     val sepsisMetric = new OpenTSDBMessageElement("sepsis", evalTimestamp, row.getLong(3), jmap)
     metricList.add(sepsisMetric)
     
     val severeSepsisMetric = new OpenTSDBMessageElement("severeSepsis", evalTimestamp, row.getLong(4), jmap)
     metricList.add(severeSepsisMetric)

    val septicShockMetric = new OpenTSDBMessageElement("septicShock", evalTimestamp, row.getLong(5), jmap)
     metricList.add(septicShockMetric)
     
    val organMetric = new OpenTSDBMessageElement("organDysfunctionSyndrome", evalTimestamp, row.getLong(6), jmap)
     metricList.add(organMetric)

    val metricsAsJson = new Gson().toJson(metricList)
      
    val post = new HttpPost(url)
    
    post.setHeader("Content-type", "application/json");
    post.setEntity(new StringEntity(metricsAsJson));

    val response = TSDBUpdater.client.execute(post) 
  //  println("response =====" + response.toString())
  }
} 
Example 18
Source File: EitherTypeModifier.scala    From mango   with Apache License 2.0 5 votes vote down vote up
package com.kakao.shaded.jackson.module.scala.modifiers

import java.lang.reflect.Type

import com.kakao.shaded.jackson.databind.JavaType
import com.kakao.shaded.jackson.databind.`type`.{ReferenceType, TypeFactory, TypeBindings, TypeModifier}
import com.kakao.shaded.jackson.module.scala.JacksonModule

private object EitherTypeModifier extends TypeModifier with GenTypeModifier {
  val EITHER = classOf[Either[AnyRef, AnyRef]]

  override def modifyType(typ: JavaType, jdkType: Type, context: TypeBindings, typeFactory: TypeFactory): JavaType = {
    if (typ.isReferenceType || typ.isContainerType) typ

    if (classObjectFor(jdkType).exists(EITHER.isAssignableFrom)) {
      ReferenceType.upgradeFrom(typ, typ)
    } else typ
  }
}

trait EitherTypeModifierModule extends JacksonModule {
  this += EitherTypeModifier
} 
Example 19
Source File: OptionTypeModifierModule.scala    From mango   with Apache License 2.0 5 votes vote down vote up
package com.kakao.shaded.jackson.module.scala.modifiers

import java.lang.reflect.Type

import com.kakao.shaded.jackson.databind.JavaType
import com.kakao.shaded.jackson.databind.`type`.{ReferenceType, TypeBindings, TypeFactory, TypeModifier}
import com.kakao.shaded.jackson.module.scala.JacksonModule

private object OptionTypeModifier extends TypeModifier with GenTypeModifier {
  val OPTION = classOf[Option[AnyRef]]

  override def modifyType(typ: JavaType, jdkType: Type, context: TypeBindings, typeFactory: TypeFactory): JavaType = {
    if (typ.isReferenceType || typ.isContainerType) return typ

    if (classObjectFor(jdkType).exists(OPTION.isAssignableFrom)) {
      ReferenceType.upgradeFrom(typ, typ.containedTypeOrUnknown(0))
    } else typ
  }
}

trait OptionTypeModifierModule extends JacksonModule {
  this += OptionTypeModifier
} 
Example 20
Source File: RPCProduct.scala    From Linkis   with Apache License 2.0 5 votes vote down vote up
package com.webank.wedatasphere.linkis.rpc.transform

import java.lang.reflect.{ParameterizedType, Type}
import java.util

import com.webank.wedatasphere.linkis.DataWorkCloudApplication
import com.webank.wedatasphere.linkis.common.utils.Logging
import com.webank.wedatasphere.linkis.rpc.exception.DWCURIException
import com.webank.wedatasphere.linkis.server.{BDPJettyServerHelper, EXCEPTION_MSG, Message}
import org.apache.commons.lang.ClassUtils
import org.json4s.jackson.Serialization
import org.json4s.{DefaultFormats, Formats, Serializer}

import scala.collection.JavaConversions


private[linkis] trait RPCProduct {

  def toMessage(t: Any): Message

  def notFound(): Message

  def ok(): Message

}
private[linkis] object RPCProduct extends Logging {
  private[rpc] val IS_SCALA_CLASS = "rpc_is_scala_class"
  private[rpc] val CLASS_VALUE = "rpc_object_class"
  private[rpc] val OBJECT_VALUE = "rpc_object_value"
  private[rpc] implicit var formats: Formats = DefaultFormats + JavaCollectionSerializer + JavaMapSerializer
  private var serializerClasses: List[Class[_]] = List.empty
  private val rpcProduct: RPCProduct = new RPCProduct {
    private val rpcFormats = DataWorkCloudApplication.getApplicationContext.getBeansOfType(classOf[RPCFormats])
    if(rpcFormats != null && !rpcFormats.isEmpty) {
      val serializers = JavaConversions.mapAsScalaMap(rpcFormats).map(_._2.getSerializers).toArray.flatMap(_.iterator)
      setFormats(serializers)
    }
    override def toMessage(t: Any): Message = {
      if(t == null) throw new DWCURIException(10001, "The transmitted bean is Null.(传输的bean为Null.)")
      val message = Message.ok("RPC Message.")
      if(isScalaClass(t)){
        message.data(IS_SCALA_CLASS, "true")
        message.data(OBJECT_VALUE, Serialization.write(t.asInstanceOf[AnyRef]))
      } else {
        message.data(IS_SCALA_CLASS, "false")
        message.data(OBJECT_VALUE, BDPJettyServerHelper.gson.toJson(t))
      }
      message.setMethod("/rpc/message")
      message.data(CLASS_VALUE, t.getClass.getName)
    }

    override def notFound(): Message = {
      val message = Message.error("RPC Message.")
      message.setMethod("/rpc/message")
      message.data(EXCEPTION_MSG, new DWCURIException(10000, "The service does not exist for the available Receiver.(服务不存在可用的Receiver.)").toMap)
    }

    override def ok(): Message = {
      val message = Message.ok("RPC Message.")
      message.setMethod("/rpc/message")
      message
    }
  }
  private[rpc] def setFormats(serializer: Array[Serializer[_]]): Unit ={
    this.formats = (serializer :+ JavaCollectionSerializer :+ JavaMapSerializer).foldLeft(DefaultFormats.asInstanceOf[Formats])(_ + _)
    serializerClasses = formats.customSerializers.map(s => getActualTypeClass(s.getClass.getGenericSuperclass))
      .filter(_ != null) ++: List(classOf[util.List[_]], classOf[util.Map[_, _]])
    info("RPC Serializers: " + this.formats.customSerializers.map(_.getClass.getSimpleName) + ", serializerClasses: " +
      "" + serializerClasses)
  }
  private def getActualTypeClass(classType: Type): Class[_] = classType match {
    case p: ParameterizedType =>
      val params = p.getActualTypeArguments
      if(params == null || params.isEmpty) null
      else getActualTypeClass(params(0))
    case c: Class[_] => c
    case _ => null
  }
  private[rpc] def isScalaClass(obj: Any): Boolean =
    (obj.isInstanceOf[Product] && obj.isInstanceOf[Serializable]) ||
      serializerClasses.exists(ClassUtils.isAssignable(obj.getClass, _)) ||
        obj.getClass.getName.startsWith("scala.")
  private[rpc] def getSerializableScalaClass(clazz: Class[_]): Class[_] =
    serializerClasses.find(ClassUtils.isAssignable(clazz, _)).getOrElse(clazz)
  def getRPCProduct: RPCProduct = rpcProduct
}