akka.http.scaladsl.model.ContentType Scala Examples

The following examples show how to use akka.http.scaladsl.model.ContentType. 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: LinkDescriptionSpec.scala    From nexus   with Apache License 2.0 5 votes vote down vote up
package ch.epfl.bluebrain.nexus.kg.resources

import akka.http.scaladsl.model.Uri.Path
import akka.http.scaladsl.model.{ContentType, ContentTypes}
import ch.epfl.bluebrain.nexus.commons.test.{EitherValues, Randomness}
import ch.epfl.bluebrain.nexus.kg.TestHelper
import ch.epfl.bluebrain.nexus.kg.resources.ProjectIdentifier.ProjectRef
import ch.epfl.bluebrain.nexus.kg.resources.Rejection.InvalidResourceFormat
import ch.epfl.bluebrain.nexus.kg.resources.file.File.{FileDescription, LinkDescription}
import ch.epfl.bluebrain.nexus.rdf.implicits._
import io.circe.Json
import io.circe.syntax._
import org.scalatest.OptionValues
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpecLike

class LinkDescriptionSpec
    extends AnyWordSpecLike
    with Matchers
    with TestHelper
    with Randomness
    with EitherValues
    with OptionValues {

  abstract private class Ctx {
    val id                                                                            = Id(ProjectRef(genUUID), genIri)
    val p                                                                             = genString() + "/" + genString()
    val f                                                                             = genString()
    val m                                                                             = "application/json"
    def jsonLink(mediaType: String = m, filename: String = f, path: String = p): Json =
      Json.obj("filename" -> filename.asJson, "path" -> path.asJson, "mediaType" -> mediaType.asJson)

  }

  "A Link Description" should {

    "be decoded correctly" in new Ctx {
      LinkDescription(id, jsonLink()).rightValue shouldEqual
        LinkDescription(Path(p), Some(f), ContentType.parse(m).toOption)

      LinkDescription(id, Json.obj("path" -> p.asJson)).rightValue shouldEqual
        LinkDescription(Path(p), None, None)
    }

    "accept missing filename" in new Ctx {
      LinkDescription(id, jsonLink().removeKeys("filename")).rightValue shouldEqual
        LinkDescription(Path(p), None, ContentType.parse(m).toOption)
    }

    "reject empty filename" in new Ctx {
      LinkDescription(id, jsonLink(filename = "")).leftValue shouldBe a[InvalidResourceFormat]
    }

    "accept missing mediaType" in new Ctx {
      LinkDescription(id, jsonLink().removeKeys("mediaType")).rightValue shouldEqual
        LinkDescription(Path(p), Some(f), None)
    }

    "reject wrong mediaType format" in new Ctx {
      LinkDescription(id, jsonLink(mediaType = genString())).leftValue shouldBe a[InvalidResourceFormat]
    }

    "reject missing path" in new Ctx {
      LinkDescription(id, jsonLink().removeKeys("path")).leftValue shouldBe a[InvalidResourceFormat]
    }

    "be converted to a FileDescription correctly" in new Ctx {
      val fileDesc1 = FileDescription.from(LinkDescription(Path("/foo/bar/file.ext"), None, None))
      fileDesc1.filename shouldEqual "file.ext"
      fileDesc1.mediaType shouldEqual None
      fileDesc1.defaultMediaType shouldEqual ContentTypes.`application/octet-stream`

      val fileDesc2 =
        FileDescription.from(LinkDescription(Path("/foo/bar/somedir/"), None, ContentType.parse(m).toOption))
      fileDesc2.filename shouldEqual "somedir"
      fileDesc2.mediaType.value shouldEqual ContentTypes.`application/json`

      val fileDesc3 =
        FileDescription.from(LinkDescription(Path("/foo/bar/baz"), Some("file.json"), ContentType.parse(m).toOption))
      fileDesc3.filename shouldEqual "file.json"
      fileDesc3.mediaType.value shouldEqual ContentTypes.`application/json`
    }
  }
} 
Example 2
Source File: StaticResourceTest.scala    From sumobot   with Apache License 2.0 5 votes vote down vote up
package com.sumologic.sumobot.http_frontend

import akka.http.scaladsl.model.{ContentType, ContentTypes, HttpCharsets, MediaTypes}
import com.sumologic.sumobot.test.annotated.SumoBotSpec

