akka.http.scaladsl.model.StatusCodes.NotFound Scala Examples

The following examples show how to use akka.http.scaladsl.model.StatusCodes.NotFound. 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: NamespacesApiTests.scala    From openwhisk   with Apache License 2.0 5 votes vote down vote up
package org.apache.openwhisk.core.controller.test

import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner

import akka.http.scaladsl.model.StatusCodes.OK
import akka.http.scaladsl.model.StatusCodes.NotFound
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport.sprayJsonUnmarshaller
import akka.http.scaladsl.server.Route

import spray.json.DefaultJsonProtocol._

import org.apache.openwhisk.core.controller.WhiskNamespacesApi
import org.apache.openwhisk.core.entity.EntityPath


  behavior of "Namespaces API"

  val collectionPath = s"/${collection.path}"
  val creds = WhiskAuthHelpers.newIdentity()
  val namespace = EntityPath(creds.subject.asString)

  it should "list namespaces for subject" in {
    implicit val tid = transid()
    Seq("", "/").foreach { p =>
      Get(collectionPath + p) ~> Route.seal(routes(creds)) ~> check {
        status should be(OK)
        val ns = responseAs[List[EntityPath]]
        ns should be(List(EntityPath(creds.subject.asString)))
      }
    }
  }

  it should "reject request for unsupported method" in {
    implicit val tid = transid()
    Seq(Get, Put, Post, Delete).foreach { m =>
      m(s"$collectionPath/${creds.subject}") ~> Route.seal(routes(creds)) ~> check {
        status should be(NotFound)
      }
    }
  }
} 
Example 2
Source File: RespondWithHeadersTests.scala    From openwhisk   with Apache License 2.0 5 votes vote down vote up
package org.apache.openwhisk.core.controller.test

import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner

import akka.http.scaladsl.model.StatusCodes.NotFound
import akka.http.scaladsl.model.StatusCodes.OK
import akka.http.scaladsl.server.Route

import org.apache.openwhisk.core.controller.RespondWithHeaders


@RunWith(classOf[JUnitRunner])
class RespondWithHeadersTests extends ControllerTestCommon with RespondWithHeaders {

  behavior of "General API"

  val routes = {
    pathPrefix("api" / "v1") {
      sendCorsHeaders {
        path("one") {
          complete(OK)
        } ~ path("two") {
          complete(OK)
        } ~ options {
          complete(OK)
        } ~ reject
      }
    } ~ pathPrefix("other") {
      complete(OK)
    }
  }

  it should "respond to options" in {
    Options("/api/v1") ~> Route.seal(routes) ~> check {
      headers should contain allOf (allowOrigin, allowHeaders)
    }
  }

  it should "respond to options on every route under /api/v1" in {
    Options("/api/v1/one") ~> Route.seal(routes) ~> check {
      headers should contain allOf (allowOrigin, allowHeaders)
    }
    Options("/api/v1/two") ~> Route.seal(routes) ~> check {
      headers should contain allOf (allowOrigin, allowHeaders)
    }
  }

  it should "respond to options even on bogus routes under /api/v1" in {
    Options("/api/v1/bogus") ~> Route.seal(routes) ~> check {
      headers should contain allOf (allowOrigin, allowHeaders)
    }
  }

  it should "not respond to options on routes before /api/v1" in {
    Options("/api") ~> Route.seal(routes) ~> check {
      status shouldBe NotFound
    }
  }

} 
Example 3
Source File: WskRestEntitlementTests.scala    From openwhisk   with Apache License 2.0 5 votes vote down vote up
package org.apache.openwhisk.core.cli.test

import akka.http.scaladsl.model.StatusCodes.BadGateway
import akka.http.scaladsl.model.StatusCodes.Forbidden
import akka.http.scaladsl.model.StatusCodes.NotFound
import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
import common.rest.WskRestOperations
import common.rest.RestResult
import common.TestUtils.RunResult
import common.WskActorSystem

@RunWith(classOf[JUnitRunner])
class WskRestEntitlementTests extends WskEntitlementTests with WskActorSystem {
  override lazy val wsk = new WskRestOperations
  override lazy val forbiddenCode = Forbidden.intValue
  override lazy val timeoutCode = BadGateway.intValue
  override lazy val notFoundCode = NotFound.intValue

