akka.http.scaladsl.model.HttpResponse Scala Examples
The following examples show how to use akka.http.scaladsl.model.HttpResponse.
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: ExecuteAfterResponse.scala From opencensus-scala with Apache License 2.0 | 6 votes |
package io.opencensus.scala.akka.http.utils import akka.NotUsed import akka.http.scaladsl.model.{HttpEntity, HttpResponse} import akka.stream.scaladsl.Flow import akka.stream.stage.{GraphStage, GraphStageLogic, InHandler, OutHandler} import akka.stream.{Attributes, FlowShape, Inlet, Outlet} object ExecuteAfterResponse { private class AfterResponseFlow[Element]( onFinish: () => Unit, onFailure: Throwable => Unit ) extends GraphStage[FlowShape[Element, Element]] { private val in = Inlet[Element]("in") private val out = Outlet[Element]("out") override def createLogic(inheritedAttributes: Attributes): GraphStageLogic = new GraphStageLogic(shape) with InHandler with OutHandler { def onPush(): Unit = push(out, grab(in)) def onPull(): Unit = pull(in) setHandler(in, this) setHandler(out, this) override def onUpstreamFinish(): Unit = { onFinish() super.onUpstreamFinish() } override def onUpstreamFailure(ex: Throwable): Unit = { onFailure(ex) super.onUpstreamFailure(ex) } } override val shape = FlowShape(in, out) } private object AfterResponseFlow { def apply[Element]( onFinish: () => Unit, onFailure: Throwable => Unit ): Flow[Element, Element, NotUsed] = Flow.fromGraph(new AfterResponseFlow(onFinish, onFailure)) } def onComplete( response: HttpResponse, onFinish: () => Unit, onFailure: Throwable => Unit ): HttpResponse = { response.copy( entity = if (response.status.allowsEntity) { response.entity.transformDataBytes( AfterResponseFlow(onFinish, onFailure) ) } else { onFinish() HttpEntity.Empty } ) } }
Example 2
Source File: GeneralError.scala From kanadi with MIT License | 5 votes |
package org.zalando.kanadi.models import akka.http.scaladsl.model.{HttpRequest, HttpResponse} import org.mdedetrich.webmodels.Problem class GeneralError(val problem: Problem, override val httpRequest: HttpRequest, override val httpResponse: HttpResponse) extends HttpServiceError(httpRequest, httpResponse, Right(problem)) { override def getMessage: String = s"Error from server, response is $problem" } final case class OtherError(error: BasicServerError) extends Exception { override def getMessage: String = s"Error from server, response is $error" } class ExpectedHeader(val headerName: String, override val httpRequest: HttpRequest, override val httpResponse: HttpResponse) extends HttpServiceError(httpRequest, httpResponse, Left("")) { override def getMessage: String = s"Expected header with name: $headerName, request is $httpRequest, response is $httpResponse" }
Example 3
Source File: StatsClient.scala From opencensus-scala with Apache License 2.0 | 5 votes |
package io.opencensus.scala.akka.http import akka.http.scaladsl.model.{HttpRequest, HttpResponse} import com.typesafe.scalalogging.LazyLogging import io.opencensus.scala.Stats import io.opencensus.scala.akka.http.stats.HttpStats import io.opencensus.scala.akka.http.utils.ExecuteAfterResponse import scala.concurrent.{ExecutionContext, Future} trait StatsClient extends HttpStats with LazyLogging { def recorded( doRequest: HttpRequest => Future[HttpResponse], routeName: String )( implicit ec: ExecutionContext ): HttpRequest => Future[HttpResponse] = req => { val start = System.currentTimeMillis() doRequest(req).map(response => ExecuteAfterResponse.onComplete( response, onFinish = () => measureClientRoundtripLatency( routeName, req.method, response.status, (System.currentTimeMillis() - start).toDouble ).fold( error => logger.warn("Failed to measure server latency", error), identity ), onFailure = _ => () ) ) } } object StatsClient extends StatsClient { override private[http] val stats = Stats }
Example 4
Source File: HttpExtractors.scala From opencensus-scala with Apache License 2.0 | 5 votes |
package io.opencensus.scala.akka.http.trace import akka.http.scaladsl.model.headers.{Host, `User-Agent`} import akka.http.scaladsl.model.{HttpRequest, HttpResponse} import io.opencensus.scala.http.{RequestExtractor, ResponseExtractor} object HttpExtractors { implicit val requestExtractor: RequestExtractor[HttpRequest] = new RequestExtractor[HttpRequest] { override def host(req: HttpRequest): String = { if (req.uri.isAbsolute) req.uri.authority.host.address() else req .header[Host] .map(_.value()) // Having no Host header with a relative URL is invalid according to rfc2616, // but Akka-HTTP still allows to create such HttpRequests. .getOrElse("") } override def method(req: HttpRequest): String = req.method.value override def path(req: HttpRequest): String = req.uri.path.toString() override def userAgent(req: HttpRequest): Option[String] = req .header[`User-Agent`] .map(_.value()) } implicit val responseExtractor: ResponseExtractor[HttpResponse] = (res: HttpResponse) => res.status.intValue().toLong }
Example 5
Source File: EndSpanResponse.scala From opencensus-scala with Apache License 2.0 | 5 votes |
package io.opencensus.scala.akka.http.trace import akka.http.scaladsl.model.HttpResponse import io.opencensus.scala.Tracing import HttpExtractors._ import io.opencensus.scala.akka.http.utils.ExecuteAfterResponse import io.opencensus.scala.http.{HttpAttributes, StatusTranslator} import io.opencensus.trace.Span private[http] object EndSpanResponse { def forServer( tracing: Tracing, response: HttpResponse, span: Span ): HttpResponse = end(tracing, response, span, "response sent") def forClient( tracing: Tracing, response: HttpResponse, span: Span ): HttpResponse = end(tracing, response, span, "response received") private def end( tracing: Tracing, response: HttpResponse, span: Span, responseAnnotation: String ): HttpResponse = { HttpAttributes.setAttributesForResponse(span, response) span.addAnnotation(responseAnnotation) tracing.setStatus( span, StatusTranslator.translate(response.status.intValue()) ) ExecuteAfterResponse.onComplete( response, onFinish = () => tracing.endSpan(span), onFailure = _ => tracing.endSpan(span) ) } }
Example 6
Source File: StatsClientSpec.scala From opencensus-scala with Apache License 2.0 | 5 votes |
package io.opencensus.scala.akka.http import akka.actor.ActorSystem import akka.http.scaladsl.model.{HttpRequest, HttpResponse} import io.opencensus.scala.http.testSuite.MockStats import io.opencensus.scala.stats.{Distribution, MeasurementDouble} import org.scalatest.BeforeAndAfterAll import org.scalatest.flatspec.AsyncFlatSpec import org.scalatest.matchers.should.Matchers import scala.concurrent.Future class StatsClientSpec extends AsyncFlatSpec with BeforeAndAfterAll with Matchers { implicit val system: ActorSystem = ActorSystem() def statsClientWithMock: (StatsClient, MockStats) = { val mockStats = new MockStats val client = new StatsClient { override private[http] val stats = mockStats } (client, mockStats) } it should "register the correct view" in { val (client, mock) = statsClientWithMock val doRequest = client.recorded(_ => Future.successful(HttpResponse()), "routeName") doRequest(HttpRequest()).flatMap(_.discardEntityBytes().future()).map { _ => mock.registeredViews should have length 1 val roundtripLatency = mock.registeredViews.head roundtripLatency.name shouldBe "opencensus.io/http/client/roundtrip_latency" roundtripLatency.measure.name shouldBe "opencensus.io/http/client/roundtrip_latency" roundtripLatency.aggregation shouldBe a[Distribution] } } it should "record the correct measure value" in { val (client, mock) = statsClientWithMock val doRequest = client.recorded(_ => Future.successful(HttpResponse()), "routeName") doRequest(HttpRequest()).flatMap(_.discardEntityBytes().future()).map { _ => val (measurement, _) = mock.recordedMeasurements.head measurement match { case MeasurementDouble(measure, value) => measure.name shouldBe "opencensus.io/http/client/roundtrip_latency" value.toInt shouldBe >(0) case other => fail(s"Expected MeasurementDouble got $other") } } } it should "record the correct measure tags" in { val (client, mock) = statsClientWithMock val doRequest = client.recorded(_ => Future.successful(HttpResponse()), "routeName") doRequest(HttpRequest()).flatMap(_.discardEntityBytes().future()).map { _ => mock.recordedMeasurements should have length 1 val (_, tags) = mock.recordedMeasurements.head val tagsKeyValues = tags.map(tag => (tag.key.getName, tag.value.asString())) val expectedTags = List( "http_client_method" -> "GET", "http_client_route" -> "routeName", "http_client_status" -> "200" ) tagsKeyValues should contain theSameElementsAs expectedTags } } override def afterAll(): Unit = { system.terminate() super.afterAll() } }
Example 7
Source File: HttpAkkaAttributesSpec.scala From opencensus-scala with Apache License 2.0 | 5 votes |
package io.opencensus.scala.akka.http.trace import akka.http.scaladsl.model.headers.{Host, `User-Agent`} import akka.http.scaladsl.model.{HttpRequest, HttpResponse} import io.opencensus.scala.http.HttpAttributesSpec class HttpAkkaAttributesSpec extends HttpAttributesSpec { import HttpExtractors._ "Akka http attributes extraction" should behave like httpAttributes( request, response ) def request: BuildRequest => HttpRequest = (request: BuildRequest) => HttpRequest( uri = request.host ++ request.path, headers = List(`User-Agent`(request.userAgent)) ++ request.hostHeader .map(Host(_)) ) def response: Int => HttpResponse = HttpResponse(_) }
Example 8
Source File: S2GraphMutateRoute.scala From incubator-s2graph with Apache License 2.0 | 5 votes |
package org.apache.s2graph.http import akka.http.scaladsl.model.{ContentTypes, HttpEntity, HttpResponse, StatusCodes} import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server.{ExceptionHandler, Route} import com.fasterxml.jackson.core.JsonParseException import org.apache.s2graph.core.rest.RequestParser import org.apache.s2graph.core.storage.MutateResponse import org.apache.s2graph.core.{GraphElement, S2Graph} import org.slf4j.LoggerFactory import play.api.libs.json.{JsValue, Json} import scala.concurrent.{ExecutionContext, Future} trait S2GraphMutateRoute extends PlayJsonSupport { val s2graph: S2Graph val logger = LoggerFactory.getLogger(this.getClass) lazy val parser = new RequestParser(s2graph) lazy val exceptionHandler = ExceptionHandler { case ex: JsonParseException => complete(StatusCodes.BadRequest -> ex.getMessage) case ex: java.lang.IllegalArgumentException => complete(StatusCodes.BadRequest -> ex.getMessage) } lazy val mutateVertex = path("vertex" / Segments) { params => implicit val ec = s2graph.ec val (operation, serviceNameOpt, columnNameOpt) = params match { case operation :: serviceName :: columnName :: Nil => (operation, Option(serviceName), Option(columnName)) case operation :: Nil => (operation, None, None) case _ => throw new RuntimeException("invalid params") } entity(as[JsValue]) { payload => val future = vertexMutate(payload, operation, serviceNameOpt, columnNameOpt).map(Json.toJson(_)) complete(future) } } lazy val mutateEdge = path("edge" / Segment) { operation => implicit val ec = s2graph.ec entity(as[JsValue]) { payload => val future = edgeMutate(payload, operation, withWait = true).map(Json.toJson(_)) complete(future) } } def vertexMutate(jsValue: JsValue, operation: String, serviceNameOpt: Option[String] = None, columnNameOpt: Option[String] = None, withWait: Boolean = true)(implicit ec: ExecutionContext): Future[Seq[Boolean]] = { val vertices = parser.toVertices(jsValue, operation, serviceNameOpt, columnNameOpt) val verticesToStore = vertices.filterNot(_.isAsync) s2graph.mutateVertices(verticesToStore, withWait).map(_.map(_.isSuccess)) } def edgeMutate(elementsWithTsv: Seq[(GraphElement, String)], withWait: Boolean)(implicit ec: ExecutionContext): Future[Seq[Boolean]] = { val elementWithIdxs = elementsWithTsv.zipWithIndex val (elementSync, elementAsync) = elementWithIdxs.partition { case ((element, tsv), idx) => !element.isAsync } val retToSkip = elementAsync.map(_._2 -> MutateResponse.Success) val (elementsToStore, _) = elementSync.map(_._1).unzip val elementsIdxToStore = elementSync.map(_._2) s2graph.mutateElements(elementsToStore, withWait).map { mutateResponses => elementsIdxToStore.zip(mutateResponses) ++ retToSkip }.map(_.sortBy(_._1).map(_._2.isSuccess)) } def edgeMutate(jsValue: JsValue, operation: String, withWait: Boolean)(implicit ec: ExecutionContext): Future[Seq[Boolean]] = { val edgesWithTsv = parser.parseJsonFormat(jsValue, operation) edgeMutate(edgesWithTsv, withWait) } // expose routes lazy val mutateRoute: Route = post { concat( handleExceptions(exceptionHandler) { mutateVertex }, handleExceptions(exceptionHandler) { mutateEdge } ) } }
Example 9
Source File: Server.scala From incubator-s2graph with Apache License 2.0 | 5 votes |
package org.apache.s2graph.http import java.time.Instant import scala.language.postfixOps import scala.concurrent.{Await, ExecutionContext, Future} import scala.concurrent.duration.Duration import scala.util.{Failure, Success} import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model.{ContentTypes, HttpEntity, HttpResponse, StatusCodes} import akka.http.scaladsl.server.Route import akka.http.scaladsl.server.Directives._ import akka.stream.ActorMaterializer import com.typesafe.config.ConfigFactory import org.apache.s2graph.core.S2Graph import org.slf4j.LoggerFactory object Server extends App with S2GraphTraversalRoute with S2GraphAdminRoute with S2GraphMutateRoute with S2GraphQLRoute { implicit val system: ActorSystem = ActorSystem("S2GraphHttpServer") implicit val materializer: ActorMaterializer = ActorMaterializer() implicit val executionContext: ExecutionContext = system.dispatcher val config = ConfigFactory.load() override val s2graph = new S2Graph(config) override val logger = LoggerFactory.getLogger(this.getClass) val port = sys.props.get("http.port").fold(8000)(_.toInt) val interface = sys.props.get("http.interface").fold("0.0.0.0")(identity) val startAt = System.currentTimeMillis() def uptime = System.currentTimeMillis() - startAt def serverHealth = s"""{ "port": ${port}, "interface": "${interface}", "started_at": ${Instant.ofEpochMilli(startAt)}, "uptime": "${uptime} millis" """ def health = HttpResponse(status = StatusCodes.OK, entity = HttpEntity(ContentTypes.`application/json`, serverHealth)) // Allows you to determine routes to expose according to external settings. lazy val routes: Route = concat( pathPrefix("graphs")(traversalRoute), pathPrefix("mutate")(mutateRoute), pathPrefix("admin")(adminRoute), pathPrefix("graphql")(graphqlRoute), get(complete(health)) ) val binding: Future[Http.ServerBinding] = Http().bindAndHandle(routes, interface, port) binding.onComplete { case Success(bound) => logger.info(s"Server online at http://${bound.localAddress.getHostString}:${bound.localAddress.getPort}/") case Failure(e) => logger.error(s"Server could not start!", e) } scala.sys.addShutdownHook { () => s2graph.shutdown() system.terminate() logger.info("System terminated") } Await.result(system.whenTerminated, Duration.Inf) }
Example 10
Source File: OrderBookAskAdapter.scala From matcher with MIT License | 5 votes |
package com.wavesplatform.dex.actors import java.util.concurrent.atomic.AtomicReference import akka.actor._ import akka.actor.typed.scaladsl.adapter._ import akka.http.scaladsl.model.HttpResponse import cats.syntax.either._ import cats.syntax.option._ import com.wavesplatform.dex.actors.orderbook.AggregatedOrderBookActor.{Depth, Query} import com.wavesplatform.dex.actors.orderbook.OrderBookActor.MarketStatus import com.wavesplatform.dex.domain.asset.AssetPair import com.wavesplatform.dex.error import com.wavesplatform.dex.error.MatcherError import com.wavesplatform.dex.model.MatcherModel.DecimalsFormat import com.wavesplatform.dex.model.OrderBookAggregatedSnapshot import scala.concurrent.Future import scala.concurrent.duration.FiniteDuration import scala.reflect.ClassTag // Will be removed after a migration to typed actors class OrderBookAskAdapter(orderBooks: AtomicReference[Map[AssetPair, Either[Unit, ActorRef]]], askTimeout: FiniteDuration)( implicit system: ActorSystem) { import system.dispatcher type Result[T] = Future[Either[MatcherError, Option[T]]] def getMarketStatus(assetPair: AssetPair): Result[MarketStatus] = get[Query.GetMarketStatus, MarketStatus](assetPair, Query.GetMarketStatus(_)) def getAggregatedSnapshot(assetPair: AssetPair): Result[OrderBookAggregatedSnapshot] = get[Query.GetAggregatedSnapshot, OrderBookAggregatedSnapshot](assetPair, Query.GetAggregatedSnapshot(_)) def getHttpView(assetPair: AssetPair, format: DecimalsFormat, depth: Depth): Result[HttpResponse] = get[Query.GetHttpView, HttpResponse](assetPair, Query.GetHttpView(format, depth, _)) private val default = Future.successful(Right(None)) private def get[M <: Query, R: ClassTag](assetPair: AssetPair, message: ActorRef => M): Result[R] = orderBooks.get().get(assetPair) match { case None => default case Some(ob) => ob match { case Left(_) => Future.successful(error.OrderBookBroken(assetPair).asLeft) case Right(ob) => val (askRef, r) = AskActor.mk[R](askTimeout) ob ! message(askRef) r.map(_.some.asRight) } } }
Example 11
Source File: HttpV1OrderBook.scala From matcher with MIT License | 5 votes |
package com.wavesplatform.dex.api.http.entities import java.nio.charset.StandardCharsets import akka.http.scaladsl.model.{HttpEntity, HttpResponse} import io.swagger.annotations.ApiModelProperty import play.api.libs.json.{Json, Reads} case class HttpV1OrderBook(@ApiModelProperty(value = "Timestamp of the last Order Book update") timestamp: Long, @ApiModelProperty( value = "List of aggregated denormalized bid levels [price, amount]", dataType = "[[Ljava.lang.String;", example = """[ [ "1.18", "43800.00000000" ], [ "1.17", "52187.00000000" ], [ "1.16", "809.00000000" ] ]""" ) bids: List[HttpV1LevelAgg], @ApiModelProperty( value = "List of aggregated denormalized ask levels [price, amount]", dataType = "[[Ljava.lang.String;", example = """[ [ "1.19", "2134.00000000" ], [ "1.20", "747.00000000" ] ]""" ) asks: List[HttpV1LevelAgg]) object HttpV1OrderBook { implicit val httpV1OrderBookReads: Reads[HttpV1OrderBook] = Json.reads def fromHttpResponse(response: HttpResponse): HttpV1OrderBook = Json.parse(response.entity.asInstanceOf[HttpEntity.Strict].getData().decodeString(StandardCharsets.UTF_8)).as[HttpV1OrderBook] }
Example 12
Source File: OrderBookHttpInfo.scala From matcher with MIT License | 5 votes |
package com.wavesplatform.dex.api.http import akka.http.scaladsl.model.{ContentTypes, HttpEntity, HttpResponse, StatusCodes} import com.wavesplatform.dex.actors.OrderBookAskAdapter import com.wavesplatform.dex.actors.orderbook.AggregatedOrderBookActor.Depth import com.wavesplatform.dex.api.http.entities.MatcherResponse.toHttpResponse import com.wavesplatform.dex.api.http.entities.{HttpMarketStatus, HttpOrderBook, OrderBookUnavailable, SimpleResponse} import com.wavesplatform.dex.domain.asset.{Asset, AssetPair} import com.wavesplatform.dex.model.MatcherModel.{DecimalsFormat, Denormalized} import com.wavesplatform.dex.time.Time import play.api.libs.json.Json import scala.concurrent.{ExecutionContext, Future} class OrderBookHttpInfo(settings: OrderBookHttpInfo.Settings, askAdapter: OrderBookAskAdapter, time: Time, assetDecimals: Asset => Option[Int])( implicit ec: ExecutionContext) { private val marketStatusNotFound = toHttpResponse( SimpleResponse(StatusCodes.NotFound, Json.obj("message" -> "There is no information about this asset pair")) ) def getMarketStatus(assetPair: AssetPair): Future[HttpResponse] = askAdapter.getMarketStatus(assetPair).map { case Left(e) => toHttpResponse(OrderBookUnavailable(e)) case Right(maybeMarketStatus) => maybeMarketStatus match { case Some(ms) => toHttpResponse(SimpleResponse(HttpMarketStatus fromMarketStatus ms)) case None => marketStatusNotFound } } def getHttpView(assetPair: AssetPair, format: DecimalsFormat, depth: Option[Depth]): Future[HttpResponse] = askAdapter.getHttpView(assetPair, format, settings.nearestBigger(depth)).map { case Right(x) => x.getOrElse(getDefaultHttpView(assetPair, format)) case Left(e) => toHttpResponse(OrderBookUnavailable(e)) } private def getDefaultHttpView(assetPair: AssetPair, format: DecimalsFormat): HttpResponse = { val entity = HttpOrderBook(time.correctedTime(), assetPair, Seq.empty, Seq.empty, assetPairDecimals(assetPair, format)) HttpResponse( entity = HttpEntity( ContentTypes.`application/json`, HttpOrderBook.toJson(entity) ) ) } private def assetPairDecimals(assetPair: AssetPair, format: DecimalsFormat): Option[(Depth, Depth)] = format match { case Denormalized => assetDecimals(assetPair.amountAsset).zip(assetDecimals(assetPair.priceAsset)).headOption case _ => None } } object OrderBookHttpInfo { case class Settings(depthRanges: List[Int], defaultDepth: Option[Int]) { def nearestBigger(to: Option[Int]): Int = to.orElse(defaultDepth) .flatMap(desiredDepth => depthRanges.find(_ >= desiredDepth)) .getOrElse(depthRanges.max) } }
Example 13
Source File: ResponseMetadata.scala From slack-scala-client with MIT License | 5 votes |
package slack.models import akka.http.scaladsl.model.HttpResponse import slack.api.InvalidResponseError import scala.compat.java8.OptionConverters._ import scala.concurrent.duration._ import scala.util.Try case class ResponseMetadata(next_cursor: Option[String]) // Retry after time in seconds returned in a rate limited response // https://api.slack.com/docs/rate-limits case class RetryAfter(seconds: Int) { def finiteDuration: FiniteDuration = seconds.seconds def invalidResponseError: InvalidResponseError = { InvalidResponseError(RetryAfter.responseCode, RetryAfter.message) } } object RetryAfter { val header = "Retry-After" val defaultRetry = "3" val message = "Too Many Requests" val responseCode = 429 def responseToInt(response: HttpResponse): Int = { response.getHeader(header).asScala match { case Some(header) => Try(header.value().toInt).getOrElse(defaultRetry.toInt) case None => defaultRetry.toInt } } }
Example 14
Source File: ScoresApiSupport.scala From avoin-voitto with MIT License | 5 votes |
package liigavoitto.scores import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model.HttpMethods._ import akka.http.scaladsl.model.{ HttpRequest, HttpResponse } import akka.stream.ActorMaterializer import liigavoitto.util.Logging import org.joda.time.format.DateTimeFormat import scala.concurrent.Await import scala.concurrent.duration._ import scala.util.{ Failure, Properties, Success, Try } trait ScoresApiSupport extends Logging { implicit val system: ActorSystem implicit val ec = system.dispatcher implicit val fm = ActorMaterializer() val oneHundredMegabytes = 100000000 val apiUrl = Properties.envOrElse("SCORES_API_URL", "http://scores.api.yle.fi/v0/") val scoresAuth = Map[String, String]( "app_id" -> Properties.envOrElse("SCORES_API_APP_ID", ""), "app_key" -> Properties.envOrElse("SCORES_API_APP_KEY", "") ) val dateFormat = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss") val timeout = 15.seconds protected def get(url: String) = { Try { val request = HttpRequest(GET, url) log.info("REQUEST: " + request) Http().singleRequest(request).map(r => getStr(r)) } match { case Success(s) => s case Failure(e) => log.warn(s"Failed to get $url: " + e.getMessage) e.printStackTrace() throw new RuntimeException("Failure: " + e) } } protected def getStr(r: HttpResponse) = { Try { val entity = Await.result(r.entity.withSizeLimit(oneHundredMegabytes).toStrict(timeout), timeout) entity.data.decodeString("UTF-8") } match { case Success(s) => s case Failure(e) => throw new RuntimeException(s"Scores api failure: " + e.getMessage) } } }
Example 15
Source File: ReliableHttpProxyFactory.scala From reliable-http-client with Apache License 2.0 | 5 votes |
package rhttpc.akkahttp.proxy import akka.NotUsed import akka.actor._ import akka.http.scaladsl.Http import akka.http.scaladsl.model.{HttpEntity, HttpRequest, HttpResponse} import akka.stream.Materializer import akka.stream.scaladsl.{Flow, Sink, Source} import org.slf4j.LoggerFactory import rhttpc.client.protocol.{Correlated, Request} import rhttpc.client.proxy._ import scala.concurrent.duration._ import scala.concurrent.{ExecutionContext, Future} import scala.util.control.NonFatal import scala.util.{Failure, Success} object ReliableHttpProxyFactory { private lazy val logger = LoggerFactory.getLogger(getClass) def send(successRecognizer: SuccessHttpResponseRecognizer, batchSize: Int, parallelConsumers: Int) (request: Request[HttpRequest]) (implicit actorSystem: ActorSystem, materialize: Materializer): Future[HttpResponse] = { import actorSystem.dispatcher send(prepareHttpFlow(batchSize * parallelConsumers), successRecognizer)(request.correlated) } private def prepareHttpFlow(parallelism: Int) (implicit actorSystem: ActorSystem, materialize: Materializer): Flow[(HttpRequest, String), HttpResponse, NotUsed] = { import actorSystem.dispatcher Http().superPool[String]().mapAsync(parallelism) { case (tryResponse, id) => tryResponse match { case Success(response) => response.toStrict(1 minute) case Failure(ex) => Future.failed(ex) } } } private def send(httpFlow: Flow[(HttpRequest, String), HttpResponse, Any], successRecognizer: SuccessHttpResponseRecognizer) (corr: Correlated[HttpRequest]) (implicit ec: ExecutionContext, materialize: Materializer): Future[HttpResponse] = { import collection.JavaConverters._ logger.debug( s"""Sending request for ${corr.correlationId} to ${corr.msg.getUri()}. Headers: |${corr.msg.getHeaders().asScala.toSeq.map(h => " " + h.name() + ": " + h.value()).mkString("\n")} |Body: |${corr.msg.entity.asInstanceOf[HttpEntity.Strict].data.utf8String}""".stripMargin ) val logResp = logResponse(corr) _ val responseFuture = Source.single((corr.msg, corr.correlationId)).via(httpFlow).runWith(Sink.head) responseFuture.onComplete { case Failure(ex) => logger.error(s"Got failure for ${corr.correlationId} to ${corr.msg.getUri()}", ex) case Success(_) => } for { response <- responseFuture transformedToFailureIfNeed <- { if (successRecognizer.isSuccess(response)) { logResp(response, "success response") Future.successful(response) } else { logResp(response, "response recognized as non-success") Future.failed(NonSuccessResponse) } } } yield transformedToFailureIfNeed } private def logResponse(corr: Correlated[HttpRequest]) (response: HttpResponse, additionalInfo: String): Unit = { import collection.JavaConverters._ logger.debug( s"""Got $additionalInfo for ${corr.correlationId} to ${corr.msg.getUri()}. Status: ${response.status.value}. Headers: |${response.getHeaders().asScala.toSeq.map(h => " " + h.name() + ": " + h.value()).mkString("\n")} |Body: |${response.entity.asInstanceOf[HttpEntity.Strict].data.utf8String}""".stripMargin ) } }
Example 16
Source File: package.scala From reliable-http-client with Apache License 2.0 | 5 votes |
package rhttpc import akka.http.scaladsl.model.{HttpRequest, HttpResponse} import org.json4s.Formats import rhttpc.akkahttp.json4s._ import rhttpc.client.proxy.ReliableProxy import rhttpc.client.{InOnlyReliableClient, InOutReliableClient} import rhttpc.transport.json4s.CommonFormats package object akkahttp { type InOutReliableHttpClient = InOutReliableClient[HttpRequest] type InOnlyReliableHttpClient = InOnlyReliableClient[HttpRequest] type ReliableHttpProxy = ReliableProxy[HttpRequest, HttpResponse] implicit val formats: Formats = CommonFormats.formats + ContentTypeSerializer + ByteStringSerializer + UriSerializer }
Example 17
Source File: AmqpSubscriberPerfSpec.scala From reliable-http-client with Apache License 2.0 | 5 votes |
package rhttpc.transport.amqp import akka.Done import akka.actor.{Actor, ActorSystem, Props} import akka.http.scaladsl.Http import akka.http.scaladsl.model.{HttpRequest, HttpResponse} import akka.pattern._ import akka.stream.ActorMaterializer import akka.testkit.{TestKit, TestProbe} import dispatch.url import org.scalatest.{BeforeAndAfterAll, FlatSpecLike, Ignore} import rhttpc.transport.{Deserializer, InboundQueueData, OutboundQueueData, Serializer} import scala.concurrent.duration._ import scala.concurrent.{Await, Future} import scala.util.{Random, Try} @Ignore class AmqpSubscriberPerfSpec extends TestKit(ActorSystem("AmqpSubscriberPerfSpec")) with FlatSpecLike with BeforeAndAfterAll { import system.dispatcher implicit val materializer = ActorMaterializer() implicit def serializer[Msg] = new Serializer[Msg] { override def serialize(obj: Msg): String = obj.toString } implicit def deserializer[Msg] = new Deserializer[Msg] { override def deserialize(value: String): Try[Msg] = Try(value.asInstanceOf[Msg]) } val queueName = "request" val outboundQueueData = OutboundQueueData(queueName, autoDelete = true, durability = false) val inboundQueueData = InboundQueueData(queueName, batchSize = 10, parallelConsumers = 10, autoDelete = true, durability = false) val count = 100 private val interface = "localhost" private val port = 8081 def handle(request: HttpRequest) = { val delay = 5 + Random.nextInt(10) after(delay.seconds, system.scheduler)(Future.successful(HttpResponse())) } it should "have a good throughput" in { val bound = Await.result( Http().bindAndHandleAsync( handle, interface, port ), 5.seconds ) val http = dispatch.Http() // .configure(_.setMaxConnections(count) // .setExecutorService(Executors.newFixedThreadPool(count))) val connection = Await.result(AmqpConnectionFactory.connect(system), 5 seconds) val transport = AmqpTransport( connection = connection ) val publisher = transport.publisher[String](outboundQueueData) val probe = TestProbe() val actor = system.actorOf(Props(new Actor { override def receive: Receive = { case str: String => http(url(s"http://$interface:$port") OK identity).map(_ => Done).pipeTo(self)(sender()) case Done => probe.ref ! Done sender() ! Done } })) val subscriber = transport.subscriber[String](inboundQueueData, actor) subscriber.start() try { measureMeanThroughput(count) { (1 to count).foreach { _ => publisher.publish("x") } probe.receiveWhile(10 minutes, messages = count) { case a => a } } } finally { Await.result(subscriber.stop(), 5.seconds) connection.close(5 * 1000) Await.result(bound.unbind(), 5.seconds) } } def measureMeanThroughput(count: Int)(consume: => Unit) = { val before = System.currentTimeMillis() consume val msgsPerSecond = count / ((System.currentTimeMillis() - before).toDouble / 1000) println(s"Throughput was: $msgsPerSecond msgs/sec") } override protected def afterAll(): Unit = { shutdown() } }
Example 18
Source File: FooBarActor.scala From reliable-http-client with Apache License 2.0 | 5 votes |
package rhttpc.sample import akka.actor._ import akka.http.scaladsl.model.{HttpEntity, HttpResponse} import rhttpc.akkapersistence.ReliableFSM import rhttpc.client.subscription.SubscriptionManager import scala.concurrent.duration._ private class FooBarActor(protected val id: String, protected val subscriptionManager: SubscriptionManager, client: DelayedEchoClient) extends ReliableFSM[FooBarState, FooBarData] { import context.dispatcher override protected def persistenceCategory: String = FooBarActor.persistenceCategory startWith(InitState, EmptyData) when(InitState) { case Event(SendMsg(msg), _) => client.requestResponse(msg) pipeTo this goto(WaitingForResponseState) replyingAfterSave() } when(WaitingForResponseState) { case Event(httpResponse: HttpResponse, _) => self forward httpResponse.entity.asInstanceOf[HttpEntity.Strict].data.utf8String stay() case Event("foo", _) => goto(FooState) acknowledgingAfterSave() case Event("bar", _) => goto(BarState) acknowledgingAfterSave() } when(FooState, stateTimeout = 5 minutes) { case Event(StateTimeout, _) => stop() } when(BarState, stateTimeout = 5 minutes) { case Event(StateTimeout, _) => stop() } whenUnhandled { case Event(CurrentState, _) => sender() ! stateName stay() case Event(StopYourself, _) => stop() } } object FooBarActor { val persistenceCategory = "foobar" def props(id: String, subscriptionManager: SubscriptionManager, client: DelayedEchoClient): Props = Props(new FooBarActor(id, subscriptionManager, client)) } sealed trait FooBarState case object InitState extends FooBarState case object WaitingForResponseState extends FooBarState case object FooState extends FooBarState case object BarState extends FooBarState sealed trait FooBarData case object EmptyData extends FooBarData case class SendMsg(msg: String) case object CurrentState case object StopYourself
Example 19
Source File: GitHub.scala From akka-api-gateway-example with MIT License | 5 votes |
package jp.co.dzl.example.akka.api.service import akka.NotUsed import akka.http.scaladsl.model.headers.RawHeader import akka.http.scaladsl.model.{ HttpRequest, HttpResponse } import akka.stream.scaladsl.Flow trait GitHub { def from(original: HttpRequest): Flow[HttpRequest, HttpRequest, NotUsed] def send: Flow[HttpRequest, HttpResponse, NotUsed] } class GitHubImpl( host: String, port: Int, timeout: Int, httpClient: HttpClient ) extends GitHub { def from(original: HttpRequest): Flow[HttpRequest, HttpRequest, NotUsed] = Flow[HttpRequest].map { req => val xForwardedHost = original.headers.find(_.is("host")).map(_.value()).getOrElse(s"$host:$port") val modifiedHeader = original.addHeader(RawHeader("X-Forwarded-Host", xForwardedHost)) .headers .filterNot(_.lowercaseName() == "host") .filterNot(_.lowercaseName() == "timeout-access") req.withHeaders(modifiedHeader) } def send: Flow[HttpRequest, HttpResponse, NotUsed] = Flow[HttpRequest].via(httpClient.connectionHttps(host, port, timeout)) }
Example 20
Source File: GitHubSpec.scala From akka-api-gateway-example with MIT License | 5 votes |
package jp.co.dzl.example.akka.api.service import akka.actor.ActorSystem import akka.http.scaladsl.model.headers.RawHeader import akka.http.scaladsl.model.{ HttpMethods, HttpRequest, HttpResponse } import akka.stream.ActorMaterializer import akka.stream.scaladsl.{ Flow, Source } import akka.stream.testkit.scaladsl.TestSink import org.scalamock.scalatest.MockFactory import org.scalatest.concurrent.ScalaFutures import org.scalatest.{ BeforeAndAfterAll, FlatSpec, Matchers } import scala.concurrent.Await import scala.concurrent.duration.Duration class GitHubSpec extends FlatSpec with Matchers with ScalaFutures with BeforeAndAfterAll with MockFactory { implicit val system = ActorSystem("github-spec") implicit val executor = system.dispatcher implicit val materializer = ActorMaterializer() override protected def afterAll: Unit = { Await.result(system.terminate(), Duration.Inf) } "#from" should "merge original headers to github request" in { val github = new GitHubImpl("127.0.0.1", 8000, 5, mock[HttpClient]) val request = HttpRequest(HttpMethods.GET, "/") .addHeader(RawHeader("host", "dummy")) .addHeader(RawHeader("timeout-access", "dummy")) val result = Source.single(HttpRequest(HttpMethods.GET, "/v1/github/users/xxxxxx")) .via(github.from(request)) .runWith(TestSink.probe[HttpRequest]) .request(1) .expectNext() result.headers.filter(_.lowercaseName() == "host") shouldBe empty result.headers.filter(_.lowercaseName() == "timeout-access") shouldBe empty result.headers.filter(_.lowercaseName() == "x-forwarded-host") shouldNot be(empty) } "#send" should "connect using http client" in { val httpResponse = HttpResponse() val httpClient = mock[HttpClient] (httpClient.connectionHttps _).expects(*, *, *).returning(Flow[HttpRequest].map(_ => httpResponse)) val github = new GitHubImpl("127.0.0.1", 8000, 5, httpClient) val result = Source.single(HttpRequest(HttpMethods.GET, "/")) .via(github.send) .runWith(TestSink.probe[HttpResponse]) .request(1) .expectNext() result shouldBe httpResponse } }
Example 21
Source File: ClickhouseHostHealthTest.scala From clickhouse-scala-client with GNU Lesser General Public License v3.0 | 5 votes |
package com.crobox.clickhouse.balancing.discovery.health import akka.http.scaladsl.model.{HttpResponse, StatusCodes} import akka.stream.scaladsl.{Sink, Source} import com.crobox.clickhouse.ClickhouseClientSpec import com.crobox.clickhouse.balancing.discovery.health.ClickhouseHostHealth.{Alive, Dead} import com.crobox.clickhouse.internal.ClickhouseHostBuilder import scala.util.{Failure, Success} class ClickhouseHostHealthTest extends ClickhouseClientSpec { private val host = ClickhouseHostBuilder.toHost("localhost", Some(8123)) it should "emit alive for ok response" in { Source .single((Success(HttpResponse(entity = "Ok.")), 0)) .via(ClickhouseHostHealth.parsingFlow(host)) .runWith(Sink.seq) .futureValue should contain theSameElementsAs Seq(Alive(host)) } it should "emit dead for response with other status code" in { Source .single((Success(HttpResponse(status = StatusCodes.InternalServerError)), 0)) .via(ClickhouseHostHealth.parsingFlow(host)) .runWith(Sink.seq) .futureValue .toList match { case Dead(`host`, _) :: Nil => succeed case _ => fail() } } it should "emit dead for failure" in { Source .single((Failure(new IllegalArgumentException), 0)) .via(ClickhouseHostHealth.parsingFlow(host)) .runWith(Sink.seq) .futureValue .toList match { case Dead(`host`, _) :: Nil => succeed case _ => fail() } } }
Example 22
Source File: FilterRecursiveListBucketHandler.scala From rokku with Apache License 2.0 | 5 votes |
package com.ing.wbaa.rokku.proxy.handler import java.net.URLDecoder import akka.NotUsed import akka.http.scaladsl.model.{ HttpRequest, HttpResponse } import akka.stream.alpakka.xml.scaladsl.{ XmlParsing, XmlWriting } import akka.stream.alpakka.xml.{ EndElement, ParseEvent, StartElement, TextEvent } import akka.stream.scaladsl.Flow import akka.util.ByteString import com.ing.wbaa.rokku.proxy.data.{ Read, RequestId, S3Request, User } import scala.collection.immutable import scala.collection.mutable.ListBuffer protected[this] def filterRecursiveListObjects(user: User, requestS3: S3Request)(implicit id: RequestId): Flow[ByteString, ByteString, NotUsed] = { def elementResult(allContentsElements: ListBuffer[ParseEvent], isContentsTag: Boolean, element: ParseEvent): immutable.Seq[ParseEvent] = { if (isContentsTag) { allContentsElements += element immutable.Seq.empty } else { immutable.Seq(element) } } def isPathOkInRangerPolicy(path: String)(implicit id: RequestId): Boolean = { val pathToCheck = normalizePath(path) val isUserAuthorized = isUserAuthorizedForRequest(requestS3.copy(s3BucketPath = Some(pathToCheck)), user) isUserAuthorized } def normalizePath(path: String): String = { val delimiter = "/" val decodedPath = URLDecoder.decode(path, "UTF-8") val delimiterIndex = decodedPath.lastIndexOf(delimiter) val pathToCheckWithoutLastSlash = if (delimiterIndex > 0) delimiter + decodedPath.substring(0, delimiterIndex) else "" val s3BucketName = requestS3.s3BucketPath.getOrElse(delimiter) val s3pathWithoutLastDelimiter = if (s3BucketName.length > 1 && s3BucketName.endsWith(delimiter)) s3BucketName.substring(0, s3BucketName.length - 1) else s3BucketName s3pathWithoutLastDelimiter + pathToCheckWithoutLastSlash } Flow[ByteString].via(XmlParsing.parser) .statefulMapConcat(() => { // state val keyTagValue = StringBuilder.newBuilder val allContentsElements = new ListBuffer[ParseEvent] var isContentsTag = false var isKeyTag = false // aggregation function parseEvent => parseEvent match { //catch <Contents> to start collecting elements case element: StartElement if element.localName == "Contents" => isContentsTag = true allContentsElements.clear() allContentsElements += element immutable.Seq.empty //catch end </Contents> to validate the path in ranger case element: EndElement if element.localName == "Contents" => isContentsTag = false allContentsElements += element if (isPathOkInRangerPolicy(keyTagValue.stripMargin)) { allContentsElements.toList } else { immutable.Seq.empty } // catch <Key> where is the patch name to match in ranger case element: StartElement if element.localName == "Key" => keyTagValue.clear() isKeyTag = true elementResult(allContentsElements, isContentsTag, element) //catch end </Key> case element: EndElement if element.localName == "Key" => isKeyTag = false elementResult(allContentsElements, isContentsTag, element) //catch all element text <..>text<\..> but only set the text from <Key> case element: TextEvent => if (isKeyTag) keyTagValue.append(element.text) elementResult(allContentsElements, isContentsTag, element) //just past through the rest of elements case element => elementResult(allContentsElements, isContentsTag, element) } }) .via(XmlWriting.writer) } }
Example 23
Source File: DarwinService.scala From darwin with Apache License 2.0 | 5 votes |
package it.agilelab.darwin.server.rest import akka.actor.ActorSystem import akka.http.scaladsl.model.{HttpResponse, StatusCodes} import akka.http.scaladsl.server.directives.DebuggingDirectives import akka.http.scaladsl.server.{Directives, Route} import akka.stream.ActorMaterializer import akka.stream.Attributes.LogLevels import it.agilelab.darwin.manager.AvroSchemaManager import org.apache.avro.Schema trait DarwinService extends Service with Directives with DebuggingDirectives with JsonSupport { val manager: AvroSchemaManager override def route: Route = logRequestResult(("darwin", LogLevels.Debug)) { get { path("schemas" / LongNumber.?) { case Some(id) => manager.getSchema(id) match { case Some(schema) => complete(schema) case None => complete { HttpResponse(StatusCodes.NotFound) } } case None => complete(manager.getAll) } } ~ post { path("schemas" / PathEnd) { entity(as[Seq[Schema]]) { schemas => complete { manager.registerAll(schemas).map(_._1) } } } } } } object DarwinService { def apply(asm: AvroSchemaManager)(implicit s: ActorSystem, m: ActorMaterializer): DarwinService = new DarwinService { override implicit val materializer: ActorMaterializer = m override implicit val system: ActorSystem = s override val manager: AvroSchemaManager = asm } }
Example 24
Source File: Error.scala From akka-http-oauth2-client with Apache License 2.0 | 5 votes |
package com.github.dakatsuka.akka.http.oauth2.client import akka.http.scaladsl.model.HttpResponse import akka.http.scaladsl.unmarshalling.Unmarshal import akka.stream.Materializer import com.github.dakatsuka.akka.http.oauth2.client.utils.JsonUnmarshaller import io.circe.Decoder import scala.concurrent.{ ExecutionContext, Future } object Error { sealed abstract class Code(val value: String) case object InvalidRequest extends Code("invalid_request") case object InvalidClient extends Code("invalid_client") case object InvalidToken extends Code("invalid_token") case object InvalidGrant extends Code("invalid_grant") case object InvalidScope extends Code("invalid_scope") case object UnsupportedGrantType extends Code("unsupported_grant_type") case object Unknown extends Code("unknown") object Code { def fromString(code: String): Code = code match { case "invalid_request" => InvalidRequest case "invalid_client" => InvalidClient case "invalid_token" => InvalidToken case "invalid_grant" => InvalidGrant case "invalid_scope" => InvalidScope case "unsupported_grant_type" => UnsupportedGrantType case _ => Unknown } } class UnauthorizedException(val code: Code, val description: String, val response: HttpResponse) extends RuntimeException(s"$code: $description") object UnauthorizedException extends JsonUnmarshaller { case class UnauthorizedResponse(error: String, errorDescription: String) implicit def decoder: Decoder[UnauthorizedResponse] = Decoder.instance { c => for { error <- c.downField("error").as[String].right description <- c.downField("error_description").as[String].right } yield UnauthorizedResponse(error, description) } def fromHttpResponse(response: HttpResponse)(implicit ec: ExecutionContext, mat: Materializer): Future[UnauthorizedException] = { Unmarshal(response).to[UnauthorizedResponse].map { r => new UnauthorizedException(Code.fromString(r.error), r.errorDescription, response) } } } }
Example 25
Source File: AccessToken.scala From akka-http-oauth2-client with Apache License 2.0 | 5 votes |
package com.github.dakatsuka.akka.http.oauth2.client import akka.http.scaladsl.model.HttpResponse import akka.http.scaladsl.unmarshalling.Unmarshal import akka.stream.Materializer import com.github.dakatsuka.akka.http.oauth2.client.utils.JsonUnmarshaller import io.circe.Decoder import scala.concurrent.Future case class AccessToken( accessToken: String, tokenType: String, expiresIn: Int, refreshToken: Option[String] ) object AccessToken extends JsonUnmarshaller { implicit def decoder: Decoder[AccessToken] = Decoder.instance { c => for { accessToken <- c.downField("access_token").as[String].right tokenType <- c.downField("token_type").as[String].right expiresIn <- c.downField("expires_in").as[Int].right refreshToken <- c.downField("refresh_token").as[Option[String]].right } yield AccessToken(accessToken, tokenType, expiresIn, refreshToken) } def apply(response: HttpResponse)(implicit mat: Materializer): Future[AccessToken] = { Unmarshal(response).to[AccessToken] } }
Example 26
Source File: Client.scala From akka-http-oauth2-client with Apache License 2.0 | 5 votes |
package com.github.dakatsuka.akka.http.oauth2.client import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model.headers.OAuth2BearerToken import akka.http.scaladsl.model.{ HttpRequest, HttpResponse, Uri } import akka.stream.Materializer import akka.stream.scaladsl.{ Flow, Sink } import com.github.dakatsuka.akka.http.oauth2.client.Error.UnauthorizedException import com.github.dakatsuka.akka.http.oauth2.client.strategy.Strategy import scala.concurrent.{ ExecutionContext, Future } class Client(config: ConfigLike, connection: Option[Flow[HttpRequest, HttpResponse, _]] = None)(implicit system: ActorSystem) extends ClientLike { def getAuthorizeUrl[A <: GrantType](grant: A, params: Map[String, String] = Map.empty)(implicit s: Strategy[A]): Option[Uri] = s.getAuthorizeUrl(config, params) def getAccessToken[A <: GrantType]( grant: A, params: Map[String, String] = Map.empty )(implicit s: Strategy[A], ec: ExecutionContext, mat: Materializer): Future[Either[Throwable, AccessToken]] = { val source = s.getAccessTokenSource(config, params) source .via(connection.getOrElse(defaultConnection)) .mapAsync(1)(handleError) .mapAsync(1)(AccessToken.apply) .runWith(Sink.head) .map(Right.apply) .recover { case ex => Left(ex) } } def getConnectionWithAccessToken(accessToken: AccessToken): Flow[HttpRequest, HttpResponse, _] = Flow[HttpRequest] .map(_.addCredentials(OAuth2BearerToken(accessToken.accessToken))) .via(connection.getOrElse(defaultConnection)) private def defaultConnection: Flow[HttpRequest, HttpResponse, _] = config.site.getScheme match { case "http" => Http().outgoingConnection(config.getHost, config.getPort) case "https" => Http().outgoingConnectionHttps(config.getHost, config.getPort) } private def handleError(response: HttpResponse)(implicit ec: ExecutionContext, mat: Materializer): Future[HttpResponse] = { if (response.status.isFailure()) UnauthorizedException.fromHttpResponse(response).flatMap(Future.failed(_)) else Future.successful(response) } } object Client { def apply(config: ConfigLike)(implicit system: ActorSystem): Client = new Client(config) def apply(config: ConfigLike, connection: Flow[HttpRequest, HttpResponse, _])(implicit system: ActorSystem): Client = new Client(config, Some(connection)) }
Example 27
Source File: AccessTokenSpec.scala From akka-http-oauth2-client with Apache License 2.0 | 5 votes |
package com.github.dakatsuka.akka.http.oauth2.client import akka.actor.ActorSystem import akka.http.scaladsl.model.{ HttpEntity, HttpResponse, StatusCodes } import akka.http.scaladsl.model.ContentTypes.`application/json` import akka.stream.{ ActorMaterializer, Materializer } import org.scalatest.concurrent.ScalaFutures import org.scalatest.time.{ Millis, Seconds, Span } import org.scalatest.{ BeforeAndAfterAll, DiagrammedAssertions, FlatSpec } import scala.concurrent.{ Await, ExecutionContext } import scala.concurrent.duration.Duration class AccessTokenSpec extends FlatSpec with DiagrammedAssertions with ScalaFutures with BeforeAndAfterAll { implicit val system: ActorSystem = ActorSystem() implicit val ec: ExecutionContext = system.dispatcher implicit val materializer: Materializer = ActorMaterializer() implicit val defaultPatience: PatienceConfig = PatienceConfig(timeout = Span(5, Seconds), interval = Span(700, Millis)) override def afterAll(): Unit = { Await.ready(system.terminate(), Duration.Inf) } behavior of "AccessToken" it should "apply from HttpResponse" in { val accessToken = "xxx" val tokenType = "bearer" val expiresIn = 86400 val refreshToken = "yyy" val httpResponse = HttpResponse( status = StatusCodes.OK, headers = Nil, entity = HttpEntity( `application/json`, s""" |{ | "access_token": "$accessToken", | "token_type": "$tokenType", | "expires_in": $expiresIn, | "refresh_token": "$refreshToken" |} """.stripMargin ) ) val result = AccessToken(httpResponse) whenReady(result) { token => assert(token.accessToken == accessToken) assert(token.tokenType == tokenType) assert(token.expiresIn == expiresIn) assert(token.refreshToken.contains(refreshToken)) } } }
Example 28
Source File: PrometheusSettings.scala From akka-http-metrics with Apache License 2.0 | 5 votes |
package fr.davit.akka.http.metrics.prometheus import akka.http.scaladsl.model.{HttpResponse, StatusCodes} import fr.davit.akka.http.metrics.core.HttpMetricsSettings import fr.davit.akka.http.metrics.prometheus.Quantiles.Quantile import scala.concurrent.duration._ sealed trait HistogramConfig sealed trait TimerConfig final case class Quantiles(qs: List[Quantile], maxAge: FiniteDuration = 10.minutes, ageBuckets: Int = 5) extends HistogramConfig with TimerConfig object Quantiles { final case class Quantile(percentile: Double, error: Double = 0.001) def apply(percentiles: Double*): Quantiles = { val quantiles = percentiles.map { p => // the higher the percentile, the lowe the error val error = (1 - p) / 10 Quantile(p, error) } Quantiles(quantiles.toList) } } final case class Buckets(bs: List[Double]) extends HistogramConfig with TimerConfig object Buckets { def apply(b: Double*): Buckets = Buckets(b.toList) } final case class PrometheusSettings( namespace: String, defineError: HttpResponse => Boolean, includeMethodDimension: Boolean, includePathDimension: Boolean, includeStatusDimension: Boolean, receivedBytesConfig: HistogramConfig, durationConfig: TimerConfig, sentBytesConfig: HistogramConfig ) extends HttpMetricsSettings { override def withNamespace(namespace: String): PrometheusSettings = copy(namespace = namespace) override def withDefineError(fn: HttpResponse => Boolean): PrometheusSettings = copy(defineError = defineError) override def withIncludeMethodDimension(include: Boolean): PrometheusSettings = copy(includeMethodDimension = include) override def withIncludePathDimension(include: Boolean): PrometheusSettings = copy(includePathDimension = include) override def withIncludeStatusDimension(include: Boolean): PrometheusSettings = copy(includeStatusDimension = include) def withReceivedBytesConfig(config: HistogramConfig): PrometheusSettings = copy(receivedBytesConfig = config) def withDurationConfig(config: TimerConfig): PrometheusSettings = copy(durationConfig = config) def withSentBytesConfig(config: HistogramConfig): PrometheusSettings = copy(sentBytesConfig = config) } object PrometheusSettings { // generic durations adapted to network durations in seconds val DurationBuckets: Buckets = { Buckets(0.005, 0.01, .025, .05, .075, .1, .25, .5, .75, 1, 2.5, 5, 7.5, 10) } // generic buckets adapted to network messages sized val BytesBuckets: Buckets = { val buckets = Range(0, 1000, 100) ++ Range(1000, 10000, 1000) ++ Range(10000, 100000, 10000) Buckets(buckets.map(_.toDouble).toList) } // basic quantiles val DefaultQuantiles: Quantiles = Quantiles(0.75, 0.95, 0.98, 0.99, 0.999) val default: PrometheusSettings = PrometheusSettings( namespace = "akka_http", defineError = _.status.isInstanceOf[StatusCodes.ServerError], includeMethodDimension = false, includePathDimension = false, includeStatusDimension = false, receivedBytesConfig = BytesBuckets, durationConfig = DurationBuckets, sentBytesConfig = BytesBuckets ) }
Example 29
Source File: HttpMetricsSettings.scala From akka-http-metrics with Apache License 2.0 | 5 votes |
package fr.davit.akka.http.metrics.core import akka.http.scaladsl.model.{HttpResponse, StatusCodes} trait HttpMetricsSettings { def includeStatusDimension: Boolean def withNamespace(namespace: String): HttpMetricsSettings def withDefineError(fn: HttpResponse => Boolean): HttpMetricsSettings def withIncludeMethodDimension(include: Boolean): HttpMetricsSettings def withIncludePathDimension(include: Boolean): HttpMetricsSettings def withIncludeStatusDimension(include: Boolean): HttpMetricsSettings } object HttpMetricsSettings { val default: HttpMetricsSettings = apply( "akka.http", _.status.isInstanceOf[StatusCodes.ServerError], includeMethodDimension = false, includePathDimension = false, includeStatusDimension = false ) def apply( namespace: String, defineError: HttpResponse => Boolean, includeMethodDimension: Boolean, includePathDimension: Boolean, includeStatusDimension: Boolean ): HttpMetricsSettings = HttpMetricsSettingsImpl( namespace, defineError, includeMethodDimension, includePathDimension, includeStatusDimension ) private case class HttpMetricsSettingsImpl( namespace: String, defineError: HttpResponse => Boolean, includeMethodDimension: Boolean, includePathDimension: Boolean, includeStatusDimension: Boolean ) extends HttpMetricsSettings { override def withNamespace(namespace: String): HttpMetricsSettings = copy(namespace = namespace) override def withDefineError(fn: HttpResponse => Boolean): HttpMetricsSettings = copy(defineError = fn) override def withIncludeMethodDimension(include: Boolean): HttpMetricsSettings = copy(includeMethodDimension = include) override def withIncludePathDimension(include: Boolean): HttpMetricsSettings = copy(includePathDimension = include) override def withIncludeStatusDimension(include: Boolean): HttpMetricsSettings = copy(includeStatusDimension = include) } }
Example 30
Source File: HttpMetricsRoute.scala From akka-http-metrics with Apache License 2.0 | 5 votes |
package fr.davit.akka.http.metrics.core.scaladsl.server import akka.NotUsed import akka.http.scaladsl.model.{HttpRequest, HttpResponse} import akka.http.scaladsl.server._ import akka.http.scaladsl.settings.{ParserSettings, RoutingSettings} import akka.stream.Materializer import akka.stream.scaladsl.Flow import fr.davit.akka.http.metrics.core.HttpMetricsHandler import fr.davit.akka.http.metrics.core.scaladsl.model.PathLabelHeader import scala.concurrent.{ExecutionContextExecutor, Future} object HttpMetricsRoute { implicit def apply(route: Route): HttpMetricsRoute = new HttpMetricsRoute(route) } final class HttpMetricsRoute private (route: Route) extends HttpMetricsDirectives { private def markUnhandled(inner: Route): Route = { Directives.mapResponse(markUnhandled).tapply(_ => inner) } private def markUnhandled(response: HttpResponse): HttpResponse = { response.addHeader(PathLabelHeader.Unhandled) } def recordMetrics(metricsHandler: HttpMetricsHandler)( implicit routingSettings: RoutingSettings, parserSettings: ParserSettings, materializer: Materializer, routingLog: RoutingLog, executionContext: ExecutionContextExecutor = null, rejectionHandler: RejectionHandler = RejectionHandler.default, exceptionHandler: ExceptionHandler = null ): Flow[HttpRequest, HttpResponse, NotUsed] = { val effectiveEC = if (executionContext ne null) executionContext else materializer.executionContext { // override the execution context passed as parameter implicit val executionContext: ExecutionContextExecutor = effectiveEC Flow[HttpRequest] .mapAsync(1)(recordMetricsAsync(metricsHandler)) .watchTermination() { case (mat, completion) => // every connection materializes a stream. metricsHandler.onConnection(completion) mat } } } def recordMetricsAsync(metricsHandler: HttpMetricsHandler)( implicit routingSettings: RoutingSettings, parserSettings: ParserSettings, materializer: Materializer, routingLog: RoutingLog, executionContext: ExecutionContextExecutor = null, rejectionHandler: RejectionHandler = RejectionHandler.default, exceptionHandler: ExceptionHandler = null ): HttpRequest => Future[HttpResponse] = { val effectiveEC = if (executionContext ne null) executionContext else materializer.executionContext val effectiveRejectionHandler = rejectionHandler.mapRejectionResponse(markUnhandled) val effectiveExceptionHandler = ExceptionHandler.seal(exceptionHandler).andThen(markUnhandled(_)) { // override the execution context passed as parameter, rejection and error handler implicit val executionContext: ExecutionContextExecutor = effectiveEC implicit val rejectionHandler: RejectionHandler = effectiveRejectionHandler implicit val exceptionHandler: ExceptionHandler = effectiveExceptionHandler request => val response = Route.asyncHandler(route).apply(request) metricsHandler.onRequest(request, response) response } } }
Example 31
Source File: PrometheusTests.scala From openwhisk with Apache License 2.0 | 5 votes |
package org.apache.openwhisk.common import akka.http.scaladsl.coding.Gzip import akka.http.scaladsl.model.{HttpCharsets, HttpResponse} import akka.http.scaladsl.model.headers.HttpEncodings.gzip import akka.http.scaladsl.model.headers.{`Accept-Encoding`, `Content-Encoding`, HttpEncoding, HttpEncodings} import akka.http.scaladsl.testkit.ScalatestRouteTest import akka.http.scaladsl.unmarshalling.Unmarshal import com.typesafe.config.ConfigFactory import kamon.Kamon import org.junit.runner.RunWith import org.scalatest.concurrent.ScalaFutures import org.scalatest.junit.JUnitRunner import org.scalatest.matchers.Matcher import org.scalatest.{BeforeAndAfterAll, FlatSpec, Matchers} import scala.concurrent.duration._ @RunWith(classOf[JUnitRunner]) class PrometheusTests extends FlatSpec with Matchers with ScalatestRouteTest with BeforeAndAfterAll with ScalaFutures { behavior of "Prometheus" override protected def beforeAll(): Unit = { super.beforeAll() //Modify Kamon to have a very small tick interval val newConfig = ConfigFactory.parseString("""kamon { | metric { | tick-interval = 50 ms | optimistic-tick-alignment = no | } |}""".stripMargin).withFallback(ConfigFactory.load()) Kamon.reconfigure(newConfig) } override protected def afterAll(): Unit = { super.afterAll() Kamon.reconfigure(ConfigFactory.load()) } it should "respond to /metrics" in { val api = new KamonPrometheus Kamon.counter("foo_bar").withoutTags().increment(42) //Sleep to ensure that Kamon metrics are pushed to reporters Thread.sleep(2.seconds.toMillis) Get("/metrics") ~> `Accept-Encoding`(gzip) ~> api.route ~> check { // Check that response confirms to what Prometheus scrapper accepts contentType.charsetOption shouldBe Some(HttpCharsets.`UTF-8`) contentType.mediaType.params("version") shouldBe "0.0.4" response should haveContentEncoding(gzip) val responseText = Unmarshal(Gzip.decodeMessage(response)).to[String].futureValue withClue(responseText) { responseText should include("foo_bar") } } api.close() } it should "not be enabled by default" in { Get("/metrics") ~> MetricsRoute() ~> check { handled shouldBe false } } private def haveContentEncoding(encoding: HttpEncoding): Matcher[HttpResponse] = be(encoding) compose { (_: HttpResponse).header[`Content-Encoding`].map(_.encodings.head).getOrElse(HttpEncodings.identity) } }
Example 32
Source File: SwaggerValidator.scala From openwhisk with Apache License 2.0 | 5 votes |
package common.rest import scala.collection.JavaConverters._ import akka.http.scaladsl.model.HttpEntity import akka.http.scaladsl.model.HttpRequest import akka.http.scaladsl.model.HttpResponse import com.atlassian.oai.validator.SwaggerRequestResponseValidator import com.atlassian.oai.validator.model.SimpleRequest import com.atlassian.oai.validator.model.SimpleResponse import com.atlassian.oai.validator.report.ValidationReport import com.atlassian.oai.validator.whitelist.ValidationErrorsWhitelist import com.atlassian.oai.validator.whitelist.rule.WhitelistRules trait SwaggerValidator { private val specWhitelist = ValidationErrorsWhitelist .create() .withRule( "Ignore action and trigger payloads", WhitelistRules.allOf( WhitelistRules.messageContains("Object instance has properties which are not allowed by the schema"), WhitelistRules.anyOf( WhitelistRules.pathContains("/web/"), WhitelistRules.pathContains("/actions/"), WhitelistRules.pathContains("/triggers/")), WhitelistRules.methodIs(io.swagger.models.HttpMethod.POST))) .withRule( "Ignore invalid action kinds", WhitelistRules.allOf( WhitelistRules.messageContains("kind"), WhitelistRules.messageContains("Instance value"), WhitelistRules.messageContains("not found"), WhitelistRules.pathContains("/actions/"), WhitelistRules.methodIs(io.swagger.models.HttpMethod.PUT))) .withRule( "Ignore tests that check for invalid DELETEs and PUTs on actions", WhitelistRules.anyOf( WhitelistRules.messageContains("DELETE operation not allowed on path '/api/v1/namespaces/_/actions/'"), WhitelistRules.messageContains("PUT operation not allowed on path '/api/v1/namespaces/_/actions/'"))) private val specValidator = SwaggerRequestResponseValidator .createFor("apiv1swagger.json") .withWhitelist(specWhitelist) .build() def validateRequestAndResponse(request: HttpRequest, response: HttpResponse): Seq[String] = { val specRequest = { val builder = new SimpleRequest.Builder(request.method.value, request.uri.path.toString()) val body = strictEntityBodyAsString(request.entity) val withBody = if (body.isEmpty) builder else builder .withBody(body) .withHeader("content-type", request.entity.contentType.value) val withHeaders = request.headers.foldLeft(builder)((b, header) => b.withHeader(header.name, header.value)) val andQuery = request.uri.query().foldLeft(withHeaders) { case (b, (key, value)) => b.withQueryParam(key, value) } andQuery.build() } val specResponse = { val builder = SimpleResponse.Builder .status(response.status.intValue) val body = strictEntityBodyAsString(response.entity) val withBody = if (body.isEmpty) builder else builder .withBody(body) .withHeader("content-type", response.entity.contentType.value) val withHeaders = response.headers.foldLeft(builder)((b, header) => b.withHeader(header.name, header.value)) withHeaders.build() } specValidator .validate(specRequest, specResponse) .getMessages .asScala .filter(m => m.getLevel == ValidationReport.Level.ERROR) .map(_.toString) .toSeq } def strictEntityBodyAsString(entity: HttpEntity): String = entity match { case s: HttpEntity.Strict => s.data.utf8String case _ => "" } }
Example 33
Source File: ApiTests.scala From openwhisk with Apache License 2.0 | 5 votes |
package org.apache.openwhisk.core.monitoring.metrics import akka.http.scaladsl.model.headers.HttpEncodings._ import akka.http.scaladsl.model.headers.{`Accept-Encoding`, `Content-Encoding`, HttpEncoding, HttpEncodings} import akka.http.scaladsl.model.{HttpCharsets, HttpEntity, HttpResponse} import akka.http.scaladsl.testkit.ScalatestRouteTest import kamon.prometheus.PrometheusReporter import org.apache.openwhisk.core.monitoring.metrics.OpenWhiskEvents.MetricConfig import org.junit.runner.RunWith import org.scalatest.concurrent.ScalaFutures import org.scalatest.junit.JUnitRunner import org.scalatest.matchers.Matcher import org.scalatest.{BeforeAndAfterAll, FlatSpec, Matchers} import pureconfig.loadConfigOrThrow import io.prometheus.client.CollectorRegistry import pureconfig.generic.auto._ import scala.concurrent.duration.DurationInt @RunWith(classOf[JUnitRunner]) class ApiTests extends FlatSpec with Matchers with ScalatestRouteTest with EventsTestHelper with ScalaFutures with BeforeAndAfterAll { implicit val timeoutConfig = PatienceConfig(1.minute) private var api: PrometheusEventsApi = _ private var consumer: EventConsumer = _ override protected def beforeAll(): Unit = { super.beforeAll() CollectorRegistry.defaultRegistry.clear() val metricConfig = loadConfigOrThrow[MetricConfig](system.settings.config, "user-events") val mericRecorder = PrometheusRecorder(new PrometheusReporter, metricConfig) consumer = createConsumer(56754, system.settings.config, mericRecorder) api = new PrometheusEventsApi(consumer, createExporter()) } protected override def afterAll(): Unit = { consumer.shutdown().futureValue super.afterAll() } behavior of "EventsApi" it should "respond ping request" in { Get("/ping") ~> api.routes ~> check { //Due to retries using a random port does not immediately result in failure handled shouldBe true } } it should "respond metrics request" in { Get("/metrics") ~> `Accept-Encoding`(gzip) ~> api.routes ~> check { contentType.charsetOption shouldBe Some(HttpCharsets.`UTF-8`) contentType.mediaType.params("version") shouldBe "0.0.4" response should haveContentEncoding(gzip) } } private def haveContentEncoding(encoding: HttpEncoding): Matcher[HttpResponse] = be(encoding) compose { (_: HttpResponse).header[`Content-Encoding`].map(_.encodings.head).getOrElse(HttpEncodings.identity) } private def createExporter(): PrometheusExporter = () => HttpEntity(PrometheusExporter.textV4, "foo".getBytes) }
Example 34
Source File: CorsSupport.scala From BusFloatingData with Apache License 2.0 | 5 votes |
package de.nierbeck.floating.data.server import akka.http.scaladsl.model.HttpMethods._ import akka.http.scaladsl.model.HttpResponse import akka.http.scaladsl.model.headers._ import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server.{ Directive0, Route } trait CorsSupport { lazy val allowedOriginHeader = `Access-Control-Allow-Origin`.* private def addAccessControlHeaders: Directive0 = { mapResponseHeaders { headers => allowedOriginHeader +: `Access-Control-Allow-Credentials`(true) +: `Access-Control-Allow-Headers`("Content-Type", "X-Requested-With", "Authorization", "Token") +: headers } } private def preflightRequestHandler: Route = options { complete(HttpResponse(200).withHeaders( `Access-Control-Allow-Methods`(OPTIONS, POST, PUT, GET, DELETE) )) } def corsHandler(r: Route) = addAccessControlHeaders { preflightRequestHandler ~ r } }
Example 35
Source File: ServiceApp.scala From BusFloatingData with Apache License 2.0 | 5 votes |
package de.nierbeck.floating.data.server import akka.actor.{ActorRef, ActorSystem, Props} import akka.event.Logging import akka.http.scaladsl.Http import akka.http.scaladsl.model.HttpMethods._ import akka.http.scaladsl.model.ws.UpgradeToWebSocket import akka.http.scaladsl.model.{HttpRequest, HttpResponse, Uri} import akka.stream.ActorMaterializer import de.nierbeck.floating.data.server.actors.websocket.{FLINK, RouterActor, SPARK, TiledVehiclesFromKafkaActor} import scala.concurrent.Await import scala.concurrent.duration.Duration import scala.util.{Failure, Success} object ServiceApp extends RestService { import ServiceConfig._ import system.dispatcher implicit val system = ActorSystem("service-api-http") implicit val mat = ActorMaterializer() override val logger = Logging(system, getClass.getName) override val session = CassandraConnector.connect() def main(args: Array[String]): Unit = { val router: ActorRef = system.actorOf(Props[RouterActor], "router") val sparkKafkaConsumer: ActorRef = system.actorOf(TiledVehiclesFromKafkaActor.props(router, "tiledVehicles", SPARK), "Kafka-Consumer-Spark") val flinkKafkaConsumer: ActorRef = system.actorOf(TiledVehiclesFromKafkaActor.props(router, "flinkTiledVehicles", FLINK), "Kafka-Consumer-Flink") val requestHandler: HttpRequest => HttpResponse = { case req@HttpRequest(GET, Uri.Path("/ws/vehicles"), _, _, _) => req.header[UpgradeToWebSocket] match { case Some(upgrade) => upgrade.handleMessages(Flows.graphFlowWithStats(router)) case None => HttpResponse(400, entity = "Not a valid websocket request!") } case _: HttpRequest => HttpResponse(404, entity = "Unknown resource!") } Http() .bindAndHandle(route(), serviceInterface, servicePort) .onComplete { case Success(_) => logger.info(s"Successfully bound to $serviceInterface:$servicePort") case Failure(e) => logger.error(s"Failed !!!! ${e.getMessage}") } Http() .bindAndHandleSync(requestHandler, serviceInterface, 8001) .onComplete { case Success(_) => logger.info(s"Successfully started Server to $serviceInterface:8001") case Failure(e) => logger.error(s"Failed !!!! ${e.getMessage}") } Await.ready(system.whenTerminated, Duration.Inf) CassandraConnector.close(session) } }
Example 36
Source File: Routes.scala From Learn-Scala-Programming with MIT License | 5 votes |
package ch14 import akka.actor.{ActorRef, ActorSystem} import akka.http.scaladsl.model.{HttpResponse, StatusCodes} import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server.Route import akka.http.scaladsl.server.directives.MethodDirectives.{delete, get, post} import akka.http.scaladsl.server.directives.PathDirectives.path import akka.http.scaladsl.server.directives.RouteDirectives.complete import akka.pattern.ask import akka.util.Timeout import ch14.Commands._ import ch14.Events.{ ArticleCreated, ArticleDeleted, ArticlesPurchased, ArticlesRestocked } import scala.concurrent.{ExecutionContext, Future} trait Routes extends JsonSupport { implicit def system: ActorSystem def inventory: ActorRef def config: Config implicit lazy val timeout: Timeout = config.timeout implicit lazy val ec: ExecutionContext = system.dispatcher lazy val articlesRoutes: Route = pathPrefix("articles") { concat( path(Segment) { name => concat( post { val changedInventory: Future[Option[ArticleCreated]] = (inventory ? CreateArticle(name, 0)) .mapTo[Option[ArticleCreated]] onSuccess(changedInventory) { case None => complete(StatusCodes.Conflict) case Some(event) => complete(StatusCodes.Created, event) } }, delete { val changedInventory: Future[Option[ArticleDeleted]] = (inventory ? DeleteArticle(name)).mapTo[Option[ArticleDeleted]] rejectEmptyResponse { complete(changedInventory) } }, get { complete((inventory ? GetArticle(name)).mapTo[Inventory]) } ) } ) } lazy val inventoryRoutes: Route = path("inventory") { get { complete((inventory ? GetInventory).mapTo[Inventory]) } } ~ path("purchase") { post { entity(as[PurchaseArticles]) { order => val response: Future[Option[ArticlesPurchased]] = (inventory ? order).mapTo[Option[ArticlesPurchased]] onSuccess(response) { case None => complete(StatusCodes.Conflict) case Some(event) => complete(event) } } } } ~ path("restock") { post { entity(as[RestockArticles]) { stock => val response: Future[Option[ArticlesRestocked]] = (inventory ? stock).mapTo[Option[ArticlesRestocked]] complete(response) } } } lazy val routes: Route = articlesRoutes ~ inventoryRoutes }
Example 37
Source File: AkkaBackend.scala From drunk with Apache License 2.0 | 5 votes |
package com.github.jarlakxen.drunk.backend import java.io.UnsupportedEncodingException import akka.actor.ActorSystem import akka.http.scaladsl.coding.{Deflate, Gzip, NoCoding} import akka.http.scaladsl.model.HttpResponse import akka.http.scaladsl.model.headers.HttpEncodings import akka.stream.ActorMaterializer import akka.util.ByteString import scala.concurrent.{ExecutionContext, Future} trait AkkaBackend { implicit val as: ActorSystem implicit val mat: ActorMaterializer def send(body: String): Future[(Int, String)] protected def encodingFromContentType(ct: String): Option[String] = ct.split(";").map(_.trim.toLowerCase).collectFirst { case s if s.startsWith("charset=") => s.substring(8) } protected def decodeResponse(response: HttpResponse): HttpResponse = { val decoder = response.encoding match { case HttpEncodings.gzip => Gzip case HttpEncodings.deflate => Deflate case HttpEncodings.identity => NoCoding case ce => throw new UnsupportedEncodingException(s"Unsupported encoding: $ce") } decoder.decodeMessage(response) } protected def bodyToString(hr: HttpResponse, charsetFromHeaders: String): Future[String] = { implicit val ec: ExecutionContext = as.dispatcher hr.entity.dataBytes .runFold(ByteString.empty)(_ ++ _) .map(_.decodeString(charsetFromHeaders)) } }
Example 38
Source File: PostcodeClient.scala From akka-http-test with Apache License 2.0 | 5 votes |
package com.github.dnvriend.component.webservices.postcode import akka.NotUsed import akka.actor.ActorSystem import akka.event.LoggingAdapter import akka.http.scaladsl.model.{ HttpRequest, HttpResponse } import akka.stream.Materializer import akka.stream.scaladsl.Flow import com.github.dnvriend.component.webservices.generic.HttpClient import spray.json.DefaultJsonProtocol import scala.concurrent.{ ExecutionContext, Future } import scala.util.Try import scala.util.matching.Regex case class Address( street: String, houseNumber: Int, houseNumberAddition: String, postcode: String, city: String, municipality: String, province: String, rdX: Option[Int], rdY: Option[Int], latitude: Double, longitude: Double, bagNumberDesignationId: String, bagAddressableObjectId: String, addressType: String, purposes: Option[List[String]], surfaceArea: Int, houseNumberAdditions: List[String] ) trait Marshallers extends DefaultJsonProtocol { implicit val addressJsonFormat = jsonFormat17(Address) } case class GetAddressRequest(zip: String, houseNumber: String) trait PostcodeClient { def address(postcode: String, houseNumber: Int): Future[Option[Address]] def address[T](implicit system: ActorSystem, mat: Materializer, ec: ExecutionContext): Flow[(GetAddressRequest, T), (Option[Address], T), NotUsed] } object PostcodeClient { import spray.json._ val ZipcodeWithoutSpacePattern: Regex = """([1-9][0-9]{3})([A-Za-z]{2})""".r val ZipcodeWithSpacePattern: Regex = """([1-9][0-9]{3})[\s]([A-Za-z]{2})""".r def mapToAddress(json: String)(implicit reader: JsonReader[Address]): Option[Address] = Try(json.parseJson.convertTo[Address]).toOption def responseToString(resp: HttpResponse)(implicit system: ActorSystem, mat: Materializer, ec: ExecutionContext): Future[String] = HttpClient.responseToString(resp) def getAddressRequestFlow[T]: Flow[(GetAddressRequest, T), (HttpRequest, T), NotUsed] = Flow[(GetAddressRequest, T)].map { case (request, id) => (HttpClient.mkGetRequest(s"/rest/addresses/${request.zip}/${request.houseNumber}/"), id) } def mapResponseToAddressFlow[T](implicit system: ActorSystem, mat: Materializer, ec: ExecutionContext, reader: JsonReader[Address]): Flow[(Try[HttpResponse], T), (Option[Address], T), NotUsed] = HttpClient.responseToString[T].map { case (json, id) => (mapToAddress(json), id) } def normalizeZipcode(zipcode: String): Option[String] = zipcode.toUpperCase match { case ZipcodeWithoutSpacePattern(numbers, letters) => Option(s"$numbers$letters") case ZipcodeWithSpacePattern(numbers, letters) => Option(s"$numbers$letters") case _ => None } def apply()(implicit system: ActorSystem, mat: Materializer, ec: ExecutionContext, log: LoggingAdapter) = new PostcodeClientImpl } class PostcodeClientImpl()(implicit val system: ActorSystem, val mat: Materializer, val ec: ExecutionContext, val log: LoggingAdapter) extends PostcodeClient with Marshallers { import PostcodeClient._ private val client = HttpClient("postcode") override def address(postcode: String, houseNumber: Int): Future[Option[Address]] = normalizeZipcode(postcode) match { case Some(zip) => client.get(s"/rest/addresses/$zip/$houseNumber/") .flatMap(responseToString).map(mapToAddress) case None => Future.successful(None) } override def address[T](implicit system: ActorSystem, mat: Materializer, ec: ExecutionContext): Flow[(GetAddressRequest, T), (Option[Address], T), NotUsed] = getAddressRequestFlow[T] .via(client.cachedHostConnectionFlow[T]) .via(mapResponseToAddressFlow[T]) }
Example 39
Source File: WeatherClient.scala From akka-http-test with Apache License 2.0 | 5 votes |
package com.github.dnvriend.component.webservices.weather import akka.NotUsed import akka.actor.ActorSystem import akka.event.LoggingAdapter import akka.http.scaladsl.model.{ HttpRequest, HttpResponse } import akka.stream.Materializer import akka.stream.scaladsl.Flow import com.github.dnvriend.component.webservices.generic.HttpClient import spray.json.DefaultJsonProtocol import scala.concurrent.{ ExecutionContext, Future } import scala.util.Try case class Wind(speed: Double, deg: Double) case class Main(temp: Double, temp_min: Double, temp_max: Double, pressure: Double, sea_level: Option[Double], grnd_level: Option[Double], humidity: Int) case class Cloud(all: Int) case class Weather(id: Int, main: String, description: String, icon: String) case class Sys(message: Double, country: String, sunrise: Long, sunset: Long) case class Coord(lon: Double, lat: Double) case class WeatherResult(coord: Coord, sys: Sys, weather: List[Weather], base: String, main: Main, wind: Wind, clouds: Cloud, dt: Long, id: Int, name: String, cod: Int) trait Marshallers extends DefaultJsonProtocol { implicit val windJsonFormat = jsonFormat2(Wind) implicit val mainJsonFormat = jsonFormat7(Main) implicit val cloudJsonFormat = jsonFormat1(Cloud) implicit val weatherJsonFormat = jsonFormat4(Weather) implicit val sysJsonFormat = jsonFormat4(Sys) implicit val coordJsonFormat = jsonFormat2(Coord) implicit val weatherResultJsonFormat = jsonFormat11(WeatherResult) } case class GetWeatherRequest(zip: String, country: String) trait OpenWeatherApi { def getWeather(zip: String, country: String): Future[Option[WeatherResult]] def getWeather[T](implicit system: ActorSystem, mat: Materializer, ec: ExecutionContext): Flow[(GetWeatherRequest, T), (Option[WeatherResult], T), NotUsed] } object OpenWeatherApi { import spray.json._ def apply()(implicit system: ActorSystem, mat: Materializer, ec: ExecutionContext, log: LoggingAdapter) = new OpenWeatherApiImpl def mapResponseToWeatherResult(json: String)(implicit reader: JsonReader[WeatherResult]): Option[WeatherResult] = Try(json.parseJson.convertTo[WeatherResult]).toOption def responseToString(resp: HttpResponse)(implicit system: ActorSystem, mat: Materializer, ec: ExecutionContext): Future[String] = HttpClient.responseToString(resp) def getWeatherRequestFlow[T]: Flow[(GetWeatherRequest, T), (HttpRequest, T), NotUsed] = Flow[(GetWeatherRequest, T)].map { case (request, id) => (HttpClient.mkGetRequest(s"/data/2.5/weather?zip=${request.zip},${request.country}"), id) } def mapResponseToWeatherResultFlow[T](implicit system: ActorSystem, mat: Materializer, ec: ExecutionContext, reader: JsonReader[WeatherResult]): Flow[(Try[HttpResponse], T), (Option[WeatherResult], T), NotUsed] = HttpClient.responseToString[T].map { case (json, id) => (mapResponseToWeatherResult(json), id) } } class OpenWeatherApiImpl()(implicit val system: ActorSystem, val ec: ExecutionContext, val mat: Materializer, val log: LoggingAdapter) extends OpenWeatherApi with Marshallers { import OpenWeatherApi._ private val client = HttpClient("weather") override def getWeather(zip: String, country: String): Future[Option[WeatherResult]] = client.get(s"/data/2.5/weather?zip=$zip,$country"). flatMap(responseToString) .map(mapResponseToWeatherResult) override def getWeather[T](implicit system: ActorSystem, mat: Materializer, ec: ExecutionContext): Flow[(GetWeatherRequest, T), (Option[WeatherResult], T), NotUsed] = getWeatherRequestFlow[T] .via(client.cachedHostConnectionFlow[T]) .via(mapResponseToWeatherResultFlow[T]) }
Example 40
Source File: ServerTestBase.scala From endpoints4s with MIT License | 5 votes |
package endpoints4s.algebra.server import java.nio.charset.StandardCharsets import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model.headers.`Content-Type` import akka.http.scaladsl.model.{HttpRequest, HttpResponse} import akka.util.ByteString import endpoints4s.algebra import org.scalatest.concurrent.ScalaFutures import org.scalatest.{BeforeAndAfter, BeforeAndAfterAll} import scala.concurrent.duration._ import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpec import scala.concurrent.{ExecutionContext, Future} trait ServerTestBase[T <: algebra.Endpoints] extends AnyWordSpec with Matchers with ScalaFutures with BeforeAndAfterAll with BeforeAndAfter { override implicit def patienceConfig: PatienceConfig = PatienceConfig(10.seconds, 10.millisecond) val serverApi: T case class Malformed(errors: Seq[String]) extends DecodedUrl[Nothing] }
Example 41
Source File: EndpointsSettings.scala From endpoints4s with MIT License | 5 votes |
package endpoints4s.akkahttp.client import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model.{HttpEntity, HttpRequest, HttpResponse, Uri} import akka.stream.Materializer import akka.stream.scaladsl.{Flow, Sink, Source} import scala.concurrent.Future import scala.concurrent.duration._ import scala.util.Try final case class EndpointsSettings( requestExecutor: AkkaHttpRequestExecutor, baseUri: Uri = Uri("/"), toStrictTimeout: FiniteDuration = 2.seconds, stringContentExtractor: HttpEntity.Strict => String = _.data.utf8String ) trait AkkaHttpRequestExecutor { def apply(request: HttpRequest): Future[HttpResponse] } object AkkaHttpRequestExecutor { def cachedHostConnectionPool(host: String, port: Int)(implicit system: ActorSystem, materializer: Materializer ): AkkaHttpRequestExecutor = default(Http().cachedHostConnectionPool[Int](host, port)) def default( poolClientFlow: Flow[ (HttpRequest, Int), (Try[HttpResponse], Int), Http.HostConnectionPool ] )(implicit materializer: Materializer): AkkaHttpRequestExecutor = new AkkaHttpRequestExecutor { override def apply(request: HttpRequest): Future[HttpResponse] = Source .single(request -> 1) .via(poolClientFlow) .map(_._1.get) .runWith(Sink.head) } }
Example 42
Source File: MarshallerTestSupport.scala From wix-http-testkit with MIT License | 5 votes |
package com.wix.e2e.http.drivers import akka.http.scaladsl.model.{HttpRequest, HttpResponse} import com.wix.e2e.http.drivers.MarshallingTestObjects.SomeCaseClass import com.wix.e2e.http.exceptions.MissingMarshallerException import com.wix.test.random.{randomInt, randomStr} import org.specs2.execute.AsResult import org.specs2.matcher.Matcher import org.specs2.matcher.ResultMatchers.beError trait MarshallerTestSupport { val someObject = SomeCaseClass(randomStr, randomInt) val content = randomStr def aResponseWith(body: String) = HttpResponse(entity = body) def aRequestWith(body: String) = HttpRequest(entity = body) val request = HttpRequest() } object MarshallingTestObjects { case class SomeCaseClass(s: String, i: Int) } object MarshallerMatchers { def beMissingMarshallerMatcherError[T : AsResult]: Matcher[T] = beError[T](new MissingMarshallerException().getMessage) }
Example 43
Source File: MarshallerTestSupport.scala From wix-http-testkit with MIT License | 5 votes |
package com.wix.e2e.http.drivers import akka.http.scaladsl.model.{HttpRequest, HttpResponse} import com.wix.e2e.http.api.Marshaller import com.wix.e2e.http.drivers.MarshallingTestObjects.SomeCaseClass import com.wix.test.random.{randomInt, randomStr} import scala.collection.concurrent.TrieMap trait MarshallerTestSupport { val someObject = SomeCaseClass(randomStr, randomInt) val content = randomStr def givenMarshallerThatUnmarshalWith(unmarshal: SomeCaseClass, forContent: String): Unit = MarshallingTestObjects.unmarshallResult.put(forContent, unmarshal) def givenMarshallerThatMarshal(content: String, to: SomeCaseClass): Unit = MarshallingTestObjects.marshallResult.put(to, content) def aResponseWith(body: String) = HttpResponse(entity = body) def aRequestWith(body: String) = HttpRequest(entity = body) val request = HttpRequest() } object MarshallingTestObjects { case class SomeCaseClass(s: String, i: Int) val marshallResult = TrieMap.empty[SomeCaseClass, String] val unmarshallResult = TrieMap.empty[String, SomeCaseClass] class MarshallerForTest extends Marshaller { def unmarshall[T: Manifest](jsonStr: String) = MarshallingTestObjects.unmarshallResult .getOrElse(jsonStr, throw new UnsupportedOperationException) .asInstanceOf[T] def marshall[T](t: T) = MarshallingTestObjects.marshallResult .getOrElse(t.asInstanceOf[SomeCaseClass], throw new UnsupportedOperationException) } }
Example 44
Source File: AkkaHttpMockWebServer.scala From wix-http-testkit with MIT License | 5 votes |
package com.wix.e2e.http.server.internals import akka.http.scaladsl.Http import akka.http.scaladsl.Http.ServerBinding import akka.http.scaladsl.model.headers.{ProductVersion, Server} import akka.http.scaladsl.model.{HttpRequest, HttpResponse} import akka.http.scaladsl.settings.ServerSettings import com.wix.e2e.http.api.BaseWebServer import com.wix.e2e.http.info.HttpTestkitVersion import com.wix.e2e.http.utils._ import com.wix.e2e.http.{BaseUri, RequestHandler, WixHttpTestkitResources} import scala.concurrent.Future import scala.concurrent.duration._ abstract class AkkaHttpMockWebServer(specificPort: Option[Int], val initialHandlers: Seq[RequestHandler]) extends BaseWebServer with AdjustableServerBehaviorSupport { import WixHttpTestkitResources.{executionContext, materializer, system} protected def serverBehavior: RequestHandler def start() = this.synchronized { val s = waitFor( Http().bindAndHandleAsync(handler = TransformToStrictAndHandle, interface = "localhost", settings = customSettings, port = specificPort.getOrElse( AllocateDynamicPort )) ) serverBinding = Option(s) println(s"Web server started on port: ${baseUri.port}.") this } def stop() = this.synchronized { serverBinding.foreach{ s => waitFor( s.unbind() ) } serverBinding = None this } def baseUri = specificPort.map( p => BaseUri("localhost", port = p) ) .orElse( serverBinding.map( s => BaseUri(port = s.localAddress.getPort) )) .getOrElse( throw new IllegalStateException("Server port and baseUri will have value after server is started") ) private var serverBinding: Option[ServerBinding] = None private val AllocateDynamicPort = 0 private val TransformToStrictAndHandle: HttpRequest => Future[HttpResponse] = _.toStrict(1.minutes).map( serverBehavior ) private def customSettings = ServerSettings(system).withTransparentHeadRequests(false) .withServerHeader( Some(Server(ProductVersion("server-http-testkit", HttpTestkitVersion))) ) }
Example 45
Source File: StubAkkaHttpMockWebServer.scala From wix-http-testkit with MIT License | 5 votes |
package com.wix.e2e.http.server.internals import akka.http.scaladsl.model.{HttpResponse, StatusCodes} import com.wix.e2e.http._ import com.wix.e2e.http.api.{AdjustableServerBehavior, MockWebServer, StubWebServer} import scala.collection.mutable.ListBuffer class StubAkkaHttpMockWebServer(initialHandlers: Seq[RequestHandler], specificPort: Option[Int]) extends AkkaHttpMockWebServer(specificPort, initialHandlers) with StubWebServer { def recordedRequests: Seq[HttpRequest] = this.synchronized { requests.toSeq } def clearRecordedRequests() = this.synchronized { requests.clear() } private val requests = ListBuffer.empty[HttpRequest] private val SuccessfulHandler: RequestHandler = { case _ => HttpResponse(status = StatusCodes.OK) } private def StubServerHandlers = (currentHandlers :+ SuccessfulHandler).reduce(_ orElse _) private val RequestRecorderHandler: RequestHandler = { case r => this.synchronized { requests.append(r) } StubServerHandlers.apply(r) } protected val serverBehavior = RequestRecorderHandler } class MockAkkaHttpWebServer(initialHandlers: Seq[RequestHandler], specificPort: Option[Int]) extends AkkaHttpMockWebServer(specificPort, initialHandlers) with MockWebServer { private val NotFoundHandler: RequestHandler = { case _ => HttpResponse(status = StatusCodes.NotFound) } private def MockServerHandlers = (currentHandlers :+ NotFoundHandler).reduce(_ orElse _) private val AdjustableHandler: RequestHandler = { case r => MockServerHandlers.apply(r) } protected val serverBehavior = AdjustableHandler } trait AdjustableServerBehaviorSupport extends AdjustableServerBehavior { private val localHandlers: ListBuffer[RequestHandler] = ListBuffer(initialHandlers:_*) def initialHandlers: Seq[RequestHandler] def currentHandlers: Seq[RequestHandler] = this.synchronized { localHandlers.toSeq } def appendAll(handlers: RequestHandler*) = this.synchronized { localHandlers.appendAll(handlers) } def replaceWith(handlers: RequestHandler*) = this.synchronized { localHandlers.clear() appendAll(handlers:_*) } }
Example 46
Source File: HomeActor.scala From sns with Apache License 2.0 | 5 votes |
package me.snov.sns.actor import akka.actor.{Actor, Props} import akka.http.scaladsl.model.{HttpEntity, HttpResponse} object HomeActor { def props = Props[HomeActor] case class CmdHello() } class HomeActor extends Actor { import me.snov.sns.actor.HomeActor._ def hello = HttpResponse(entity = HttpEntity("Hello, Akka")) override def receive = { case CmdHello => sender ! hello case _ => sender ! HttpResponse(500, entity = "Invalid message") } }
Example 47
Source File: TopicApi.scala From sns with Apache License 2.0 | 5 votes |
package me.snov.sns.api import akka.actor.ActorRef import akka.actor.Status.Success import akka.http.scaladsl.model.HttpResponse import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server.Route import akka.pattern.ask import akka.util.Timeout import me.snov.sns.actor.SubscribeActor.{CmdListTopics, CmdDeleteTopic, CmdCreateTopic} import me.snov.sns.model.Topic import me.snov.sns.response.TopicResponse import scala.concurrent.ExecutionContext object TopicApi { private val namePattern = """([\w+_-]{1,256})""".r private val arnPattern = """([\w+_:-]{1,512})""".r def route(actor: ActorRef)(implicit timeout: Timeout, ec: ExecutionContext): Route = { pathSingleSlash { formField('Action ! "CreateTopic") { formField('Name) { case namePattern(name) => complete { (actor ? CmdCreateTopic(name)).mapTo[Topic].map { TopicResponse.create } } case _ => complete(HttpResponse(400, entity = "InvalidParameter: invalid topic name")) } ~ complete(HttpResponse(400, entity = "Topic name is missing")) } ~ formField('Action ! "DeleteTopic") { formField('TopicArn) { case arnPattern(arn) => complete { (actor ? CmdDeleteTopic(arn)).map { case Success => TopicResponse.delete case _ => HttpResponse(404, entity = "NotFound") } } case _ => complete(HttpResponse(400, entity = "Invalid topic ARN")) } ~ complete(HttpResponse(404, entity = "NotFound")) } ~ formField('Action ! "ListTopics") { complete { (actor ? CmdListTopics).mapTo[Iterable[Topic]].map { TopicResponse.list } } } } } }
Example 48
Source File: PublishApi.scala From sns with Apache License 2.0 | 5 votes |
package me.snov.sns.api import akka.actor.ActorRef import akka.event.Logging import akka.http.scaladsl.model.HttpResponse import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server.Route import akka.pattern.ask import akka.util.Timeout import me.snov.sns.actor.PublishActor.CmdPublish import me.snov.sns.model.{Message, MessageAttribute, TopicNotFoundException} import me.snov.sns.response.PublishResponse import spray.json.DefaultJsonProtocol._ import spray.json._ import scala.concurrent.{ExecutionContext, Future} case class InvalidTopicArnException(msg: String) extends Exception(msg) object PublishApi { private val arnPattern = """([\w+_:-]{1,512})""".r def route(actorRef: ActorRef)(implicit timeout: Timeout, ec: ExecutionContext): Route = { pathSingleSlash { formField('Action ! "Publish") { formFieldSeq { fields => val messageAttributes: Map[String, MessageAttribute] = MessageAttribute.parse(fields) formFields('TopicArn.?, 'TargetArn.?, 'MessageStructure.?, 'Message) { (topicArnMaybe, targetArnMaybe, messageStructure, message) => try { topicArn(topicArnMaybe, targetArnMaybe) match { case arnPattern(topic) => complete { val bodies = messageStructure match { case Some("json") => message.parseJson.asJsObject.convertTo[Map[String, String]] case Some(_) => throw new RuntimeException("Invalid MessageStructure value"); case None => Map("default" -> message) } (actorRef ? CmdPublish(topic, bodies, messageAttributes)).collect { case m: Message => PublishResponse.publish(m) }.recover { case t: TopicNotFoundException => PublishResponse.topicNotFound(t.getMessage) case t: Throwable => HttpResponse(500, entity = t.getMessage) } } case _ => complete(HttpResponse(400, entity = "Invalid topic ARN")) } } catch { case e: InvalidTopicArnException => complete(HttpResponse(400, entity = e.getMessage)) case e: RuntimeException => complete(HttpResponse(400, entity = e.getMessage)) } } } ~ complete(HttpResponse(400, entity = "TopicArn is required")) } } } private def topicArn(topicArnMaybe: Option[String], targetArnMaybe: Option[String]): String = { topicArnMaybe.getOrElse(targetArnMaybe.getOrElse(throw InvalidTopicArnException("Neither TopicArn nor TargetArn provided"))) } }
Example 49
Source File: SubscribeApi.scala From sns with Apache License 2.0 | 5 votes |
package me.snov.sns.api import akka.actor.ActorRef import akka.actor.Status.{Success, Failure} import akka.http.scaladsl.model.HttpResponse import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server.Route import akka.pattern.ask import akka.util.Timeout import me.snov.sns.actor.SubscribeActor.{CmdListSubscriptions, CmdListSubscriptionsByTopic, CmdSubscribe, CmdUnsubscribe,CmdSetSubscriptionAttributes,CmdGetSubscriptionAttributes} import me.snov.sns.model.Subscription import me.snov.sns.response.SubscribeResponse import scala.concurrent.ExecutionContext object SubscribeApi { private val arnPattern = """([\w+_:-]{1,512})""".r def route(actorRef: ActorRef)(implicit timeout: Timeout, ec: ExecutionContext): Route = { pathSingleSlash { formField('Action ! "Subscribe") { formFields('Endpoint, 'Protocol, 'TopicArn) { (endpoint, protocol, topicArn) => complete { (actorRef ? CmdSubscribe(topicArn, protocol, endpoint)).mapTo[Subscription] map { SubscribeResponse.subscribe } } } ~ complete(HttpResponse(400, entity = "Endpoint, Protocol, TopicArn are required")) } ~ formField('Action ! "ListSubscriptionsByTopic") { formField('TopicArn) { case arnPattern(topicArn) => complete { (actorRef ? CmdListSubscriptionsByTopic(topicArn)).mapTo[Iterable[Subscription]] map { SubscribeResponse.listByTopic } } case _ => complete(HttpResponse(400, entity = "Invalid topic ARN")) } ~ complete(HttpResponse(400, entity = "TopicArn is missing")) } ~ formField('Action ! "ListSubscriptions") { complete { (actorRef ? CmdListSubscriptions()).mapTo[Iterable[Subscription]] map { SubscribeResponse.list } } } ~ formField('Action ! "Unsubscribe") { formField('SubscriptionArn) { (arn) => complete { (actorRef ? CmdUnsubscribe(arn)).map { case Success => SubscribeResponse.unsubscribe case _ => HttpResponse(404, entity = "NotFound") } } } ~ complete(HttpResponse(400, entity = "SubscriptionArn is missing")) } ~ formField('Action ! "SetSubscriptionAttributes") { formField('SubscriptionArn, 'AttributeName, 'AttributeValue) { (arn, name, value) => complete { (actorRef ? CmdSetSubscriptionAttributes(arn, name, value)).map { case Success => SubscribeResponse.setSubscriptionAttributes case Failure(ex) => HttpResponse(404, entity = "NotFound") } } } ~ complete(HttpResponse(400, entity = "SubscriptionArn is missing")) } ~ formField('Action ! "GetSubscriptionAttributes") { formField('SubscriptionArn) { (arn) => complete { (actorRef ? CmdGetSubscriptionAttributes(arn)).mapTo[Option[Map[String,String]]] map { attributes => attributes .map(SubscribeResponse.getSubscriptionAttributes) .getOrElse { HttpResponse(404, entity = "Not Found") } } } } ~ complete(HttpResponse(400, entity = "SubscriptionArn is missing")) } } } }
Example 50
Source File: TopicResponse.scala From sns with Apache License 2.0 | 5 votes |
package me.snov.sns.response import java.util.UUID import akka.http.scaladsl.model.StatusCodes.OK import akka.http.scaladsl.model.HttpResponse import me.snov.sns.model.Topic object TopicResponse extends XmlHttpResponse { def delete = { response( OK, <DeleteTopicResponse xmlns="http://sns.amazonaws.com/doc/2010-03-31/"> <ResponseMetadata> <RequestId> {UUID.randomUUID} </RequestId> </ResponseMetadata> </DeleteTopicResponse> ) } def create(topic: Topic): HttpResponse = { response( OK, <CreateTopicResponse xmlns="http://sns.amazonaws.com/doc/2010-03-31/"> <CreateTopicResult> <TopicArn> {topic.arn} </TopicArn> </CreateTopicResult> <ResponseMetadata> <RequestId> {UUID.randomUUID} </RequestId> </ResponseMetadata> </CreateTopicResponse> ) } def list(topics: Iterable[Topic]): HttpResponse = { response( OK, <ListTopicsResponse xmlns="http://sns.amazonaws.com/doc/2010-03-31/"> <ListTopicsResult> <Topics> {topics.map(topic => <member> <TopicArn> {topic.arn} </TopicArn> </member> )} </Topics> </ListTopicsResult> <ResponseMetadata> <RequestId> {UUID.randomUUID} </RequestId> </ResponseMetadata> </ListTopicsResponse> ) } }
Example 51
Source File: PublishResponse.scala From sns with Apache License 2.0 | 5 votes |
package me.snov.sns.response import java.util.UUID import akka.http.scaladsl.model.HttpResponse import akka.http.scaladsl.model.StatusCodes._ import me.snov.sns.model.Message object PublishResponse extends XmlHttpResponse { def publish(message: Message): HttpResponse = { response( OK, <PublishResponse xmlns="http://sns.amazonaws.com/doc/2010-03-31/"> <PublishResult> <MessageId>{message.uuid}</MessageId> </PublishResult> <ResponseMetadata> <RequestId>{UUID.randomUUID}</RequestId> </ResponseMetadata> </PublishResponse> ) } def topicNotFound(message: String): HttpResponse = { response( NotFound, <ErrorResponse xmlns="http://sns.amazonaws.com/doc/2010-03-31/"> <Error> <Code>NotFound</Code> <Message>{message}</Message> </Error> <RequestId>{UUID.randomUUID}</RequestId> </ErrorResponse> ) } }
Example 52
Source File: TopicSpec.scala From sns with Apache License 2.0 | 5 votes |
package me.snov.sns.api import java.util.concurrent.TimeUnit import akka.actor.ActorRef import akka.http.scaladsl.model.{FormData, HttpResponse, StatusCodes} import akka.http.scaladsl.testkit.ScalatestRouteTest import akka.testkit.{TestActor, TestProbe} import akka.util.Timeout import me.snov.sns.actor.SubscribeActor.{CmdDeleteTopic, CmdCreateTopic} import me.snov.sns.model.Topic import org.scalatest.{Matchers, WordSpec} class TopicSpec extends WordSpec with Matchers with ScalatestRouteTest { implicit val timeout = new Timeout(100, TimeUnit.MILLISECONDS) val probe = TestProbe() val route = TopicApi.route(probe.ref) "Requires topic name" in { Post("/", FormData(Map("Action" -> "CreateTopic"))) ~> route ~> check { status shouldBe StatusCodes.BadRequest } } "Validates topic name" in { Post("/", FormData(Map("Action" -> "CreateTopic", "Name" -> "f$$"))) ~> route ~> check { status shouldBe StatusCodes.BadRequest } } "TopicDelete validates topic name" in { Post("/", FormData(Map("Action" -> "DeleteTopic", "TopicArn" -> "f$$"))) ~> route ~> check { status shouldBe StatusCodes.BadRequest } } "Sends create command to actor" in { probe.setAutoPilot(new TestActor.AutoPilot { def run(sender: ActorRef, msg: Any) = { sender ! new Topic("foo", "bar") this } }) Post("/", FormData(Map("Action" -> "CreateTopic", "Name" -> "foo"))) ~> route ~> check { probe.expectMsg(CmdCreateTopic("foo")) } } "Sends delete command to actor" in { probe.setAutoPilot(new TestActor.AutoPilot { def run(sender: ActorRef, msg: Any) = { sender ! new Topic("foo", "bar") this } }) Post("/", FormData(Map("Action" -> "DeleteTopic", "TopicArn" -> "arn-foo"))) ~> route ~> check { probe.expectMsg(CmdDeleteTopic("arn-foo")) } } }
Example 53
Source File: MockOAuth2Server.scala From incubator-retired-gearpump with Apache License 2.0 | 5 votes |
package org.apache.gearpump.services.security.oauth2 import scala.concurrent.{Await, Future} import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.Http.ServerBinding import akka.http.scaladsl.model.{HttpRequest, HttpResponse} import akka.stream.ActorMaterializer import akka.stream.scaladsl.Sink import org.apache.gearpump.util.Util // NOTE: This cannot be removed!! import org.apache.gearpump.services.util.UpickleUtil._ class MockOAuth2Server( actorSystem: ActorSystem, var requestHandler: HttpRequest => HttpResponse) { implicit val system: ActorSystem = actorSystem implicit val materializer = ActorMaterializer() implicit val ec = system.dispatcher private var _port: Int = 0 private var bindingFuture: Future[ServerBinding] = null def port: Int = _port def start(): Unit = { _port = Util.findFreePort().get val serverSource = Http().bind(interface = "127.0.0.1", port = _port) bindingFuture = { serverSource.to(Sink.foreach { connection => connection handleWithSyncHandler requestHandler }).run() } } def stop(): Unit = { import scala.concurrent.duration._ Await.result(bindingFuture.map(_.unbind()), 120.seconds) } }
Example 54
Source File: RoutingDSL.scala From introduction-to-akkahttp with Apache License 2.0 | 5 votes |
package com.shashank.akkahttp.basic.routing import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model.{HttpResponse, StatusCodes} import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server._ import akka.stream.{ActorMaterializer, Materializer} object RoutingDSL { def main(args: Array[String]) { implicit val sys = ActorSystem("IntroductionToAkkaHttp") implicit val mat:Materializer = ActorMaterializer() val route = path("welcome"){ get{ complete { "welcome to rest service" } } } ~ path("demo"){ get{ complete { "welcome to demonstration" } } } Http().bindAndHandle(route, "localhost", 8090) } }
Example 55
Source File: Failure.scala From introduction-to-akkahttp with Apache License 2.0 | 5 votes |
package com.shashank.akkahttp.basic.routing import akka.actor.ActorSystem import akka.http.scaladsl.model.StatusCodes import akka.http.scaladsl.Http import akka.http.scaladsl.model.HttpResponse import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server.ExceptionHandler import akka.stream.{ActorMaterializer, Materializer} object Failure { def main(args: Array[String]) { implicit val sys = ActorSystem("IntroductionToAkkaHttp") implicit val mat:Materializer = ActorMaterializer() implicit def myExceptionHandler = ExceptionHandler { case _: ArithmeticException => complete(HttpResponse(StatusCodes.BadRequest, entity = "Bad numbers, bad result!!!")) case e: Throwable => { println(e.getMessage) println(e.getStackTraceString) complete(HttpResponse(StatusCodes.BadRequest, entity = e.getMessage)) } } val route = path("welcome"){ get{ complete { "welcome to rest service" } } } ~ path("demo"){ get { complete { 100/0 "welcome to demonstration" } } } Http().bindAndHandle(route, "localhost", 8090) } }
Example 56
Source File: UnMarshalling.scala From introduction-to-akkahttp with Apache License 2.0 | 5 votes |
package com.shashank.akkahttp.basic.routing import akka.actor.ActorSystem import akka.http.scaladsl.marshalling.Marshal import akka.http.scaladsl.model.{HttpMethods, HttpRequest, HttpResponse, MessageEntity} import akka.http.scaladsl.unmarshalling.Unmarshal import akka.stream.{ActorMaterializer, Materializer} import akka.util.ByteString import scala.concurrent.Await import scala.concurrent.duration._ import scala.concurrent.ExecutionContext.Implicits.global import spray.json._ object UnMarshalling { def main(args: Array[String]) { implicit val sys = ActorSystem("IntroductionToAkkaHttp") implicit val mat:Materializer = ActorMaterializer() //type FromStringUnmarshaller[T] = Unmarshaller[String, T] val intFuture = Unmarshal("42").to[Int] val int = Await.result(intFuture, 1.second) println("int unmarshalling "+int) //type FromStringUnmarshaller[T] = Unmarshaller[String, T] val boolFuture = Unmarshal("off").to[Boolean] val bool = Await.result(boolFuture, 1.second) println("off unmarshalling "+bool) //type ToEntityMarshaller[T] = Marshaller[T, MessageEntity] val string = "Yeah" val entityFuture = Marshal(string).to[MessageEntity] val entity = Await.result(entityFuture, 1.second) // don't block in non-test code! println(entity) //type ToResponseMarshaller[T] = Marshaller[T, HttpResponse] val errorMsg = "Not found, pal!" val responseFuture = Marshal(404 -> errorMsg).to[HttpResponse] val response = Await.result(responseFuture, 1.second) println(response) //type FromEntityUnmarshaller[T] = Unmarshaller[HttpEntity, T] val jsonByteString = ByteString("""{"name":"Hello"}""") val httpRequest = HttpRequest(HttpMethods.POST, entity = jsonByteString) val jsonDataUnmarshalledFuture = Unmarshal(httpRequest).to[String] val jsonDataUnmarshalled = Await.result(jsonDataUnmarshalledFuture, 1.second) println(jsonDataUnmarshalled) sys.terminate() } }
Example 57
Source File: Rejection.scala From introduction-to-akkahttp with Apache License 2.0 | 5 votes |
package com.shashank.akkahttp.basic.routing import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model.{HttpResponse, StatusCodes} import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server._ import akka.stream.{ActorMaterializer, Materializer} object Rejection { def main(args: Array[String]) { implicit val sys = ActorSystem("IntroductionToAkkaHttp") implicit val mat:Materializer = ActorMaterializer() implicit def myRejectionHandler = RejectionHandler.newBuilder().handle{ case MissingCookieRejection(cookieName) => complete(HttpResponse(StatusCodes.BadRequest, entity = "No cookies, no service!!!")) }.handleNotFound { complete((StatusCodes.NotFound, "Not here!")) }.result() val route = path("welcome"){ get{ complete { "welcome to rest service" } } } ~ path("demo"){ get{ complete { "welcome to demonstration" } } } ~ path("wrong"){ reject{ ValidationRejection("Invalid path", None) } } Http().bindAndHandle(route, "localhost", 8090) } }
Example 58
Source File: StreamingUpload.scala From ws_to_kafka with MIT License | 5 votes |
package com.pkinsky import akka.http.scaladsl.Http import akka.http.scaladsl.model.{HttpResponse, HttpRequest} import akka.http.scaladsl.model.ws.{TextMessage, Message} import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server.PathMatchers.PathEnd import akka.stream._ import akka.stream.scaladsl._ import com.softwaremill.react.kafka.ReactiveKafka import play.api.libs.json.Json import scala.concurrent.{Future, ExecutionContext} import scala.concurrent.duration._ import scala.util.Success import scala.language.postfixOps object StreamingUpload extends App with AppContext { val kafkaPublisherGraph: RunnableGraph[SourceQueue[Event]] = Source.queue[Event](1024, OverflowStrategy.backpressure).to(kafka.publish[Event](eventTopic)) val sourceQueue: SourceQueue[Event] = kafkaPublisherGraph.run val queueWriter: Sink[Event, Unit] = Flow[Event].mapAsync(1){ elem => sourceQueue.offer(elem) .andThen{ case Success(false) => println(s"failed to publish $elem to topic $eventTopic") case Success(true) => println(s"published $elem to topic $eventTopic") } }.to(Sink.ignore) val parseMessages: Flow[Message, Event, Unit] = Flow[Message] .collect{ case TextMessage.Strict(t) => val js = Json.parse(t) Json.fromJson[Event](js).get } val wsHandlerFlow: Flow[Message, Message, Unit] = Flow.fromSinkAndSource( sink = parseMessages.to(queueWriter), source = Source.maybe ) val routes: Flow[HttpRequest, HttpResponse, Unit] = get { path(PathEnd) { getFromResource("test.html") } ~ path("ws") { println("ws connection accepted") handleWebsocketMessages(wsHandlerFlow) } } Http().bindAndHandle(routes, "localhost", port).andThen{ case util.Failure(t) => println(s"error binding to localhost: $t")} println(s"listening on port $port, press ENTER to stop") awaitTermination() } object KafkaListener extends App with AppContext { val graph = kafka.consume[Event](eventTopic, "kafka_listener").toMat(Sink.foreach(println))(Keep.right) graph.run.onComplete(println) println(s"listening to Kafka topic $eventTopic, press ENTER to stop") awaitTermination() }
Example 59
Source File: AkkaJsonHandler.scala From chronicler with Apache License 2.0 | 5 votes |
package com.github.fsanaulla.chronicler.akka.shared.handlers import akka.http.scaladsl.model.HttpResponse import akka.stream.ActorMaterializer import com.github.fsanaulla.chronicler.akka.shared.implicits.futureFunctor import com.github.fsanaulla.chronicler.core.alias.ErrorOr import com.github.fsanaulla.chronicler.core.components.JsonHandler import org.typelevel.jawn.ast.JValue import scala.concurrent.{ExecutionContext, Future} final class AkkaJsonHandler( unm: AkkaBodyUnmarshaller )(implicit ex: ExecutionContext, mat: ActorMaterializer) extends JsonHandler[Future, HttpResponse] { override def responseBody(response: HttpResponse): Future[ErrorOr[JValue]] = unm(response.entity) override def responseHeader(response: HttpResponse): Seq[(String, String)] = response.headers.map(hd => hd.name() -> hd.value()) override def responseCode(response: HttpResponse): Int = response.status.intValue() }
Example 60
Source File: AkkaResponseHandler.scala From chronicler with Apache License 2.0 | 5 votes |
package com.github.fsanaulla.chronicler.akka.shared.handlers import akka.http.scaladsl.model.HttpResponse import akka.stream.scaladsl.{Framing, Source} import akka.util.ByteString import com.github.fsanaulla.chronicler.akka.shared.implicits._ import com.github.fsanaulla.chronicler.core.alias.ErrorOr import com.github.fsanaulla.chronicler.core.components.{JsonHandler, ResponseHandler} import com.github.fsanaulla.chronicler.core.either import com.github.fsanaulla.chronicler.core.either.EitherOps import com.github.fsanaulla.chronicler.core.jawn.RichJParser import com.github.fsanaulla.chronicler.core.model.{InfluxReader, ParsingException} import org.typelevel.jawn.ast.{JArray, JParser} import scala.concurrent.{ExecutionContext, Future} import scala.reflect.ClassTag class AkkaResponseHandler( jsonHandler: JsonHandler[Future, HttpResponse] )(implicit ex: ExecutionContext) extends ResponseHandler[Future, HttpResponse](jsonHandler) { final def queryChunkedResultJson(response: HttpResponse): Source[ErrorOr[Array[JArray]], Any] = { response.entity.dataBytes .via(Framing.delimiter(ByteString("\n"), Int.MaxValue)) .map(_.utf8String) .map(JParser.parseFromStringEither) .map( _.flatMapRight( jv => jsonHandler .queryResult(jv) .toRight[Throwable](new ParsingException("Can't extract query result from response")) ) ) } final def queryChunkedResult[T: ClassTag]( response: HttpResponse )(implicit rd: InfluxReader[T] ): Source[ErrorOr[Array[T]], Any] = { queryChunkedResultJson(response) .map(_.flatMapRight { arr => either.array(arr.map(rd.read)) }) } }
Example 61
Source File: AkkaManagementClient.scala From chronicler with Apache License 2.0 | 5 votes |
package com.github.fsanaulla.chronicler.akka.management import _root_.akka.actor.ActorSystem import _root_.akka.http.scaladsl.HttpsConnectionContext import akka.http.scaladsl.model.{HttpResponse, RequestEntity, Uri} import akka.stream.ActorMaterializer import com.github.fsanaulla.chronicler.akka.shared.InfluxAkkaClient import com.github.fsanaulla.chronicler.akka.shared.handlers._ import com.github.fsanaulla.chronicler.core.ManagementClient import com.github.fsanaulla.chronicler.core.alias.ErrorOr import com.github.fsanaulla.chronicler.core.model._ import scala.concurrent.{ExecutionContext, Future} final class AkkaManagementClient( host: String, port: Int, credentials: Option[InfluxCredentials], httpsContext: Option[HttpsConnectionContext], terminateActorSystem: Boolean )(implicit val ex: ExecutionContext, val system: ActorSystem, val F: Functor[Future], val FK: FunctionK[Future, Future]) extends InfluxAkkaClient(terminateActorSystem, httpsContext) with ManagementClient[Future, Future, HttpResponse, Uri, RequestEntity] { implicit val mat: ActorMaterializer = ActorMaterializer() implicit val qb: AkkaQueryBuilder = new AkkaQueryBuilder(schema, host, port, credentials) implicit val jh: AkkaJsonHandler = new AkkaJsonHandler(new AkkaBodyUnmarshaller(false)) implicit val re: AkkaRequestExecutor = new AkkaRequestExecutor(ctx) implicit val rh: AkkaResponseHandler = new AkkaResponseHandler(jh) override def ping: Future[ErrorOr[InfluxDBInfo]] = { re.get(qb.buildQuery("/ping"), compressed = false) .flatMap(rh.pingResult) } }
Example 62
Source File: AkkaMeasurementApi.scala From chronicler with Apache License 2.0 | 5 votes |
package com.github.fsanaulla.chronicler.akka.io import akka.http.scaladsl.model.{HttpResponse, RequestEntity, Uri} import akka.stream.scaladsl.Source import com.github.fsanaulla.chronicler.akka.shared.handlers.{ AkkaQueryBuilder, AkkaRequestExecutor, AkkaResponseHandler } import com.github.fsanaulla.chronicler.core.alias.ErrorOr import com.github.fsanaulla.chronicler.core.api.MeasurementApi import com.github.fsanaulla.chronicler.core.components.BodyBuilder import com.github.fsanaulla.chronicler.core.enums.{Epoch, Epochs} import com.github.fsanaulla.chronicler.core.model.{Failable, Functor, InfluxReader} import scala.concurrent.Future import scala.reflect.ClassTag final class AkkaMeasurementApi[T: ClassTag]( dbName: String, measurementName: String, gzipped: Boolean )(implicit qb: AkkaQueryBuilder, bd: BodyBuilder[RequestEntity], re: AkkaRequestExecutor, rh: AkkaResponseHandler, F: Functor[Future], FA: Failable[Future]) extends MeasurementApi[Future, Future, HttpResponse, Uri, RequestEntity, T]( dbName, measurementName, gzipped ) { def readChunked( query: String, epoch: Epoch = Epochs.None, pretty: Boolean = false, chunkSize: Int )(implicit rd: InfluxReader[T] ): Future[Source[ErrorOr[Array[T]], Any]] = { val uri = chunkedQuery(dbName, query, epoch, pretty, chunkSize) F.map(re.get(uri, compressed = false))(rh.queryChunkedResult[T]) } }
Example 63
Source File: AkkaIOClient.scala From chronicler with Apache License 2.0 | 5 votes |
package com.github.fsanaulla.chronicler.akka.io import akka.actor.ActorSystem import akka.http.scaladsl.HttpsConnectionContext import akka.http.scaladsl.model.{HttpResponse, RequestEntity, Uri} import akka.stream.ActorMaterializer import com.github.fsanaulla.chronicler.akka.shared.InfluxAkkaClient import com.github.fsanaulla.chronicler.akka.shared.handlers._ import com.github.fsanaulla.chronicler.akka.shared.implicits._ import com.github.fsanaulla.chronicler.core.IOClient import com.github.fsanaulla.chronicler.core.alias.ErrorOr import com.github.fsanaulla.chronicler.core.model.{InfluxCredentials, InfluxDBInfo} import scala.concurrent.{ExecutionContext, Future} import scala.reflect.ClassTag final class AkkaIOClient( host: String, port: Int, credentials: Option[InfluxCredentials], compress: Boolean, httpsContext: Option[HttpsConnectionContext], terminateActorSystem: Boolean )(implicit ex: ExecutionContext, system: ActorSystem) extends InfluxAkkaClient(terminateActorSystem, httpsContext) with IOClient[Future, Future, HttpResponse, Uri, RequestEntity] { implicit val mat: ActorMaterializer = ActorMaterializer() implicit val bb: AkkaBodyBuilder = new AkkaBodyBuilder() implicit val qb: AkkaQueryBuilder = new AkkaQueryBuilder(schema, host, port, credentials) implicit val jh: AkkaJsonHandler = new AkkaJsonHandler(new AkkaBodyUnmarshaller(compress)) implicit val re: AkkaRequestExecutor = new AkkaRequestExecutor(ctx) implicit val rh: AkkaResponseHandler = new AkkaResponseHandler(jh) override def database(dbName: String): AkkaDatabaseApi = new AkkaDatabaseApi(dbName, compress) override def measurement[A: ClassTag]( dbName: String, measurementName: String ): AkkaMeasurementApi[A] = new AkkaMeasurementApi[A](dbName, measurementName, compress) override def ping: Future[ErrorOr[InfluxDBInfo]] = { re.get(qb.buildQuery("/ping", Nil), compressed = false) .flatMap(rh.pingResult) } }
Example 64
Source File: AkkaDatabaseApi.scala From chronicler with Apache License 2.0 | 5 votes |
package com.github.fsanaulla.chronicler.akka.io import akka.http.scaladsl.model.{HttpResponse, RequestEntity, Uri} import akka.stream.scaladsl.Source import com.github.fsanaulla.chronicler.akka.shared.handlers.{ AkkaQueryBuilder, AkkaRequestExecutor, AkkaResponseHandler } import com.github.fsanaulla.chronicler.core.alias.{ErrorOr, JPoint} import com.github.fsanaulla.chronicler.core.api.DatabaseApi import com.github.fsanaulla.chronicler.core.components.BodyBuilder import com.github.fsanaulla.chronicler.core.enums.{Epoch, Epochs} import com.github.fsanaulla.chronicler.core.model.{FunctionK, Functor} import scala.concurrent.Future final class AkkaDatabaseApi( dbName: String, compressed: Boolean )(implicit qb: AkkaQueryBuilder, bd: BodyBuilder[RequestEntity], re: AkkaRequestExecutor, rh: AkkaResponseHandler, F: Functor[Future], FK: FunctionK[Future, Future]) extends DatabaseApi[Future, Future, HttpResponse, Uri, RequestEntity](dbName, compressed) { def readChunkedJson( query: String, epoch: Epoch = Epochs.None, pretty: Boolean = false, chunkSize: Int ): Future[Source[ErrorOr[Array[JPoint]], Any]] = { val uri = chunkedQuery(dbName, query, epoch, pretty, chunkSize) F.map(re.get(uri, compressed))(rh.queryChunkedResultJson) } }
Example 65
Source File: ExtraDirectives.scala From eclair with Apache License 2.0 | 5 votes |
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 66
Source File: WebService.scala From heimdallr with Apache License 2.0 | 5 votes |
package chat import scala.concurrent.ExecutionContext.Implicits._ import scala.util.{Failure,Success} import akka.actor.ActorSystem import akka.stream.Materializer import akka.http.scaladsl.Http import akka.http.scaladsl.Http.{ ServerBinding } import akka.http.scaladsl.model.{ HttpRequest, HttpResponse, Uri } import akka.stream.scaladsl.{ Flow, Sink, Source } import org.slf4j.LoggerFactory trait WebService { val log = LoggerFactory.getLogger("total") private var binding: scala.concurrent.Future[ServerBinding] = null def serviceBind(serviceName: String, bindRoute: Flow[HttpRequest, HttpResponse, Any], bindPort: Int) (implicit actorSystem: ActorSystem, materializer: Materializer): Unit = { binding = Http().bindAndHandle(bindRoute,"0.0.0.0", bindPort) // the rest of the sample code will go here binding.onComplete { //binding success check case Success(binding) => val localAddress = binding.localAddress log.info(s"${serviceName} is listening on ${localAddress.getAddress}:${localAddress.getPort}") case Failure(e) => log.error(s"${serviceName} Binding failed with ${e.getMessage}") } } def serviceUnbind(serviceName: String) = { if( binding != null ) { binding .flatMap(_.unbind()) .onComplete(_ => log.info(s"${serviceName} listening port unbinding ... ") ) } else log.info( s"${serviceName} Unbinding Failed !" ) } }
Example 67
Source File: EventSource.scala From nexus with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.kg.client import java.util.UUID import akka.NotUsed import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model.headers.OAuth2BearerToken import akka.http.scaladsl.model.{HttpRequest, HttpResponse} import akka.persistence.query.{NoOffset, Offset, Sequence, TimeBasedUUID} import akka.stream.Materializer import akka.stream.alpakka.sse.scaladsl.{EventSource => SSESource} import akka.stream.scaladsl.Source import ch.epfl.bluebrain.nexus.iam.auth.AccessToken import ch.epfl.bluebrain.nexus.rdf.implicits._ import ch.epfl.bluebrain.nexus.rdf.Iri.AbsoluteIri import com.typesafe.scalalogging.Logger import io.circe.Decoder import io.circe.parser.decode import scala.concurrent.{ExecutionContext, Future} import scala.util.Try trait EventSource[A] { def apply[A: Decoder]( config: KgClientConfig )(implicit as: ActorSystem, mt: Materializer, ec: ExecutionContext): EventSource[A] = new EventSource[A] { private val logger = Logger[this.type] private val http = Http() private def addCredentials(request: HttpRequest)(implicit cred: Option[AccessToken]): HttpRequest = cred.map(token => request.addCredentials(OAuth2BearerToken(token.value))).getOrElse(request) private def send(request: HttpRequest)(implicit cred: Option[AccessToken]): Future[HttpResponse] = http.singleRequest(addCredentials(request)).map { resp => if (!resp.status.isSuccess()) logger.warn(s"HTTP response when performing SSE request: status = '${resp.status}'") resp } private def toOffset(id: String): Offset = Try(TimeBasedUUID(UUID.fromString(id))).orElse(Try(Sequence(id.toLong))).getOrElse(NoOffset) override def apply(iri: AbsoluteIri, offset: Option[String])(implicit cred: Option[AccessToken] ): Source[(Offset, A), NotUsed] = SSESource(iri.asAkka, send, offset, config.sseRetryDelay).flatMapConcat { sse => val offset = sse.id.map(toOffset).getOrElse(NoOffset) decode[A](sse.data) match { case Right(ev) => Source.single(offset -> ev) case Left(err) => logger.error(s"Failed to decode admin event '$sse'", err) Source.empty } } } }
Example 68
Source File: ErrorDirectivesSpec.scala From nexus with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.commons.http.directives import akka.http.scaladsl.marshalling.Marshal import akka.http.scaladsl.model.{HttpEntity, HttpResponse, StatusCodes} import akka.util.ByteString import ch.epfl.bluebrain.nexus.commons.circe.ContextUri import ch.epfl.bluebrain.nexus.commons.http.RdfMediaTypes import ch.epfl.bluebrain.nexus.commons.http.directives.ErrorDirectives._ import ch.epfl.bluebrain.nexus.commons.http.directives.ErrorDirectivesSpec.CustomError import ch.epfl.bluebrain.nexus.rdf.syntax.iri._ import ch.epfl.bluebrain.nexus.util.ActorSystemFixture import io.circe.generic.auto._ import org.scalatest.concurrent.ScalaFutures import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike import scala.concurrent.duration._ class ErrorDirectivesSpec extends ActorSystemFixture("ErrorDirectivesSpec") with AnyWordSpecLike with Matchers with ScalaFutures { implicit override val patienceConfig = PatienceConfig(3.seconds, 100.millis) "A ErrorDirectives" should { import system.dispatcher implicit val statusFromJson: StatusFrom[CustomError] = StatusFrom((_: CustomError) => StatusCodes.NotFound) implicit val contextUri: ContextUri = ContextUri(url"http://localhost.com/error/") "marshall error JSON-LD" in { val error = CustomError("some error") val jsonString = s"""{"@context":"${contextUri.value}","message":"${error.message}"}""" Marshal(error).to[HttpResponse].futureValue shouldEqual HttpResponse( status = StatusCodes.NotFound, entity = HttpEntity.Strict(RdfMediaTypes.`application/ld+json`, ByteString(jsonString, "UTF-8")) ) } } } object ErrorDirectivesSpec { final case class CustomError(message: String) }
Example 69
Source File: Gateway.scala From reactive-microservices with MIT License | 5 votes |
import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.client.RequestBuilding import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._ import akka.http.scaladsl.model.StatusCodes._ import akka.http.scaladsl.model.{HttpRequest, HttpResponse} import akka.http.scaladsl.unmarshalling.Unmarshal import akka.stream.FlowMaterializer import akka.stream.scaladsl.{Sink, Source} import java.io.IOException import scala.concurrent.{ExecutionContext, Future} case class InternalLoginRequest(identityId: Long, authMethod: String = "codecard") case class InternalReloginRequest(tokenValue: String, authMethod: String = "codecard") class Gateway(implicit actorSystem: ActorSystem, materializer: FlowMaterializer, ec: ExecutionContext) extends JsonProtocols with Config { private val identityManagerConnectionFlow = Http().outgoingConnection(identityManagerHost, identityManagerPort) private val tokenManagerConnectionFlow = Http().outgoingConnection(tokenManagerHost, tokenManagerPort) private def requestIdentityManager(request: HttpRequest): Future[HttpResponse] = { Source.single(request).via(identityManagerConnectionFlow).runWith(Sink.head) } private def requestTokenManager(request: HttpRequest): Future[HttpResponse] = { Source.single(request).via(tokenManagerConnectionFlow).runWith(Sink.head) } def requestToken(tokenValue: String): Future[Either[String, Token]] = { requestTokenManager(RequestBuilding.Get(s"/tokens/$tokenValue")).flatMap { response => response.status match { case Success(_) => Unmarshal(response.entity).to[Token].map(Right(_)) case NotFound => Future.successful(Left("Token expired or not found")) case _ => Future.failed(new IOException(s"Token request failed with status ${response.status} and error ${response.entity}")) } } } def requestNewIdentity(): Future[Identity] = { requestIdentityManager(RequestBuilding.Post("/identities")).flatMap { response => response.status match { case Success(_) => Unmarshal(response.entity).to[Identity] case _ => Future.failed(new IOException(s"Identity request failed with status ${response.status} and error ${response.entity}")) } } } def requestLogin(identityId: Long): Future[Token] = { val loginRequest = InternalLoginRequest(identityId) requestTokenManager(RequestBuilding.Post("/tokens", loginRequest)).flatMap { response => response.status match { case Success(_) => Unmarshal(response.entity).to[Token] case _ => Future.failed(new IOException(s"Login request failed with status ${response.status} and error ${response.entity}")) } } } def requestRelogin(tokenValue: String): Future[Option[Token]] = { requestTokenManager(RequestBuilding.Patch("/tokens", InternalReloginRequest(tokenValue))).flatMap { response => response.status match { case Success(_) => Unmarshal(response.entity).to[Token].map(Option(_)) case NotFound => Future.successful(None) case _ => Future.failed(new IOException(s"Relogin request failed with status ${response.status} and error ${response.entity}")) } } } }
Example 70
Source File: Gateway.scala From reactive-microservices with MIT License | 5 votes |
import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.client.RequestBuilding import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._ import akka.http.scaladsl.model.StatusCodes._ import akka.http.scaladsl.model.{HttpRequest, HttpResponse} import akka.http.scaladsl.unmarshalling.Unmarshal import akka.stream.FlowMaterializer import akka.stream.scaladsl.{Sink, Source} import java.io.IOException import scala.concurrent.{ExecutionContext, Future} case class InternalLoginRequest(identityId: Long, authMethod: String = "password") case class InternalReloginRequest(tokenValue: String, authMethod: String = "password") class Gateway(implicit actorSystem: ActorSystem, materializer: FlowMaterializer, ec: ExecutionContext) extends JsonProtocols with Config { private val identityManagerConnectionFlow = Http().outgoingConnection(identityManagerHost, identityManagerPort) private val tokenManagerConnectionFlow = Http().outgoingConnection(tokenManagerHost, tokenManagerPort) private def requestIdentityManager(request: HttpRequest): Future[HttpResponse] = { Source.single(request).via(identityManagerConnectionFlow).runWith(Sink.head) } private def requestTokenManager(request: HttpRequest): Future[HttpResponse] = { Source.single(request).via(tokenManagerConnectionFlow).runWith(Sink.head) } def requestToken(tokenValue: String): Future[Either[String, Token]] = { requestTokenManager(RequestBuilding.Get(s"/tokens/$tokenValue")).flatMap { response => response.status match { case Success(_) => Unmarshal(response.entity).to[Token].map(Right(_)) case NotFound => Future.successful(Left("Token expired or not found")) case _ => Future.failed(new IOException(s"Token request failed with status ${response.status} and error ${response.entity}")) } } } def requestNewIdentity(): Future[Identity] = { requestIdentityManager(RequestBuilding.Post("/identities")).flatMap { response => response.status match { case Success(_) => Unmarshal(response.entity).to[Identity] case _ => Future.failed(new IOException(s"Identity request failed with status ${response.status} and error ${response.entity}")) } } } def requestLogin(identityId: Long): Future[Token] = { val loginRequest = InternalLoginRequest(identityId) requestTokenManager(RequestBuilding.Post("/tokens", loginRequest)).flatMap { response => response.status match { case Success(_) => Unmarshal(response.entity).to[Token] case _ => Future.failed(new IOException(s"Login request failed with status ${response.status} and error ${response.entity}")) } } } def requestRelogin(tokenValue: String): Future[Option[Token]] = { requestTokenManager(RequestBuilding.Patch("/tokens", InternalReloginRequest(tokenValue))).flatMap { response => response.status match { case Success(_) => Unmarshal(response.entity).to[Token].map(Option(_)) case NotFound => Future.successful(None) case _ => Future.failed(new IOException(s"Relogin request failed with status ${response.status} and error ${response.entity}")) } } } }
Example 71
Source File: MetricsDirectives.scala From reactive-microservices with MIT License | 5 votes |
package metrics.common import akka.http.scaladsl.model.{HttpRequest, HttpResponse} import akka.http.scaladsl.server.Directive0 case class RequestResponseStats(request: HttpRequest, response: HttpResponse, time: Long) trait MetricsDirectives { import akka.http.scaladsl.server.directives.BasicDirectives._ def measureRequestResponse(f: (RequestResponseStats => Unit)): Directive0 = { extractRequestContext.flatMap { ctx => val start = System.currentTimeMillis() mapResponse { response => val stop = System.currentTimeMillis() f(RequestResponseStats(ctx.request, response, stop - start)) response } } } } object MetricsDirectives extends MetricsDirectives
Example 72
Source File: SessionManager.scala From reactive-microservices with MIT License | 5 votes |
import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.client.RequestBuilding import akka.http.scaladsl.model.{HttpResponse, HttpRequest} import akka.http.scaladsl.server.Directives._ import akka.stream.ActorFlowMaterializer import akka.stream.scaladsl.{Sink, Source} import com.typesafe.config.ConfigFactory import scala.concurrent.Future object SessionManager extends App { val config = ConfigFactory.load() val interface = config.getString("http.interface") val port = config.getInt("http.port") val tokenManagerHost = config.getString("services.token-manager.host") val tokenManagerPort = config.getInt("services.token-manager.port") implicit val actorSystem = ActorSystem() implicit val materializer = ActorFlowMaterializer() implicit val dispatcher = actorSystem.dispatcher val tokenManagerConnectionFlow = Http().outgoingConnection(tokenManagerHost, tokenManagerPort) def requestTokenManager(request: HttpRequest): Future[HttpResponse] = { Source.single(request).via(tokenManagerConnectionFlow).runWith(Sink.head) } Http().bindAndHandle(interface = interface, port = port, handler = { logRequestResult("session-manager") { path("session") { headerValueByName("Auth-Token") { tokenValue => pathEndOrSingleSlash { get { complete { requestTokenManager(RequestBuilding.Get(s"/tokens/$tokenValue")) } } ~ delete { complete { requestTokenManager(RequestBuilding.Delete(s"/tokens/$tokenValue")) } } } } } } }) }
Example 73
Source File: Gateway.scala From reactive-microservices with MIT License | 5 votes |
import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.client.RequestBuilding import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._ import akka.http.scaladsl.model.StatusCodes._ import akka.http.scaladsl.model.{HttpRequest, HttpResponse} import akka.http.scaladsl.unmarshalling.Unmarshal import akka.stream.FlowMaterializer import akka.stream.scaladsl.{Sink, Source} import com.restfb.DefaultFacebookClient import com.restfb.types.User import java.io.IOException import scala.concurrent.{blocking, ExecutionContext, Future} import scala.util.Try case class InternalLoginRequest(identityId: Long, authMethod: String = "fb") case class InternalReloginRequest(tokenValue: String, authMethod: String = "fb") class Gateway(implicit actorSystem: ActorSystem, materializer: FlowMaterializer, ec: ExecutionContext) extends JsonProtocols with Config { private val identityManagerConnectionFlow = Http().outgoingConnection(identityManagerHost, identityManagerPort) private val tokenManagerConnectionFlow = Http().outgoingConnection(tokenManagerHost, tokenManagerPort) private def requestIdentityManager(request: HttpRequest): Future[HttpResponse] = { Source.single(request).via(identityManagerConnectionFlow).runWith(Sink.head) } private def requestTokenManager(request: HttpRequest): Future[HttpResponse] = { Source.single(request).via(tokenManagerConnectionFlow).runWith(Sink.head) } def requestToken(tokenValue: String): Future[Either[String, Token]] = { requestTokenManager(RequestBuilding.Get(s"/tokens/$tokenValue")).flatMap { response => response.status match { case Success(_) => Unmarshal(response.entity).to[Token].map(Right(_)) case NotFound => Future.successful(Left("Token expired or not found")) case _ => Future.failed(new IOException(s"Token request failed with status ${response.status} and error ${response.entity}")) } } } def requestNewIdentity(): Future[Identity] = { requestIdentityManager(RequestBuilding.Post("/identities")).flatMap { response => response.status match { case Success(_) => Unmarshal(response.entity).to[Identity] case _ => Future.failed(new IOException(s"Identity request failed with status ${response.status} and error ${response.entity}")) } } } def requestLogin(identityId: Long): Future[Token] = { val loginRequest = InternalLoginRequest(identityId) requestTokenManager(RequestBuilding.Post("/tokens", loginRequest)).flatMap { response => response.status match { case Success(_) => Unmarshal(response.entity).to[Token] case _ => Future.failed(new IOException(s"Login request failed with status ${response.status} and error ${response.entity}")) } } } def requestRelogin(tokenValue: String): Future[Option[Token]] = { requestTokenManager(RequestBuilding.Patch("/tokens", InternalReloginRequest(tokenValue))).flatMap { response => response.status match { case Success(_) => Unmarshal(response.entity).to[Token].map(Option(_)) case NotFound => Future.successful(None) case _ => Future.failed(new IOException(s"Relogin request failed with status ${response.status} and error ${response.entity}")) } } } def getFbUserDetails(accessToken: String): Try[User] = { Try { blocking { val client = new DefaultFacebookClient(accessToken) client.fetchObject("me", classOf[User]) } } } }
Example 74
Source File: HttpClientProvider.scala From reactive-nakadi with MIT License | 5 votes |
package org.zalando.react.nakadi.client.providers import java.security.SecureRandom import java.security.cert.X509Certificate import javax.net.ssl.{SSLContext, TrustManager, X509TrustManager} import akka.actor.ActorContext import akka.http.scaladsl.Http.OutgoingConnection import akka.http.scaladsl.model.{HttpRequest, HttpResponse} import akka.http.scaladsl.settings.ClientConnectionSettings import akka.http.scaladsl.{Http, HttpsConnectionContext} import akka.stream.scaladsl.Flow import scala.concurrent.Future import scala.concurrent.duration._ class HttpClientProvider(actorContext: ActorContext, server: String, port: Int, isConnectionSSL: Boolean = false, acceptAnyCertificate: Boolean = false, connectionTimeout: FiniteDuration) { val http = Http(actorContext.system) private val settings = { ClientConnectionSettings .apply(actorContext.system) .withConnectingTimeout(connectionTimeout) .withIdleTimeout(Duration.Inf) } val connection: Flow[HttpRequest, HttpResponse, Future[OutgoingConnection]] = { isConnectionSSL match { case true => val sslContext = if (!acceptAnyCertificate) SSLContext.getDefault else { val permissiveTrustManager: TrustManager = new X509TrustManager() { override def checkClientTrusted(chain: Array[X509Certificate], authType: String): Unit = {} override def checkServerTrusted(chain: Array[X509Certificate], authType: String): Unit = {} override def getAcceptedIssuers(): Array[X509Certificate] = Array.empty } val ctx = SSLContext.getInstance("TLS") ctx.init(Array.empty, Array(permissiveTrustManager), new SecureRandom()) ctx } http.outgoingConnectionHttps(server, port, new HttpsConnectionContext(sslContext), settings = settings) case false => http.outgoingConnection(server, port, settings = settings) } } }
Example 75
Source File: HttpZipDecoder.scala From CM-Well with Apache License 2.0 | 5 votes |
package cmwell.util.akka.http import akka.http.scaladsl.coding.{Deflate, Gzip, NoCoding} import akka.http.scaladsl.model.HttpResponse import akka.http.scaladsl.model.headers.{HttpEncoding, HttpEncodings} object HttpZipDecoder { def decodeResponse(response: HttpResponse): HttpResponse = { val decoder = response.encoding match { case HttpEncodings.gzip ⇒ Gzip case HttpEncodings.deflate ⇒ Deflate case HttpEncodings.identity ⇒ NoCoding case HttpEncoding(_) ⇒ ??? } decoder.decodeMessage(response) } }
Example 76
Source File: AkkaUtils.scala From CM-Well with Apache License 2.0 | 5 votes |
package cmwell.tools.data.utils.akka import java.net.InetSocketAddress import akka.http.scaladsl.ClientTransport import akka.http.scaladsl.coding.{Deflate, Gzip, NoCoding} import akka.http.scaladsl.model.HttpResponse import akka.http.scaladsl.model.headers.{HttpEncoding, HttpEncodings} import akka.http.scaladsl.settings.{ClientConnectionSettings, ConnectionPoolSettings} import com.typesafe.config.ConfigFactory object AkkaUtils { def generateClientConnectionSettings(userAgent : String) ={ val settings = ClientConnectionSettings( ConfigFactory .parseString(s"akka.http.host-connection-pool.client.user-agent-header=$userAgent") .withFallback(config) ) val proxyHost = config.getString("akka.http.client.proxy.https.host") val proxyPort = config.getInt("akka.http.client.proxy.https.port") if (proxyHost!="") { val httpsProxyTransport = ClientTransport.httpsProxy(InetSocketAddress.createUnresolved(proxyHost, proxyPort)) settings.withTransport(httpsProxyTransport) } else settings } def generateConnectionPoolSettings(userAgent : Option[String] = None) ={ val settings = userAgent.fold{ ConnectionPoolSettings(config) .withConnectionSettings(ClientConnectionSettings(config)) }{userAgentV => ConnectionPoolSettings( ConfigFactory .parseString(s"akka.http.host-connection-pool.client.user-agent-header=$userAgentV") .withFallback(config) ) } val proxyHost = config.getString("akka.http.client.proxy.https.host") val proxyPort = config.getInt("akka.http.client.proxy.https.port") if (proxyHost!="") { val httpsProxyTransport = ClientTransport.httpsProxy(InetSocketAddress.createUnresolved(proxyHost, proxyPort)) settings.withTransport(httpsProxyTransport) } else settings } def decodeResponse(response: HttpResponse) = { val decoder = response.encoding match { case HttpEncodings.gzip ⇒ Gzip case HttpEncodings.deflate ⇒ Deflate case HttpEncodings.identity ⇒ NoCoding case HttpEncoding(_) ⇒ ??? } decoder.decodeMessage(response) } }
Example 77
Source File: HttpUtil.scala From CM-Well with Apache License 2.0 | 5 votes |
package cmwell.analytics.util import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model.RequestEntityAcceptance.Tolerated import akka.http.scaladsl.model.{HttpMethod, HttpRequest, HttpResponse} import akka.stream.ActorMaterializer import akka.stream.scaladsl.Sink import akka.util.ByteString import com.fasterxml.jackson.databind.{JsonNode, ObjectMapper} import com.typesafe.config.ConfigFactory import scala.concurrent.duration.{MILLISECONDS, _} import scala.concurrent.{Await, ExecutionContextExecutor, Future} object HttpUtil { private val mapper = new ObjectMapper() private val config = ConfigFactory.load private val ReadTimeout = FiniteDuration(config.getDuration("extract-index-from-es.read-timeout").toMillis, MILLISECONDS) // Elasticsearch uses the POST verb in some places where the request is actually idempotent. // Requests that use POST, but are known to be idempotent can use this method. // The presence of any non-idempotent request in-flight causes Akka to not retry, and that will tend result in // entire downloads failing more often. val SAFE_POST = HttpMethod( value = "POST", isSafe = true, isIdempotent = true, requestEntityAcceptance = Tolerated) def resultAsync(request: HttpRequest, action: String) (implicit system: ActorSystem, executionContext: ExecutionContextExecutor, actorMaterializer: ActorMaterializer): Future[ByteString] = Http().singleRequest(request).map { case HttpResponse(status, _, entity, _) if status.isSuccess => entity.dataBytes .fold(ByteString.empty)(_ ++ _) .runWith(Sink.head) case HttpResponse(status, _, entity, _) => val message = Await.result(entity.toStrict(10.seconds).map(_.data), 10.seconds).utf8String throw new RuntimeException(s"HTTP request for $action failed. Status code: $status, message:$message") } .flatMap(identity) def result(request: HttpRequest, action: String, timeout: FiniteDuration = ReadTimeout) (implicit system: ActorSystem, executionContext: ExecutionContextExecutor, actorMaterializer: ActorMaterializer): ByteString = Await.result(resultAsync(request, action), timeout) def jsonResult(request: HttpRequest, action: String, timeout: FiniteDuration = ReadTimeout) (implicit system: ActorSystem, executionContext: ExecutionContextExecutor, actorMaterializer: ActorMaterializer): JsonNode = mapper.readTree(result(request, action, timeout).utf8String) def jsonResultAsync(request: HttpRequest, action: String) (implicit system: ActorSystem, executionContext: ExecutionContextExecutor, actorMaterializer: ActorMaterializer): Future[JsonNode] = resultAsync(request, action).map((bytes: ByteString) => mapper.readTree(bytes.utf8String)) }
Example 78
Source File: PrometheusUtils.scala From kafka-lag-exporter with Apache License 2.0 | 5 votes |
package com.lightbend.kafkalagexporter.integration import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model.{HttpRequest, HttpResponse, StatusCodes} import akka.http.scaladsl.unmarshalling.Unmarshal import akka.stream.Materializer import com.lightbend.kafkalagexporter.MetricsSink.GaugeDefinition import org.scalatest.Matchers import org.scalatest.concurrent.ScalaFutures import org.slf4j.{Logger, LoggerFactory} import scala.concurrent.{ExecutionContext, Future} import scala.util.matching.Regex val regex = s"""$name\\{$labels.*\\}\\s+(-?.+)""".r log.debug(s"Created regex: {}", regex.pattern.toString) Rule(regex, assertion) } } case class Rule(regex: Regex, assertion: String => _) case class Result(rule: Rule, groupResults: List[String]) { def assertDne(): Unit = { log.debug(s"Rule: ${rule.regex.toString}") groupResults.length shouldBe 0 } def assert(): Unit = { log.debug(s"Rule: ${rule.regex.toString}") groupResults.length shouldBe 1 log.debug(s"Actual value is ${groupResults.head}") rule.assertion(groupResults.head) } } }
Example 79
Source File: PublicApi.scala From affinity with Apache License 2.0 | 5 votes |
import akka.http.scaladsl.model.HttpMethods._ import akka.http.scaladsl.model.HttpResponse import akka.http.scaladsl.model.StatusCodes._ import akka.http.scaladsl.model.Uri.{Path, Query} import io.amient.affinity.avro.record.AvroRecord import io.amient.affinity.core.actor.GatewayHttp import io.amient.affinity.core.http.Encoder import io.amient.affinity.core.http.RequestMatchers.{HTTP, PATH} import io.amient.affinity.core.state.KVStore import scala.concurrent.Promise case class ProtectedProfile(hello: String = "world") extends AvroRecord trait PublicApi extends GatewayHttp { private val settings: KVStore[String, ConfigEntry] = global[String, ConfigEntry]("settings") abstract override def handle: Receive = super.handle orElse { case HTTP(GET, uri@PATH("profile", _), query, response) => AUTH_DSA(uri, query, response) { (sig: String) => Encoder.json(OK, Map( "signature" -> sig, "profile" -> ProtectedProfile() )) } case HTTP(GET, uri@PATH("verify"), query, response) => AUTH_DSA(uri, query, response) { _ => Encoder.json(OK, Some(ProtectedProfile())) } } object AUTH_DSA { def apply(path: Path, query: Query, response: Promise[HttpResponse])(code: (String) => HttpResponse): Unit = { try { query.get("signature") match { case None => throw new IllegalAccessError case Some(sig) => sig.split(":") match { case Array(k, clientSignature) => settings(k) match { case None => throw new IllegalAccessError(s"Invalid api key $k") case Some(configEntry) => if (configEntry.crypto.verify(clientSignature, path.toString)) { val serverSignature = configEntry.crypto.sign(clientSignature) response.success(code(serverSignature)) } else { throw new IllegalAccessError(configEntry.crypto.sign(path.toString)) } } case _ => throw new IllegalAccessError } } } catch { case _: IllegalAccessError => response.success(Encoder.json(Unauthorized, "Unauthorized")) case e: Throwable => response.success(handleException(headers = List())(e)) } } } }
Example 80
Source File: AffinityMetrics.scala From affinity with Apache License 2.0 | 5 votes |
package io.amient.affinity.core.util import java.util.concurrent.ConcurrentHashMap import akka.actor.ActorSystem import akka.http.scaladsl.model.HttpResponse import com.codahale.metrics.{MetricRegistry, Timer} import scala.concurrent.{Future, Promise} import scala.util.{Failure, Success} object AffinityMetrics { private val reporters = scala.collection.mutable.ListBuffer[MetricRegistry => Unit]() def apply(f: MetricRegistry => Unit): Unit = reporters += f private val metricsRegistries = new ConcurrentHashMap[ActorSystem, AffinityMetrics]() def forActorSystem(system: ActorSystem): AffinityMetrics = { metricsRegistries.get(system) match { case null => val registry = new AffinityMetrics reporters.foreach(_(registry)) metricsRegistries.put(system, registry) registry case registry => registry } } } class AffinityMetrics extends MetricRegistry { private implicit val executor = scala.concurrent.ExecutionContext.Implicits.global private val processMetricsMap = new ConcurrentHashMap[String, ProcessMetrics]() def meterAndHistogram(name: String): ProcessMetrics = { processMetricsMap.get(name) match { case null => val m = new ProcessMetrics(name) processMetricsMap.put(name, m) m case some => some } } def process(groupName: String, result: Promise[_]): Unit = process(groupName, result.future) def process(groupName: String, result: Future[Any]): Unit = { val metrics = meterAndHistogram(groupName) val startTime = metrics.markStart() result.onComplete { case Success(response: HttpResponse) => if (response.status.intValue() < 400) metrics.markSuccess(startTime) else metrics.markFailure(startTime) case Success(_) => metrics.markSuccess(startTime) case Failure(_) => metrics.markFailure(startTime) } } class ProcessMetrics(name: String) { val durations = timer(s"$name.timer") val successes = meter(s"$name.success") val failures = meter(s"$name.failure") def markStart(): Timer.Context = durations.time() def markSuccess(context: Timer.Context, n: Long = 1): Unit = { context.stop successes.mark(n) } def markFailure(context: Timer.Context): Unit = { context.stop failures.mark() } } }
Example 81
Source File: TlsGatewaySystemTest.scala From affinity with Apache License 2.0 | 5 votes |
package io.amient.affinity.core.http import akka.http.scaladsl.model.HttpMethods.GET import akka.http.scaladsl.model.StatusCodes._ import akka.http.scaladsl.model.{HttpEntity, HttpResponse} import io.amient.affinity.AffinityActorSystem import io.amient.affinity.core.actor.GatewayHttp import io.amient.affinity.core.cluster.Node import io.amient.affinity.core.http.RequestMatchers.HTTP import io.amient.affinity.core.util.AffinityTestBase import org.scalatest.{BeforeAndAfterAll, FlatSpec, Matchers} class TlsGateway extends GatewayHttp { override def handle: Receive = { case HTTP(GET, _, _, response) => response.success(HttpResponse(OK, entity = "Hello World")) } } class TlsGatewaySystemTest extends FlatSpec with AffinityTestBase with BeforeAndAfterAll with Matchers { def config = configure("tlstests") val system = AffinityActorSystem.create(config) val node = new Node(config) override def beforeAll: Unit = try { node.start() node.awaitClusterReady() } finally { super.beforeAll() } override def afterAll(): Unit = try { node.shutdown() } finally { super.afterAll() } "HTTPS Requests" should "be handled correctly as TLS" in { node.https_get("/tls-hello").entity should equal(HttpEntity("Hello World")) } }
Example 82
Source File: FailoverTestGateway.scala From affinity with Apache License 2.0 | 5 votes |
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 83
Source File: EventSource.scala From nexus-iam with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.iam.client import akka.NotUsed import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model.headers.OAuth2BearerToken import akka.http.scaladsl.model.{HttpRequest, HttpResponse} import akka.stream.Materializer import akka.stream.alpakka.sse.scaladsl.{EventSource => SSESource} import akka.stream.scaladsl.Source import ch.epfl.bluebrain.nexus.iam.client.config.IamClientConfig import ch.epfl.bluebrain.nexus.iam.client.types.AuthToken import ch.epfl.bluebrain.nexus.rdf.Iri.AbsoluteIri import ch.epfl.bluebrain.nexus.rdf.implicits._ import com.typesafe.scalalogging.Logger import io.circe.Decoder import io.circe.parser.decode import scala.concurrent.{ExecutionContext, Future} trait EventSource[A] { def apply[A: Decoder]( config: IamClientConfig )(implicit as: ActorSystem, mt: Materializer, ec: ExecutionContext): EventSource[A] = new EventSource[A] { private val logger = Logger[this.type] private val http = Http() private def addCredentials(request: HttpRequest)(implicit cred: Option[AuthToken]): HttpRequest = cred.map(token => request.addCredentials(OAuth2BearerToken(token.value))).getOrElse(request) private def send(request: HttpRequest)(implicit cred: Option[AuthToken]): Future[HttpResponse] = http.singleRequest(addCredentials(request)).map { resp => if (!resp.status.isSuccess()) logger.warn(s"HTTP response when performing SSE request: status = '${resp.status}'") resp } override def apply(iri: AbsoluteIri, offset: Option[String])( implicit cred: Option[AuthToken] ): Source[A, NotUsed] = SSESource(iri.asAkka, send, offset, config.sseRetryDelay).flatMapConcat { sse => decode[A](sse.data) match { case Right(ev) => Source.single(ev) case Left(err) => logger.error(s"Failed to decode admin event '$sse'", err) Source.empty } } } }
Example 84
Source File: PlayRouter.scala From play-grpc with Apache License 2.0 | 5 votes |
package play.grpc.internal import java.util.Optional import java.util.concurrent.CompletionStage import akka.annotation.InternalApi import akka.dispatch.Dispatchers import akka.http.scaladsl.model.HttpRequest import akka.http.scaladsl.model.HttpResponse import akka.stream.Materializer import play.api.inject.Injector import play.api.mvc.Handler import play.api.mvc.akkahttp.AkkaHttpHandler import play.api.routing.Router import play.api.routing.Router.Routes import play.mvc.Http import scala.concurrent.ExecutionContext import scala.concurrent.Future import scala.compat.java8.FutureConverters._ import scala.compat.java8.OptionConverters._ final override def withPrefix(prefix: String): Router = if (prefix == "/") this else throw new UnsupportedOperationException( "Prefixing gRPC services is not widely supported by clients, " + s"strongly discouraged by the specification and therefore not supported. " + s"Attempted to prefix with [$prefix], yet already default prefix known to be [${this.prefix}]. " + s"When binding gRPC routers the path in `routes` MUST BE `/`.", ) }
Example 85
Source File: VinylDNSRouteTestHelper.scala From vinyldns with Apache License 2.0 | 5 votes |
package vinyldns.api.route import akka.http.scaladsl.model.{ContentTypes, HttpEntity, HttpResponse, StatusCodes} import akka.http.scaladsl.server.Directives.{complete, extractUnmatchedPath} import akka.http.scaladsl.server.{MalformedRequestContentRejection, RejectionHandler} import akka.http.scaladsl.testkit.ScalatestRouteTest import org.json4s.MappingException trait VinylDNSRouteTestHelper { this: ScalatestRouteTest => import scala.concurrent.duration._ import akka.http.scaladsl.testkit.RouteTestTimeout implicit val testTimeout = RouteTestTimeout(10.seconds) implicit def validationRejectionHandler: RejectionHandler = RejectionHandler .newBuilder() .handle { case MalformedRequestContentRejection(msg, MappingException(_, _)) => complete( HttpResponse( status = StatusCodes.BadRequest, entity = HttpEntity(ContentTypes.`application/json`, msg) ) ) } .handleNotFound { extractUnmatchedPath { p => complete((StatusCodes.NotFound, s"The requested path [$p] does not exist.")) } } .result() }
Example 86
Source File: PrometheusRoutingSpec.scala From vinyldns with Apache License 2.0 | 5 votes |
package vinyldns.api.route import akka.http.scaladsl.model.{HttpProtocol, HttpResponse, StatusCodes} import akka.http.scaladsl.testkit.ScalatestRouteTest import io.prometheus.client.CollectorRegistry import io.prometheus.client.dropwizard.DropwizardExports import org.scalatestplus.mockito.MockitoSugar import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpec import org.scalatest.BeforeAndAfterEach import vinyldns.core.VinylDNSMetrics class PrometheusRoutingSpec extends AnyWordSpec with ScalatestRouteTest with PrometheusRoute with BeforeAndAfterEach with MockitoSugar with Matchers { val metricRegistry = VinylDNSMetrics.metricsRegistry val collectorRegistry = CollectorRegistry.defaultRegistry collectorRegistry.register(new DropwizardExports(metricRegistry)) "GET /metrics/prometheus" should { "return metrics logged in prometheus" in { Get("/metrics/prometheus") ~> prometheusRoute ~> check { response.status shouldBe StatusCodes.OK val resultStatus = responseAs[HttpResponse] resultStatus.protocol shouldBe HttpProtocol("HTTP/1.1") } } } }
Example 87
Source File: AkkaHttpHelpers.scala From akka-viz with MIT License | 5 votes |
package akkaviz.server import akka.http.scaladsl.marshalling.{Marshal, Marshaller} import akka.http.scaladsl.model.HttpEntity.ChunkStreamPart import akka.http.scaladsl.model.{HttpEntity, HttpResponse, MediaTypes} import akka.http.scaladsl.server.{Directives, StandardRoute} import akka.stream.scaladsl.{Flow, Source} import scala.concurrent.ExecutionContext trait AkkaHttpHelpers { def asJsonArray[T](implicit m: Marshaller[T, String], ec: ExecutionContext): Flow[T, HttpEntity.ChunkStreamPart, _] = { Flow.apply[T] .mapAsync[String](4)(t => Marshal(t).to[String]) .scan[Option[ChunkStreamPart]](None) { case (None, s: String) => Some(ChunkStreamPart(s)) case (_, s: String) => Some(ChunkStreamPart(s",${s}")) }.mapConcat(_.toList) .prepend(Source.single(ChunkStreamPart("["))) .concat(Source.single(ChunkStreamPart("]"))) } def completeAsJson[T](source: Source[T, _])(implicit m: Marshaller[T, String], ec: ExecutionContext): StandardRoute = { Directives.complete(HttpResponse( entity = HttpEntity.Chunked(MediaTypes.`application/json`, source.via(asJsonArray)) )) } } object AkkaHttpHelpers extends AkkaHttpHelpers
Example 88
Source File: DevApi.scala From mist with Apache License 2.0 | 5 votes |
package io.hydrosphere.mist.master.interfaces.http import akka.http.scaladsl.marshalling import akka.http.scaladsl.model import akka.http.scaladsl.model.{HttpResponse, StatusCodes} import akka.http.scaladsl.server.Directives import akka.http.scaladsl.server.directives.ParameterDirectives import cats.data._ import cats.implicits._ import io.hydrosphere.mist.master.{JobDetails, MainService} import io.hydrosphere.mist.master.interfaces.JsonCodecs import io.hydrosphere.mist.master.interfaces.http.HttpV2Base.completeOpt import io.hydrosphere.mist.master.models.DevJobStartRequestModel object DevApi { import Directives._ import JsonCodecs._ import ParameterDirectives.ParamMagnet import akka.http.scaladsl.server._ import scala.concurrent.ExecutionContext.Implicits.global def devRoutes(masterService: MainService): Route = { val exceptionHandler = ExceptionHandler { case iae: IllegalArgumentException => complete((StatusCodes.BadRequest, s"Bad request: ${iae.getMessage}")) case ex => complete(HttpResponse(StatusCodes.InternalServerError, entity = s"Server error: ${ex.getMessage}")) } path( "v2" / "hidden" / "devrun" ) { post( parameter('force ? false) { force => entity(as[DevJobStartRequestModel]) { req => handleExceptions(exceptionHandler) { val execInfo = masterService.devRun(req.toCommon, JobDetails.Source.Http) if (force) complete(execInfo.map(_.toJobResult)) else complete(execInfo.map(_.toJobStartResponse)) } } }) } } }
Example 89
Source File: TestUtils.scala From mist with Apache License 2.0 | 5 votes |
package io.hydrosphere.mist.master import akka.http.scaladsl.model.{HttpRequest, HttpResponse} import akka.stream.scaladsl.Flow import com.typesafe.config.ConfigFactory import scala.concurrent.duration.{Duration, FiniteDuration} import scala.concurrent.{Await, Future, Promise} trait TestUtils { implicit class AwaitSyntax[A](f: => Future[A]) { def await: A = Await.result(f, Duration.Inf) def await(d: FiniteDuration): A = Await.result(f, d) } } object TestUtils extends TestUtils { val cfgStr = """ |context-defaults { | downtime = Inf | streaming-duration = 1 seconds | max-parallel-jobs = 20 | precreated = false | spark-conf = { } | worker-mode = "shared" | run-options = "--opt" | max-conn-failures = 5 |} | |context { | | foo { | spark-conf { | spark.master = "local[2]" | } | } |} """.stripMargin val contextSettings = { val cfg = ConfigFactory.parseString(cfgStr) ContextsSettings(cfg) } val FooContext = contextSettings.contexts.get("foo").get object MockHttpServer { import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.stream.ActorMaterializer import akka.util.Timeout import scala.concurrent.duration._ def onServer[A]( routes: Flow[HttpRequest, HttpResponse, _], f: (Http.ServerBinding) => A): Future[A] = { implicit val system = ActorSystem("mock-http-cli") implicit val materializer = ActorMaterializer() implicit val executionContext = system.dispatcher implicit val timeout = Timeout(1.seconds) val binding = Http().bindAndHandle(routes, "localhost", 0) val close = Promise[Http.ServerBinding] close.future .flatMap(binding => binding.unbind()) .onComplete(_ => { materializer.shutdown() Await.result(system.terminate(), Duration.Inf) }) val result = binding.flatMap(binding => { try { Future.successful(f(binding)) } catch { case e: Throwable => Future.failed(e) } finally { close.success(binding) } }) result } } }
Example 90
Source File: GreeterServer.scala From akka-grpc with Apache License 2.0 | 5 votes |
//#full-server package example.myapp.helloworld import akka.actor.ActorSystem import akka.http.scaladsl.model.{ HttpRequest, HttpResponse } import akka.http.scaladsl.{ Http, HttpConnectionContext } import akka.stream.{ ActorMaterializer, Materializer } import com.typesafe.config.ConfigFactory import example.myapp.helloworld.grpc._ import scala.concurrent.{ ExecutionContext, Future } object GreeterServer { def main(args: Array[String]): Unit = { // Important: enable HTTP/2 in ActorSystem's config // We do it here programmatically, but you can also set it in the application.conf val conf = ConfigFactory .parseString("akka.http.server.preview.enable-http2 = on") .withFallback(ConfigFactory.defaultApplication()) val system = ActorSystem("HelloWorld", conf) new GreeterServer(system).run() // ActorSystem threads will keep the app alive until `system.terminate()` is called } } class GreeterServer(system: ActorSystem) { def run(): Future[Http.ServerBinding] = { // Akka boot up code implicit val sys: ActorSystem = system implicit val mat: Materializer = ActorMaterializer() implicit val ec: ExecutionContext = sys.dispatcher // Create service handlers val service: HttpRequest => Future[HttpResponse] = GreeterServiceHandler(new GreeterServiceImpl()) // Bind service handler servers to localhost:8080/8081 val binding = Http().bindAndHandleAsync( service, interface = "127.0.0.1", port = 8080, connectionContext = HttpConnectionContext()) // report successful binding binding.foreach { binding => println(s"gRPC server bound to: ${binding.localAddress}") } binding } } //#full-server
Example 91
Source File: PowerGreeterServer.scala From akka-grpc with Apache License 2.0 | 5 votes |
//#full-server package example.myapp.helloworld import akka.actor.ActorSystem import akka.http.scaladsl.model.{ HttpRequest, HttpResponse } import akka.http.scaladsl.{ Http, HttpConnectionContext } import akka.stream.{ ActorMaterializer, Materializer } import example.myapp.helloworld.grpc._ import scala.concurrent.{ ExecutionContext, Future } class PowerGreeterServer(system: ActorSystem) { def run(): Future[Http.ServerBinding] = { // Akka boot up code implicit val sys: ActorSystem = system implicit val mat: Materializer = ActorMaterializer() implicit val ec: ExecutionContext = sys.dispatcher // Create service handlers val service: HttpRequest => Future[HttpResponse] = GreeterServicePowerApiHandler(new PowerGreeterServiceImpl(mat)) // Bind service handler servers to localhost:8080/8081 val binding = Http().bindAndHandleAsync( service, interface = "127.0.0.1", port = 8081, connectionContext = HttpConnectionContext()) // report successful binding binding.foreach { binding => println(s"gRPC server bound to: ${binding.localAddress}") } binding } } //#full-server
Example 92
Source File: CombinedServer.scala From akka-grpc with Apache License 2.0 | 5 votes |
package example.myapp import scala.concurrent.ExecutionContext import scala.concurrent.Future import akka.actor.ActorSystem import akka.http.scaladsl.{ Http, HttpConnectionContext } import akka.http.scaladsl.model.HttpRequest import akka.http.scaladsl.model.HttpResponse import akka.grpc.scaladsl.ServerReflection import akka.stream.ActorMaterializer import akka.stream.Materializer import com.typesafe.config.ConfigFactory import example.myapp.helloworld._ import example.myapp.echo._ import example.myapp.echo.grpc._ import example.myapp.helloworld.grpc.GreeterService //#concatOrNotFound import akka.grpc.scaladsl.ServiceHandler //#concatOrNotFound //#grpc-web import akka.grpc.scaladsl.WebHandler //#grpc-web object CombinedServer { def main(args: Array[String]): Unit = { // important to enable HTTP/2 in ActorSystem's config val conf = ConfigFactory .parseString("akka.http.server.preview.enable-http2 = on") .withFallback(ConfigFactory.defaultApplication()) implicit val sys: ActorSystem = ActorSystem("HelloWorld", conf) implicit val mat: Materializer = ActorMaterializer() implicit val ec: ExecutionContext = sys.dispatcher //#concatOrNotFound // explicit types not needed but included in example for clarity val greeterService: PartialFunction[HttpRequest, Future[HttpResponse]] = example.myapp.helloworld.grpc.GreeterServiceHandler.partial(new GreeterServiceImpl()) val echoService: PartialFunction[HttpRequest, Future[HttpResponse]] = EchoServiceHandler.partial(new EchoServiceImpl) val reflectionService = ServerReflection.partial(List(GreeterService, EchoService)) val serviceHandlers: HttpRequest => Future[HttpResponse] = ServiceHandler.concatOrNotFound(greeterService, echoService, reflectionService) Http() .bindAndHandleAsync( serviceHandlers, interface = "127.0.0.1", port = 8080, connectionContext = HttpConnectionContext()) //#concatOrNotFound .foreach { binding => println(s"gRPC server bound to: ${binding.localAddress}") } //#grpc-web val grpcWebServiceHandlers = WebHandler.grpcWebHandler(greeterService, echoService) Http() .bindAndHandleAsync( grpcWebServiceHandlers, interface = "127.0.0.1", port = 8081, connectionContext = HttpConnectionContext()) //#grpc-web .foreach { binding => println(s"gRPC-Web server bound to: ${binding.localAddress}") } } }
Example 93
Source File: WebHandler.scala From akka-grpc with Apache License 2.0 | 5 votes |
package akka.grpc.scaladsl import scala.collection.immutable import scala.concurrent.Future import akka.actor.ClassicActorSystemProvider import akka.annotation.ApiMayChange import akka.http.javadsl.{ model => jmodel } import akka.http.scaladsl.model.{ HttpMethods, HttpRequest, HttpResponse } import akka.http.scaladsl.model.headers._ import akka.http.scaladsl.server.Route import akka.http.scaladsl.server.directives.MarshallingDirectives.handleWith import ch.megard.akka.http.cors.scaladsl.CorsDirectives.cors import ch.megard.akka.http.cors.scaladsl.model.HttpHeaderRange import ch.megard.akka.http.cors.scaladsl.settings.CorsSettings @ApiMayChange object WebHandler { def grpcWebHandler(handlers: PartialFunction[HttpRequest, Future[HttpResponse]]*)( implicit as: ClassicActorSystemProvider, corsSettings: CorsSettings = defaultCorsSettings): HttpRequest => Future[HttpResponse] = { implicit val system = as.classicSystem val servicesHandler = ServiceHandler.concat(handlers: _*) Route.asyncHandler(cors(corsSettings) { handleWith(servicesHandler) }) } }
Example 94
Source File: ServiceHandler.scala From akka-grpc with Apache License 2.0 | 5 votes |
package akka.grpc.scaladsl import akka.annotation.ApiMayChange import akka.grpc.GrpcProtocol import akka.grpc.internal.{ GrpcProtocolNative, GrpcProtocolWeb, GrpcProtocolWebText } import akka.http.javadsl.{ model => jmodel } import akka.http.scaladsl.model.{ HttpRequest, HttpResponse, StatusCodes } import scala.concurrent.Future @ApiMayChange object ServiceHandler { private[scaladsl] val notFound: Future[HttpResponse] = Future.successful(HttpResponse(StatusCodes.NotFound)) private[scaladsl] val unsupportedMediaType: Future[HttpResponse] = Future.successful(HttpResponse(StatusCodes.UnsupportedMediaType)) private def matchesVariant(variants: Set[GrpcProtocol])(request: jmodel.HttpRequest) = variants.exists(_.mediaTypes.contains(request.entity.getContentType.mediaType)) private[grpc] val isGrpcRequest: jmodel.HttpRequest => Boolean = matchesVariant(Set(GrpcProtocolNative)) private[grpc] val isGrpcWebRequest: jmodel.HttpRequest => Boolean = matchesVariant( Set(GrpcProtocolWeb, GrpcProtocolWebText)) def concatOrNotFound( handlers: PartialFunction[HttpRequest, Future[HttpResponse]]*): HttpRequest => Future[HttpResponse] = concat(handlers: _*).orElse { case _ => notFound } def concat(handlers: PartialFunction[HttpRequest, Future[HttpResponse]]*) : PartialFunction[HttpRequest, Future[HttpResponse]] = handlers.foldLeft(PartialFunction.empty[HttpRequest, Future[HttpResponse]]) { case (acc, pf) => acc.orElse(pf) } }
Example 95
Source File: ServerReflection.scala From akka-grpc with Apache License 2.0 | 5 votes |
package akka.grpc.scaladsl import akka.actor.ClassicActorSystemProvider import akka.annotation.ApiMayChange import akka.grpc.ServiceDescription import akka.grpc.internal.ServerReflectionImpl import akka.http.scaladsl.model.{ HttpRequest, HttpResponse } import grpc.reflection.v1alpha.reflection.ServerReflectionHandler @ApiMayChange(issue = "https://github.com/akka/akka-grpc/issues/850") object ServerReflection { @ApiMayChange(issue = "https://github.com/akka/akka-grpc/issues/850") def apply(objects: List[ServiceDescription])( implicit sys: ClassicActorSystemProvider): HttpRequest => scala.concurrent.Future[HttpResponse] = ServerReflectionHandler.apply(ServerReflectionImpl(objects.map(_.descriptor), objects.map(_.name))) @ApiMayChange(issue = "https://github.com/akka/akka-grpc/issues/850") def partial(objects: List[ServiceDescription])( implicit sys: ClassicActorSystemProvider): PartialFunction[HttpRequest, scala.concurrent.Future[HttpResponse]] = ServerReflectionHandler.partial(ServerReflectionImpl(objects.map(_.descriptor), objects.map(_.name))) }
Example 96
Source File: GrpcExceptionHandler.scala From akka-grpc with Apache License 2.0 | 5 votes |
package akka.grpc.scaladsl import akka.actor.ActorSystem import akka.actor.ClassicActorSystemProvider import akka.annotation.{ ApiMayChange, InternalStableApi } import akka.grpc.{ GrpcServiceException, Trailers } import akka.grpc.GrpcProtocol.GrpcProtocolWriter import akka.grpc.internal.{ GrpcResponseHelpers, MissingParameterException } import akka.http.scaladsl.model.HttpResponse import io.grpc.Status import scala.concurrent.{ ExecutionException, Future } @ApiMayChange object GrpcExceptionHandler { private val INTERNAL = Trailers(Status.INTERNAL) private val INVALID_ARGUMENT = Trailers(Status.INVALID_ARGUMENT) private val UNIMPLEMENTED = Trailers(Status.UNIMPLEMENTED) def defaultMapper(system: ActorSystem): PartialFunction[Throwable, Trailers] = { case e: ExecutionException => if (e.getCause == null) INTERNAL else defaultMapper(system)(e.getCause) case grpcException: GrpcServiceException => Trailers(grpcException.status, grpcException.metadata) case _: NotImplementedError => UNIMPLEMENTED case _: UnsupportedOperationException => UNIMPLEMENTED case _: MissingParameterException => INVALID_ARGUMENT case other => system.log.error(other, s"Unhandled error: [${other.getMessage}].") INTERNAL } @InternalStableApi def default( implicit system: ClassicActorSystemProvider, writer: GrpcProtocolWriter): PartialFunction[Throwable, Future[HttpResponse]] = from(defaultMapper(system.classicSystem)) @InternalStableApi def from(mapper: PartialFunction[Throwable, Trailers])( implicit system: ClassicActorSystemProvider, writer: GrpcProtocolWriter): PartialFunction[Throwable, Future[HttpResponse]] = mapper.orElse(defaultMapper(system.classicSystem)).andThen(s => Future.successful(GrpcResponseHelpers.status(s))) }
Example 97
Source File: GrpcResponseHelpers.scala From akka-grpc with Apache License 2.0 | 5 votes |
package akka.grpc.internal import akka.NotUsed import akka.actor.ActorSystem import akka.actor.ClassicActorSystemProvider import akka.annotation.InternalApi import akka.grpc.{ ProtobufSerializer, Trailers } import akka.grpc.GrpcProtocol.{ GrpcProtocolWriter, TrailerFrame } import akka.grpc.scaladsl.{ headers, GrpcExceptionHandler } import akka.http.scaladsl.model.{ HttpEntity, HttpResponse } import akka.http.scaladsl.model.HttpEntity.ChunkStreamPart import akka.stream.Materializer import akka.stream.scaladsl.Source import io.grpc.Status import scala.collection.immutable import scala.concurrent.{ ExecutionContext, Future } @InternalApi // consumed from generated classes so cannot be private object GrpcResponseHelpers { def apply[T](e: Source[T, NotUsed])( implicit m: ProtobufSerializer[T], writer: GrpcProtocolWriter, system: ClassicActorSystemProvider): HttpResponse = GrpcResponseHelpers(e, Source.single(GrpcEntityHelpers.trailer(Status.OK))) def apply[T](e: Source[T, NotUsed], eHandler: ActorSystem => PartialFunction[Throwable, Trailers])( implicit m: ProtobufSerializer[T], writer: GrpcProtocolWriter, system: ClassicActorSystemProvider): HttpResponse = GrpcResponseHelpers(e, Source.single(GrpcEntityHelpers.trailer(Status.OK)), eHandler) def apply[T](e: Source[T, NotUsed], status: Future[Status])( implicit m: ProtobufSerializer[T], mat: Materializer, writer: GrpcProtocolWriter, system: ClassicActorSystemProvider): HttpResponse = GrpcResponseHelpers(e, status, GrpcExceptionHandler.defaultMapper _) def apply[T]( e: Source[T, NotUsed], status: Future[Status], eHandler: ActorSystem => PartialFunction[Throwable, Trailers])( implicit m: ProtobufSerializer[T], mat: Materializer, writer: GrpcProtocolWriter, system: ClassicActorSystemProvider): HttpResponse = { implicit val ec: ExecutionContext = mat.executionContext GrpcResponseHelpers( e, Source.lazilyAsync(() => status.map(GrpcEntityHelpers.trailer)).mapMaterializedValue(_ => NotUsed), eHandler) } def apply[T]( e: Source[T, NotUsed], trail: Source[TrailerFrame, NotUsed], eHandler: ActorSystem => PartialFunction[Throwable, Trailers] = GrpcExceptionHandler.defaultMapper)( implicit m: ProtobufSerializer[T], writer: GrpcProtocolWriter, system: ClassicActorSystemProvider): HttpResponse = { response(GrpcEntityHelpers(e, trail, eHandler)) } private def response[T](entity: Source[ChunkStreamPart, NotUsed])(implicit writer: GrpcProtocolWriter) = { HttpResponse( headers = immutable.Seq(headers.`Message-Encoding`(writer.messageEncoding.name)), entity = HttpEntity.Chunked(writer.contentType, entity)) } def status(trailer: Trailers)(implicit writer: GrpcProtocolWriter): HttpResponse = response(Source.single(writer.encodeFrame(GrpcEntityHelpers.trailer(trailer.status, trailer.metadata)))) }
Example 98
Source File: GrpcExceptionHandlerSpec.scala From akka-grpc with Apache License 2.0 | 5 votes |
package akka.grpc.scaladsl import akka.actor.ActorSystem import akka.grpc.GrpcServiceException import akka.grpc.internal.{ GrpcProtocolNative, GrpcResponseHelpers, Identity } import akka.grpc.scaladsl.GrpcExceptionHandler.defaultMapper import akka.http.scaladsl.model.HttpEntity._ import akka.http.scaladsl.model.HttpResponse import akka.stream.ActorMaterializer import io.grpc.Status import org.scalatest._ import org.scalatest.concurrent.ScalaFutures import org.scalatest.matchers.should.Matchers import org.scalatest.time.{ Millis, Seconds, Span } import org.scalatest.wordspec.AnyWordSpec import scala.concurrent.{ ExecutionException, Future } class GrpcExceptionHandlerSpec extends AnyWordSpec with Matchers with ScalaFutures with BeforeAndAfterAll { implicit val system = ActorSystem("Test") implicit val materializer = ActorMaterializer() implicit override val patienceConfig = PatienceConfig(timeout = scaled(Span(2, Seconds)), interval = scaled(Span(5, Millis))) implicit val writer = GrpcProtocolNative.newWriter(Identity) val expected: Function[Throwable, Status] = { case e: ExecutionException => if (e.getCause == null) Status.INTERNAL else expected(e.getCause) case grpcException: GrpcServiceException => grpcException.status case _: NotImplementedError => Status.UNIMPLEMENTED case _: UnsupportedOperationException => Status.UNIMPLEMENTED case _ => Status.INTERNAL } val otherTypes: Seq[Throwable] = Seq( new GrpcServiceException(status = Status.DEADLINE_EXCEEDED), new NotImplementedError, new UnsupportedOperationException, new NullPointerException, new RuntimeException) val executionExceptions: Seq[Throwable] = otherTypes.map(new ExecutionException(_)) :+ new ExecutionException("doh", null) "defaultMapper" should { (otherTypes ++ executionExceptions).foreach { e => val exp = expected(e) s"Map $e to $exp" in { defaultMapper(system)(e).status shouldBe exp } } } "default(defaultMapper)" should { (otherTypes ++ executionExceptions).foreach { e => s"Correctly map $e" in { val exp = GrpcResponseHelpers.status(defaultMapper(system)(e)) val expChunks = getChunks(exp) val act = GrpcExceptionHandler.from(defaultMapper(system))(system, writer)(e).futureValue val actChunks = getChunks(act) // Following is because aren't equal act.status shouldBe exp.status actChunks.toString shouldEqual expChunks.toString } } } def getChunks(resp: HttpResponse): Seq[ChunkStreamPart] = (resp.entity match { case Chunked(_, chunks) => chunks.runFold(Seq.empty[ChunkStreamPart]) { case (seq, chunk) => seq :+ chunk } case _ => Future.successful(Seq.empty[ChunkStreamPart]) }).futureValue override def afterAll(): Unit = { super.afterAll() system.terminate() } }
Example 99
Source File: AkkaGrpcServerScala.scala From akka-grpc with Apache License 2.0 | 5 votes |
package akka.grpc.interop import java.io.FileInputStream import java.nio.file.{ Files, Paths } import java.security.cert.CertificateFactory import java.security.spec.PKCS8EncodedKeySpec import java.security.{ KeyFactory, KeyStore, SecureRandom } import scala.concurrent.duration._ import akka.actor.ActorSystem import akka.util.ByteString import akka.http.scaladsl.Http.ServerBinding import akka.http.scaladsl.model.{ HttpRequest, HttpResponse } import akka.http.scaladsl.{ Http2, HttpsConnectionContext } import akka.stream.SystemMaterializer import io.grpc.internal.testing.TestUtils import javax.net.ssl.{ KeyManagerFactory, SSLContext } import scala.concurrent.{ Await, Future } case class AkkaGrpcServerScala(serverHandlerFactory: ActorSystem => HttpRequest => Future[HttpResponse]) extends GrpcServer[(ActorSystem, ServerBinding)] { override def start() = { implicit val sys = ActorSystem() implicit val mat = SystemMaterializer(sys).materializer val testService = serverHandlerFactory(sys) val bindingFuture = Http2().bindAndHandleAsync( testService, interface = "127.0.0.1", port = 0, parallelism = 256, // TODO remove once https://github.com/akka/akka-http/pull/2146 is merged connectionContext = serverHttpContext()) val binding = Await.result(bindingFuture, 10.seconds) (sys, binding) } override def stop(binding: (ActorSystem, ServerBinding)) = binding match { case (sys, binding) => sys.log.info("Exception thrown, unbinding") Await.result(binding.unbind(), 10.seconds) Await.result(sys.terminate(), 10.seconds) } private def serverHttpContext() = { val keyEncoded = new String(Files.readAllBytes(Paths.get(TestUtils.loadCert("server1.key").getAbsolutePath)), "UTF-8") .replace("-----BEGIN PRIVATE KEY-----\n", "") .replace("-----END PRIVATE KEY-----\n", "") .replace("\n", "") val decodedKey = ByteString(keyEncoded).decodeBase64.toArray val spec = new PKCS8EncodedKeySpec(decodedKey) val kf = KeyFactory.getInstance("RSA") val privateKey = kf.generatePrivate(spec) val fact = CertificateFactory.getInstance("X.509") val is = new FileInputStream(TestUtils.loadCert("server1.pem")) val cer = fact.generateCertificate(is) val ks = KeyStore.getInstance("PKCS12") ks.load(null) ks.setKeyEntry("private", privateKey, Array.empty, Array(cer)) val keyManagerFactory = KeyManagerFactory.getInstance("SunX509") keyManagerFactory.init(ks, null) val context = SSLContext.getInstance("TLS") context.init(keyManagerFactory.getKeyManagers, null, new SecureRandom) new HttpsConnectionContext(context) } override def getPort(binding: (ActorSystem, ServerBinding)): Int = binding._2.localAddress.getPort }
Example 100
Source File: AkkaHttpClient.scala From graphql-gateway with Apache License 2.0 | 5 votes |
package sangria.gateway.http.client import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model.HttpHeader.ParsingResult import akka.http.scaladsl.model._ import akka.http.scaladsl.model.Uri.Query import akka.http.scaladsl.model.headers.Location import akka.http.scaladsl.unmarshalling.Unmarshal import akka.stream.Materializer import akka.util.ByteString import sangria.gateway.util.Logging import scala.concurrent.{ExecutionContext, Future} class AkkaHttpClient(implicit system: ActorSystem, mat: Materializer, ec: ExecutionContext) extends HttpClient with Logging { import AkkaHttpClient._ import HttpClient._ override def request(method: Method.Value, url: String, queryParams: Seq[(String, String)] = Seq.empty, headers: Seq[(String, String)] = Seq.empty, body: Option[(String, String)] = None) = { val m = mapMethod(method) val query = Query(queryParams: _*) val hs = headers.map(header) val uri = Uri(url).withQuery(query) val entity = body.fold(HttpEntity.Empty){case (tpe, content) ⇒ HttpEntity(contentType(tpe), ByteString(content))} val request = HttpRequest(m, uri, hs.toVector, entity) val client = Http().singleRequest(_: HttpRequest) val richClient = RichHttpClient.httpClientWithRedirect(client) logger.debug(s"Http request: ${m.value} $url") richClient(request).map(AkkaHttpResponse(m, url, _)) } override def oauthClientCredentials(url: String, clientId: String, clientSecret: String, scopes: Seq[String]): Future[HttpResponse] = throw new IllegalStateException("Not yet implemented, please use play implementation.") private def contentType(str: String) = ContentType.parse(str).fold( errors ⇒ throw ClientError(s"Invalid content type '$str'", errors.map(_.detail)), identity) private def header(nameValue: (String, String)) = HttpHeader.parse(nameValue._1, nameValue._2) match { case ParsingResult.Ok(_, errors) if errors.nonEmpty ⇒ throw ClientError(s"Invalid header '${nameValue._1}'", errors.map(_.detail)) case ParsingResult.Error(error) ⇒ throw ClientError(s"Invalid header '${nameValue._1}'", Seq(error.detail)) case ParsingResult.Ok(h, _) ⇒ h } def mapMethod(method: Method.Value) = method match { case Method.Get ⇒ HttpMethods.GET case Method.Post ⇒ HttpMethods.POST } object RichHttpClient { import akka.http.scaladsl.model.HttpResponse type HttpClient = HttpRequest ⇒ Future[HttpResponse] def redirectOrResult(client: HttpClient)(response: HttpResponse): Future[HttpResponse] = response.status match { case StatusCodes.Found | StatusCodes.MovedPermanently | StatusCodes.SeeOther ⇒ val newUri = response.header[Location].get.uri // Always make sure you consume the response entity streams (of type Source[ByteString,Unit]) by for example connecting it to a Sink (for example response.discardEntityBytes() if you don’t care about the response entity), since otherwise Akka HTTP (and the underlying Streams infrastructure) will understand the lack of entity consumption as a back-pressure signal and stop reading from the underlying TCP connection! response.discardEntityBytes() logger.debug(s"Http redirect: ${HttpMethods.GET.value} $newUri") client(HttpRequest(method = HttpMethods.GET, uri = newUri)) case _ ⇒ Future.successful(response) } def httpClientWithRedirect(client: HttpClient): HttpClient = { lazy val redirectingClient: HttpClient = req ⇒ client(req).flatMap(redirectOrResult(redirectingClient)) // recurse to support multiple redirects redirectingClient } } case class ClientError(message: String, errors: Seq[String]) extends Exception(message + ":\n" + errors.map(" * " + _).mkString("\n")) } object AkkaHttpClient { case class AkkaHttpResponse(method: HttpMethod, url: String, response: HttpResponse)(implicit mat: Materializer) extends HttpClient.HttpResponse { def asString = Unmarshal(response).to[String] def statusCode = response.status.intValue() def isSuccessful = response.status.isSuccess() def debugInfo = s"${method.value} $url" } }
Example 101
Source File: AkkaHttpActionAdapter.scala From akka-http-pac4j with Mozilla Public License 2.0 | 5 votes |
package com.stackstate.pac4j.http import akka.http.scaladsl.model.{HttpEntity, HttpHeader, HttpResponse, StatusCodes, Uri} import org.pac4j.core.context.HttpConstants import org.pac4j.core.http.adapter.HttpActionAdapter import akka.http.scaladsl.model.StatusCodes._ import akka.http.scaladsl.model.headers.Location import akka.http.scaladsl.server.RouteResult import akka.http.scaladsl.server.RouteResult.Complete import com.stackstate.pac4j.AkkaHttpWebContext import org.pac4j.core.exception.http.{ BadRequestAction, ForbiddenAction, FoundAction, HttpAction, NoContentAction, OkAction, SeeOtherAction, TemporaryRedirectAction, UnauthorizedAction } import scala.concurrent.Future object AkkaHttpActionAdapter extends HttpActionAdapter[Future[RouteResult], AkkaHttpWebContext] { override def adapt(action: HttpAction, context: AkkaHttpWebContext): Future[Complete] = { Future.successful(Complete(action match { case _: UnauthorizedAction => // XHR requests don't receive a TEMP_REDIRECT but a UNAUTHORIZED. The client can handle this // to trigger the proper redirect anyway, but for a correct flow the session cookie must be set context.addResponseSessionCookie() HttpResponse(Unauthorized) case _: BadRequestAction => HttpResponse(BadRequest) case _ if action.getCode == HttpConstants.CREATED => HttpResponse(Created) case _: ForbiddenAction => HttpResponse(Forbidden) case a: FoundAction => context.addResponseSessionCookie() HttpResponse(SeeOther, headers = List[HttpHeader](Location(Uri(a.getLocation)))) case a: SeeOtherAction => context.addResponseSessionCookie() HttpResponse(SeeOther, headers = List[HttpHeader](Location(Uri(a.getLocation)))) case a: OkAction => val contentBytes = a.getContent.getBytes val entity = context.getContentType.map(ct => HttpEntity(ct, contentBytes)).getOrElse(HttpEntity(contentBytes)) HttpResponse(OK, entity = entity) case _: NoContentAction => HttpResponse(NoContent) case _ if action.getCode == 500 => HttpResponse(InternalServerError) case _ => HttpResponse(StatusCodes.getForKey(action.getCode).getOrElse(custom(action.getCode, ""))) })) } }
Example 102
Source File: AkkaHttpActionAdapterTest.scala From akka-http-pac4j with Mozilla Public License 2.0 | 5 votes |
package com.stackstate.pac4j import akka.http.scaladsl.model.{ContentTypes, HttpEntity, HttpRequest, HttpResponse} import org.scalatest.{Matchers, WordSpecLike} import akka.http.scaladsl.model.StatusCodes._ import akka.util.ByteString import com.stackstate.pac4j.AkkaHttpActionAdapterTest.ActionInt import com.stackstate.pac4j.http.AkkaHttpActionAdapter import com.stackstate.pac4j.store.ForgetfulSessionStorage import org.pac4j.core.exception.http.{ BadRequestAction, ForbiddenAction, FoundAction, HttpAction, NoContentAction, OkAction, StatusAction, UnauthorizedAction } import org.scalatest.concurrent.ScalaFutures class AkkaHttpActionAdapterTest extends WordSpecLike with Matchers with ScalaFutures { "AkkaHttpActionAdapter" should { "convert 200 to OK" in withContext { context => AkkaHttpActionAdapter.adapt(new OkAction(""), context).futureValue.response shouldEqual HttpResponse( OK, Nil, HttpEntity(ContentTypes.`application/octet-stream`, ByteString("")) ) } "convert 401 to Unauthorized" in withContext { context => AkkaHttpActionAdapter.adapt(UnauthorizedAction.INSTANCE, context).futureValue.response shouldEqual HttpResponse(Unauthorized) context.getChanges.cookies.map(_.name) shouldBe List(AkkaHttpWebContext.DEFAULT_COOKIE_NAME) } "convert 302 to SeeOther (to support login flow)" in withContext { context => val r = AkkaHttpActionAdapter.adapt(new FoundAction("/login"), context).futureValue.response r.status shouldEqual SeeOther r.headers.head.value() shouldEqual "/login" context.getChanges.cookies.map(_.name) shouldBe List(AkkaHttpWebContext.DEFAULT_COOKIE_NAME) } "convert 400 to BadRequest" in withContext { context => AkkaHttpActionAdapter.adapt(BadRequestAction.INSTANCE, context).futureValue.response shouldEqual HttpResponse(BadRequest) } "convert 201 to Created" in withContext { context => AkkaHttpActionAdapter.adapt(201.action(), context).futureValue.response shouldEqual HttpResponse(Created) } "convert 403 to Forbidden" in withContext { context => AkkaHttpActionAdapter.adapt(ForbiddenAction.INSTANCE, context).futureValue.response shouldEqual HttpResponse(Forbidden) } "convert 204 to NoContent" in withContext { context => AkkaHttpActionAdapter.adapt(NoContentAction.INSTANCE, context).futureValue.response shouldEqual HttpResponse(NoContent) } "convert 200 to OK with content set from the context" in withContext { context => AkkaHttpActionAdapter.adapt(new OkAction("content"), context).futureValue.response shouldEqual HttpResponse .apply(OK, Nil, HttpEntity(ContentTypes.`application/octet-stream`, ByteString("content"))) } "convert 200 to OK with content type set from the context" in withContext { context => context.setResponseContentType("application/json") AkkaHttpActionAdapter.adapt(new OkAction(""), context).futureValue.response shouldEqual HttpResponse .apply(OK, Nil, HttpEntity(ContentTypes.`application/json`, ByteString(""))) } } def withContext(f: AkkaHttpWebContext => Unit): Unit = { f(AkkaHttpWebContext(HttpRequest(), Seq.empty, new ForgetfulSessionStorage, AkkaHttpWebContext.DEFAULT_COOKIE_NAME)) } } object AkkaHttpActionAdapterTest { implicit class ActionInt(val i: Int) extends AnyVal { def action(): HttpAction = new StatusAction(i) } }
Example 103
Source File: HealthEndpoint.scala From akka-http-health with MIT License | 5 votes |
package io.github.lhotari.akka.http.health import java.util.concurrent.atomic.AtomicBoolean import akka.http.scaladsl.model.{ContentTypes, HttpEntity, HttpResponse, StatusCodes} import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server.Route import scala.concurrent.{ExecutionContext, Future} trait HealthEndpoint { protected lazy val checkers = createCheckers protected def createCheckers = { Seq(new LowDiskSpaceDetector(), new LowMemoryDetector()) } private lazy val successResponse: HttpResponse = createSuccessResponse protected def decorateResponse(response: HttpResponse) = response protected def createSuccessResponse = { decorateResponse(HttpResponse(entity = HttpEntity(ContentTypes.`text/plain(UTF-8)`, "OK"))) } private lazy val errorResponse: HttpResponse = createErrorResponse protected def createErrorResponse = { decorateResponse(HttpResponse(status = StatusCodes.ServiceUnavailable)) } private val started = new AtomicBoolean(false) def createHealthRoute(endpoint: String = HealthEndpoint.DefaultEndpoint)(implicit executor: ExecutionContext): Route = get { path(endpoint) { completeHealthCheck } } def completeHealthCheck(implicit executor: ExecutionContext) = { complete { Future { if (isHealthy()) successResponse else errorResponse } } } def start(): Unit = { if (started.compareAndSet(false, true)) { checkers.foreach(_.start()) } } def stop(): Unit = { if (started.compareAndSet(true, false)) { checkers.foreach(_.stop()) } } def isHealthy() = { start() checkers.forall(_.isHealthy()) } } object HealthEndpoint extends HealthEndpoint { lazy val defaultHealthChecker = new HealthEndpoint {} val DefaultEndpoint: String = "health" def createDefaultHealthRoute(endpoint: String = DefaultEndpoint)(implicit executor: ExecutionContext): Route = { defaultHealthChecker.createHealthRoute(endpoint) } }
Example 104
Source File: QueryValidationApi.scala From NSDb with Apache License 2.0 | 5 votes |
package io.radicalbit.nsdb.web.routes import akka.actor.ActorRef import akka.event.LoggingAdapter import akka.http.scaladsl.model.HttpResponse import akka.http.scaladsl.model.StatusCodes._ import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server.Route import akka.pattern.ask import akka.util.Timeout import io.radicalbit.nsdb.common.statement.SelectSQLStatement import io.radicalbit.nsdb.protocol.MessageProtocol.Commands.ValidateStatement import io.radicalbit.nsdb.protocol.MessageProtocol.Events._ import io.radicalbit.nsdb.security.http.NSDBAuthProvider import io.radicalbit.nsdb.security.model.Metric import io.radicalbit.nsdb.sql.parser.SQLStatementParser import io.radicalbit.nsdb.sql.parser.StatementParserResult._ import io.swagger.annotations._ import javax.ws.rs.Path import org.json4s.Formats import scala.annotation.meta.field import scala.util.{Failure, Success} @ApiModel(description = "Query Validation body") case class QueryValidationBody(@(ApiModelProperty @field)(value = "database name ") db: String, @(ApiModelProperty @field)(value = "namespace name ") namespace: String, @(ApiModelProperty @field)(value = "metric name ") metric: String, @(ApiModelProperty @field)(value = "sql query string") queryString: String) extends Metric @Api(value = "/query/validate", produces = "application/json") @Path("/query/validate") trait QueryValidationApi { import io.radicalbit.nsdb.web.NSDbJson._ def readCoordinator: ActorRef def authenticationProvider: NSDBAuthProvider implicit val timeout: Timeout implicit val formats: Formats @ApiOperation(value = "Perform query", nickname = "query", httpMethod = "POST", response = classOf[String]) @ApiImplicitParams( Array( new ApiImplicitParam(name = "body", value = "query definition", required = true, dataTypeClass = classOf[QueryValidationBody], paramType = "body") )) @ApiResponses( Array( new ApiResponse(code = 200, message = "Query is valid"), new ApiResponse(code = 404, message = "Not found item reason"), new ApiResponse(code = 400, message = "statement is invalid") )) def queryValidationApi(implicit logger: LoggingAdapter): Route = { path("query" / "validate") { post { entity(as[QueryValidationBody]) { qb => optionalHeaderValueByName(authenticationProvider.headerName) { header => authenticationProvider.authorizeMetric(ent = qb, header = header, writePermission = false) { new SQLStatementParser().parse(qb.db, qb.namespace, qb.queryString) match { case SqlStatementParserSuccess(_, statement: SelectSQLStatement) => onComplete(readCoordinator ? ValidateStatement(statement)) { case Success(SelectStatementValidated(_)) => complete(HttpResponse(OK)) case Success(SelectStatementValidationFailed(_, reason, MetricNotFound(_))) => complete(HttpResponse(NotFound, entity = reason)) case Success(SelectStatementValidationFailed(_, reason, _)) => complete(HttpResponse(BadRequest, entity = reason)) case Success(r) => logger.error("unknown response received {}", r) complete(HttpResponse(InternalServerError, entity = "unknown response")) case Failure(ex) => logger.error("", ex) complete(HttpResponse(InternalServerError, entity = ex.getMessage)) } case SqlStatementParserSuccess(queryString, _) => complete(HttpResponse(BadRequest, entity = s"statement ${queryString} is not a select statement")) case SqlStatementParserFailure(queryString, _) => complete(HttpResponse(BadRequest, entity = s"statement ${queryString} is invalid")) } } } } } } } }
Example 105
Source File: DataApi.scala From NSDb with Apache License 2.0 | 5 votes |
package io.radicalbit.nsdb.web.routes import javax.ws.rs.Path import akka.actor.ActorRef import akka.pattern.ask import akka.http.scaladsl.model.HttpResponse import akka.http.scaladsl.model.StatusCodes.{BadRequest, InternalServerError} import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server.Route import akka.util.Timeout import io.radicalbit.nsdb.common.protocol.Bit import io.radicalbit.nsdb.protocol.MessageProtocol.Commands.MapInput import io.radicalbit.nsdb.protocol.MessageProtocol.Events.{InputMapped, RecordRejected} import io.radicalbit.nsdb.security.http.NSDBAuthProvider import io.radicalbit.nsdb.security.model.Metric import io.swagger.annotations._ import org.json4s.Formats import scala.annotation.meta.field import scala.util.{Failure, Success} @ApiModel(description = "Data insertion body") case class InsertBody(@(ApiModelProperty @field)(value = "database name") db: String, @(ApiModelProperty @field)(value = "namespace name") namespace: String, @(ApiModelProperty @field)(value = "metric name") metric: String, @(ApiModelProperty @field)( value = "bit representing a single row" ) bit: Bit) extends Metric @Api(value = "/data", produces = "application/json") @Path("/data") trait DataApi { import io.radicalbit.nsdb.web.NSDbJson._ import io.radicalbit.nsdb.web.validation.ValidationDirective._ import io.radicalbit.nsdb.web.validation.Validators._ def writeCoordinator: ActorRef def authenticationProvider: NSDBAuthProvider implicit val timeout: Timeout implicit val formats: Formats @ApiOperation(value = "Insert Bit", nickname = "insert", httpMethod = "POST", response = classOf[String]) @ApiImplicitParams( Array( new ApiImplicitParam(name = "body", value = "bit definition", required = true, dataTypeClass = classOf[InsertBody], paramType = "body") )) @ApiResponses( Array( new ApiResponse(code = 500, message = "Internal server error"), new ApiResponse(code = 400, message = "insert statement is invalid") )) def dataApi: Route = pathPrefix("data") { post { entity(as[InsertBody]) { insertBody => optionalHeaderValueByName(authenticationProvider.headerName) { header => validateModel(insertBody).apply { validatedInsertBody => authenticationProvider.authorizeMetric(ent = validatedInsertBody, header = header, writePermission = true) { onComplete( writeCoordinator ? MapInput(validatedInsertBody.bit.timestamp, validatedInsertBody.db, validatedInsertBody.namespace, validatedInsertBody.metric, validatedInsertBody.bit)) { case Success(_: InputMapped) => complete("OK") case Success(RecordRejected(_, _, _, _, _, reasons, _)) => complete(HttpResponse(BadRequest, entity = reasons.mkString(","))) case Success(_) => complete(HttpResponse(InternalServerError, entity = "unknown response")) case Failure(ex) => complete(HttpResponse(InternalServerError, entity = ex.getMessage)) } } } } } } } }
Example 106
Source File: HttpResponseAssert.scala From asura with MIT License | 5 votes |
package asura.core.assertion.engine import akka.http.scaladsl.model.HttpResponse import asura.core.http.{HeaderUtils, HttpRequestReportModel, HttpResponseReportModel, HttpResult} import asura.core.runtime.RuntimeContext import asura.core.util.JsonPathUtils import scala.concurrent.Future object HttpResponseAssert { val KEY_STATUS = "status" val KEY_HEADERS = "headers" val KEY_ENTITY = "entity" def generateHttpReport( docId: String, assert: Map[String, Any], response: HttpResponse, entity: String, caseRequest: HttpRequestReportModel, caseContext: RuntimeContext ): Future[HttpResult] = { var isJson = false caseContext.setCurrentStatus(response.status.intValue()) val headers = new java.util.HashMap[String, String]() response.headers.foreach(header => { headers.put(header.name(), header.value()) if (HeaderUtils.isApplicationJson(header)) { isJson = true } }) caseContext.setCurrentHeaders(headers) if (isJson) { val entityDoc = JsonPathUtils.parse(entity) caseContext.setCurrentEntity(entityDoc) } else { try { val entityDoc = JsonPathUtils.parse(entity) caseContext.setCurrentEntity(entityDoc) } catch { case _: Throwable => caseContext.setCurrentEntity(entity) } } import scala.collection.JavaConverters.mapAsScalaMap val caseResponse = HttpResponseReportModel( response.status.intValue(), response.status.reason(), mapAsScalaMap(headers), { val mediaType = response.entity.getContentType().mediaType s"${mediaType.mainType}/${mediaType.subType}" }, entity ) HttpResult.eval(docId, assert, caseContext, caseRequest, caseResponse) } }
Example 107
Source File: package.scala From squbs with Apache License 2.0 | 5 votes |
package org.squbs import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model.{HttpRequest, HttpResponse, Uri} import akka.stream.ActorMaterializer import akka.util.ByteString import scala.concurrent.Future package object testkit { case object TestPing case object TestPong def entityAsString(uri: String)(implicit am: ActorMaterializer, system: ActorSystem): Future[String] = { import system.dispatcher get(uri) flatMap extractEntityAsString } def get(uri: String)(implicit am: ActorMaterializer, system: ActorSystem): Future[HttpResponse] = { Http().singleRequest(HttpRequest(uri = Uri(uri))) } def extractEntityAsString(response: HttpResponse) (implicit am: ActorMaterializer, system: ActorSystem): Future[String] = { import system.dispatcher response.entity.dataBytes.runFold(ByteString(""))(_ ++ _) map(_.utf8String) } }
Example 108
Source File: RequestContextSpec.scala From squbs with Apache License 2.0 | 5 votes |
package org.squbs.pipeline import akka.actor.ActorSystem import akka.http.scaladsl.model.headers.RawHeader import akka.http.scaladsl.model.{HttpRequest, HttpResponse} import akka.testkit.TestKit import org.scalatest.OptionValues._ import org.scalatest.{BeforeAndAfterAll, FlatSpecLike, Matchers} import scala.util.Try class RequestContextSpec extends TestKit(ActorSystem("RequestContextSpecSys")) with FlatSpecLike with Matchers with BeforeAndAfterAll { override def afterAll(): Unit = { system.terminate() } it should "handle attributes correctly (withAttributes/removeAttributes/expanded variable args)" in { val attributeAdditions = List( "key1" -> "val1", "key2" -> 1, "key3" -> new Exception("Bad Val") ) val rc1 = RequestContext(HttpRequest(), 0) val rc2 = rc1.withAttributes(attributeAdditions:_*) rc1.attributes should have size 0 rc2.attributes should equal(attributeAdditions.toMap) val addedKeys = attributeAdditions.map(_._1).toSet val removeKeys = Set(addedKeys.head, addedKeys.last) val rc3 = rc2.removeAttributes("ibetternotexist" +: removeKeys.toList:_*) rc3.attributes.keys.toSet should equal(addedKeys -- removeKeys) } it should "handle attributes correctly (withAttributes/variable args)" in { val rc = RequestContext(HttpRequest(), 0) rc.withAttributes( "key1" -> "val1", "key2" -> 1 ).attributes should equal(Map("key1" -> "val1", "key2" -> 1)) rc.attributes should have size 0 } it should "handle attributes correctly (withAttribute/removeAttribute)" in { val rc1 = RequestContext(HttpRequest(), 0) rc1.attributes should have size 0 val rc2 = rc1.withAttribute("key1", "val1") rc1.attributes should have size 0 // Immutability rc2.attribute("key1") should be(Some("val1")) val rc3 = rc2.withAttribute("key2", 1).withAttribute("key3", new Exception("Bad Val")) rc3.attribute("key2") should be(Some(1)) rc3.attribute[Exception]("key3").value.getMessage should be("Bad Val") rc3.attribute("key1") should be(Some("val1")) rc3.attributes should have size 3 val rc4 = rc3.removeAttribute("key1") rc3.attributes should have size 3 // Immutability rc4.attributes should have size 2 val rc5 = rc4.removeAttribute("notexists") rc5.attributes should have size 2 rc5.attribute("key2") should be(Some(1)) rc5.attribute[Exception]("key3").value.getMessage should be("Bad Val") } it should "handle headers correctly" in { val rc1 = RequestContext(HttpRequest(), 0) val rc2 = rc1.withRequestHeader(RawHeader("key1", "val1")) val rc3 = rc2.withRequestHeaders(RawHeader("key2", "val2"), RawHeader("key3", "val3")) rc3.request.headers should have size 3 rc3.response should be(None) rc3.request.headers should contain(RawHeader("key1", "val1")) val rc4 = rc3.withResponse(Try(HttpResponse())) rc4.response.value.get.headers should have size 0 val rc5 = rc4.withResponseHeader(RawHeader("key4", "val4")) val rc6 = rc5.withResponseHeaders(RawHeader("key5", "val5"), RawHeader("key6", "val6")) val rc7 = rc6.withResponseHeader(RawHeader("key7", "val7")) rc7.request.headers should have size 3 rc7.response.value.get.headers should have size 4 rc7.response.value.get.headers should contain(RawHeader("key4", "val4")) } }
Example 109
Source File: ConfigurableInitTimeActor.scala From squbs with Apache License 2.0 | 5 votes |
package org.squbs.unicomplex.timeout import akka.NotUsed import akka.http.scaladsl.model.{HttpRequest, HttpResponse, StatusCodes} import akka.stream.scaladsl.Flow import org.slf4j.LoggerFactory import org.squbs.unicomplex.FlowDefinition object ConfigurableInitTimeActor { def log = LoggerFactory.getLogger(classOf[ConfigurableInitTimeActor]) } class ConfigurableInitTimeActor extends FlowDefinition { import ConfigurableInitTimeActor.log override def flow: Flow[HttpRequest, HttpResponse, NotUsed] = { val system = this.context.system val initTime = Option(system.settings.config.getDuration("squbs.test.actor-init-time")) .get log.info(s"I'll be ready to go in $initTime") Thread.sleep(initTime.toMillis) log.info("Ready to work!") Flow[HttpRequest].map { r => HttpResponse(StatusCodes.OK, entity = "Hello") } } }
Example 110
Source File: JavaConverters.scala From squbs with Apache License 2.0 | 5 votes |
package akka.http.org.squbs.util import java.util.Optional import akka.NotUsed import akka.http.impl.util.JavaMapping import akka.http.javadsl.{model => jm} import akka.http.scaladsl.Http.HostConnectionPool import akka.http.scaladsl.HttpsConnectionContext import akka.http.scaladsl.model.{HttpRequest, HttpResponse} import akka.http.scaladsl.settings.ConnectionPoolSettings import akka.http.{javadsl => jd} import akka.japi.Pair import akka.stream.scaladsl.{BidiFlow, Flow} import akka.stream.{javadsl => js} import scala.util.Try object JavaConverters { def fromJava(connectionContext: Optional[jd.HttpsConnectionContext], settings: Optional[jd.settings.ConnectionPoolSettings]): (Option[HttpsConnectionContext], Option[ConnectionPoolSettings]) = { import scala.compat.java8.OptionConverters._ val cCtx = connectionContext.asScala.asInstanceOf[Option[HttpsConnectionContext]] val sSettings = settings.asScala.asInstanceOf[Option[ConnectionPoolSettings]] (cCtx, sSettings) } def toJava[In1, Out1, In2, Out2, Context](bidiFlow: BidiFlow[(In1, Context), (Out1, Context), (In2, Context), (Out2, Context), NotUsed]): js.BidiFlow[Pair[In1, Context], Pair[Out1, Context], Pair[In2, Context], Pair[Out2, Context], NotUsed] = { implicit val sIn1Mapping = JavaMapping.identity[In1] implicit val sOut1Mapping = JavaMapping.identity[Out1] implicit val sIn2Mapping = JavaMapping.identity[In2] implicit val sOut2Mapping = JavaMapping.identity[Out2] implicit val contextMapping = JavaMapping.identity[Context] val javaToScalaAdapter = JavaMapping.adapterBidiFlow[Pair[In1, Context], (In1, Context), (Out2, Context), Pair[Out2, Context]] val scalaToJavaAdapter = JavaMapping.adapterBidiFlow[Pair[In2, Context], (In2, Context), (Out1, Context), Pair[Out1, Context]].reversed javaToScalaAdapter.atop(bidiFlow).atop(scalaToJavaAdapter).asJava } private def adaptTupleFlow[T](scalaFlow: Flow[(HttpRequest, T), (Try[HttpResponse], T), HostConnectionPool]): js.Flow[Pair[jm.HttpRequest, T], Pair[Try[jm.HttpResponse], T], jd.HostConnectionPool] = { implicit val _ = JavaMapping.identity[T] implicit object HostConnectionPoolMapping extends JavaMapping[jd.HostConnectionPool, HostConnectionPool] { def toScala(javaObject: jd.HostConnectionPool): HostConnectionPool = throw new UnsupportedOperationException("jd.HostConnectionPool cannot be converted to Scala") def toJava(scalaObject: HostConnectionPool): jd.HostConnectionPool = scalaObject.toJava } JavaMapping.toJava(scalaFlow)(JavaMapping.flowMapping[Pair[jm.HttpRequest, T], (HttpRequest, T), Pair[Try[jm.HttpResponse], T], (Try[HttpResponse], T), jd.HostConnectionPool, HostConnectionPool]) } def toJava[T](flow: Flow[(HttpRequest, T), (Try[HttpResponse], T), HostConnectionPool]): js.Flow[Pair[jm.HttpRequest, T], Pair[Try[jm.HttpResponse], T], jd.HostConnectionPool] = { adaptTupleFlow[T](flow) } def toScala(uri: akka.http.javadsl.model.Uri) = JavaMapping.toScala(uri) }
Example 111
Source File: MetronomeFacade.scala From metronome with Apache License 2.0 | 5 votes |
package dcos.metronome.integrationtest.utils import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.client.RequestBuilding.{Get, Post} import akka.http.scaladsl.model.{ContentTypes, HttpEntity, HttpRequest, HttpResponse} import akka.stream.Materializer import com.mesosphere.utils.http.RestResult import scala.concurrent.Await.result import scala.concurrent.Future import scala.concurrent.duration.{FiniteDuration, _} class MetronomeFacade(val url: String, implicit val waitTime: FiniteDuration = 30.seconds)(implicit val system: ActorSystem, mat: Materializer ) { import scala.concurrent.ExecutionContext.Implicits.global //info -------------------------------------------------- def info(): RestResult[HttpResponse] = { result(request(Get(s"$url/info")), waitTime) } def createJob(jobDef: String): RestResult[HttpResponse] = { val e = HttpEntity(ContentTypes.`application/json`, jobDef) result(request(Post(s"$url/v1/jobs", e)), waitTime) } def startRun(jobId: String): RestResult[HttpResponse] = { val e = HttpEntity(ContentTypes.`application/json`, "") result(request(Post(s"$url/v1/jobs/${jobId}/runs", e)), waitTime) } def getJob(jobId: String): RestResult[HttpResponse] = { result(request(Get(s"$url/v1/jobs/${jobId}")), waitTime) } def getJobs(): RestResult[HttpResponse] = { result(request(Get(s"$url/v1/jobs")), waitTime) } def getRuns(jobId: String): RestResult[HttpResponse] = { result(request(Get(s"$url/v1/jobs/${jobId}/runs")), waitTime) } private[this] def request(request: HttpRequest): Future[RestResult[HttpResponse]] = { Http(system).singleRequest(request).flatMap { response => response.entity.toStrict(waitTime).map(_.data.decodeString("utf-8")).map(RestResult(response, _)) } } }
Example 112
Source File: AkkaHttpClient.scala From sttp with Apache License 2.0 | 5 votes |
package sttp.client.akkahttp import akka.actor.ActorSystem import akka.event.LoggingAdapter import akka.http.scaladsl.model.ws.{Message, WebSocketRequest, WebSocketUpgradeResponse} import akka.http.scaladsl.model.{HttpRequest, HttpResponse} import akka.http.scaladsl.server.{ExceptionHandler, RejectionHandler, Route, RoutingLog} import akka.http.scaladsl.settings.{ClientConnectionSettings, ConnectionPoolSettings, ParserSettings, RoutingSettings} import akka.http.scaladsl.{Http, HttpsConnectionContext} import akka.stream.Materializer import akka.stream.scaladsl.Flow import scala.concurrent.{ExecutionContext, ExecutionContextExecutor, Future} trait AkkaHttpClient { def singleRequest( request: HttpRequest, settings: ConnectionPoolSettings ): Future[HttpResponse] def singleWebsocketRequest[WS_RESULT]( request: WebSocketRequest, clientFlow: Flow[Message, Message, WS_RESULT], settings: ClientConnectionSettings )(implicit ec: ExecutionContext, mat: Materializer): Future[(WebSocketUpgradeResponse, WS_RESULT)] } object AkkaHttpClient { def default( system: ActorSystem, connectionContext: Option[HttpsConnectionContext], customLog: Option[LoggingAdapter] ): AkkaHttpClient = new AkkaHttpClient { private val http = Http()(system) override def singleRequest( request: HttpRequest, settings: ConnectionPoolSettings ): Future[HttpResponse] = { http.singleRequest( request, connectionContext.getOrElse(http.defaultClientHttpsContext), settings, customLog.getOrElse(system.log) ) } override def singleWebsocketRequest[WS_RESULT]( request: WebSocketRequest, clientFlow: Flow[Message, Message, WS_RESULT], settings: ClientConnectionSettings )(implicit ec: ExecutionContext, mat: Materializer): Future[(WebSocketUpgradeResponse, WS_RESULT)] = { val (wsResponse, wsResult) = http.singleWebSocketRequest( request, clientFlow, connectionContext.getOrElse(http.defaultClientHttpsContext), None, settings, customLog.getOrElse(system.log) ) wsResponse.map((_, wsResult)) } } def stubFromAsyncHandler(run: HttpRequest => Future[HttpResponse]): AkkaHttpClient = new AkkaHttpClient { def singleRequest(request: HttpRequest, settings: ConnectionPoolSettings): Future[HttpResponse] = run(request) override def singleWebsocketRequest[WS_RESULT]( request: WebSocketRequest, clientFlow: Flow[Message, Message, WS_RESULT], settings: ClientConnectionSettings )(implicit ec: ExecutionContext, mat: Materializer): Future[(WebSocketUpgradeResponse, WS_RESULT)] = Future.failed(new RuntimeException("Websockets are not supported")) } def stubFromRoute(route: Route)(implicit routingSettings: RoutingSettings, parserSettings: ParserSettings, materializer: Materializer, routingLog: RoutingLog, executionContext: ExecutionContextExecutor = null, rejectionHandler: RejectionHandler = RejectionHandler.default, exceptionHandler: ExceptionHandler = null ): AkkaHttpClient = stubFromAsyncHandler(Route.asyncHandler(route)) }
Example 113
Source File: Routes.scala From akka-ddd-cqrs-es-example with MIT License | 5 votes |
package com.github.j5ik2o.bank.adaptor.controller import akka.actor.ActorSystem import akka.http.scaladsl.model.{ ContentTypes, HttpEntity, HttpResponse } import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server.Route import akka.stream.Materializer import cats.syntax.either._ import com.github.j5ik2o.bank.adaptor.generator.IdGenerator import com.github.j5ik2o.bank.domain.model.BankAccountId import com.github.j5ik2o.bank.useCase.{ BankAccountAggregateUseCase, BankAccountReadModelUseCase } import org.hashids.Hashids object Routes { trait ResponseJson { val isSuccessful: Boolean val errorMessages: Seq[String] } case class OpenBankAccountRequestJson(name: String) case class OpenBankAccountResponseJson(id: String, errorMessages: Seq[String] = Seq.empty) extends ResponseJson { override val isSuccessful: Boolean = errorMessages.isEmpty } case class UpdateBankAccountRequestJson(name: String) case class UpdateBankAccountResponseJson(id: String, errorMessages: Seq[String] = Seq.empty) extends ResponseJson { override val isSuccessful: Boolean = errorMessages.isEmpty } case class AddBankAccountEventRequestJson(`type`: String, amount: Long, currencyCode: String) case class AddBankAccountEventResponseJson(id: String, errorMessages: Seq[String] = Seq.empty) extends ResponseJson { override val isSuccessful: Boolean = errorMessages.isEmpty } case class CloseBankAccountRequestJson(id: String) case class CloseBankAccountResponseJson(id: String, errorMessages: Seq[String] = Seq.empty) extends ResponseJson { override val isSuccessful: Boolean = errorMessages.isEmpty } case class BankAccountEventJson(`type`: String, amount: Long, currencyCode: String, createAt: Long) case class ResolveBankAccountEventsResponseJson(id: String, values: Seq[BankAccountEventJson], errorMessages: Seq[String] = Seq.empty) extends ResponseJson { override val isSuccessful: Boolean = errorMessages.isEmpty } case class ValidationErrorsResponseJson(errorMessages: Seq[String]) extends ResponseJson { override val isSuccessful: Boolean = false } implicit class HashidsStringOps(val self: String) extends AnyVal { def decodeFromHashid(implicit hashIds: Hashids): Either[Throwable, Long] = { Either.catchNonFatal(hashIds.decode(self)(0)) } } implicit class HashidsLongOps(val self: Long) extends AnyVal { def encodeToHashid(implicit hashIds: Hashids): Either[Throwable, String] = Either.catchNonFatal(hashIds.encode(self)) } } case class Routes(bankAccountIdGenerator: IdGenerator[BankAccountId], bankAccountAggregateUseCase: BankAccountAggregateUseCase, bankAccountReadModelUseCase: BankAccountReadModelUseCase)( implicit system: ActorSystem, mat: Materializer ) extends BankAccountController { implicit val ec = system.dispatcher def root: Route = RouteLogging.default.httpLogRequestResult { pathEndOrSingleSlash { get { index() } } ~ toBankAccountRoutes } private def index() = complete( HttpResponse( entity = HttpEntity( ContentTypes.`text/plain(UTF-8)`, "Wellcome to Bank API" ) ) ) }
Example 114
Source File: RouteLogging.scala From akka-ddd-cqrs-es-example with MIT License | 5 votes |
package com.github.j5ik2o.bank.adaptor.controller import akka.event.Logging import akka.http.scaladsl.model.headers.{ Authorization, BasicHttpCredentials } import akka.http.scaladsl.model.{ HttpRequest, HttpResponse } import akka.http.scaladsl.server.{ Directive0, RouteResult } import akka.http.scaladsl.server.directives.{ DebuggingDirectives, LogEntry } trait RequestFormatter { def formatRequest(request: HttpRequest): LogEntry } trait RequestResultFormatter { def formatRequestResponse(request: HttpRequest): RouteResult => Option[LogEntry] } object DefaultRequestLoggingFormatter { implicit object requestFormatter extends RequestFormatter { override def formatRequest(request: HttpRequest): LogEntry = { LogEntry(s"${formatRequestToString(request)}/", Logging.InfoLevel) } } implicit object requestResultFormatter extends RequestResultFormatter { override def formatRequestResponse(request: HttpRequest): (RouteResult) => Option[LogEntry] = { case RouteResult.Complete(response) => val req = formatRequestToString(request) val res = formatResponseToString(response) Some(LogEntry(s"$req/$res", Logging.InfoLevel)) case _ => val req = formatRequestToString(request) Some(LogEntry(req, Logging.InfoLevel)) } } private def formatRequestToString(request: HttpRequest): String = { val protocol = request.protocol.value val method = request.method.name() val path = request.uri.toString() val headers = request.headers .collect { case Authorization(_: BasicHttpCredentials) => "authorization:Basic ***" case Authorization(_) => "authorization:***" case h => s"'${h.lowercaseName()}':'${h.value()}'" } .mkString(", ") s"$protocol $method $path [$headers]" } private def formatResponseToString(request: HttpResponse): String = { val status = request.status.value val headers = request.headers.map(h => s"'${h.lowercaseName()}':'${h.value()}'").mkString(", ") s"$status [$headers]" } } class RouteLogging()(implicit requestFormatter: RequestFormatter, requestResultFormatter: RequestResultFormatter) { val httpLogRequest: Directive0 = DebuggingDirectives.logRequest(requestFormatter.formatRequest _) val httpLogRequestResult: Directive0 = DebuggingDirectives.logRequestResult(requestResultFormatter.formatRequestResponse _) } object RouteLogging { def apply()(implicit requestFormatter: RequestFormatter, requestResultFormatter: RequestResultFormatter): RouteLogging = new RouteLogging() val default: RouteLogging = new RouteLogging()(DefaultRequestLoggingFormatter.requestFormatter, DefaultRequestLoggingFormatter.requestResultFormatter) }
Example 115
Source File: PropertiesApiSuite.scala From iep-apps with Apache License 2.0 | 5 votes |
package com.netflix.iep.archaius import java.io.StringReader import java.util.Properties import akka.http.scaladsl.model.HttpResponse import akka.http.scaladsl.model.MediaTypes import akka.http.scaladsl.model.StatusCode import akka.http.scaladsl.model.StatusCodes import akka.http.scaladsl.model.headers._ import akka.http.scaladsl.testkit.RouteTestTimeout import akka.http.scaladsl.testkit.ScalatestRouteTest import com.netflix.atlas.akka.RequestHandler import com.netflix.atlas.json.Json import com.netflix.spectator.api.DefaultRegistry import com.netflix.spectator.api.ManualClock import org.scalatest.funsuite.AnyFunSuite class PropertiesApiSuite extends AnyFunSuite with ScalatestRouteTest { import scala.concurrent.duration._ implicit val routeTestTimeout = RouteTestTimeout(5.second) val clock = new ManualClock() val registry = new DefaultRegistry(clock) val propContext = new PropertiesContext(registry) val endpoint = new PropertiesApi(propContext, system) val routes = RequestHandler.standardOptions(endpoint.routes) private def assertJsonContentType(response: HttpResponse): Unit = { assert(response.entity.contentType.mediaType === MediaTypes.`application/json`) } private def assertResponse(response: HttpResponse, expected: StatusCode): Unit = { assert(response.status === expected) assertJsonContentType(response) } test("no asg") { Get("/api/v1/property") ~> routes ~> check { assert(response.status === StatusCodes.BadRequest) } } test("empty") { propContext.update(Nil) Get("/api/v1/property?asg=foo-main-v001") ~> addHeader(Accept(MediaTypes.`application/json`)) ~> routes ~> check { assertResponse(response, StatusCodes.OK) assert(responseAs[String] === "[]") } } test("properties response") { propContext.update( List( PropertiesApi.Property("foo-main::a", "foo-main", "a", "b", 12345L), PropertiesApi.Property("foo-main::1", "foo-main", "1", "2", 12345L), PropertiesApi.Property("bar-main::c", "bar-main", "c", "d", 12345L) ) ) Get("/api/v1/property?asg=foo-main-v001") ~> routes ~> check { assert(response.status === StatusCodes.OK) val props = new Properties props.load(new StringReader(responseAs[String])) assert(props.size === 2) assert(props.getProperty("a") === "b") assert(props.getProperty("1") === "2") } } test("json response") { propContext.update( List( PropertiesApi.Property("foo-main::a", "foo-main", "a", "b", 12345L) ) ) Get("/api/v1/property?asg=foo-main-v001") ~> addHeader(Accept(MediaTypes.`application/json`)) ~> routes ~> check { assertResponse(response, StatusCodes.OK) val props = Json.decode[List[PropertiesApi.Property]](responseAs[String]) assert(props === List(PropertiesApi.Property("foo-main::a", "foo-main", "a", "b", 12345L))) } } }
Example 116
Source File: RoutingHelper.scala From sumobot with Apache License 2.0 | 5 votes |
package com.sumologic.sumobot.http_frontend import akka.http.scaladsl.model.headers._ import akka.http.scaladsl.model.{HttpEntity, HttpRequest, HttpResponse, ResponseEntity} import akka.http.scaladsl.model.HttpMethods.{GET, HEAD} import akka.stream.Materializer case class RoutingHelper(origin: String)(implicit materializer: Materializer) { def withAllowOriginHeader(routing: PartialFunction[HttpRequest, HttpResponse]): PartialFunction[HttpRequest, HttpResponse] = { routing.andThen { response: HttpResponse => val oldHeaders = response.headers.filter(header => header.isNot(`Access-Control-Allow-Origin`.lowercaseName)).toList val accessOrigin = if (origin == "*") { `Access-Control-Allow-Origin`.* } else `Access-Control-Allow-Origin`(origin) val newHeaders = oldHeaders :+ accessOrigin response.withHeaders(newHeaders) } } def withHeadRequests(routing: PartialFunction[HttpRequest, HttpResponse]): PartialFunction[HttpRequest, HttpResponse] = { routing.orElse { case request@HttpRequest(HEAD, _, _, _, _) if routing.isDefinedAt(request.withMethod(GET)) => val response = routing(request.withMethod(GET)) val oldEntity = response.entity val newEntity = HttpEntity(oldEntity.contentType, Array.empty[Byte]) response.withEntity(newEntity) } } def withForbiddenFallback(routing: PartialFunction[HttpRequest, HttpResponse]): PartialFunction[HttpRequest, HttpResponse] = { routing.orElse { case invalid: HttpRequest => invalid.discardEntityBytes() HttpResponse(403) } } }
Example 117
Source File: BasicAuthentication.scala From sumobot with Apache License 2.0 | 5 votes |
package com.sumologic.sumobot.http_frontend.authentication import java.security.MessageDigest import akka.http.scaladsl.model.headers._ import akka.http.scaladsl.model.{HttpRequest, HttpResponse} import com.typesafe.config.Config class BasicAuthentication(config: Config) extends HttpAuthentication { private val username = config.getString("username") private val password = config.getString("password") override def authentication(request: HttpRequest): AuthenticationResult = { request.header[`Authorization`] match { case Some(header) => val receivedCredentials = BasicHttpCredentials(header.credentials.token()) val authenticated = MessageDigest.isEqual(receivedCredentials.username.getBytes, username.getBytes) && MessageDigest.isEqual(receivedCredentials.password.getBytes, password.getBytes) if (authenticated) AuthenticationSucceeded(AuthenticationInfo(Some(s"Logged in as: $username"), None, Seq.empty)) else AuthenticationForbidden(HttpResponse(403)) case None => val header = `WWW-Authenticate`(List(HttpChallenge("basic", Some("SumoBot")))) AuthenticationForbidden(HttpResponse(401, headers = List(header))) } } }
Example 118
Source File: ModelServiceSpec.scala From full-scala-stack with Apache License 2.0 | 5 votes |
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 119
Source File: ZIODirectives.scala From full-scala-stack with Apache License 2.0 | 5 votes |
package api import akka.http.scaladsl.marshalling.{ Marshaller, Marshalling } import akka.http.scaladsl.model.HttpResponse import akka.http.scaladsl.server.{ Directive, Directive1, Route, RouteResult } import akka.http.scaladsl.marshalling.ToResponseMarshaller import akka.http.scaladsl.server.directives.RouteDirectives import akka.http.scaladsl.util.FastFuture._ import zio.{ DefaultRuntime, Task, ZIO } import scala.concurrent.{ Future, Promise } import scala.util.{ Failure, Success } def zioCompleteOrRecoverWith(magnet: ZIOCompleteOrRecoverWithMagnet): Directive1[Throwable] = magnet.directive } object ZIODirectives extends ZIODirectives trait ZIOCompleteOrRecoverWithMagnet { def directive: Directive1[Throwable] } object ZIOCompleteOrRecoverWithMagnet extends ZIODirectives { implicit def apply[T]( task: => Task[T] )(implicit m: ToResponseMarshaller[T]): ZIOCompleteOrRecoverWithMagnet = new ZIOCompleteOrRecoverWithMagnet { override val directive: Directive1[Throwable] = Directive[Tuple1[Throwable]] { inner => ctx => val future = unsafeRunToFuture(task) import ctx.executionContext future.fast.transformWith { case Success(res) => ctx.complete(res) case Failure(error) => inner(Tuple1(error))(ctx) } } } }
Example 120
Source File: FormSupportSpec.scala From twitter4s with Apache License 2.0 | 5 votes |
package com.danielasfregola.twitter4s.http.serializers import akka.http.scaladsl.model.{ContentTypes, HttpEntity, HttpResponse} import com.danielasfregola.twitter4s.exceptions.TwitterException import com.danielasfregola.twitter4s.helpers.{AwaitableFuture, TestActorSystem, TestExecutionContext} import org.specs2.mutable.SpecificationLike import org.specs2.specification.Scope import scala.concurrent.Future class FormSupportSpec extends TestActorSystem with SpecificationLike with AwaitableFuture with TestExecutionContext { case class MyTest(a: String, b: String, c: String, d: Boolean) implicit object MyTestFromMap extends FromMap[MyTest] { def apply(m: Map[String, String]) = for { a <- m.get("a") b <- m.get("b") c <- m.get("c") d <- m.get("d") } yield MyTest(a, b, c, toBoolean(d)) } abstract class FormSupportContext extends Scope def buildResponse(text: String): HttpResponse = HttpResponse(entity = HttpEntity.apply(ContentTypes.`text/html(UTF-8)`, text)) "FormSupport" should { "unmarshall a well-formed text into a case class" in new FormSupportContext { val text = "a=hello&c=test&b=foobar&d=true" val result: Future[MyTest] = FormSupport.unmarshallText[MyTest](buildResponse(text)) result.await === MyTest(a = "hello", b = "foobar", c = "test", d = true) } "throw TwitterException if text is not well formed" in new FormSupportContext { val text = "non-well-formed-string" val result: Future[MyTest] = FormSupport.unmarshallText[MyTest](buildResponse(text)) result.await should throwA[TwitterException] } } }
Example 121
Source File: RestClientSpec.scala From twitter4s with Apache License 2.0 | 5 votes |
package com.danielasfregola.twitter4s.http.clients.rest import akka.http.scaladsl.model.{ContentTypes, HttpResponse, StatusCodes} import com.danielasfregola.twitter4s.exceptions.{Errors, TwitterError, TwitterException} import com.danielasfregola.twitter4s.helpers.ClientSpec import scala.concurrent.Future class RestClientSpec extends ClientSpec { class ClientSpecContext extends RestClientSpecContext { import restClient._ def exampleRequest(): Future[Unit] = Get("an-example-request", ContentTypes.`application/json`).respondAs[Unit] } "Rest Client" should { "throw twitter exception to twitter rejection" in new ClientSpecContext { val response = { val entity = """{"errors":[{"message":"Sorry, that page does not exist","code":34}]}""" HttpResponse(StatusCodes.NotFound, entity = entity) } val result = when(exampleRequest).expectRequest(identity(_)).respondWith(response) val expectedTwitterException = TwitterException(code = StatusCodes.NotFound, errors = Errors(TwitterError("Sorry, that page does not exist", 34))) result.await should throwAn(expectedTwitterException) } "throw twitter exception to generic failure http response" in new ClientSpecContext { val body = "Something bad happened" val response = HttpResponse(StatusCodes.RequestTimeout, entity = body) val result = when(exampleRequest).expectRequest(identity(_)).respondWith(response) val expectedTwitterException = TwitterException(code = StatusCodes.RequestTimeout, errors = Errors(body)) result.await should throwAn(expectedTwitterException) } } }
Example 122
Source File: FormSupport.scala From twitter4s with Apache License 2.0 | 5 votes |
package com.danielasfregola.twitter4s.http.serializers import akka.http.scaladsl.model.{HttpResponse, StatusCodes} import akka.stream.Materializer import com.danielasfregola.twitter4s.exceptions.TwitterException import scala.concurrent.Future import scala.concurrent.duration._ private[twitter4s] trait FromMap[T] { def apply(m: Map[String, String]): Option[T] def toBoolean(text: String): Boolean = text.trim.toLowerCase == "true" } private[twitter4s] object FormSupport { def unmarshallText[T: Manifest](response: HttpResponse)(implicit materializer: Materializer, fromMap: FromMap[T]): Future[T] = { implicit val ec = materializer.executionContext response.entity.toStrict(5 seconds).map(_.data.decodeString("UTF-8")).map { body => fromMap(asMap(body)).getOrElse(throw unmarshallError[T](body)) } } private def asMap(body: String): Map[String, String] = body .split("&") .map { fields => val tokens = fields.split("=", 2) tokens.head -> tokens.tail.mkString.trim } .toMap private def unmarshallError[T: Manifest](body: String): TwitterException = { val errorMsg = s"""Could not serialise body to ${manifest[T].runtimeClass.getSimpleName}. | |Please report it at https://github.com/DanielaSfregola/twitter4s/issues/new |Body was: $body""".stripMargin TwitterException(StatusCodes.InternalServerError, errorMsg) } }
Example 123
Source File: Client.scala From twitter4s with Apache License 2.0 | 5 votes |
package com.danielasfregola.twitter4s.http.clients import akka.actor.ActorSystem import akka.http.scaladsl.model.{HttpRequest, HttpResponse} import akka.stream.Materializer import akka.stream.scaladsl.{Sink, Source} import com.danielasfregola.twitter4s.http.oauth.OAuth1Provider import scala.concurrent.Future trait Client extends OAuthClient { val withLogRequest = false val withLogRequestResponse = true def oauthProvider: OAuth1Provider protected def sendAndReceive[T](request: HttpRequest, f: HttpResponse => Future[T])( implicit system: ActorSystem, materializer: Materializer): Future[T] = { implicit val r: HttpRequest = request val requestStartTime = System.currentTimeMillis if (withLogRequest) logRequest Source .single(request) .via(connection) .mapAsync(1)(implicit response => unmarshal(requestStartTime, f)) .runWith(Sink.head) } }
Example 124
Source File: CommonClient.scala From twitter4s with Apache License 2.0 | 5 votes |
package com.danielasfregola.twitter4s.http.clients import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model.{HttpEntity, HttpRequest, HttpResponse} import akka.stream.Materializer import com.danielasfregola.twitter4s.exceptions.{Errors, TwitterException} import com.danielasfregola.twitter4s.http.serializers.JsonSupport import com.typesafe.scalalogging.LazyLogging import org.json4s.native.Serialization import scala.concurrent.Future import scala.concurrent.duration._ import scala.util.Try private[twitter4s] trait CommonClient extends JsonSupport with LazyLogging { def withLogRequest: Boolean def withLogRequestResponse: Boolean protected def connection(implicit request: HttpRequest, system: ActorSystem) = { val scheme = request.uri.scheme val host = request.uri.authority.host.toString val port = request.uri.effectivePort if (scheme == "https") Http().outgoingConnectionHttps(host, port) else Http().outgoingConnection(host, port) } protected def unmarshal[T](requestStartTime: Long, f: HttpResponse => Future[T])(implicit request: HttpRequest, response: HttpResponse, materializer: Materializer) = { implicit val ec = materializer.executionContext if (withLogRequestResponse) logRequestResponse(requestStartTime) if (response.status.isSuccess) f(response) else parseFailedResponse(response).flatMap(Future.failed) } protected def parseFailedResponse(response: HttpResponse)(implicit materializer: Materializer) = { implicit val ec = materializer.executionContext response.entity.toStrict(50 seconds).map { sink => val body = sink.data.utf8String val errors = Try { Serialization.read[Errors](body) } getOrElse Errors(body) TwitterException(response.status, errors) } } // TODO - logRequest, logRequestResponse customisable? def logRequest(implicit request: HttpRequest, materializer: Materializer): HttpRequest = { implicit val ec = materializer.executionContext logger.info(s"${request.method.value} ${request.uri}") if (logger.underlying.isDebugEnabled) { for { requestBody <- toBody(request.entity) } yield logger.debug(s"${request.method.value} ${request.uri} | $requestBody") } request } def logRequestResponse(requestStartTime: Long)(implicit request: HttpRequest, materializer: Materializer): HttpResponse => HttpResponse = { response => implicit val ec = materializer.executionContext val elapsed = System.currentTimeMillis - requestStartTime logger.info(s"${request.method.value} ${request.uri} (${response.status}) | ${elapsed}ms") if (logger.underlying.isDebugEnabled) { for { responseBody <- toBody(response.entity) } yield logger.debug( s"${request.method.value} ${request.uri} (${response.status}) | ${response.headers.mkString(", ")} | $responseBody") } response } private def toBody(entity: HttpEntity)(implicit materializer: Materializer): Future[String] = { implicit val ec = materializer.executionContext entity.toStrict(5 seconds).map(_.data.decodeString("UTF-8")) } }
Example 125
Source File: RestClient.scala From twitter4s with Apache License 2.0 | 5 votes |
package com.danielasfregola.twitter4s.http.clients.rest import akka.actor.ActorSystem import akka.http.scaladsl.model.{HttpRequest, HttpResponse} import akka.http.scaladsl.unmarshalling.Unmarshal import akka.stream.{ActorMaterializer, Materializer} import com.danielasfregola.twitter4s.entities.{AccessToken, ConsumerToken, RateLimit, RatedData} import com.danielasfregola.twitter4s.http.clients.Client import com.danielasfregola.twitter4s.http.oauth.OAuth1Provider import scala.concurrent.Future private[twitter4s] class RestClient(val consumerToken: ConsumerToken, val accessToken: AccessToken)( implicit val system: ActorSystem) extends Client { lazy val oauthProvider = new OAuth1Provider(consumerToken, Some(accessToken)) private[twitter4s] implicit class RichRestHttpRequest(val request: HttpRequest) { implicit val materializer = ActorMaterializer() implicit val ec = materializer.executionContext def respondAs[T: Manifest]: Future[T] = for { requestWithAuth <- withOAuthHeader(None)(materializer)(request) t <- sendReceiveAs[T](requestWithAuth) } yield t def respondAsRated[T: Manifest]: Future[RatedData[T]] = for { requestWithAuth <- withOAuthHeader(None)(materializer)(request) t <- sendReceiveAsRated[T](requestWithAuth) } yield t def sendAsFormData: Future[Unit] = for { requestWithAuth <- withSimpleOAuthHeader(None)(materializer)(request) _ <- sendIgnoreResponse(requestWithAuth) } yield () } def sendIgnoreResponse(httpRequest: HttpRequest)(implicit system: ActorSystem, materializer: Materializer): Future[Unit] = { sendAndReceive(httpRequest, _ => Future.successful((): Unit)) } def sendReceiveAs[T: Manifest](httpRequest: HttpRequest)(implicit system: ActorSystem, materializer: Materializer): Future[T] = { implicit val ec = materializer.executionContext implicit val jsonSerialization = serialization sendAndReceive(httpRequest, response => Unmarshal(response.entity).to[T]) } def sendReceiveAsRated[T: Manifest](httpRequest: HttpRequest)(implicit system: ActorSystem, materializer: Materializer): Future[RatedData[T]] = { implicit val ec = materializer.executionContext implicit val jsonSerialization = serialization val unmarshallRated: HttpResponse => Future[RatedData[T]] = { response => val rate = RateLimit(response.headers) val data = Unmarshal(response.entity).to[T] data.map(d => RatedData(rate, d)) } sendAndReceive(httpRequest, unmarshallRated) } }
Example 126
Source File: JsonMarshallers.scala From scala-openrtb with Apache License 2.0 | 5 votes |
package com.powerspace.openrtb.akka.marshallers import akka.http.scaladsl.marshalling.{ToEntityMarshaller, ToResponseMarshaller} import akka.http.scaladsl.model.{ContentTypes, HttpEntity, HttpResponse, MediaTypes} import com.google.openrtb.{BidRequest, BidResponse} import io.circe.Encoder object JsonMarshallers { import akka.http.scaladsl.marshalling.Marshaller._ def bidRequestJsonMarshaller(implicit encoder: Encoder[BidRequest]): ToEntityMarshaller[BidRequest] = withFixedContentType(ContentTypes.`application/json`)( bidRequest => HttpEntity(MediaTypes.`application/json`, encoder.apply(bidRequest).toString) ) def bidResponseJsonMarshaller(implicit encoder: Encoder[BidResponse]): ToEntityMarshaller[BidResponse] = withFixedContentType(ContentTypes.`application/json`)( bidResponse => HttpEntity(MediaTypes.`application/json`, encoder.apply(bidResponse).toString) ) def bidRequestHttpJsonMarshaller(implicit encoder: Encoder[BidRequest]): ToResponseMarshaller[BidRequest] = withFixedContentType(ContentTypes.`application/json`)( bidRequest => HttpResponse() .withEntity(HttpEntity(MediaTypes.`application/json`, encoder.apply(bidRequest).toString)) ) def bidResponseHttpJsonMarshaller(implicit encoder: Encoder[BidResponse]): ToResponseMarshaller[BidResponse] = withFixedContentType(ContentTypes.`application/json`)( bidResponse => HttpResponse() .withEntity(HttpEntity(MediaTypes.`application/json`, encoder.apply(bidResponse).toString)) ) }
Example 127
Source File: Hello.scala From sbt-guardrail with MIT License | 5 votes |
package helloworld import scala.concurrent.Future import akka.http.scaladsl.model.{HttpRequest, HttpResponse} import akka.stream.Materializer import akka.actor.ActorSystem import akka.stream.ActorMaterializer import cats.implicits._ import com.example.clients.petstore.user.UserClient object Hello { def buildUserClient(): UserClient = { import scala.concurrent.ExecutionContext.Implicits.global val server = buildServer() implicit val actorSys = ActorSystem() implicit val materializer = ActorMaterializer() UserClient.httpClient(server) } private def buildServer(): HttpRequest => Future[HttpResponse] = { import com.example.servers.petstore.user._ import akka.http.scaladsl.server.Route import akka.http.scaladsl.settings.RoutingSettings implicit val actorSys = ActorSystem() implicit val materializer = ActorMaterializer() implicit val routingSettings = RoutingSettings(actorSys) Route.asyncHandler( UserResource.routes(new DummyUserHandler()) ) } } class DummyUserHandler extends com.example.servers.petstore.user.UserHandler { import com.example.servers.petstore.user._ import com.example.servers.petstore.definitions._ import scala.collection._ def createUser(respond: UserResource.CreateUserResponse.type)(body: User): scala.concurrent.Future[UserResource.CreateUserResponse] = ??? def createUsersWithArrayInput(respond: UserResource.CreateUsersWithArrayInputResponse.type)(body: Vector[User]): scala.concurrent.Future[UserResource.CreateUsersWithArrayInputResponse] = ??? def createUsersWithListInput(respond: UserResource.CreateUsersWithListInputResponse.type)(body: Vector[User]): scala.concurrent.Future[UserResource.CreateUsersWithListInputResponse] = ??? def loginUser(respond: UserResource.LoginUserResponse.type)(username: String, password: String): scala.concurrent.Future[UserResource.LoginUserResponse] = ??? def logoutUser(respond: UserResource.LogoutUserResponse.type)(): scala.concurrent.Future[UserResource.LogoutUserResponse] = ??? def getUserByName(respond: UserResource.GetUserByNameResponse.type)(username: String): scala.concurrent.Future[UserResource.GetUserByNameResponse] = { val user = new User( id = Some(1234), username = Some(username), firstName = Some("First"), lastName = Some("Last"), email = Some(username + "@example.com")) Future.successful(respond.OK(user)) } def updateUser(respond: UserResource.UpdateUserResponse.type)(username: String, body: User): scala.concurrent.Future[UserResource.UpdateUserResponse] = ??? def deleteUser(respond: UserResource.DeleteUserResponse.type)(username: String): scala.concurrent.Future[UserResource.DeleteUserResponse] = ??? }
Example 128
Source File: PropertiesApi.scala From iep-apps with Apache License 2.0 | 5 votes |
package com.netflix.iep.archaius import java.io.StringWriter import java.util.Properties import akka.actor.ActorRefFactory import akka.http.scaladsl.model.HttpCharsets import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.model.HttpEntity import akka.http.scaladsl.model.HttpRequest import akka.http.scaladsl.model.HttpResponse import akka.http.scaladsl.model.MediaTypes import akka.http.scaladsl.model.StatusCodes import akka.http.scaladsl.server.Route import com.netflix.atlas.akka.CustomDirectives._ import com.netflix.atlas.akka.WebApi import com.netflix.atlas.json.Json import com.netflix.frigga.Names class PropertiesApi( val propContext: PropertiesContext, implicit val actorRefFactory: ActorRefFactory ) extends WebApi { def routes: Route = { endpointPath("api" / "v1" / "property") { get { parameter("asg") { asg => extractRequest { request => val cluster = Names.parseName(asg).getCluster if (propContext.initialized) { val props = propContext.getClusterProps(cluster) complete(encode(request, props)) } else { complete(HttpResponse(StatusCodes.ServiceUnavailable)) } } } } } } private def encode(request: HttpRequest, props: List[PropertiesApi.Property]): HttpResponse = { val useJson = request.headers.exists(h => h.is("accept") && h.value == "application/json") if (useJson) { HttpResponse( StatusCodes.OK, entity = HttpEntity(MediaTypes.`application/json`, Json.encode(props)) ) } else { val ps = new Properties props.foreach { p => ps.setProperty(p.key, p.value) } val writer = new StringWriter() ps.store(writer, s"count: ${ps.size}") writer.close() val entity = HttpEntity(MediaTypes.`text/plain`.toContentType(HttpCharsets.`UTF-8`), writer.toString) HttpResponse(StatusCodes.OK, entity = entity) } } } object PropertiesApi { case class Property(id: String, cluster: String, key: String, value: String, timestamp: Long) }
Example 129
Source File: PersistenceApi.scala From iep-apps with Apache License 2.0 | 5 votes |
package com.netflix.atlas.persistence import akka.http.scaladsl.model.HttpResponse import akka.http.scaladsl.model.StatusCodes import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server.Route import com.netflix.atlas.akka.CustomDirectives._ import com.netflix.atlas.akka.DiagnosticMessage import com.netflix.atlas.akka.WebApi import com.netflix.atlas.webapi.PublishApi class PersistenceApi(localFileService: LocalFilePersistService) extends WebApi { override def routes: Route = { post { endpointPath("api" / "v1" / "persistence") { handleReq } ~ endpointPath("api" / "v1" / "publish") { handleReq } ~ endpointPath("api" / "v1" / "publish-fast") { // Legacy path from when there was more than one publish mode handleReq } } } private def handleReq: Route = { parseEntity(customJson(p => PublishApi.decodeBatch(p))) { datapoints => datapoints match { case Nil => complete(DiagnosticMessage.error(StatusCodes.BadRequest, "empty payload")) case _ => { datapoints.foreach(localFileService.persist) complete(HttpResponse(StatusCodes.OK)) } } } } }
Example 130
Source File: StatsApiSuite.scala From iep-apps with Apache License 2.0 | 5 votes |
package com.netflix.iep.lwc import akka.http.scaladsl.model.HttpResponse import akka.http.scaladsl.model.MediaTypes import akka.http.scaladsl.model.StatusCode import akka.http.scaladsl.model.StatusCodes import akka.http.scaladsl.testkit.RouteTestTimeout import akka.http.scaladsl.testkit.ScalatestRouteTest import com.netflix.atlas.akka.RequestHandler import com.netflix.atlas.core.model.Datapoint import com.netflix.atlas.json.Json import com.netflix.spectator.atlas.impl.Subscription import com.typesafe.config.ConfigFactory import org.scalatest.BeforeAndAfter import org.scalatest.funsuite.AnyFunSuite class StatsApiSuite extends AnyFunSuite with ScalatestRouteTest with BeforeAndAfter { import scala.jdk.CollectionConverters._ import scala.concurrent.duration._ private implicit val routeTestTimeout = RouteTestTimeout(5.second) private val config = ConfigFactory.load() private val evaluator = new ExpressionsEvaluator(config) private val endpoint = new StatsApi(evaluator) private val routes = RequestHandler.standardOptions(endpoint.routes) private def assertJsonContentType(response: HttpResponse): Unit = { assert(response.entity.contentType.mediaType === MediaTypes.`application/json`) } private def assertResponse(response: HttpResponse, expected: StatusCode): Unit = { assert(response.status === expected) assertJsonContentType(response) } before { evaluator.clear() } test("empty") { Get("/api/v1/stats") ~> routes ~> check { assertResponse(response, StatusCodes.OK) assert(responseAs[String] === "[]") } } test("has data") { // Add sample subscription val subs = List( new Subscription() .withId("1") .withExpression("name,ssCpuUser,:eq,:sum"), new Subscription() .withId("2") .withExpression("name,ssCpuSystem,:eq,:sum") ) evaluator.sync(subs.asJava) // Stats only get updated when data is sent val datapoints = List( Datapoint(Map("name" -> "ssCpuUser", "node" -> "i-1"), 60000, 42.0), Datapoint(Map("name" -> "ssCpuUser", "node" -> "i-2"), 60000, 44.0) ) evaluator.eval(60000, datapoints) // Query the data Get("/api/v1/stats") ~> routes ~> check { assertResponse(response, StatusCodes.OK) val stats = Json.decode[List[ExpressionsEvaluator.SubscriptionStats]](responseAs[String]) assert(stats.length === 1) assert(stats.head.updateCount.get() === 2) } } }
Example 131
Source File: StatsApi.scala From iep-apps with Apache License 2.0 | 5 votes |
package com.netflix.iep.lwc import akka.http.scaladsl.model.HttpEntity import akka.http.scaladsl.model.HttpResponse import akka.http.scaladsl.model.MediaTypes import akka.http.scaladsl.model.StatusCodes import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server.Route import com.netflix.atlas.akka.CustomDirectives._ import com.netflix.atlas.akka.WebApi import com.netflix.atlas.json.Json class StatsApi(evaluator: ExpressionsEvaluator) extends WebApi { override def routes: Route = { endpointPathPrefix("api" / "v1" / "stats") { get { val stats = Json.encode(evaluator.stats) val entity = HttpEntity(MediaTypes.`application/json`, stats) val response = HttpResponse(StatusCodes.OK, Nil, entity) complete(response) } } } }
Example 132
Source File: ExplainApi.scala From iep-apps with Apache License 2.0 | 5 votes |
package com.netflix.atlas.druid import akka.actor.ActorRefFactory import akka.http.scaladsl.model.HttpEntity import akka.http.scaladsl.model.HttpResponse import akka.http.scaladsl.model.MediaTypes import akka.http.scaladsl.model.StatusCodes import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server.Route import akka.http.scaladsl.server.RouteResult import akka.pattern.ask import akka.util.Timeout import com.netflix.atlas.akka.CustomDirectives._ import com.netflix.atlas.akka.WebApi import com.netflix.atlas.druid.ExplainApi.ExplainRequest import com.netflix.atlas.eval.graph.Grapher import com.netflix.atlas.json.Json import com.netflix.atlas.webapi.GraphApi.DataRequest import com.typesafe.config.Config import scala.concurrent.duration._ class ExplainApi(config: Config, implicit val actorRefFactory: ActorRefFactory) extends WebApi { private val grapher: Grapher = Grapher(config) private val dbRef = actorRefFactory.actorSelection("/user/db") private implicit val ec = actorRefFactory.dispatcher override def routes: Route = { endpointPath("explain" / "v1" / "graph") { get { ctx => val graphCfg = grapher.toGraphConfig(ctx.request) dbRef .ask(ExplainRequest(DataRequest(graphCfg)))(Timeout(10.seconds)) .map { response => val json = Json.encode(response) val entity = HttpEntity(MediaTypes.`application/json`, json) RouteResult.Complete(HttpResponse(StatusCodes.OK, entity = entity)) } } } } } object ExplainApi { case class ExplainRequest(dataRequest: DataRequest) }
Example 133
Source File: UpdateApi.scala From iep-apps with Apache License 2.0 | 5 votes |
package com.netflix.atlas.aggregator import javax.inject.Inject import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.model.HttpEntity import akka.http.scaladsl.model.HttpResponse import akka.http.scaladsl.model.MediaTypes import akka.http.scaladsl.model.StatusCode import akka.http.scaladsl.model.StatusCodes import akka.http.scaladsl.server.Route import com.fasterxml.jackson.core.JsonParser import com.netflix.atlas.akka.CustomDirectives._ import com.netflix.atlas.akka.WebApi import com.netflix.atlas.core.validation.ValidationResult import com.netflix.atlas.eval.stream.Evaluator import com.typesafe.scalalogging.StrictLogging class UpdateApi @Inject()( evaluator: Evaluator, aggrService: AtlasAggregatorService ) extends WebApi with StrictLogging { import UpdateApi._ require(aggrService != null, "no binding for aggregate registry") def routes: Route = { endpointPath("api" / "v4" / "update") { post { parseEntity(customJson(p => processPayload(p, aggrService))) { response => complete(response) } } } } } object UpdateApi { private val decoder = PayloadDecoder.default private[aggregator] def processPayload( parser: JsonParser, service: AtlasAggregatorService ): HttpResponse = { val result = decoder.decode(parser, service) createResponse(result.numDatapoints, result.failures) } private val okResponse = { val entity = HttpEntity(MediaTypes.`application/json`, "{}") HttpResponse(StatusCodes.OK, entity = entity) } private def createErrorResponse(status: StatusCode, msg: FailureMessage): HttpResponse = { val entity = HttpEntity(MediaTypes.`application/json`, msg.toJson) HttpResponse(status, entity = entity) } private def createResponse(numDatapoints: Int, failures: List[ValidationResult]): HttpResponse = { if (failures.isEmpty) { okResponse } else { val numFailures = failures.size if (numDatapoints > numFailures) { // Partial failure val msg = FailureMessage.partial(failures, numFailures) createErrorResponse(StatusCodes.Accepted, msg) } else { // All datapoints dropped val msg = FailureMessage.error(failures, numFailures) createErrorResponse(StatusCodes.BadRequest, msg) } } } }
Example 134
Source File: TestServer.scala From typed-schema with Apache License 2.0 | 5 votes |
package ru.tinkoff.tschema.examples import java.util.Locale import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model.{ContentTypes, HttpEntity, HttpResponse} import akka.http.scaladsl.server.Directives.{complete, get, pathPrefix, _} import akka.stream.ActorMaterializer import cats.instances.list._ import cats.syntax.foldable._ import de.heikoseeberger.akkahttpcirce.FailFastCirceSupport._ import io.circe.Printer import ru.tinkoff.tschema.swagger.{OpenApiInfo, PathDescription} object TestServer { implicit val system = ActorSystem() implicit val mat = ActorMaterializer() import system.dispatcher val descriptions = PathDescription.utf8I18n("swagger", Locale.forLanguageTag("ru")) val modules = List[ExampleModule]( TestModule, VersionModule, FiltersModule, FormFieldsModule, Authorize, CustomAuth, MultiParameters, ProxyModule ).combineAll private[this] implicit val printer: Printer = Printer.noSpaces.copy(dropNullValues = true) val route = pathPrefix("api") { modules.route } ~ path("swagger")( get( complete( modules.swag .describe(descriptions) .make(OpenApiInfo()) .addServer("/api") ) ) ) ~ pathPrefix("webjars")( getFromResourceDirectory("META-INF/resources/webjars") ) ~ path("swagger.php")( complete( HttpResponse( entity = HttpEntity( contentType = ContentTypes.`text/html(UTF-8)`, string = SwaggerIndex.index.render ) ) ) ) def main(args: Array[String]): Unit = { for (_ <- Http().bindAndHandle(route, "localhost", 8081)) println("server started at http://localhost:8081/swagger.php") } }
Example 135
Source File: InnerSuite.scala From typed-schema with Apache License 2.0 | 5 votes |
package ru.tinkoff.tschema.akkaHttp import akka.http.scaladsl.marshalling.{Marshaller, Marshalling, ToResponseMarshaller} import akka.http.scaladsl.model.{ContentTypes, HttpResponse} import akka.http.scaladsl.testkit.ScalatestRouteTest import ru.tinkoff.tschema.syntax._ import org.scalatest.flatspec.AsyncFlatSpec import org.scalatest.matchers.should.Matchers import scala.language.reflectiveCalls class InnerSuite extends AsyncFlatSpec with ScalatestRouteTest with Matchers { object impl { object first { def get: String = "first" def post(message: String) = s"first $message" } val second = new { def get: String = "second" def post(message: String) = s"second $message" } } implicit val unitAsPlainText: ToResponseMarshaller[Unit] = Marshaller.strict(_ => Marshalling.WithFixedContentType(ContentTypes.NoContentType, () => HttpResponse())) def api = ( groupPrefix("first") |> (( opGet |> $$[String] ) <> ( opPost |> queryParam[String]("message") |> $$[String] )) ) <> ( groupPrefix("second") |> (( opGet |> $$[String] ) <> ( opPost |> body[String]("message") |> $$[String] )) ) val route = MkRoute(api)(impl) "first group" should "handle get" in Get("/first") ~> route ~> check { responseAs[String] shouldBe "first" } it should "handle post" in Post("/first?message=hello+oleg") ~> route ~> check { responseAs[String] shouldBe "first hello oleg" } "second group" should "handle get" in Get("/second") ~> route ~> check { responseAs[String] shouldBe "second" } it should "handle post" in Post("/second", "hello oleg") ~> route ~> check { responseAs[String] shouldBe "second hello oleg" } }
Example 136
Source File: EventSource.scala From nexus-kg with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.kg.client import java.util.UUID import akka.NotUsed import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model.headers.OAuth2BearerToken import akka.http.scaladsl.model.{HttpRequest, HttpResponse} import akka.persistence.query.{NoOffset, Offset, Sequence, TimeBasedUUID} import akka.stream.Materializer import akka.stream.alpakka.sse.scaladsl.{EventSource => SSESource} import akka.stream.scaladsl.Source import ch.epfl.bluebrain.nexus.rdf.implicits._ import ch.epfl.bluebrain.nexus.iam.client.types.AuthToken import ch.epfl.bluebrain.nexus.rdf.Iri.AbsoluteIri import com.typesafe.scalalogging.Logger import io.circe.Decoder import io.circe.parser.decode import scala.concurrent.{ExecutionContext, Future} import scala.util.Try trait EventSource[A] { def apply[A: Decoder]( config: KgClientConfig )(implicit as: ActorSystem, mt: Materializer, ec: ExecutionContext): EventSource[A] = new EventSource[A] { private val logger = Logger[this.type] private val http = Http() private def addCredentials(request: HttpRequest)(implicit cred: Option[AuthToken]): HttpRequest = cred.map(token => request.addCredentials(OAuth2BearerToken(token.value))).getOrElse(request) private def send(request: HttpRequest)(implicit cred: Option[AuthToken]): Future[HttpResponse] = http.singleRequest(addCredentials(request)).map { resp => if (!resp.status.isSuccess()) logger.warn(s"HTTP response when performing SSE request: status = '${resp.status}'") resp } private def toOffset(id: String): Offset = Try(TimeBasedUUID(UUID.fromString(id))).orElse(Try(Sequence(id.toLong))).getOrElse(NoOffset) override def apply(iri: AbsoluteIri, offset: Option[String])( implicit cred: Option[AuthToken] ): Source[(Offset, A), NotUsed] = SSESource(iri.asAkka, send, offset, config.sseRetryDelay).flatMapConcat { sse => val offset = sse.id.map(toOffset).getOrElse(NoOffset) decode[A](sse.data) match { case Right(ev) => Source.single(offset -> ev) case Left(err) => logger.error(s"Failed to decode admin event '$sse'", err) Source.empty } } } }
Example 137
Source File: SseConnector.scala From vamp with Apache License 2.0 | 5 votes |
package io.vamp.common.http import akka.Done import akka.actor.ActorSystem import akka.event.LoggingAdapter import akka.http.scaladsl.model.HttpHeader.ParsingResult.Ok import akka.http.scaladsl.model.sse.ServerSentEvent import akka.http.scaladsl.model.{ HttpHeader, HttpRequest, HttpResponse, Uri } import akka.stream.ActorMaterializer import akka.stream.scaladsl.{ Sink, Source } import io.vamp.common.http.EventSource.EventSource import scala.collection.mutable import scala.concurrent.Future import scala.concurrent.duration.{ FiniteDuration, _ } import scala.language.postfixOps import scala.util.{ Failure, Success } private case class SseConnectionConfig(url: String, headers: List[(String, String)], tlsCheck: Boolean) private case class SseConnectionEntryValue(source: EventSource) trait SseListener { def onEvent(event: ServerSentEvent): Unit } object SseConnector { private val retryDelay: FiniteDuration = 5 second private val listeners: mutable.Map[SseConnectionConfig, Set[SseListener]] = mutable.Map() private val connections: mutable.Map[SseConnectionConfig, Future[Done]] = mutable.Map() def open(url: String, headers: List[(String, String)] = Nil, tlsCheck: Boolean)(listener: SseListener)(implicit system: ActorSystem, logger: LoggingAdapter): Unit = synchronized { val config = SseConnectionConfig(url, headers, tlsCheck) implicit val materializer: ActorMaterializer = ActorMaterializer() listeners.update(config, listeners.getOrElse(config, Set()) + listener) connections.getOrElseUpdate(config, { logger.info(s"Opening SSE connection: $url") EventSource(Uri(url), send(config), None, retryDelay).takeWhile { event ⇒ event.eventType.foreach(t ⇒ logger.info(s"SSE: $t")) val receivers = listeners.getOrElse(config, Set()) receivers.foreach(_.onEvent(event)) val continue = receivers.nonEmpty if (!continue) logger.info(s"Closing SSE connection: $url") continue }.runWith(Sink.ignore) }) } def close(listener: SseListener): Unit = synchronized { listeners.transform((_, v) ⇒ v - listener) } private def send(config: SseConnectionConfig)(request: HttpRequest)(implicit system: ActorSystem, materializer: ActorMaterializer): Future[HttpResponse] = { val httpHeaders = config.headers.map { case (k, v) ⇒ HttpHeader.parse(k, v) } collect { case Ok(h, _) ⇒ h } filterNot request.headers.contains Source.single(request.withHeaders(request.headers ++ httpHeaders) → 1).via(HttpClient.pool[Any](config.url, config.tlsCheck)).map { case (Success(response: HttpResponse), _) ⇒ response case (Failure(f), _) ⇒ throw new RuntimeException(f.getMessage) }.runWith(Sink.head) } }
Example 138
Source File: WSSecurityProperties.scala From scala-loci with Apache License 2.0 | 5 votes |
package loci package communicator package ws.akka import java.security.cert.Certificate import akka.http.scaladsl.model.{HttpMessage, HttpRequest, HttpResponse, Uri} import akka.http.scaladsl.model.headers._ import akka.http.scaladsl.model.ws.WebSocketRequest private case class WSSecurityProperties( isAuthenticated: Boolean, isProtected: Boolean, isEncrypted: Boolean, certificates: Seq[Certificate]) private object WSSecurityProperties { final val HTTPS = "https" final val WSS = "wss" final val NoProtocol = "NONE" // TLSv1, etc. final val NoCipher = "SSL_NULL_WITH_NULL_NULL" // see RFC2246, RFC3268, etc. final val NoEncryptionFragment = "WITH_NULL" def apply(request: WebSocketRequest, response: HttpResponse, authenticated: Boolean): WSSecurityProperties = create(request.uri, response, authenticated) def apply(request: HttpRequest, authenticated: Boolean): WSSecurityProperties = create(request.uri, request, authenticated) private def create(uri: Uri, message: HttpMessage, authenticated: Boolean) : WSSecurityProperties = { val tls = uri.scheme == HTTPS || uri.scheme == WSS val properties = message.header[`Tls-Session-Info`] map { info => val protocol = info.session.getProtocol val cipher = info.session.getCipherSuite val tls = protocol != NoProtocol && cipher != NoCipher val certificates = info.peerCertificates val isAuthenticated = tls && certificates.nonEmpty val isProtected = tls val isEncrypted = tls && !(cipher contains NoEncryptionFragment) WSSecurityProperties(authenticated || isAuthenticated, isProtected, isEncrypted, certificates) } properties getOrElse { WSSecurityProperties(authenticated, tls, tls, Seq.empty) } } }