class StaticResourceTest extends SumoBotSpec {
  "StaticResource" should {
    "return correct content type" when {
      "passing .html file" in {
        StaticResource("test.html").contentType should
          be(ContentTypes.`text/html(UTF-8)`)
      }

      "passing .css file" in {
        StaticResource("test.css").contentType should
          be(ContentType(MediaTypes.`text/css`, HttpCharsets.`UTF-8`))
      }

      "passing unknown extension" in {
        StaticResource("test.unknown").contentType should
          be(ContentTypes.`text/html(UTF-8)`)
      }
    }
  }
} 
Example 3
Source File: DynamicResource.scala    From sumobot   with Apache License 2.0 5 votes vote down vote up
package com.sumologic.sumobot.http_frontend

import akka.http.scaladsl.model.{ContentType, ContentTypes}
import org.apache.commons.io.IOUtils
import org.fusesource.scalate.TemplateEngine
import org.fusesource.scalate.util.{FileResourceLoader, Resource}

object DynamicResource {
  private val Engine = new TemplateEngine

  Engine.allowReload = false
  Engine.allowCaching = true
  Engine.resourceLoader = new FileResourceLoader {
    override def resource(filename: String): Option[Resource] = {
      val uri = getClass.getResource(filename).toURI.toString
      val stream = getClass.getResourceAsStream(filename)
      val templateText = IOUtils.toString(stream)
      stream.close()

      Some(Resource.fromText(uri, templateText))
    }
  }
}

case class DynamicResource(templateFile: String) {
  DynamicResource.Engine.load(templateFile)

  def contents(templateVariables: Map[String, Any]): String = {
    DynamicResource.Engine.layout(templateFile, templateVariables)
  }

  val contentType: ContentType.NonBinary = ContentTypes.`text/html(UTF-8)`
} 
Example 4
Source File: StaticResource.scala    From sumobot   with Apache License 2.0 5 votes vote down vote up
package com.sumologic.sumobot.http_frontend

import akka.http.scaladsl.model.{ContentType, ContentTypes, HttpCharsets, MediaTypes}
import org.apache.commons.io.IOUtils

object StaticResource {
  private[http_frontend] val DefaultContentType = ContentTypes.`text/html(UTF-8)`

  case class ContentTypePair(extension: String, contentType: ContentType)

  private[http_frontend] val KnownContentTypes = Array(
    ContentTypePair(".html", ContentTypes.`text/html(UTF-8)`),
    ContentTypePair(".css", ContentType(MediaTypes.`text/css`, HttpCharsets.`UTF-8`)),
    ContentTypePair(".js", ContentType(MediaTypes.`application/javascript`, HttpCharsets.`UTF-8`))
  )
}

case class StaticResource(filename: String) {
  def contents: Array[Byte] = {
    val stream = getClass.getResourceAsStream(filename)
    val result = IOUtils.toByteArray(stream)

    stream.close()
    result
  }

  def contentType: ContentType = {
    val contentType = StaticResource.KnownContentTypes.find(contentType => filename.endsWith(contentType.extension))
      .map(contentTypePair => contentTypePair.contentType)
    contentType.getOrElse(StaticResource.DefaultContentType)
  }
} 
Example 5
Source File: ModelServiceSpec.scala    From full-scala-stack   with Apache License 2.0 5 votes vote down vote up
package api

import akka.http.scaladsl.marshalling.Marshal
import akka.http.scaladsl.model.{ContentType, ContentTypes, HttpEntity, HttpResponse, MessageEntity, RequestEntity}
import akka.http.scaladsl.testkit.ScalatestRouteTest
import dao.{CRUDOperations, MockRepository, Repository}
import de.heikoseeberger.akkahttpupickle.UpickleSupport
import mail.{CourierPostman, Postman}
import model.{SampleModelObject, SimpleSearch}
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec
import routes.{ModelRoutes, SampleModelObjectRoute}
import upickle.default._
import util.ModelPickler
import zio.{IO, ZIO}
import zioslick.RepositoryException

import scala.concurrent.ExecutionContext