  override def verifyAction(action: RunResult): org.scalatest.Assertion = {
    val stdout = action.stdout
    stdout should include("name")
    stdout should include("parameters")
    stdout should include("limits")
    stdout should include(""""key":"a"""")
    stdout should include(""""value":"A"""")
  }

  override def verifyPackageList(packageList: RunResult,
                                 namespace: String,
                                 packageName: String,
                                 actionName: String): Unit = {
    val packageListResultRest = packageList.asInstanceOf[RestResult]
    val packages = packageListResultRest.getBodyListJsObject
    val ns = s"$namespace/$packageName"
    packages.exists(pack =>
      RestResult.getField(pack, "namespace") == ns && RestResult.getField(pack, "name") == actionName) shouldBe true
  }

  override def verifyPackageSharedList(packageList: RunResult, namespace: String, packageName: String): Unit = {
    val packageListResultRest = packageList.asInstanceOf[RestResult]
    val packages = packageListResultRest.getBodyListJsObject
    packages.exists(
      pack =>
        RestResult.getField(pack, "namespace") == namespace && RestResult
          .getField(pack, "name") == packageName) shouldBe true
  }

  override def verifyPackageNotSharedList(packageList: RunResult, namespace: String, packageName: String): Unit = {
    val packageListResultRest = packageList.asInstanceOf[RestResult]
    val packages = packageListResultRest.getBodyListJsObject
    packages.exists(
      pack =>
        RestResult.getField(pack, "namespace") == namespace && RestResult
          .getField(pack, "name") == packageName) shouldBe false
  }
} 
Example 4
Source File: MediaServer.scala    From wix-http-testkit   with MIT License 5 votes vote down vote up
package com.wix.e2e.http.examples

import akka.http.scaladsl.model.HttpMethods.{GET, PUT}
import akka.http.scaladsl.model.MediaTypes.`image/png`
import akka.http.scaladsl.model.StatusCodes.NotFound
import akka.http.scaladsl.model.Uri.Path
import akka.http.scaladsl.model._
import com.wix.e2e.http.RequestHandler
import com.wix.e2e.http.client.extractors._
import com.wix.e2e.http.server.WebServerFactory.aMockWebServerWith

import scala.collection.concurrent.TrieMap

class MediaServer(port: Int, uploadPath: String, downloadPath: String) {

  private val mockWebServer = aMockWebServerWith( {
    case HttpRequest(PUT, u, headers, entity, _) if u.path.tail == Path(uploadPath) =>
      handleMediaPost(u, headers.toList, entity)

    case HttpRequest(GET, u, headers, _, _) if u.path.tail.toString().startsWith(downloadPath) =>
      handleMediaGet(u, headers.toList)

  } : RequestHandler).onPort(port)
                     .build.start()

  def stop() = mockWebServer.stop()

  private def handleMediaPost(uri: Uri, headers: List[HttpHeader], entity: HttpEntity): HttpResponse = {
    val fileName = headers.find( _.name == "filename").map( _.value ).orElse( uri.query().toMap.get("f") ).get
    val media = entity.extractAsBytes
    files.put(fileName, media)
    HttpResponse()
  }

  private def handleMediaGet(uri: Uri, headers: List[HttpHeader]): HttpResponse = {
    val fileName = uri.path.reverse
                      .head.toString
                      .stripPrefix("/")
    files.get(fileName)
      .map( i => HttpResponse(entity = HttpEntity(`image/png`, i)) )
      .getOrElse( HttpResponse(status = NotFound) )
  }

  private val files = TrieMap.empty[String, Array[Byte]]
} 
Example 5
Source File: ReservationViewEndpointSpec.scala    From ddd-leaven-akka-v2   with MIT License 5 votes vote down vote up
package ecommerce.sales.app

import java.sql.Date

import akka.http.scaladsl.model.StatusCodes.NotFound
import akka.http.scaladsl.server._
import akka.http.scaladsl.testkit.ScalatestRouteTest
import com.typesafe.config.ConfigFactory
import ecommerce.sales.view.{ReservationDao, ReservationView, ViewTestSupport}
import ecommerce.sales.{ReservationStatus, SalesSerializationHintsProvider}
import org.joda.time.DateTime._
import org.json4s.Formats
import org.scalatest.{BeforeAndAfter, Matchers, WordSpecLike}
import pl.newicom.dddd.serialization.JsonSerHints._
import pl.newicom.dddd.utils.UUIDSupport.uuid7

class ReservationViewEndpointSpec extends WordSpecLike with Matchers with ScalatestRouteTest with ViewTestSupport with BeforeAndAfter {

  override lazy val config = ConfigFactory.load
  implicit val formats: Formats = new SalesSerializationHintsProvider().hints()

  lazy val dao = new ReservationDao
  val reservationId = uuid7

  before {
    viewStore.run {
      dao.createOrUpdate(ReservationView(reservationId, "client-1", ReservationStatus.Opened, new Date(now.getMillis)))
    }.futureValue
  }

  after {
    viewStore.run {
      dao.remove(reservationId)
    }.futureValue
  }

  "Reservation view endpoint" should {

    def response = responseAs[String]

    val route: Route = new ReservationViewEndpoint().route(viewStore)

    "respond to /reservation/all with all reservations" in {
      Get("/reservation/all") ~> route ~> check {
        response should include (reservationId)
      }
    }

    "respond to /reservation/{reservationId} with requested reservation" in {
      Get(s"/reservation/$reservationId") ~> route ~> check {
        response should include (reservationId)
      }
    }

    "respond to /reservation/{reservationId} with NotFound if reservation unknown" in {
      Get(s"/reservation/invalid") ~> route ~> check {
        status shouldBe NotFound
      }
    }

  }

  def ensureSchemaDropped = dao.ensureSchemaDropped
  def ensureSchemaCreated = dao.ensureSchemaCreated

} 
Example 6
Source File: ShipmentViewEndpointSpec.scala    From ddd-leaven-akka-v2   with MIT License 5 votes vote down vote up
package ecommerce.shipping.app

import akka.http.scaladsl.model.StatusCodes.NotFound
import akka.http.scaladsl.server._
import akka.http.scaladsl.testkit.{RouteTestTimeout, ScalatestRouteTest}
import akka.testkit.TestDuration
import com.typesafe.config.ConfigFactory
import ecommerce.sales.view.ViewTestSupport
import ecommerce.shipping.view.{ShipmentDao, ShipmentView}
import ecommerce.shipping.{ShippingSerializationHintsProvider, ShippingStatus}
import org.json4s.Formats
import org.scalatest.{BeforeAndAfter, Matchers, WordSpecLike}
import pl.newicom.dddd.serialization.JsonSerHints._
import pl.newicom.dddd.utils.UUIDSupport.uuid7

import scala.concurrent.duration.DurationInt

class ShipmentViewEndpointSpec extends WordSpecLike with Matchers with ScalatestRouteTest
  with ViewTestSupport with BeforeAndAfter {

  override lazy val config = ConfigFactory.load
  implicit val formats: Formats = new ShippingSerializationHintsProvider().hints()

  implicit val routeTimeout = RouteTestTimeout(3.seconds dilated)

  lazy val dao = new ShipmentDao
  val shipmentId = uuid7

  before {
    viewStore.run {
      dao.createOrUpdate(ShipmentView(shipmentId, "order-1", ShippingStatus.Delivered))
    }.futureValue
  }

  after {
    viewStore.run {
      dao.remove(shipmentId)
    }.futureValue
  }

  "Shipment view endpoint" should {

    def response = responseAs[String]

    val route: Route = new ShipmentViewEndpoint().route(viewStore)

    "respond to /shipment/all with all shipments" in {
      Get("/shipment/all") ~> route ~> check {
        response should include (shipmentId)
      }
    }

    "respond to /shipment/{shipmentId} with requested shipment" in {
      Get(s"/shipment/$shipmentId") ~> route ~> check {
        response should include (shipmentId)
      }
    }

    "respond to /shipment/{shipmentId} with NotFound if shipment unknown" in {
      Get(s"/shipment/invalid") ~> route ~> check {
        status shouldBe NotFound
      }
    }

  }

  def ensureSchemaDropped = dao.ensureSchemaDropped
  def ensureSchemaCreated = dao.ensureSchemaCreated

} 
Example 7
Source File: ExtraDirectives.scala    From eclair   with Apache License 2.0 5 votes vote down vote up
package fr.acinq.eclair.api

import akka.http.scaladsl.marshalling.ToResponseMarshaller
import akka.http.scaladsl.model.StatusCodes.NotFound
import akka.http.scaladsl.model.{ContentTypes, HttpResponse}
import akka.http.scaladsl.server.{Directive1, Directives, MalformedFormFieldRejection, Route}
import fr.acinq.bitcoin.ByteVector32
import fr.acinq.bitcoin.Crypto.PublicKey
import fr.acinq.eclair.ApiTypes.ChannelIdentifier
import fr.acinq.eclair.api.FormParamExtractors._
import fr.acinq.eclair.api.JsonSupport._
import fr.acinq.eclair.payment.PaymentRequest
import fr.acinq.eclair.{MilliSatoshi, ShortChannelId}

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

trait ExtraDirectives extends Directives {

  // named and typed URL parameters used across several routes
  val shortChannelIdFormParam = "shortChannelId".as[ShortChannelId](shortChannelIdUnmarshaller)
  val shortChannelIdsFormParam = "shortChannelIds".as[List[ShortChannelId]](shortChannelIdsUnmarshaller)
  val channelIdFormParam = "channelId".as[ByteVector32](sha256HashUnmarshaller)
  val channelIdsFormParam = "channelIds".as[List[ByteVector32]](sha256HashesUnmarshaller)
  val nodeIdFormParam = "nodeId".as[PublicKey]
  val nodeIdsFormParam = "nodeIds".as[List[PublicKey]](pubkeyListUnmarshaller)
  val paymentHashFormParam = "paymentHash".as[ByteVector32](sha256HashUnmarshaller)
  val fromFormParam = "from".as[Long]
  val toFormParam = "to".as[Long]
  val amountMsatFormParam = "amountMsat".as[MilliSatoshi]
  val invoiceFormParam = "invoice".as[PaymentRequest]

  // custom directive to fail with HTTP 404 (and JSON response) if the element was not found
  def completeOrNotFound[T](fut: Future[Option[T]])(implicit marshaller: ToResponseMarshaller[T]): Route = onComplete(fut) {
    case Success(Some(t)) => complete(t)
    case Success(None) =>
      complete(HttpResponse(NotFound).withEntity(ContentTypes.`application/json`, serialization.writePretty(ErrorResponse("Not found"))))
    case Failure(_) => reject
  }

  def withChannelIdentifier: Directive1[ChannelIdentifier] = formFields(channelIdFormParam.?, shortChannelIdFormParam.?).tflatMap {
    case (Some(channelId), None) => provide(Left(channelId))
    case (None, Some(shortChannelId)) => provide(Right(shortChannelId))
    case _ => reject(MalformedFormFieldRejection("channelId/shortChannelId", "Must specify either the channelId or shortChannelId (not both)"))
  }

  def withChannelsIdentifier: Directive1[List[ChannelIdentifier]] = formFields(channelIdFormParam.?, channelIdsFormParam.?, shortChannelIdFormParam.?, shortChannelIdsFormParam.?).tflatMap {
    case (None, None, None, None) => reject(MalformedFormFieldRejection("channelId(s)/shortChannelId(s)", "Must specify channelId, channelIds, shortChannelId or shortChannelIds"))
    case (channelId_opt, channelIds_opt, shortChannelId_opt, shortChannelIds_opt) =>
      val channelId: List[ChannelIdentifier] = channelId_opt.map(cid => Left(cid)).toList
      val channelIds: List[ChannelIdentifier] = channelIds_opt.map(_.map(cid => Left(cid))).toList.flatten
      val shortChannelId: List[ChannelIdentifier] = shortChannelId_opt.map(scid => Right(scid)).toList
      val shortChannelIds: List[ChannelIdentifier] = shortChannelIds_opt.map(_.map(scid => Right(scid))).toList.flatten
      provide((channelId ++ channelIds ++ shortChannelId ++ shortChannelIds).distinct)
  }

} 
Example 8
Source File: ProjectRejection.scala    From nexus   with Apache License 2.0 5 votes vote down vote up
package ch.epfl.bluebrain.nexus.admin.projects

import java.util.UUID

import akka.http.scaladsl.model.StatusCodes.{BadRequest, Conflict, NotFound}
import ch.epfl.bluebrain.nexus.commons.http.directives.StatusFrom
import ch.epfl.bluebrain.nexus.rdf.implicits._
import ch.epfl.bluebrain.nexus.service.config.Contexts._
import ch.epfl.bluebrain.nexus.service.routes.ResourceRejection
import io.circe.generic.extras.Configuration
import io.circe.generic.extras.semiauto.deriveConfiguredEncoder
import io.circe.{Encoder, Json}

import scala.annotation.nowarn

sealed abstract class ProjectRejection(val msg: String) extends ResourceRejection

object ProjectRejection {

  
  final case class IncorrectRev(expected: Long, provided: Long)
      extends ProjectRejection(
        s"Incorrect revision '$provided' provided, expected '$expected', the project may have been updated since last seen."
      )

  @nowarn("cat=unused")
  implicit val projectRejectionEncoder: Encoder[ProjectRejection] = {
    implicit val rejectionConfig: Configuration = Configuration.default.withDiscriminator("@type")
    val enc                                     = deriveConfiguredEncoder[ProjectRejection].mapJson(_ addContext errorCtxUri)
    Encoder.instance(r => enc(r) deepMerge Json.obj("reason" -> Json.fromString(r.msg)))
  }

  implicit val projectStatusFrom: StatusFrom[ProjectRejection] = StatusFrom {
    case _: IncorrectRev             => Conflict
    case _: ProjectAlreadyExists     => Conflict
    case _: ProjectNotFound          => NotFound
    case _: OrganizationNotFound     => NotFound
    case _: ProjectIsDeprecated      => BadRequest
    case _: OrganizationIsDeprecated => BadRequest
    case _: InvalidProjectFormat     => BadRequest
  }

} 
Example 9
Source File: OrganizationRejection.scala    From nexus   with Apache License 2.0 5 votes vote down vote up
package ch.epfl.bluebrain.nexus.admin.organizations

import java.util.UUID

import akka.http.scaladsl.model.StatusCodes.{BadRequest, Conflict, NotFound}
import ch.epfl.bluebrain.nexus.commons.http.directives.StatusFrom
import ch.epfl.bluebrain.nexus.rdf.implicits._
import ch.epfl.bluebrain.nexus.service.config.Contexts._
import ch.epfl.bluebrain.nexus.service.routes.ResourceRejection
import io.circe.generic.extras.Configuration
import io.circe.generic.extras.semiauto.deriveConfiguredEncoder
import io.circe.{Encoder, Json}

import scala.annotation.nowarn

sealed abstract class OrganizationRejection(val msg: String) extends ResourceRejection

object OrganizationRejection {

  
  final case class IncorrectRev(expected: Long, provided: Long)
      extends OrganizationRejection(
        s"Incorrect revision '$provided' provided, expected '$expected', the organization may have been updated since last seen."
      )

  @nowarn("cat=unused")
  implicit val organizationRejectionEncoder: Encoder[OrganizationRejection] = {
    implicit val rejectionConfig: Configuration = Configuration.default.withDiscriminator("@type")
    val enc                                     = deriveConfiguredEncoder[OrganizationRejection].mapJson(_ addContext errorCtxUri)
    Encoder.instance(r => enc(r) deepMerge Json.obj("reason" -> Json.fromString(r.msg)))
  }

  implicit val organizationStatusFrom: StatusFrom[OrganizationRejection] = StatusFrom {
    case _: IncorrectRev              => Conflict
    case _: OrganizationAlreadyExists => Conflict
    case _: OrganizationNotFound      => NotFound
    case _: InvalidOrganizationFormat => BadRequest
  }
} 
Example 10
Source File: AclRejection.scala    From nexus   with Apache License 2.0 5 votes vote down vote up
package ch.epfl.bluebrain.nexus.iam.acls

import akka.http.scaladsl.model.StatusCodes.{BadRequest, Conflict, NotFound}
import ch.epfl.bluebrain.nexus.commons.http.directives.StatusFrom
import ch.epfl.bluebrain.nexus.iam.types.Permission
import ch.epfl.bluebrain.nexus.rdf.Iri.Path
import ch.epfl.bluebrain.nexus.rdf.implicits._
import ch.epfl.bluebrain.nexus.service.config.Contexts.errorCtxUri
import ch.epfl.bluebrain.nexus.service.routes.ResourceRejection
import io.circe.generic.extras.Configuration
import io.circe.generic.extras.semiauto.deriveConfiguredEncoder
import io.circe.{Encoder, Json}

import scala.annotation.nowarn

sealed abstract class AclRejection(val msg: String) extends ResourceRejection

object AclRejection {

  
  final case class UnknownPermissions(permissions: Set[Permission])
      extends AclRejection(
        s"Some of the permissions specified are not known: '${permissions.mkString("\"", ", ", "\"")}'"
      )

  @nowarn("cat=unused")
  implicit val aclRejectionEncoder: Encoder[AclRejection] = {
    implicit val rejectionConfig: Configuration = Configuration.default.withDiscriminator("@type")
    val enc                                     = deriveConfiguredEncoder[AclRejection].mapJson(_ addContext errorCtxUri)
    Encoder.instance(r => enc(r) deepMerge Json.obj("reason" -> Json.fromString(r.msg)))
  }

  implicit val aclRejectionStatusFrom: StatusFrom[AclRejection] =
    StatusFrom {
      case _: NothingToBeUpdated                        => BadRequest
      case _: AclIsEmpty                                => BadRequest
      case _: AclCannotContainEmptyPermissionCollection => BadRequest
      case _: AclNotFound                               => NotFound
      case _: IncorrectRev                              => Conflict
      case _: UnknownPermissions                        => BadRequest
    }
} 
Example 11
Source File: FailoverTestGateway.scala    From affinity   with Apache License 2.0 5 votes vote down vote up
package io.amient.affinity.core.cluster

import akka.http.scaladsl.model.HttpMethods.{GET, POST}
import akka.http.scaladsl.model.StatusCodes.{NotFound, OK, SeeOther}
import akka.http.scaladsl.model.{HttpResponse, Uri, headers}
import akka.util.Timeout
import com.typesafe.config.ConfigFactory
import io.amient.affinity.core.actor.GatewayHttp
import io.amient.affinity.core.cluster.FailoverTestPartition.{GetValue, PutValue}
import io.amient.affinity.core.http.RequestMatchers.{HTTP, PATH}
import io.amient.affinity.core.http.{Encoder, HttpInterfaceConf}