class ModelServiceSpec
  extends AnyWordSpec
    with Matchers
    with ScalatestRouteTest
    with ZIODirectives
  with UpickleSupport
    with ModelPickler
 {

  val objects = Seq(
    SampleModelObject(0, "Zero"),
    SampleModelObject(1, "One"),
    SampleModelObject(2, "Two"),
    SampleModelObject(3, "Three"),
    SampleModelObject(4, "Four"),
  )

  val service = new SampleModelObjectRoute  with MockRepository with CourierPostman with Config {
    override def repository: Repository.Service = new Repository.Service {
      override val sampleModelObjectOps: CRUDOperations[SampleModelObject, Int, SimpleSearch, Any] = new MockOps {
        override def search(search: Option[SimpleSearch])(
          implicit session: Any
        ): IO[RepositoryException, Seq[SampleModelObject]] = ZIO.succeed(objects)
        override def get(pk: Int)(implicit session: Any): IO[RepositoryException, Option[SampleModelObject]] =
          ZIO.succeed(objects.headOption)

      }
    }
  }

  //TODO test your route here, we would probably not have a test like the one below in reality, since it's super simple.
  "The Service" should  {
    "return one objects on a get" in {
      Get("/sampleModelObject/1") ~> service.crudRoute.route("") ~> check {
        val res = responseAs[Seq[SampleModelObject]].headOption

        println(res)
        res shouldEqual objects.headOption
      }
    }
    "return some objects on a search" in {
      Post("/sampleModelObject/search", HttpEntity(ContentTypes.`application/json`, write(SimpleSearch()))) ~> service.crudRoute.route("") ~> check {
        val str = responseAs[ujson.Value]
        val res = responseAs[Seq[SampleModelObject]]

        println(res)
        res shouldEqual objects
      }
    }
  }
} 
Example 6
Source File: LinkDescriptionSpec.scala    From nexus-kg   with Apache License 2.0 5 votes vote down vote up
package ch.epfl.bluebrain.nexus.kg.resources

import akka.http.scaladsl.model.Uri.Path
import akka.http.scaladsl.model.{ContentType, ContentTypes}
import ch.epfl.bluebrain.nexus.commons.test.{EitherValues, Randomness}
import ch.epfl.bluebrain.nexus.kg.TestHelper
import ch.epfl.bluebrain.nexus.kg.resources.ProjectIdentifier.ProjectRef
import ch.epfl.bluebrain.nexus.kg.resources.Rejection.InvalidResourceFormat
import ch.epfl.bluebrain.nexus.kg.resources.file.File.{FileDescription, LinkDescription}
import ch.epfl.bluebrain.nexus.rdf.implicits._
import io.circe.Json
import io.circe.syntax._
import org.scalatest.OptionValues
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpecLike

class LinkDescriptionSpec
    extends AnyWordSpecLike
    with Matchers
    with TestHelper
    with Randomness
    with EitherValues
    with OptionValues {

  private abstract class Ctx {
    val id = Id(ProjectRef(genUUID), genIri)
    val p  = genString() + "/" + genString()
    val f  = genString()
    val m  = "application/json"
    def jsonLink(mediaType: String = m, filename: String = f, path: String = p): Json =
      Json.obj("filename" -> filename.asJson, "path" -> path.asJson, "mediaType" -> mediaType.asJson)

  }

  "A Link Description" should {

    "be decoded correctly" in new Ctx {
      LinkDescription(id, jsonLink()).rightValue shouldEqual
        LinkDescription(Path(p), Some(f), ContentType.parse(m).toOption)

      LinkDescription(id, Json.obj("path" -> p.asJson)).rightValue shouldEqual
        LinkDescription(Path(p), None, None)
    }

    "accept missing filename" in new Ctx {
      LinkDescription(id, jsonLink().removeKeys("filename")).rightValue shouldEqual
        LinkDescription(Path(p), None, ContentType.parse(m).toOption)
    }

    "reject empty filename" in new Ctx {
      LinkDescription(id, jsonLink(filename = "")).leftValue shouldBe a[InvalidResourceFormat]
    }

    "accept missing mediaType" in new Ctx {
      LinkDescription(id, jsonLink().removeKeys("mediaType")).rightValue shouldEqual
        LinkDescription(Path(p), Some(f), None)
    }

    "reject wrong mediaType format" in new Ctx {
      LinkDescription(id, jsonLink(mediaType = genString())).leftValue shouldBe a[InvalidResourceFormat]
    }

    "reject missing path" in new Ctx {
      LinkDescription(id, jsonLink().removeKeys("path")).leftValue shouldBe a[InvalidResourceFormat]
    }

    "be converted to a FileDescription correctly" in new Ctx {
      val fileDesc1 = FileDescription.from(LinkDescription(Path("/foo/bar/file.ext"), None, None))
      fileDesc1.filename shouldEqual "file.ext"
      fileDesc1.mediaType shouldEqual None
      fileDesc1.defaultMediaType shouldEqual ContentTypes.`application/octet-stream`

      val fileDesc2 =
        FileDescription.from(LinkDescription(Path("/foo/bar/somedir/"), None, ContentType.parse(m).toOption))
      fileDesc2.filename shouldEqual "somedir"
      fileDesc2.mediaType.value shouldEqual ContentTypes.`application/json`

      val fileDesc3 =
        FileDescription.from(LinkDescription(Path("/foo/bar/baz"), Some("file.json"), ContentType.parse(m).toOption))
      fileDesc3.filename shouldEqual "file.json"
      fileDesc3.mediaType.value shouldEqual ContentTypes.`application/json`
    }
  }
} 
Example 7
Source File: EntityUtils.scala    From asura   with MIT License 5 votes vote down vote up
package asura.core.http

import java.net.URLEncoder

import akka.http.scaladsl.model.{ContentType, ContentTypes, HttpEntity, RequestEntity}
import akka.util.ByteString
import asura.common.util.{LogUtils, StringUtils}
import asura.core.es.model.{HttpCaseRequest, KeyValueObject}
import asura.core.http.UriUtils.UTF8
import asura.core.runtime.RuntimeContext
import asura.core.util.JacksonSupport
import com.fasterxml.jackson.core.`type`.TypeReference
import com.typesafe.scalalogging.Logger

object EntityUtils {

  val logger = Logger("EntityUtils")

  def toEntity(cs: HttpCaseRequest, context: RuntimeContext): RequestEntity = {
    val request = cs.request
    var contentType: ContentType = ContentTypes.`text/plain(UTF-8)`
    var byteString: ByteString = ByteString.empty
    if (StringUtils.isNotEmpty(request.contentType) && null != request.body && request.body.nonEmpty) {
      request.contentType match {
        case HttpContentTypes.JSON =>
          contentType = ContentTypes.`application/json`
          val body = request.body.find(_.contentType == HttpContentTypes.JSON)
          if (body.nonEmpty) {
            byteString = ByteString(context.renderBodyAsString(body.get.data))
          }
        case HttpContentTypes.X_WWW_FORM_URLENCODED =>
          contentType = ContentTypes.`application/x-www-form-urlencoded`
          val body = request.body.find(_.contentType == HttpContentTypes.X_WWW_FORM_URLENCODED)
          if (body.nonEmpty) {
            var bodyStr: String = null
            try {
              val sb = StringBuilder.newBuilder
              val params = JacksonSupport.parse(body.get.data, new TypeReference[Seq[KeyValueObject]]() {})
              for (pair <- params if (pair.enabled && StringUtils.isNotEmpty(pair.key))) {
                val rendered = context.renderBodyAsString(pair.value)
                sb.append(pair.key).append("=").append(URLEncoder.encode(rendered, UTF8)).append("&")
              }
              if (sb.nonEmpty) {
                sb.deleteCharAt(sb.length - 1)
              }
              bodyStr = sb.toString
            } catch {
              case t: Throwable =>
                val errLog = LogUtils.stackTraceToString(t)
                logger.warn(errLog)
                bodyStr = errLog
            }
            byteString = ByteString(bodyStr)
          }
        case HttpContentTypes.TEXT_PLAIN =>
          contentType = ContentTypes.`text/plain(UTF-8)`
          val body = request.body.find(_.contentType == HttpContentTypes.TEXT_PLAIN)
          if (body.nonEmpty) {
            byteString = ByteString(context.renderBodyAsString(body.get.data))
          }
        case _ =>
      }
    }
    HttpEntity(contentType, byteString)
  }
} 
Example 8
Source File: TextFilesDirectives.scala    From akka-http-extensions   with Mozilla Public License 2.0 5 votes vote down vote up
package akka.http.extensions.resources

import akka.actor.ActorSystem
import akka.http.scaladsl.model.ContentType
import akka.http.scaladsl.server.Directive
import akka.http.scaladsl.server.directives.ContentTypeResolver
import akka.http.scaladsl.server.directives.FileAndResourceDirectives.ResourceFile

trait TextFilesDirectives {

  def resource(resourceName: String,
               classLoader: ClassLoader = classOf[ActorSystem].getClassLoader)
              (implicit resolver: ContentTypeResolver) =  Directive[Tuple1[ResourceFile]]{ inner=>ctx=>
    if (!resourceName.endsWith("/"))
        Option(classLoader.getResource(resourceName)) flatMap ResourceFile.apply match {
          case Some(resource) ⇒  inner(Tuple1(resource))(ctx)
           case other=> ctx.reject()
        }
    else ctx.reject()
  }
} 
Example 9
Source File: AttributesComputation.scala    From nexus   with Apache License 2.0 5 votes vote down vote up
package ch.epfl.bluebrain.nexus.storage.attributes

import java.nio.file.{Files, Path}
import java.security.MessageDigest