import scala.collection.JavaConverters._
import scala.concurrent.duration._
import io.amient.affinity.core.ack
import scala.language.postfixOps

class FailoverTestGateway extends GatewayHttp {

  override val rejectSuspendedHttpRequests = false

  override def listenerConfigs: Seq[HttpInterfaceConf] = List(HttpInterfaceConf(
    ConfigFactory.parseMap(Map("host" -> "127.0.0.1", "port" -> "0").asJava)))

  implicit val executor = scala.concurrent.ExecutionContext.Implicits.global

  implicit val scheduler = context.system.scheduler

  val keyspace1 = keyspace("keyspace1")

  override def handle: Receive = {
    case HTTP(GET, PATH(key), _, response) => handleWith(response) {
      implicit val timeout = Timeout(1 seconds)
      keyspace1 ?! GetValue(key) map {
        _ match {
          case None => HttpResponse(NotFound)
          case Some(value) => Encoder.json(OK, value, gzip = false)
        }
      }
    }

    case HTTP(POST, PATH(key, value), _, response) => handleWith(response) {
      implicit val timeout = Timeout(1 seconds)
      keyspace1 ?! PutValue(key, value) map {
        case _ => HttpResponse(SeeOther, headers = List(headers.Location(Uri(s"/$key"))))
      }
    }
  }
} 
Example 12
Source File: AclRejection.scala    From nexus-iam   with Apache License 2.0 5 votes vote down vote up
package ch.epfl.bluebrain.nexus.iam.acls