import akka.http.scaladsl.model.HttpCharsets.`UTF-8`
import akka.http.scaladsl.model.MediaTypes.{`application/octet-stream`, `application/x-tar`}
import akka.http.scaladsl.model.{ContentType, MediaType, MediaTypes}
import akka.stream.Materializer
import akka.stream.scaladsl.{Keep, Sink}
import akka.util.ByteString
import cats.effect.Effect
import cats.implicits._
import ch.epfl.bluebrain.nexus.storage.File.{Digest, FileAttributes}
import ch.epfl.bluebrain.nexus.storage.StorageError.InternalError
import ch.epfl.bluebrain.nexus.storage._
import org.apache.commons.io.FilenameUtils

import scala.concurrent.{ExecutionContext, Future}
import scala.util.{Failure, Success, Try}

trait AttributesComputation[F[_], Source] {

  
  implicit def akkaAttributes[F[_]](implicit
      ec: ExecutionContext,
      mt: Materializer,
      F: Effect[F]
  ): AttributesComputation[F, AkkaSource] =
    (path: Path, algorithm: String) => {
      if (!Files.exists(path)) F.raiseError(InternalError(s"Path not found '$path'"))
      else
        Try(MessageDigest.getInstance(algorithm)) match {
          case Success(msgDigest) =>
            val isDir  = Files.isDirectory(path)
            val source = if (isDir) folderSource(path) else fileSource(path)
            source
              .alsoToMat(sinkSize)(Keep.right)
              .toMat(sinkDigest(msgDigest)) { (bytesF, digestF) =>
                (bytesF, digestF).mapN {
                  case (bytes, digest) => FileAttributes(path.toAkkaUri, bytes, digest, detectMediaType(path, isDir))
                }
              }
              .run()
              .to[F]
          case Failure(_)         => F.raiseError(InternalError(s"Invalid algorithm '$algorithm'."))
        }

    }
} 
Example 10
Source File: File.scala    From nexus   with Apache License 2.0 5 votes vote down vote up
package ch.epfl.bluebrain.nexus.storage

import akka.http.scaladsl.model.{ContentType, Uri}
import ch.epfl.bluebrain.nexus.rdf.implicits._
import ch.epfl.bluebrain.nexus.storage.config.Contexts.resourceCtxUri
import scala.annotation.nowarn
import io.circe.generic.extras.Configuration
import io.circe.generic.extras.semiauto._
import io.circe.{Decoder, Encoder}

// $COVERAGE-OFF$
object File {

  @nowarn("cat=unused")
  implicit private val config: Configuration = Configuration.default
    .copy(transformMemberNames = {
      case "@context" => "@context"
      case key        => s"_$key"
    })

  
  final case class Digest(algorithm: String, value: String)

  object Digest {
    val empty: Digest                           = Digest("", "")
    implicit val digestEncoder: Encoder[Digest] = deriveConfiguredEncoder[Digest]
    implicit val digestDecoder: Decoder[Digest] = deriveConfiguredDecoder[Digest]
  }

}
// $COVERAGE-ON$ 
Example 11
Source File: LineageHeaders.scala    From rokku   with Apache License 2.0 5 votes vote down vote up
package com.ing.wbaa.rokku.proxy.data

import akka.http.scaladsl.model.{ ContentType, HttpMethod }

case class LineageHeaders(
    host: Option[String],
    bucket: String,
    pseduoDir: Option[String],
    bucketObject: Option[String],
    method: HttpMethod,
    contentType: ContentType,
    clientType: Option[String],
    queryParams: Option[String],
    copySource: Option[String],
    classifications: Map[ClassificationFor, Seq[String]],
    metadata: Option[Map[String, String]])

sealed class ClassificationFor
case class BucketClassification() extends ClassificationFor
case class DirClassification() extends ClassificationFor
case class ObjectClassification() extends ClassificationFor
case class UnknownClassification() extends ClassificationFor 
Example 12
Source File: HttpClientTransformers.scala    From wix-http-testkit   with MIT License 5 votes vote down vote up
package com.wix.e2e.http.client.transformers

import java.io.File

import akka.http.scaladsl.model.{ContentType, ContentTypes, HttpCharsets, MediaTypes}
import com.wix.e2e.http.client.transformers.internals._

trait HttpClientTransformers extends HttpClientRequestUrlTransformers
                                with HttpClientRequestHeadersTransformers
                                with HttpClientRequestBodyTransformers
                                with HttpClientRequestTransformersOps

object HttpClientTransformers extends HttpClientTransformers

trait HttpClientContentTypes {
  val TextPlain = ContentTypes.`text/plain(UTF-8)`
  val JsonContent = ContentTypes.`application/json`
  val XmlContent = ContentType(MediaTypes.`application/xml`, HttpCharsets.`UTF-8`)
  val BinaryStream = ContentTypes.`application/octet-stream`
  val FormUrlEncoded = ContentTypes.`application/x-www-form-urlencoded`
}

object HttpClientContentTypes extends HttpClientContentTypes

sealed trait RequestPart
case class PlainRequestPart(body: String, contentType: ContentType = TextPlain) extends RequestPart
case class BinaryRequestPart(body: Array[Byte], contentType: ContentType = BinaryStream, filename: Option[String] = None) extends RequestPart
case class FileRequestPart(file: File, contentType: ContentType = BinaryStream, filename: Option[String] = None) extends RequestPart
case class FileNameRequestPart(filepath: String, contentType: ContentType = BinaryStream, filename: Option[String] = None) extends RequestPart 
Example 13
Source File: ChunkedEntities.scala    From endpoints4s   with MIT License 5 votes vote down vote up
package endpoints4s.akkahttp.server

import akka.http.scaladsl.marshalling.Marshaller
import akka.http.scaladsl.model.{ContentType, ContentTypes, HttpEntity, HttpRequest, MessageEntity}
import akka.http.scaladsl.server.Directives
import akka.http.scaladsl.unmarshalling.Unmarshaller
import akka.stream.scaladsl.Source
import akka.util.ByteString
import endpoints4s.algebra

import scala.concurrent.Future


trait ChunkedJsonEntities
    extends algebra.ChunkedJsonEntities
    with ChunkedEntities
    with JsonEntitiesFromCodecs {

  def jsonChunksRequest[A](implicit
      codec: JsonCodec[A]
  ): RequestEntity[Chunks[A]] = {
    val decoder = stringCodec(codec)
    chunkedRequestEntity { byteString =>
      val string = byteString.utf8String
      decoder
        .decode(string)
        .toEither
        .left
        .map(errors => new Throwable(errors.mkString(". ")))
    }
  }

  def jsonChunksResponse[A](implicit
      codec: JsonCodec[A]
  ): ResponseEntity[Chunks[A]] = {
    val encoder = stringCodec(codec)
    chunkedResponseEntity(
      ContentTypes.`application/json`,
      a => ByteString(encoder.encode(a))
    )
  }

} 
Example 14
Source File: ChunkedEntities.scala    From endpoints4s   with MIT License 5 votes vote down vote up
package endpoints4s.akkahttp.client

import akka.http.scaladsl.model.{ContentType, ContentTypes, HttpEntity}
import akka.stream.scaladsl.Source
import akka.util.ByteString
import endpoints4s.algebra

import scala.concurrent.Future


trait ChunkedJsonEntities
    extends algebra.ChunkedJsonEntities
    with ChunkedEntities
    with JsonEntitiesFromCodecs {

  def jsonChunksRequest[A](implicit
      codec: JsonCodec[A]
  ): RequestEntity[Chunks[A]] = {
    val encoder = stringCodec(codec)
    chunkedRequestEntity(
      ContentTypes.`application/json`,
      a => ByteString.fromString(encoder.encode(a))
    )
  }

  def jsonChunksResponse[A](implicit
      codec: JsonCodec[A]
  ): ResponseEntity[Chunks[A]] = {
    val decoder = stringCodec(codec)
    chunkedResponseEntity { byteString =>
      val string = byteString.utf8String
      decoder
        .decode(string)
        .toEither
        .left
        .map(errors => new Throwable(errors.mkString(". ")))
    }
  }

} 
Example 15
Source File: PrometheusEventsApi.scala    From openwhisk   with Apache License 2.0 5 votes vote down vote up
package org.apache.openwhisk.core.monitoring.metrics

import akka.http.scaladsl.model.StatusCodes.ServiceUnavailable
import akka.http.scaladsl.model.{ContentType, HttpCharsets, MediaType, MessageEntity}
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.Route
import org.apache.openwhisk.connector.kafka.KafkaMetricRoute

import scala.concurrent.ExecutionContext

trait PrometheusExporter {
  def getReport(): MessageEntity
}

object PrometheusExporter {
  val textV4: ContentType = ContentType.apply(
    MediaType.textWithFixedCharset("plain", HttpCharsets.`UTF-8`).withParams(Map("version" -> "0.0.4")))
}