import akka.http.scaladsl.model.StatusCodes.{BadRequest, Conflict, NotFound}
import ch.epfl.bluebrain.nexus.commons.http.directives.StatusFrom
import ch.epfl.bluebrain.nexus.iam.config.Contexts.errorCtxUri
import ch.epfl.bluebrain.nexus.iam.types.{Permission, ResourceRejection}
import ch.epfl.bluebrain.nexus.rdf.Iri.Path
import ch.epfl.bluebrain.nexus.rdf.implicits._
import com.github.ghik.silencer.silent
import io.circe.generic.extras.Configuration
import io.circe.generic.extras.semiauto.deriveConfiguredEncoder
import io.circe.{Encoder, Json}

sealed abstract class AclRejection(val msg: String) extends ResourceRejection

object AclRejection {

  
  final case class UnknownPermissions(permissions: Set[Permission])
      extends AclRejection(
        s"Some of the permissions specified are not known: '${permissions.mkString("\"", ", ", "\"")}'"
      )

  @silent // rejectionConfig is not recognized as being used
  implicit val aclRejectionEncoder: Encoder[AclRejection] = {
    implicit val rejectionConfig: Configuration = Configuration.default.withDiscriminator("@type")
    val enc                                     = deriveConfiguredEncoder[AclRejection].mapJson(_ addContext errorCtxUri)
    Encoder.instance(r => enc(r) deepMerge Json.obj("reason" -> Json.fromString(r.msg)))
  }

  implicit val aclRejectionStatusFrom: StatusFrom[AclRejection] =
    StatusFrom {
      case _: NothingToBeUpdated                        => BadRequest
      case _: AclIsEmpty                                => BadRequest
      case _: AclCannotContainEmptyPermissionCollection => BadRequest
      case _: AclNotFound                               => NotFound
      case _: IncorrectRev                              => Conflict
      case _: UnknownPermissions                        => BadRequest
    }
} 
Example 13
Source File: HealthRoute.scala    From vamp   with Apache License 2.0 5 votes vote down vote up
package io.vamp.http_api