class PrometheusEventsApi(consumer: EventConsumer, prometheus: PrometheusExporter)(implicit ec: ExecutionContext) {
  val routes: Route = {
    get {
      path("ping") {
        if (consumer.isRunning) {
          complete("pong")
        } else {
          complete(ServiceUnavailable -> "Consumer not running")
        }
      } ~ path("metrics") {
        encodeResponse {
          complete(prometheus.getReport())
        }
      } ~ KafkaMetricRoute(consumer)
    }
  }
} 
Example 16
Source File: MemoryAttachmentStore.scala    From openwhisk   with Apache License 2.0 5 votes vote down vote up
package org.apache.openwhisk.core.database.memory

import akka.actor.ActorSystem
import akka.http.scaladsl.model.ContentType
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.{Keep, Sink, Source}
import akka.util.{ByteString, ByteStringBuilder}
import org.apache.openwhisk.common.LoggingMarkers.{
  DATABASE_ATTS_DELETE,
  DATABASE_ATT_DELETE,
  DATABASE_ATT_GET,
  DATABASE_ATT_SAVE
}
import org.apache.openwhisk.common.{Logging, TransactionId}
import org.apache.openwhisk.core.database.StoreUtils._
import org.apache.openwhisk.core.database._
import org.apache.openwhisk.core.entity.DocId

import scala.collection.concurrent.TrieMap
import scala.concurrent.{ExecutionContext, Future}
import scala.reflect.ClassTag

object MemoryAttachmentStoreProvider extends AttachmentStoreProvider {
  override def makeStore[D <: DocumentSerializer: ClassTag]()(implicit actorSystem: ActorSystem,
                                                              logging: Logging,
                                                              materializer: ActorMaterializer): AttachmentStore =
    new MemoryAttachmentStore(implicitly[ClassTag[D]].runtimeClass.getSimpleName.toLowerCase)
}


  override protected[core] def readAttachment[T](docId: DocId, name: String, sink: Sink[ByteString, Future[T]])(
    implicit transid: TransactionId): Future[T] = {

    val start =
      transid.started(
        this,
        DATABASE_ATT_GET,
        s"[ATT_GET] '$dbName' finding attachment '$name' of document 'id: $docId'")

    val f = attachments.get(attachmentKey(docId, name)) match {
      case Some(Attachment(bytes)) =>
        val r = Source.single(bytes).toMat(sink)(Keep.right).run
        r.map(t => {
          transid.finished(this, start, s"[ATT_GET] '$dbName' completed: found attachment '$name' of document '$docId'")
          t
        })
      case None =>
        transid.finished(
          this,
          start,
          s"[ATT_GET] '$dbName', retrieving attachment '$name' of document '$docId'; not found.")
        Future.failed(NoDocumentException("Not found on 'readAttachment'."))
    }
    reportFailure(
      f,
      start,
      failure => s"[ATT_GET] '$dbName' internal error, name: '$name', doc: '$docId', failure: '${failure.getMessage}'")
  }

  override protected[core] def deleteAttachments(docId: DocId)(implicit transid: TransactionId): Future[Boolean] = {
    val start = transid.started(this, DATABASE_ATTS_DELETE, s"[ATTS_DELETE] uploading attachment of document '$docId'")

    val prefix = docId + "/"
    attachments --= attachments.keySet.filter(_.startsWith(prefix))
    transid.finished(this, start, s"[ATTS_DELETE] completed: delete attachment of document '$docId'")
    Future.successful(true)
  }

  override protected[core] def deleteAttachment(docId: DocId, name: String)(
    implicit transid: TransactionId): Future[Boolean] = {
    val start = transid.started(this, DATABASE_ATT_DELETE, s"[ATT_DELETE] uploading attachment of document '$docId'")
    attachments.remove(attachmentKey(docId, name))
    transid.finished(this, start, s"[ATT_DELETE] completed: delete attachment of document '$docId'")
    Future.successful(true)
  }

  def attachmentCount: Int = attachments.size

  def isClosed = closed

  override def shutdown(): Unit = {
    closed = true
  }

  private def attachmentKey(docId: DocId, name: String) = s"${docId.id}/$name"
} 
Example 17
Source File: Attachments.scala    From openwhisk   with Apache License 2.0 5 votes vote down vote up
package org.apache.openwhisk.core.entity

import akka.http.scaladsl.model.ContentType
import spray.json.DefaultJsonProtocol._
import spray.json._
import org.apache.openwhisk.core.entity.size._

import scala.util.Try

object Attachments {

  
  sealed trait Attachment[+T]

  case class Inline[T](value: T) extends Attachment[T]

  case class Attached(attachmentName: String,
                      attachmentType: ContentType,
                      length: Option[Long] = None,
                      digest: Option[String] = None)
      extends Attachment[Nothing]

  // Attachments are considered free because the name/content type are system determined
  // and a size check for the content is done during create/update
  implicit class SizeAttachment[T](a: Attachment[T])(implicit ev: T => SizeConversion) extends SizeConversion {
    def sizeIn(unit: SizeUnits.Unit): ByteSize = a match {
      case Inline(v) => (v: SizeConversion).sizeIn(unit)
      case _         => 0.bytes
    }
  }

  implicit class OptionSizeAttachment[T](a: Option[Attachment[T]])(implicit ev: T => SizeConversion)
      extends SizeConversion {
    def sizeIn(unit: SizeUnits.Unit): ByteSize = a match {
      case Some(Inline(v)) => (v: SizeConversion).sizeIn(unit)
      case _               => 0.bytes
    }
  }

  object Attached {
    implicit val serdes = {
      implicit val contentTypeSerdes = new RootJsonFormat[ContentType] {
        override def write(c: ContentType) = JsString(c.value)

        override def read(js: JsValue): ContentType = {
          Try(js.convertTo[String]).toOption.flatMap(ContentType.parse(_).toOption).getOrElse {
            throw new DeserializationException("Could not deserialize content-type")
          }
        }
      }

      jsonFormat4(Attached.apply)
    }
  }

  implicit def serdes[T: JsonFormat] = new JsonFormat[Attachment[T]] {
    val sub = implicitly[JsonFormat[T]]

    def write(a: Attachment[T]): JsValue = a match {
      case Inline(v)   => sub.write(v)
      case a: Attached => Attached.serdes.write(a)
    }

    def read(js: JsValue): Attachment[T] =
      Try {
        Inline(sub.read(js))
      } recover {
        case _: DeserializationException => Attached.serdes.read(js)
      } getOrElse {
        throw new DeserializationException("Could not deserialize as attachment record: " + js)
      }
  }
} 
Example 18
Source File: Prometheus.scala    From openwhisk   with Apache License 2.0 5 votes vote down vote up
package org.apache.openwhisk.common
import java.nio.charset.StandardCharsets.UTF_8

import akka.http.scaladsl.model.{ContentType, HttpCharsets, HttpEntity, MediaType}
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.Route
import kamon.Kamon
import kamon.prometheus.PrometheusReporter

class KamonPrometheus extends AutoCloseable {
  private val reporter = new PrometheusReporter
  private val v4: ContentType = ContentType.apply(
    MediaType.textWithFixedCharset("plain", HttpCharsets.`UTF-8`).withParams(Map("version" -> "0.0.4")))
  Kamon.registerModule("prometheus", reporter)

  def route: Route = path("metrics") {
    get {
      encodeResponse {
        complete(getReport())
      }
    }
  }

  private def getReport() = HttpEntity(v4, reporter.scrapeData().getBytes(UTF_8))

  override def close(): Unit = reporter.stop()
}

object MetricsRoute {
  private val impl =
    if (TransactionId.metricsKamon && TransactionId.metricConfig.prometheusEnabled) Some(new KamonPrometheus)
    else None

  def apply(): Route = impl.map(_.route).getOrElse(reject)
} 
Example 19
Source File: PrometheusMarshallers.scala    From akka-http-metrics   with Apache License 2.0 5 votes vote down vote up
package fr.davit.akka.http.metrics.prometheus.marshalling

import java.io.StringWriter

import akka.http.scaladsl.marshalling.{Marshaller, ToEntityMarshaller}
import akka.http.scaladsl.model.{ContentType, HttpCharsets, HttpEntity, MediaTypes}
import fr.davit.akka.http.metrics.prometheus.PrometheusRegistry
import io.prometheus.client.exporter.common.TextFormat

trait PrometheusMarshallers {

  val PrometheusContentType: ContentType = {
    MediaTypes.`text/plain` withParams Map("version" -> "0.0.4") withCharset HttpCharsets.`UTF-8`
  }

  implicit val marshaller: ToEntityMarshaller[PrometheusRegistry] = {
    Marshaller.opaque { registry =>
      val output = new StringWriter()
      try {
        TextFormat.write004(output, registry.underlying.metricFamilySamples)
        HttpEntity(output.toString).withContentType(PrometheusContentType)
      } finally {
        output.close()
      }
    }
  }
}

object PrometheusMarshallers extends PrometheusMarshallers