import akka.http.scaladsl.model.StatusCodes.{ NotFound, OK }
import akka.http.scaladsl.server.Route
import akka.util.Timeout
import io.vamp.common.Namespace
import io.vamp.common.http.HttpApiDirectives
import io.vamp.operation.controller.HealthController

import io.vamp.model.artifact.{ Gateway, Deployment }

object HealthRoute {
  val path = "health"
}

trait HealthRoute extends AbstractRoute with HealthController {
  this: HttpApiDirectives ⇒

  def healthRoutes(implicit namespace: Namespace, timeout: Timeout): Route = pathPrefix(HealthRoute.path) {
    get {
      parameters("window".?) { window ⇒
        path(Gateway.kind / Segment) { gateway ⇒
          pathEndOrSingleSlash {
            onSuccess(gatewayHealth(gateway)(window)) {
              case Some(result) ⇒ respondWith(OK, result)
              case _            ⇒ respondWith(NotFound, None)
            }
          }
        } ~ path(Gateway.kind / Segment / "routes" / Segment) { (gateway, route) ⇒
          pathEndOrSingleSlash {
            onSuccess(routeHealth(gateway, route)(window)) {
              case Some(result) ⇒ respondWith(OK, result)
              case _            ⇒ respondWith(NotFound, None)
            }
          }
        } ~ path(Deployment.kind / Segment) { deployment ⇒
          pathEndOrSingleSlash {
            onSuccess(deploymentHealth(deployment)(window)) {
              case Some(result) ⇒ respondWith(OK, result)
              case _            ⇒ respondWith(NotFound, None)
            }
          }
        } ~ path(Deployment.kind / Segment / "clusters" / Segment) { (deployment, cluster) ⇒
          pathEndOrSingleSlash {
            onSuccess(clusterHealth(deployment, cluster)(window)) {
              case Some(result) ⇒ respondWith(OK, result)
              case _            ⇒ respondWith(NotFound, None)
            }
          }
        } ~ path(Deployment.kind / Segment / "clusters" / Segment / "services" / Segment) { (deployment, cluster, service) ⇒
          pathEndOrSingleSlash {
            onSuccess(serviceHealth(deployment, cluster, service)(window)) {
              case Some(result) ⇒ respondWith(OK, result)
              case _            ⇒ respondWith(NotFound, None)
            }
          }
        } ~ path(Deployment.kind / Segment / "clusters" / Segment / "services" / Segment / "instances" / Segment) { (deployment, cluster, service, instance) ⇒
          pathEndOrSingleSlash {
            onSuccess(instanceHealth(deployment, cluster, service, instance)(window)) {
              case Some(result) ⇒ respondWith(OK, result)
              case _            ⇒ respondWith(NotFound, None)
            }
          }
        }
      }
    }
  }
} 
Example 14
Source File: MetricsRoute.scala    From vamp   with Apache License 2.0 5 votes vote down vote up
package io.vamp.http_api

import akka.http.scaladsl.model.StatusCodes.{ NotFound, OK }
import akka.http.scaladsl.server.Route
import akka.util.Timeout
import io.vamp.common.Namespace
import io.vamp.common.http.HttpApiDirectives
import io.vamp.operation.controller.MetricsController

import io.vamp.model.artifact.{ Gateway, Deployment }

object MetricsRoute {
  val path = "metrics"
}

trait MetricsRoute extends AbstractRoute with MetricsController {
  this: HttpApiDirectives ⇒

  def metricsRoutes(implicit namespace: Namespace, timeout: Timeout): Route = pathPrefix(MetricsRoute.path) {
    get {
      parameters("window".?) { window ⇒
        path(Gateway.kind / Segment / Segment) { (gateway, metrics) ⇒
          pathEndOrSingleSlash {
            onSuccess(gatewayMetrics(gateway, metrics)(window)) {
              case Some(result) ⇒ respondWith(OK, result)
              case _            ⇒ respondWith(NotFound, None)
            }
          }
        } ~ path(Gateway.kind / Segment / "routes" / Segment / Segment) { (gateway, route, metrics) ⇒
          pathEndOrSingleSlash {
            onSuccess(routeMetrics(gateway, route, metrics)(window)) {
              case Some(result) ⇒ respondWith(OK, result)
              case _            ⇒ respondWith(NotFound, None)
            }
          }
        } ~ path(Deployment.kind / Segment / "clusters" / Segment / "ports" / Segment / Segment) { (deployment, cluster, port, metrics) ⇒
          pathEndOrSingleSlash {
            onSuccess(clusterMetrics(deployment, cluster, port, metrics)(window)) {
              case Some(result) ⇒ respondWith(OK, result)
              case _            ⇒ respondWith(NotFound, None)
            }
          }
        } ~ path(Deployment.kind / Segment / "clusters" / Segment / "services" / Segment / "ports" / Segment / Segment) { (deployment, cluster, service, port, metrics) ⇒
          pathEndOrSingleSlash {
            onSuccess(serviceMetrics(deployment, cluster, service, port, metrics)(window)) {
              case Some(result) ⇒ respondWith(OK, result)
              case _            ⇒ respondWith(NotFound, None)
            }
          }
        } ~ path(Deployment.kind / Segment / "clusters" / Segment / "services" / Segment / "instances" / Segment / "ports" / Segment / Segment) { (deployment, cluster, service, instance, port, metrics) ⇒
          pathEndOrSingleSlash {
            onSuccess(instanceMetrics(deployment, cluster, service, instance, port, metrics)(window)) {
              case Some(result) ⇒ respondWith(OK, result)
              case _            ⇒ respondWith(NotFound, None)
            }
          }
        }
      }
    }
  }
} 
Example 15
Source File: Api.scala    From whirlwind-tour-akka-typed   with Apache License 2.0 5 votes vote down vote up
package de.heikoseeberger.wtat

import akka.actor.{ ActorSystem, Scheduler }
import akka.http.scaladsl.Http
import akka.http.scaladsl.Http.ServerBinding
import akka.http.scaladsl.model.StatusCodes.{ Conflict, Created, NoContent, NotFound }
import akka.http.scaladsl.server.{ Directives, Route }
import akka.stream.Materializer
import akka.actor.typed.scaladsl.Actor
import akka.actor.typed.scaladsl.AskPattern.Askable
import akka.actor.typed.{ ActorRef, Behavior }
import akka.util.Timeout
import de.heikoseeberger.akkahttpcirce.ErrorAccumulatingCirceSupport
import java.net.InetSocketAddress
import org.apache.logging.log4j.scala.Logging
import scala.concurrent.duration.FiniteDuration
import scala.util.{ Failure, Success }

object Api extends Logging {

  sealed trait Command
  private final case object HandleBindFailure                      extends Command
  private final case class HandleBound(address: InetSocketAddress) extends Command

  final val Name = "api"

  def apply(address: String,
            port: Int,
            userRepository: ActorRef[UserRepository.Command],
            userView: ActorRef[UserView.Command],
            askTimeout: FiniteDuration)(implicit mat: Materializer): Behavior[Command] =
    Actor.deferred { context =>
      import akka.actor.typed.scaladsl.adapter._
      import context.executionContext
      implicit val s: ActorSystem = context.system.toUntyped

      val self = context.self
      Http()
        .bindAndHandle(route(userRepository, userView)(askTimeout, context.system.scheduler),
                       address,
                       port)
        .onComplete {
          case Failure(_)                      => self ! HandleBindFailure
          case Success(ServerBinding(address)) => self ! HandleBound(address)
        }

      Actor.immutable {
        case (_, HandleBindFailure) =>
          logger.error(s"Stopping, because cannot bind to $address:$port!")
          Actor.stopped

        case (_, HandleBound(address)) =>
          logger.info(s"Bound to $address")
          Actor.ignore
      }
    }

  def route(
      userRepository: ActorRef[UserRepository.Command],
      userView: ActorRef[UserView.Command]
  )(implicit askTimeout: Timeout, scheduler: Scheduler): Route = {
    import Directives._
    import ErrorAccumulatingCirceSupport._
    import io.circe.generic.auto._
    import io.circe.refined._

    pathEndOrSingleSlash {
      get {
        complete {
          import UserView._
          (userView ? GetUsers).mapTo[Users]
        }
      } ~
      post {
        entity(as[User]) { user =>
          import UserRepository._
          onSuccess(userRepository ? addUser(user)) {
            case UsernameTaken(_) => complete(Conflict)
            case UserAdded(_)     => complete(Created)
          }
        }
      }
    } ~
    path(Segment) { username =>
      delete {
        import UserRepository._
        onSuccess(userRepository ? removeUser(username)) {
          case UsernameUnknown(_) => complete(NotFound)
          case UserRemoved(_)     => complete(NoContent)
        }
      }
    }
  }
}