play.api.libs.ws.WSClient Scala Examples

The following examples show how to use play.api.libs.ws.WSClient. 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: BintrayDownloadPoms.scala    From scaladex   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
package ch.epfl.scala.index.data
package bintray

import download.PlayWsDownloader
import java.nio.charset.StandardCharsets
import java.nio.file.{Files, Path}

import play.api.libs.ws.{WSClient, WSRequest, WSResponse}
import play.api.libs.ws.ahc.AhcWSClient
import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import org.slf4j.LoggerFactory

class BintrayDownloadPoms(paths: DataPaths)(
    implicit val system: ActorSystem,
    implicit val materializer: ActorMaterializer
) extends PlayWsDownloader {

  private val log = LoggerFactory.getLogger(getClass)

  private val bintrayPomBase = paths.poms(LocalPomRepository.Bintray)

  
  def run(): Unit = {

    download[BintraySearch, Unit]("Downloading POMs",
                                  searchesBySha1,
                                  downloadRequest,
                                  processPomDownload,
                                  parallelism = 32)
    ()
  }
} 
Example 2
Source File: AkkaDiscoveryServiceLocatorSpec.scala    From lagom   with Apache License 2.0 5 votes vote down vote up
package com.lightbend.lagom.scaladsl.akka.discovery

import akka.NotUsed
import com.lightbend.lagom.scaladsl.api.Service
import com.lightbend.lagom.scaladsl.api.ServiceCall
import com.lightbend.lagom.scaladsl.api.ServiceLocator
import com.lightbend.lagom.scaladsl.server.LagomApplication
import com.lightbend.lagom.scaladsl.server.LagomApplicationContext
import com.lightbend.lagom.scaladsl.server.LagomServer
import com.lightbend.lagom.scaladsl.server.LocalServiceLocator
import com.lightbend.lagom.scaladsl.testkit.ServiceTest
import org.scalatest._
import play.api.libs.ws.WSClient
import play.api.libs.ws.ahc.AhcWSComponents

import scala.concurrent.Future
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AsyncWordSpec

class AkkaDiscoveryServiceLocatorSpec extends AsyncWordSpec with Matchers with BeforeAndAfterAll with OptionValues {
  "ServiceLocator" should {
    "retrieve registered services" in {
      val serviceLocator = server.application.serviceLocator

      serviceLocator.locate("fake-service").map { res =>
        res.value.toString shouldBe "http://fake-service-host:9119"
      }
    }
  }

  private val server = ServiceTest.startServer(ServiceTest.defaultSetup) { ctx =>
    new LagomTestApplication(ctx)
  }

  protected override def afterAll() = server.stop()

  class LagomTestApplication(ctx: LagomApplicationContext)
      extends LagomApplication(ctx)
      with AhcWSComponents
      with AkkaDiscoveryComponents {
    override def lagomServer: LagomServer = serverFor[TestService](new TestServiceImpl)
  }

  trait TestService extends Service {
    def hello(name: String): ServiceCall[NotUsed, String]

    override def descriptor = {
      import Service._
      named("test-service")
        .withCalls(
          pathCall("/hello/:name", hello _)
        )
        .withAutoAcl(true)
    }
  }

  class TestServiceImpl extends TestService {
    override def hello(name: String) = ServiceCall { _ =>
      Future.successful(s"Hello $name")
    }
  }
} 
Example 3
Source File: AdditionalRoutersSpec.scala    From lagom   with Apache License 2.0 5 votes vote down vote up
package com.lightbend.lagom.scaladsl.it.routers

import akka.NotUsed
import com.lightbend.lagom.scaladsl.api.ServiceLocator
import com.lightbend.lagom.scaladsl.server.LagomApplication
import com.lightbend.lagom.scaladsl.server.LagomApplicationContext
import com.lightbend.lagom.scaladsl.server.LagomServer
import com.lightbend.lagom.scaladsl.server.LocalServiceLocator
import com.lightbend.lagom.scaladsl.testkit.ServiceTest
import com.lightbend.lagom.scaladsl.testkit.ServiceTest.TestServer
import org.scalatest.concurrent.ScalaFutures
import play.api.http.DefaultWriteables
import play.api.http.HeaderNames
import play.api.libs.ws.WSClient
import play.api.libs.ws.ahc.AhcWSComponents
import play.api.mvc
import play.api.mvc._
import play.api.routing.SimpleRouterImpl
import play.api.test.FakeHeaders
import play.api.test.FakeRequest
import play.api.test.Helpers
import play.core.j.JavaRouterAdapter
import play.api.test.Helpers._

import scala.concurrent.ExecutionContext
import scala.concurrent.Future
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec

class AdditionalRoutersSpec extends AnyWordSpec with Matchers with ScalaFutures {
  "A LagomServer " should {
    "be extensible with a Play Router" in withServer { server =>
      val request = FakeRequest(GET, "/hello/")
      val result  = Helpers.route(server.application.application, request).get.futureValue

      result.header.status shouldBe OK
      val body = result.body.consumeData(server.materializer).futureValue.utf8String
      body shouldBe "hello"
    }
  }

  def withServer(block: TestServer[TestApp] => Unit): Unit = {
    ServiceTest.withServer(ServiceTest.defaultSetup.withCassandra(false).withCluster(false)) { ctx =>
      new TestApp(ctx)
    } { server =>
      block(server)
    }
  }

  class TestApp(context: LagomApplicationContext)
      extends LagomApplication(context)
      with AhcWSComponents
      with LocalServiceLocator {
    override def lagomServer: LagomServer =
      serverFor[AdditionalRoutersService](new AdditionalRoutersServiceImpl)
        .additionalRouter(FixedResponseRouter("hello").withPrefix("/hello"))
  }
}


object FixedResponseRouter {
  def apply(msg: String) =
    new SimpleRouterImpl({
      case _ =>
        new Action[Unit] {
          override def parser: BodyParser[Unit] = mvc.BodyParsers.utils.empty

          override def apply(request: Request[Unit]): Future[Result] =
            Future.successful(Results.Ok(msg))

          override def executionContext: ExecutionContext =
            scala.concurrent.ExecutionContext.global
        }
    })
} 
Example 4
package commons_test.test_helpers

import commons_test.test_helpers.WsScalaTestClientWithHost.TestWsClient
import org.scalatestplus.play.PortNumber
import play.api.libs.ws.{WSClient, WSRequest}

trait WsScalaTestClientWithHost {

  def wsUrl(url: String)(implicit testWsClient: TestWsClient): WSRequest = {
    testWsClient.url(url)
  }

}

object WsScalaTestClientWithHost {
  case class TestWsClient(host: Host, portNumber: PortNumber, wsClient: WSClient) {
    def url(url: String): WSRequest = {
      wsClient.url(host.value + portNumber.value + url)
    }
  }
} 
Example 5
Source File: ReCaptchaService.scala    From silhouette-vuejs-app   with Apache License 2.0 5 votes vote down vote up
package models.services.captcha

import javax.inject.Inject
import play.api.libs.json.{JsPath, Reads}
import play.api.libs.functional.syntax._
import play.api.libs.ws.WSClient

import scala.concurrent.{ExecutionContext, Future}

trait CaptchaService {
  def validate(response: String, remoteIp: String): Future[Boolean]
}

class ReCaptchaService @Inject()(config: ReCaptchaConfig, ws: WSClient)(implicit ec: ExecutionContext) extends CaptchaService {

  def validate(recaptchaResponse: String, remoteIp: String) = {
    ws
      .url("https://www.google.com/recaptcha/api/siteverify")
      .withHttpHeaders("Accept" -> "application/json")
      .withQueryStringParameters(
        "secret" -> config.secretKey,
        "response" -> recaptchaResponse,
        "remoteip" -> remoteIp
      )
      .get()
      .map(r => r.json.as[ReCaptchaValidationResponse])
      .map { r =>
        val e = r.errors.getOrElse(Vector())
        if (e.isEmpty) {
          r.success
        } else {
          throw new Exception("Failed to retrieve reCaptcha confirmed response: " + e.mkString(";"))
        }
      }
  }
}

case class ReCaptchaConfig(secretKey: String)

private[captcha] case class ReCaptchaValidationResponse(success: Boolean, errors: Option[Vector[String]])

private[captcha] object ReCaptchaValidationResponse {
  implicit val reads: Reads[ReCaptchaValidationResponse] = (
    (JsPath \ "success").read[Boolean] and
      (JsPath \ "error-codes").readNullable[Vector[String]]
    ) (ReCaptchaValidationResponse.apply _)
} 
Example 6
Source File: AthanorClient.scala    From tap   with Apache License 2.0 5 votes vote down vote up
package controllers.external.athanor

import io.heta.tap.data.results.StringListResult
import javax.inject.Inject
import io.heta.tap.util.AppConfig
import play.api.Logger
import play.api.libs.ws.{WSClient, WSRequest, WSResponse}

import scala.concurrent.{ExecutionContext, Future}
import scala.concurrent.duration._ // scalastyle:ignore




class AthanorClient @Inject()(wsClient: WSClient, config: AppConfig)(implicit ec: ExecutionContext) {

  val logger: Logger = Logger(this.getClass)

  val athanorURL= config.getAthanorURL

  def process(text:String,parameter:String,start:Long):Future[StringListResult] = {
    //logger.info(s"Analysing with athanor: $text")

    val url = athanorURL + parameter
    logger.info(s"Analysing with athanor at this url: $url")

    val request: WSRequest = wsClient.url(url)

    val athanorRequest: WSRequest = request
      .withHttpHeaders("Accept" -> "application/json")
      .withRequestTimeout(30000.millis)

    val futureResponse: Future[WSResponse] = athanorRequest.post(text)


    val result: Future[StringListResult] = {
      val decoded = futureResponse.map { response =>
        val res = decodeRepsonse(response)
        val queryTime = (System.currentTimeMillis() - start).toInt
        StringListResult(res,"ok",querytime = queryTime)
      }
      val errMsg = "There was a problem connecting to the Athanor server."
      futureResponse.recover {
        case e: Any => {
          val msg = s"$errMsg: $e"
          logger.error(msg)
          StringListResult(Vector(),msg)
        }
        //case _ => logger.error(errMsg)
      }
      decoded
    }
    result
  }

  case class AthanorMsg(message: String, results: Vector[Vector[String]])

  def decodeRepsonse(response:WSResponse): Vector[Vector[String]] = {
    val resBody = response.body
    if(resBody.nonEmpty && resBody.contains(":[[")) {
      logger.debug("Decoding response: "+ resBody)

      import play.api.libs.functional.syntax._ //scalastyle:ignore
      import play.api.libs.json._ //scalastyle:ignore

      implicit val AMWrites: Writes[AthanorMsg] = (
        (JsPath \ "message").write[String] and
          (JsPath \ "results").write[Vector[Vector[String]]]
        ) (unlift(AthanorMsg.unapply))

      implicit val AMReads: Reads[AthanorMsg] = (
        (JsPath \ "message").read[String] and
          (JsPath \ "results").read[Vector[Vector[String]]]
        ) (AthanorMsg.apply _)

      val athanorMsg:AthanorMsg = response.json.as[AthanorMsg]
      logger.debug("Athanor message: " + athanorMsg.message)
      logger.debug("Athanor results: " + athanorMsg.results)
      athanorMsg.results
    } else {
      logger.error("There was a problem: " + resBody) //TODO If we get to here, we need to return the message to the client!!
      Vector()
    }
  }
} 
Example 7
Source File: WSClientProvider.scala    From http-verbs   with Apache License 2.0 5 votes vote down vote up
package uk.gov.hmrc.play.connectors

import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import play.api.libs.ws.WSClient
import play.api.libs.ws.ahc.{AhcConfigBuilder, AhcWSClient}

trait WSClientProvider {
  implicit val client: WSClient
}

trait DefaultWSClientProvider extends WSClientProvider {
  val builder    = new AhcConfigBuilder()
  val ahcBuilder = builder.configure()
  val ahcConfig  = ahcBuilder.build()

  implicit val system       = ActorSystem()
  implicit val materializer = ActorMaterializer()

  implicit val client = new AhcWSClient(ahcConfig)
} 
Example 8
Source File: Connector.scala    From http-verbs   with Apache License 2.0 5 votes vote down vote up
package uk.gov.hmrc.play.connectors

import play.api.libs.ws.{WSClient, WSRequest}
import uk.gov.hmrc.http.HeaderCarrier
import uk.gov.hmrc.play.HeaderCarrierConverter

trait RequestBuilder {
  def buildRequest(url: String)(implicit hc: HeaderCarrier): WSRequest
}

object RequestBuilder {
  def headers(hc: HeaderCarrier): Seq[(String, String)] =
    hc.headers.filter {
      case (name, value) => name != HeaderCarrierConverter.Path
    }
}

trait WSClientRequestBuilder extends RequestBuilder {
  def client: WSClient
  def buildRequest(url: String)(implicit hc: HeaderCarrier): WSRequest =
    client.url(url).withHttpHeaders(RequestBuilder.headers(hc): _*)
} 
Example 9
Source File: HttpTimeoutSpec.scala    From http-verbs   with Apache License 2.0 5 votes vote down vote up
package uk.gov.hmrc.play.http

import java.net.{ServerSocket, URI}
import java.util.concurrent.TimeoutException

import org.scalatest.concurrent.ScalaFutures
import org.scalatest.BeforeAndAfterAll
import org.scalatest.wordspec.AnyWordSpecLike
import org.scalatest.matchers.should.Matchers
import org.webbitserver.handler.{DelayedHttpHandler, StringHttpHandler}
import org.webbitserver.netty.NettyWebServer
import play.api.inject.guice.GuiceApplicationBuilder
import play.api.libs.ws.WSClient
import play.api.test.WsTestClient
import play.api.{Configuration, Play}
import uk.gov.hmrc.http.HeaderCarrier
import uk.gov.hmrc.play.http.ws.WSHttp
import uk.gov.hmrc.play.test.TestHttpCore

import scala.concurrent.{Await, ExecutionContext}
import scala.concurrent.duration.DurationInt

class HttpTimeoutSpec extends AnyWordSpecLike with Matchers with ScalaFutures with BeforeAndAfterAll {

  import ExecutionContext.Implicits.global

  lazy val fakeApplication =
    GuiceApplicationBuilder(configuration = Configuration("play.ws.timeout.request" -> "1000ms")).build()

  override def beforeAll() {
    super.beforeAll()
    Play.start(fakeApplication)
  }

  override def afterAll() {
    super.afterAll()
    Play.stop(fakeApplication)
  }

  WsTestClient.withClient{ client =>

    "HttpCalls" should {

      "be gracefully timeout when no response is received within the 'timeout' frame" in {
        val http = new WSHttp with TestHttpCore {
          override val wsClient = fakeApplication.injector.instanceOf[WSClient]
        }

        // get an unused port
        val ss = new ServerSocket(0)
        ss.close()
        val executor = ExecutionContext.global // fromExecutorService(ExecutionContext.global)
        val publicUri = URI.create(s"http://localhost:${ss.getLocalPort}")
        val ws        = new NettyWebServer(executor, ss.getLocalSocketAddress, publicUri)
        try {
          //starts web server
          ws.add(
            "/test",
            new DelayedHttpHandler(executor, 2000, new StringHttpHandler("application/json", "{name:'pong'}")))
          ws.start().get()

          implicit val hc = HeaderCarrier()

          val start = System.currentTimeMillis()
          intercept[TimeoutException] {
            //make request to web server
            Await.result(http.doPost(s"$publicUri/test", "{name:'ping'}", Seq()), 5.seconds)
          }
          val diff = (System.currentTimeMillis() - start).toInt
          // there is test execution delay around 700ms
          diff should be >= 1000
          diff should be < 2500
        } finally {
          ws.stop()
        }
      }
    }
  }
} 
Example 10
Source File: CouchDBStorageModule.scala    From cluster-broccoli   with Apache License 2.0 5 votes vote down vote up
package de.frosner.broccoli.instances.storage.couchdb

import com.google.inject.{AbstractModule, Provides, Singleton}
import de.frosner.broccoli.BroccoliConfiguration
import de.frosner.broccoli.instances.storage.InstanceStorage
import net.codingwell.scalaguice.ScalaModule
import play.api.inject.ApplicationLifecycle
import play.api.libs.ws.WSClient

import scala.concurrent.Future


  @Provides
  @Singleton
  def provideCouchDBInstanceStorage(
      config: BroccoliConfiguration,
      client: WSClient,
      applicationLifecycle: ApplicationLifecycle
  ): InstanceStorage = {
    val couchdbConfig = config.instances.storage.couchdb
    val storage = new CouchDBInstanceStorage(couchdbConfig.url, couchdbConfig.database, client)
    applicationLifecycle.addStopHook(() => {
      if (!storage.isClosed) {
        storage.close()
      }
      Future.successful({})
    })
    storage
  }
} 
Example 11
Source File: WSClientContext.scala    From cluster-broccoli   with Apache License 2.0 5 votes vote down vote up
package de.frosner.broccoli.test.contexts

import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import org.specs2.execute.{AsResult, Result}
import org.specs2.specification.ForEach
import org.specs2.specification.mutable.ExecutionEnvironment
import play.api.libs.ws.WSClient
import play.api.libs.ws.ahc.AhcWSClient


trait WSClientContext extends ForEach[WSClient] {
  self: ExecutionEnvironment =>

  override protected def foreach[R: AsResult](f: (WSClient) => R): Result = {
    implicit val actorSystem = ActorSystem("nomad-http-client")
    try {
      implicit val materializer = ActorMaterializer()
      val client: WSClient = AhcWSClient()
      try AsResult(f(client))
      finally client.close()
    } finally {
      actorSystem.terminate()
    }
  }
} 
Example 12
Source File: NomadHttpClientIntegrationSpec.scala    From cluster-broccoli   with Apache License 2.0 5 votes vote down vote up
package de.frosner.broccoli.nomad

import cats.instances.future._
import com.netaporter.uri.Uri
import com.netaporter.uri.dsl._
import de.frosner.broccoli.nomad.models.{Allocation, Job, WithId}
import de.frosner.broccoli.test.contexts.WSClientContext
import de.frosner.broccoli.test.contexts.docker.BroccoliDockerContext
import de.frosner.broccoli.test.contexts.docker.BroccoliTestService.{Broccoli, Nomad}
import org.scalacheck.Gen
import org.specs2.concurrent.ExecutionEnv
import org.specs2.mutable.Specification
import org.specs2.specification.mutable.ExecutionEnvironment
import play.api.libs.json.Json
import play.api.libs.ws.WSClient

import scala.collection.immutable
import scala.concurrent.blocking
import scala.concurrent.duration._

class NomadHttpClientIntegrationSpec
    extends Specification
    with WSClientContext
    with BroccoliDockerContext
    with ExecutionEnvironment {

  
  override def broccoliDockerConfig: BroccoliDockerContext.Configuration =
    BroccoliDockerContext.Configuration.services(Broccoli, Nomad)

  private val broccoliApi = "http://localhost:9000/api/v1"

  override def is(implicit executionEnv: ExecutionEnv): Any =
    "The NomadHttpClient" should {
      "get allocations for a running nomad job" >> { wsClient: WSClient =>
        // Generate a random identifier for the instance
        val identifier = Gen.resize(10, Gen.identifier).sample.get
        val client = new NomadHttpClient(Uri.parse("http://localhost:4646"), wsClient)
        (for {
          // Create and start a simple instance to look at it's allocations
          _ <- wsClient
            .url(broccoliApi / "instances")
            .post(
              Json.obj("templateId" -> "http-server",
                       "parameters" -> Json.obj(
                         "id" -> identifier
                       )))
            .map(response => {
              // Ensure that the
              response.status must beEqualTo(201)
              response
            })
          _ <- wsClient
            .url(broccoliApi / "instances" / identifier)
            .post(Json.obj("status" -> "running"))
            .map(response => {
              response.status must beEqualTo(200)
              // Wait until the service is up
              blocking(Thread.sleep(1.seconds.toMillis))
              response
            })
          allocations <- client.getAllocationsForJob(shapeless.tag[Job.Id](identifier)).value
        } yield {
          allocations must beRight(
            (v: WithId[immutable.Seq[Allocation]]) => (v.jobId === identifier) and (v.payload must have length 1))
        }).await(5, broccoliDockerConfig.startupPatience + 2.seconds)
      }
    }
} 
Example 13
Source File: DownloadParentPoms.scala    From scaladex   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
package ch.epfl.scala.index
package data
package maven

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

import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import ch.epfl.scala.index.data.download.PlayWsDownloader
import org.slf4j.LoggerFactory
import play.api.libs.ws.{WSClient, WSRequest, WSResponse}

import scala.util.Failure

class DownloadParentPoms(repository: LocalPomRepository,
                         paths: DataPaths,
                         tmp: Option[Path] = None)(
    implicit val system: ActorSystem,
    implicit val materializer: ActorMaterializer
) extends PlayWsDownloader {

  private val log = LoggerFactory.getLogger(getClass)

  assert(
    repository == LocalPomRepository.MavenCentral || repository == LocalPomRepository.Bintray
  )

  val parentPomsPath = paths.parentPoms(repository)
  val pomReader =
    tmp match {
      case Some(path) => PomsReader.tmp(paths, path)
      case None       => PomsReader(repository, paths)
    }

  
    val parentPomsToDownload: Set[Dependency] =
      pomReader
        .load()
        .collect {
          case Failure(m: MissingParentPom) => m.dep
        }
        .toSet

    log.debug(s"to download: ${parentPomsToDownload.size}")
    log.debug(s"last failed: $lastFailedToDownload")

    if (parentPomsToDownload.size > lastFailedToDownload) {

      val downloaded =
        download[Dependency, Int]("Download parent POMs",
                                  parentPomsToDownload,
                                  downloadRequest,
                                  processResponse,
                                  parallelism = 32)
      val failedDownloads = downloaded.sum

      log.warn(s"failed downloads: $failedDownloads")

      if (0 < failedDownloads && parentPomsToDownload.size != failedDownloads) {

        run(failedDownloads) // grand-parent poms, etc
      }
    }
  }
} 
Example 14
Source File: EndpointsTest.scala    From endpoints4s   with MIT License 5 votes vote down vote up
package endpoints4s.play.client

import endpoints4s.algebra.client
import endpoints4s.algebra
import endpoints4s.algebra.circe
import play.api.libs.ws.WSClient
import play.api.test.WsTestClient

import scala.concurrent.{ExecutionContext, Future}

class TestClient(address: String, wsClient: WSClient)(implicit
    EC: ExecutionContext
) extends Endpoints(address, wsClient)
    with BasicAuthentication
    with JsonEntitiesFromCodecs
    with algebra.BasicAuthenticationTestApi
    with algebra.EndpointsTestApi
    with algebra.JsonFromCodecTestApi
    with algebra.TextEntitiesTestApi
    with algebra.SumTypedEntitiesTestApi
    with circe.JsonFromCirceCodecTestApi
    with circe.JsonEntitiesFromCodecs

class EndpointsTest
    extends client.EndpointsTestSuite[TestClient]
    with client.BasicAuthTestSuite[TestClient]
    with client.JsonFromCodecTestSuite[TestClient]
    with client.SumTypedEntitiesTestSuite[TestClient]
    with client.TextEntitiesTestSuite[TestClient] {

  import ExecutionContext.Implicits.global

  val wsClient = new WsTestClient.InternalWSClient("http", wiremockPort)
  val client: TestClient =
    new TestClient(s"http://localhost:$wiremockPort", wsClient)

  def call[Req, Resp](
      endpoint: client.Endpoint[Req, Resp],
      args: Req
  ): Future[Resp] = endpoint(args)

  def encodeUrl[A](url: client.Url[A])(a: A): String = url.encode(a)

  clientTestSuite()
  basicAuthSuite()
  jsonFromCodecTestSuite()
  sumTypedRequestsTestSuite()
  textEntitiesTestSuite()

  override def afterAll(): Unit = {
    wsClient.close()
    Thread.sleep(
      6000
    ) // Unfortunate hack to let WSTestClient terminate its ActorSystem. See https://github.com/playframework/playframework/blob/8b0d5afb8c353dd8cd8d9e8057136e1858ad0173/transport/client/play-ahc-ws/src/main/scala/play/api/test/WSTestClient.scala#L142
    super.afterAll()
  }

} 
Example 15
Source File: MillinerHatSignup.scala    From HAT2.0   with GNU Affero General Public License v3.0 5 votes vote down vote up
package org.hatdex.hat.resourceManagement

import org.hatdex.hat.resourceManagement.models.HatSignup
import play.api.cache.AsyncCacheApi
import play.api.http.Status._
import play.api.libs.json.{ JsError, JsSuccess }
import play.api.libs.ws.{ WSClient, WSRequest, WSResponse }
import play.api.{ Configuration, Logger }

import scala.concurrent.duration._
import scala.concurrent.{ ExecutionContext, Future }

trait MillinerHatSignup {
  val logger: Logger
  val ws: WSClient
  val configuration: Configuration
  val schema: String = configuration.get[String]("resourceManagement.millinerAddress") match {
    case address if address.startsWith("https") => "https://"
    case address if address.startsWith("http")  => "http://"
    case _                                      => "https://"
  }

  val millinerAddress: String = configuration.get[String]("resourceManagement.millinerAddress")
    .stripPrefix("http://")
    .stripPrefix("https://")
  val hatSharedSecret: String = configuration.get[String]("resourceManagement.hatSharedSecret")

  val cache: AsyncCacheApi

  def getHatSignup(hatAddress: String)(implicit ec: ExecutionContext): Future[HatSignup] = {
    // Cache the signup information for subsequent calls (For private/public key and database details)
    cache.getOrElseUpdate[HatSignup](s"configuration:$hatAddress") {
      val request: WSRequest = ws.url(s"$schema$millinerAddress/api/manage/configuration/$hatAddress")
        .withVirtualHost(millinerAddress)
        .withHttpHeaders("Accept" -> "application/json", "X-Auth-Token" -> hatSharedSecret)

      val futureResponse: Future[WSResponse] = request.get()
      futureResponse.map { response =>
        response.status match {
          case OK =>
            response.json.validate[HatSignup] match {
              case signup: JsSuccess[HatSignup] =>
                logger.debug(s"Got back configuration: ${signup.value}")
                cache.set(s"configuration:$hatAddress", signup.value, 1.minute)
                signup.value
              case e: JsError =>
                logger.error(s"Parsing HAT configuration failed: $e")
                throw new HatServerDiscoveryException("Fetching HAT configuration failed")
            }
          case _ =>
            logger.error(s"Fetching HAT configuration failed: ${response.body}")
            throw new HatServerDiscoveryException("Fetching HAT configuration failed")
        }
      }
    }
  }

} 
Example 16
Source File: HatDatabaseProvider.scala    From HAT2.0   with GNU Affero General Public License v3.0 5 votes vote down vote up
package org.hatdex.hat.resourceManagement

import javax.inject.{ Inject, Singleton }

import com.typesafe.config.{ Config, ConfigFactory }
import org.hatdex.hat.dal.HatDbSchemaMigration
import org.hatdex.hat.resourceManagement.models.HatSignup
import org.hatdex.libs.dal.HATPostgresProfile.api.Database
import play.api.cache.AsyncCacheApi
import play.api.libs.ws.WSClient
import play.api.{ Configuration, Logger }

import scala.concurrent.{ ExecutionContext, Future }
import scala.collection.JavaConverters._
import scala.concurrent.duration.Duration

trait HatDatabaseProvider {
  protected val configuration: Configuration

  def database(hat: String)(implicit ec: ExecutionContext): Future[Database]

  def shutdown(db: Database): Future[Unit] = {
    // Execution context for the future is defined by specifying the executor during initialisation

    db.shutdown
  }

  def update(db: Database)(implicit ec: ExecutionContext): Future[Unit] = {
    new HatDbSchemaMigration(configuration, db, ec).run("hat.schemaMigrations")
  }
}

@Singleton
class HatDatabaseProviderConfig @Inject() (val configuration: Configuration) extends HatDatabaseProvider {
  def database(hat: String)(implicit ec: ExecutionContext): Future[Database] = {
    Future {
      Database.forConfig(s"hat.${hat.replace(':', '.')}.database", configuration.underlying)
    } recoverWith {
      case e =>
        Future.failed(new HatServerDiscoveryException(s"Database configuration for $hat incorrect or unavailable", e))
    }
  }
}

@Singleton
class HatDatabaseProviderMilliner @Inject() (
    val configuration: Configuration,
    val cache: AsyncCacheApi,
    val ws: WSClient) extends HatDatabaseProvider with MillinerHatSignup {
  val logger = Logger(this.getClass)

  def database(hat: String)(implicit ec: ExecutionContext): Future[Database] = {
    getHatSignup(hat) map { signup =>
      val config = signupDatabaseConfig(signup)
      //      val databaseUrl = s"jdbc:postgresql://${signup.databaseServer.get.host}:${signup.databaseServer.get.port}/${signup.database.get.name}"
      //      val executor = AsyncExecutor(hat, numThreads = 3, queueSize = 1000)
      //      Database.forURL(databaseUrl, signup.database.get.name, signup.database.get.password, driver = "org.postgresql.Driver"  )
      Database.forConfig("", config)
    }
  }

  def signupDatabaseConfig(signup: HatSignup): Config = {
    val database = signup.database.get
    val config = Map(
      "dataSourceClass" -> "org.postgresql.ds.PGSimpleDataSource",
      "properties" -> Map[String, String](
        "user" -> database.name,
        "password" -> database.password,
        "databaseName" -> database.name,
        "portNumber" -> signup.databaseServer.get.port.toString,
        "serverName" -> signup.databaseServer.get.host).asJava,
      "numThreads" -> configuration.get[Int]("resourceManagement.hatDBThreads").toString,
      "idleTimeout" -> configuration.get[Duration]("resourceManagement.hatDBIdleTimeout").toMillis.toString).asJava

    ConfigFactory.parseMap(config)
  }
} 
Example 17
Source File: TrustedApplicationProvider.scala    From HAT2.0   with GNU Affero General Public License v3.0 5 votes vote down vote up
package org.hatdex.hat.api.service.applications

import javax.inject.Inject

import org.hatdex.dex.apiV2.services.DexClient
import org.hatdex.hat.api.models.applications.Application
import org.hatdex.hat.api.service.RemoteExecutionContext
import play.api.Configuration
import play.api.cache.AsyncCacheApi
import play.api.libs.ws.WSClient

import scala.concurrent.Future
import scala.concurrent.duration.FiniteDuration
import scala.concurrent.duration._

trait TrustedApplicationProvider {
  def applications: Future[Seq[Application]]

  def application(id: String): Future[Option[Application]]
}

class TrustedApplicationProviderDex @Inject() (
    wsClient: WSClient,
    configuration: Configuration,
    cache: AsyncCacheApi)(implicit val rec: RemoteExecutionContext) extends TrustedApplicationProvider {

  private val dexClient = new DexClient(
    wsClient,
    configuration.underlying.getString("exchange.address"),
    configuration.underlying.getString("exchange.scheme"),
    "v1.1")

  private val includeUnpublished: Boolean = configuration.getOptional[Boolean]("exchange.beta").getOrElse(false)

  private val dexApplicationsCacheDuration: FiniteDuration = 30.minutes

  def applications: Future[Seq[Application]] = {
    cache.getOrElseUpdate("apps:dexApplications", dexApplicationsCacheDuration) {
      dexClient.applications(includeUnpublished = includeUnpublished)
    }
  }

  def application(id: String): Future[Option[Application]] = {
    applications.map(_.find(_.id == id))
  }
} 
Example 18
Source File: ApplicationRequestProxy.scala    From HAT2.0   with GNU Affero General Public License v3.0 5 votes vote down vote up
package org.hatdex.hat.api.controllers

import com.mohiva.play.silhouette.api.Silhouette
import javax.inject.Inject
import org.hatdex.hat.api.json.ApplicationJsonProtocol
import org.hatdex.hat.api.models.applications.HatApplication
import org.hatdex.hat.api.models.{ ApplicationManage, ErrorMessage, Owner }
import org.hatdex.hat.api.service.RemoteExecutionContext
import org.hatdex.hat.api.service.applications.ApplicationsService
import org.hatdex.hat.authentication.{ ContainsApplicationRole, HatApiAuthEnvironment, HatApiController, WithRole }
import play.api.Logger
import play.api.http.HttpEntity
import play.api.libs.json.Json
import play.api.libs.ws.WSClient
import play.api.mvc.{ Action, AnyContent, ControllerComponents }

import scala.concurrent.Future

class ApplicationRequestProxy @Inject() (
    components: ControllerComponents,
    silhouette: Silhouette[HatApiAuthEnvironment],
    wsClient: WSClient)(
    implicit
    val ec: RemoteExecutionContext,
    applicationsService: ApplicationsService)
  extends HatApiController(components, silhouette) with ApplicationJsonProtocol {

  import org.hatdex.hat.api.json.HatJsonFormats.errorMessage

  val logger = Logger(this.getClass)

  def proxyRequest(id: String, path: String, method: String = "GET"): Action[AnyContent] = SecuredAction(ContainsApplicationRole(Owner(), ApplicationManage(id)) || WithRole(Owner())).async { implicit request =>
    logger.info(s"Proxy $method request for $id to $path with parameters: ${request.queryString}")
    applicationsService.applicationStatus(id).flatMap { maybeStatus ⇒
      maybeStatus map {
        case HatApplication(app, _, true, _, _, _) ⇒

          applicationsService.applicationToken(request.identity, app)
            .flatMap { token ⇒
              val baseRequest = wsClient.url(s"${app.kind.url}/$path")
                .withHttpHeaders("x-auth-token" → token.accessToken)
                .addQueryStringParameters(request.queryString.map(p ⇒ (p._1, p._2.head)).toSeq: _*)
                .withMethod(method)

              request.body.asJson.fold(baseRequest)(b ⇒ baseRequest.withBody(b))
                .stream()
                .map(r ⇒ new Status(r.status).sendEntity(HttpEntity.Strict(r.bodyAsBytes, Some("application/json"))))
            }

        case _ ⇒ Future.successful(BadRequest(Json.toJson(ErrorMessage(
          "Application not active",
          s"Application $id does not appear to be activated by the user"))))
      } getOrElse {
        Future.successful(NotFound(Json.toJson(ErrorMessage(
          "Application not Found",
          s"Application $id does not appear to be a valid application registered with the DEX"))))
      }
    }
  }
} 
Example 19
Source File: AuthenticationEnvironment.scala    From play-silhouette-postgres-async-seed   with Apache License 2.0 5 votes vote down vote up
package services.user

import com.mohiva.play.silhouette.api.util.{ Clock, PlayHTTPLayer }
import com.mohiva.play.silhouette.api.{ Environment, EventBus }
import com.mohiva.play.silhouette.impl.authenticators._
import com.mohiva.play.silhouette.impl.providers.{ BasicAuthProvider, CredentialsProvider }
import com.mohiva.play.silhouette.impl.repositories.DelegableAuthInfoRepository
import com.mohiva.play.silhouette.impl.services.GravatarService
import com.mohiva.play.silhouette.impl.util.{ BCryptPasswordHasher, DefaultFingerprintGenerator, SecureRandomIDGenerator }
import models.user.User
import play.api.libs.ws.WSClient

import scala.concurrent.duration._

@javax.inject.Singleton
class AuthenticationEnvironment @javax.inject.Inject() (val wsClient: WSClient) extends Environment[User, CookieAuthenticator] {
  override implicit val executionContext = play.api.libs.concurrent.Execution.Implicits.defaultContext

  private[this] val fingerprintGenerator = new DefaultFingerprintGenerator(false)

  override val identityService = UserSearchService

  val userService = UserService

  private[this] val httpLayer = new PlayHTTPLayer(wsClient)

  val hasher = new BCryptPasswordHasher()

  val idGenerator = new SecureRandomIDGenerator()

  val clock = Clock()

  val authInfoService = new DelegableAuthInfoRepository(PasswordInfoService, OAuth1InfoService, OAuth2InfoService, OpenIdInfoService)

  val credentials = new CredentialsProvider(authInfoService, hasher, Seq(hasher))

  private[this] val sap = new SocialAuthProviders(play.api.Play.current.configuration, httpLayer, hasher, authInfoService, credentials, idGenerator, clock)

  val authProvider = new BasicAuthProvider(authInfoService, hasher, Nil)

  override val requestProviders = Seq(authProvider)
  val providersSeq = sap.providers
  val providersMap = sap.providers.toMap

  val avatarService = new GravatarService(httpLayer)

  override val eventBus = EventBus()

  override val authenticatorService = {
    val cfg = play.api.Play.current.configuration.getConfig("silhouette.authenticator.cookie").getOrElse {
      throw new IllegalArgumentException("Missing cookie configuration.")
    }
    new CookieAuthenticatorService(CookieAuthenticatorSettings(
      cookieName = cfg.getString("name").getOrElse(throw new IllegalArgumentException()),
      cookiePath = cfg.getString("path").getOrElse(throw new IllegalArgumentException()),
      cookieDomain = cfg.getString("domain"),
      secureCookie = cfg.getBoolean("secure").getOrElse(throw new IllegalArgumentException()),
      httpOnlyCookie = true,
      useFingerprinting = cfg.getBoolean("useFingerprinting").getOrElse(throw new IllegalArgumentException()),
      cookieMaxAge = cfg.getInt("maxAge").map(_.seconds),
      authenticatorIdleTimeout = cfg.getInt("idleTimeout").map(_.seconds),
      authenticatorExpiry = cfg.getInt("expiry").map(_.seconds).getOrElse(throw new IllegalArgumentException())
    ), Some(SessionInfoService), fingerprintGenerator, idGenerator, clock)
  }
} 
Example 20
Source File: PlaySpecs2Spec.scala    From play-grpc   with Apache License 2.0 5 votes vote down vote up
package play.grpc.specs2

import org.junit.runner.RunWith
import org.specs2.runner.JUnitRunner

import play.api.inject.bind
import play.api.inject.guice.GuiceApplicationBuilder
import play.api.libs.ws.WSClient
import play.api.libs.ws.WSRequest
import play.api.routing.Router
import play.api.test._

import akka.grpc.internal.GrpcProtocolNative

import example.myapp.helloworld.grpc.helloworld._

import io.grpc.Status


@RunWith(classOf[JUnitRunner])
class PlaySpecs2Spec extends ForServer with ServerGrpcClient with PlaySpecification with ApplicationFactories {

  protected def applicationFactory: ApplicationFactory =
    withGuiceApp(GuiceApplicationBuilder().overrides(bind[Router].to[GreeterServiceImpl]))

  // RICH: Still need to work out how to make WSClient work properly with endpoints
  def wsUrl(path: String)(implicit running: RunningServer): WSRequest = {
    val ws  = running.app.injector.instanceOf[WSClient]
    val url = running.endpoints.httpEndpoint.get.pathUrl(path)
    ws.url(url)
  }

  "A Play server bound to a gRPC router" should {
    "give a 404 when routing a non-gRPC request" >> { implicit rs: RunningServer =>
      val result = await(wsUrl("/").get)
      result.status must ===(404) // Maybe should be a 426, see #396
    }
    "give a 415 error when not using a gRPC content-type" >> { implicit rs: RunningServer =>
      val result = await(wsUrl(s"/${GreeterService.name}/FooBar").get)
      result.status must ===(415) // Maybe should be a 426, see #396
    }
    "give a grpc UNIMPLEMENTED when routing a non-existent gRPC method" >> { implicit rs: RunningServer =>
      val result = await(
        wsUrl(s"/${GreeterService.name}/FooBar")
          .addHttpHeaders("Content-Type" -> GrpcProtocolNative.contentType.toString)
          .get,
      )
      result.status must ===(200) // Maybe should be a 426, see #396
      result.header("grpc-status") must beSome(Status.Code.UNIMPLEMENTED.value().toString)
    }
    "give a grpc INVALID_ARGUMENT error when routing an empty request to a gRPC method" >> {
      implicit rs: RunningServer =>
        val result = await(
          wsUrl(s"/${GreeterService.name}/SayHello")
            .addHttpHeaders("Content-Type" -> GrpcProtocolNative.contentType.toString)
            .get,
        )
        result.status must ===(200) // Maybe should be a 426, see #396

        // grpc-status 3 means INVALID_ARGUMENT error. See https://developers.google.com/maps-booking/reference/grpc-api/status_codes
        result.header("grpc-status") must beSome(Status.Code.INVALID_ARGUMENT.value().toString)
    }
    "work with a gRPC client" >> { implicit rs: RunningServer =>
      withGrpcClient[GreeterServiceClient] { client: GreeterServiceClient =>
        val reply = await(client.sayHello(HelloRequest("Alice")))
        reply.message must ===("Hello, Alice!")
      }
    }
  }
} 
Example 21
Source File: PlayScalaTestSpec.scala    From play-grpc   with Apache License 2.0 5 votes vote down vote up
package play.grpc.scalatest

import io.grpc.Status

import org.scalatest.concurrent.IntegrationPatience
import org.scalatest.concurrent.ScalaFutures
import org.scalatestplus.play.PlaySpec
import org.scalatestplus.play.guice.GuiceOneServerPerTest

import play.api.Application
import play.api.inject.bind
import play.api.inject.guice.GuiceApplicationBuilder
import play.api.libs.ws.WSClient
import play.api.routing.Router

import akka.grpc.internal.GrpcProtocolNative

import example.myapp.helloworld.grpc.helloworld._


class PlayScalaTestSpec
    extends PlaySpec
    with GuiceOneServerPerTest
    with ServerGrpcClient
    with ScalaFutures
    with IntegrationPatience {

  override def fakeApplication(): Application = {
    GuiceApplicationBuilder()
      .overrides(bind[Router].to[GreeterServiceImpl])
      .build()
  }

  implicit def ws: WSClient = app.injector.instanceOf(classOf[WSClient])

  "A Play server bound to a gRPC router" must {
    "give a 404 when routing a non-gRPC request" in {
      val result = wsUrl("/").get.futureValue
      result.status must be(404) // Maybe should be a 426, see #396
    }
    "give a 415 error when not using a gRPC content-type" in {
      val result = wsUrl(s"/${GreeterService.name}/FooBar").get.futureValue
      result.status must be(415)
    }
    "give a grpc 'unimplemented' error when routing a non-existent gRPC method" in {
      val result = wsUrl(s"/${GreeterService.name}/FooBar")
        .addHttpHeaders("Content-Type" -> GrpcProtocolNative.contentType.toString)
        .get
        .futureValue
      result.status must be(200) // Maybe should be a 426, see #396
      result.header("grpc-status") mustEqual Some(Status.Code.UNIMPLEMENTED.value().toString)
    }
    "give a grpc 'invalid argument' error when routing an empty request to a gRPC method" in {
      val result = wsUrl(s"/${GreeterService.name}/SayHello")
        .addHttpHeaders("Content-Type" -> GrpcProtocolNative.contentType.toString)
        .get
        .futureValue
      result.status must be(200)
      result.header("grpc-status") mustEqual Some(Status.Code.INVALID_ARGUMENT.value().toString)
    }
    "work with a gRPC client" in withGrpcClient[GreeterServiceClient] { client: GreeterServiceClient =>
      val reply = client.sayHello(HelloRequest("Alice")).futureValue
      reply.message must be("Hello, Alice!")
    }
  }
} 
Example 22
Source File: LeaderProxyFilter.scala    From metronome   with Apache License 2.0 5 votes vote down vote up
package dcos.metronome
package api.v1

import akka.util.ByteString
import dcos.metronome.api.ApiConfig
import mesosphere.marathon.core.election.ElectionService
import org.slf4j.LoggerFactory
import play.api.http.HttpEntity
import play.api.libs.streams.Accumulator
import play.api.libs.ws.WSClient
import play.api.mvc._

class LeaderProxyFilter(ws: WSClient, electionService: ElectionService, config: ApiConfig)
    extends EssentialFilter
    with Results {

  import LeaderProxyFilter._
  import scala.concurrent.ExecutionContext.Implicits.global
  val log = LoggerFactory.getLogger(getClass)

  val localHostPort = config.hostnameWithPort
  val localRoutes = Set("/ping", "/v1/metrics")

  override def apply(next: EssentialAction): EssentialAction =
    new EssentialAction {
      override def apply(request: RequestHeader): Accumulator[ByteString, Result] = {
        def isProxiedToSelf = request.headers.get(HEADER_VIA).contains(localHostPort)
        def doNotProxy() = localRoutes(request.path)
        if (electionService.isLeader || doNotProxy()) {
          next(request)
        } else if (isProxiedToSelf) {
          Accumulator.done(BadRequest("Prevent proxying already proxied request"))
        } else {
          electionService.leaderHostPort match {
            case Some(hostPort) => proxyRequest(request, hostPort)
            case None => Accumulator.done(ServiceUnavailable("No consistent leadership"))
          }
        }
      }
    }

  def proxyRequest(request: RequestHeader, leaderHostPort: String): Accumulator[ByteString, Result] = {
    log.info(s"Proxy request ${request.path} to $leaderHostPort")
    val headers = request.headers.headers ++ Seq(HEADER_LEADER -> leaderHostPort, HEADER_VIA -> localHostPort)
    val scheme = if (request.secure) "https" else "http"
    Accumulator.source[ByteString].mapFuture { source =>
      ws.url(s"$scheme://$leaderHostPort${request.path}?${request.rawQueryString}")
        .withMethod(request.method)
        .withHttpHeaders(headers: _*)
        .withRequestTimeout(config.leaderProxyTimeout)
        .withBody(source)
        .execute()
        .map { r =>
          val proxyHeaders = Map(HEADER_LEADER -> leaderHostPort, HEADER_VIA -> localHostPort)
          val responseHeaders = r.headers.map { case (k, v) => k -> v.mkString(", ") }
          val header = ResponseHeader(r.status, proxyHeaders ++ responseHeaders, Some(r.statusText))
          val body = HttpEntity.Strict(r.bodyAsBytes, None)
          Result(header, body)
        }
    }
  }
}

object LeaderProxyFilter {
  val HEADER_VIA = "X-VIA"
  val HEADER_LEADER = "X-LEADER"
} 
Example 23
Source File: OpenApiController.scala    From theGardener   with Apache License 2.0 5 votes vote down vote up
package controllers

import io.swagger.annotations.{ApiOperation, ApiResponse, ApiResponses}
import javax.inject._
import play.api.libs.json.Json
import play.api.libs.ws.WSClient
import play.api.mvc._

import scala.concurrent.ExecutionContext

class OpenApiController @Inject()(ws: WSClient)(implicit ec: ExecutionContext) extends InjectedController {

  @ApiOperation(value = "Apply get method for swagger ui", response = classOf[Any])
  @ApiResponses(Array(new ApiResponse(code = 404, message = "Action not found")))
  def getSwaggerResponse(url: String): Action[AnyContent] = Action.async {
    ws.url(url.replaceAll("amp", "&")).get().map(result => Ok(Json.parse(result.body)))
  }

  @ApiOperation(value = "Apply put method for swagger ui", response = classOf[Any])
  @ApiResponses(Array(new ApiResponse(code = 404, message = "Action not found")))
  def swaggerPutMethod(url: String, body: String): Action[AnyContent] = Action.async {
    if (!body.equals("undefined")) {
      ws.url(url.replaceAll("amp", "&")).put(Json.parse(body)).map(result => Ok(result.body))
    } else {
      ws.url(url.replaceAll("amp", "&")).put(Json.parse("{}")).map(result => Ok(result.body))
    }
  }

  @ApiOperation(value = "Apply post method for swagger ui", response = classOf[Any])
  @ApiResponses(Array(new ApiResponse(code = 404, message = "Action not found")))
  def swaggerPostMethod(url: String, body: String): Action[AnyContent] = Action.async {
    if (!body.equals("undefined")) {
      ws.url(url.replaceAll("amp", "&")).post(Json.parse(body)).map(result => Ok(result.body))
    } else {
      ws.url(url.replaceAll("amp", "&")).post(Json.parse("{}")).map(result => Ok(result.body))
    }
  }

  @ApiOperation(value = "Apply delete method for swagger ui", response = classOf[Any])
  @ApiResponses(Array(new ApiResponse(code = 404, message = "Action not found")))
  def swaggerDeleteMethod(url: String): Action[AnyContent] = Action.async {
    ws.url(url.replaceAll("amp", "&")).delete().map(result => Ok(result.body))
  }

} 
Example 24
Source File: PagerdutyNotifications.scala    From sundial   with MIT License 5 votes vote down vote up
package service.notifications

import java.util.UUID

import dao.SundialDaoFactory
import model.{PagerdutyNotification, Process, ProcessStatusType}
import play.api.Logging
import play.api.libs.ws.WSClient
import speedwing.pagerduty.api.v0.Client
import speedwing.pagerduty.api.v0.models.{CreateEvent, EventType}

import scala.util.{Failure, Success}

class PagerdutyNotifications(wsClient: WSClient, daoFactory: SundialDaoFactory)
    extends Notification
    with Logging {

  private final val PagerdutyPageMessage =
    "The Sundial Job %s, has failed at least %s time(s) in a row."

  override def notifyProcessFinished(processId: UUID): Unit =
    daoFactory.withSundialDao { implicit dao =>
      for {
        process <- dao.processDao.loadProcess(processId)
        processDef <- dao.processDefinitionDao.loadProcessDefinition(
          process.processDefinitionName)
      } yield {

        val pagerdutyNotifications = processDef.notifications.collect {
          case pagerduty: PagerdutyNotification => pagerduty
        }

        if (pagerdutyNotifications.nonEmpty) {
          val maxNumFailures =
            pagerdutyNotifications.map(_.numConsecutiveFailures).max
          val recentProcesses: Seq[Process] =
            dao.processDao.findProcesses(processDefinitionName =
                                           Some(process.processDefinitionName),
                                         limit = Some(maxNumFailures))
          val numConsecutiveFailedProcesses =
            getNumberConsecutiveFailures(recentProcesses)
          processPagerdutyNotifications(process,
                                        pagerdutyNotifications,
                                        numConsecutiveFailedProcesses)
        }
      }
    }

  private def processPagerdutyNotifications(
      process: Process,
      pagerdutyNotifications: Seq[PagerdutyNotification],
      numConsecutiveFailedProcesses: Int) = {
    import scala.concurrent.ExecutionContext.Implicits.global
    pagerdutyNotifications.foreach(pagerdutyNotification => {
      if (numConsecutiveFailedProcesses >= pagerdutyNotification.numConsecutiveFailures) {
        val createEvent = CreateEvent(
          pagerdutyNotification.serviceKey,
          EventType.Trigger,
          incidentKey = None,
          Some(
            PagerdutyPageMessage.format(
              process.processDefinitionName,
              pagerdutyNotification.numConsecutiveFailures.toString)),
          None,
          Some("Sundial"),
          None
        )
        val pagerdutyClient = new Client(wsClient, pagerdutyNotification.apiUrl)

        val pagerdutyRequest = pagerdutyClient.createEvents.post(createEvent)

        pagerdutyRequest.onComplete {
          case Success(pageId) =>
            logger.info(
              s"Successfully submitted Pagerduty request with Id [${pageId.incidentKey}]")
          case Failure(e) =>
            logger.error(s"Failed to submit Pagerduty request", e)
        }
      }
    })
  }

  private def getNumberConsecutiveFailures(recentProcesses: Seq[Process]): Int =
    recentProcesses
      .takeWhile(_.status.statusType == ProcessStatusType.Failed)
      .size

} 
Example 25
Source File: Config.scala    From sundial   with MIT License 5 votes vote down vote up
package config

import javax.inject.Inject
import com.google.inject.{AbstractModule, Provides, Singleton}
import dao.SundialDaoFactory
import dto.DisplayModels
import org.lyranthe.prometheus.client.{DefaultRegistry, Registry, jmx}
import play.api.libs.ws.WSClient
import play.api.{Configuration, Environment, Logging}
import service._
import service.notifications.{
  DevelopmentEmailNotifications,
  EmailNotifications,
  Notification,
  PagerdutyNotifications
}
import software.amazon.awssdk.services.batch.BatchClient
import software.amazon.awssdk.services.cloudformation.CloudFormationClient
import software.amazon.awssdk.services.cloudwatchlogs.CloudWatchLogsClient
import software.amazon.awssdk.services.ec2.Ec2Client
import software.amazon.awssdk.services.emr.EmrClient
import software.amazon.awssdk.services.s3.S3Client
import software.amazon.awssdk.services.ses.SesClient

@Singleton
class PrometheusJmxInstrumentation @Inject()(implicit val registry: Registry) {
  jmx.register
}

class Config(environment: Environment, configuration: Configuration)
    extends AbstractModule
    with Logging {

  override def configure(): Unit = {

    logger.info(s" *** Starting Sundial *** ")

    logger.info("Env Variables:")
    sys.env.foreach {
      case (key, value) => logger.info(s"Key($key), Value($value)")
    }

    logger.info("Sundial Configuration:")
    configuration.entrySet.foreach { entry =>
      logger.info(s"Key(${entry._1}), Value[${entry._2.toString}]")
    }

    bind(classOf[Registry]).toInstance(DefaultRegistry())

    // AWS Clients
    bind(classOf[BatchClient]).toInstance(BatchClient.create())
    bind(classOf[S3Client]).toInstance(S3Client.create())
    bind(classOf[CloudWatchLogsClient])
      .toInstance(CloudWatchLogsClient.create())
    bind(classOf[Ec2Client]).toInstance(Ec2Client.create())
    bind(classOf[EmrClient]).toInstance(EmrClient.create())
    bind(classOf[CloudFormationClient])
      .toInstance(CloudFormationClient.create())
    bind(classOf[SesClient])
      .toInstance(SesClient.create())

    bind(classOf[PrometheusJmxInstrumentation]).asEagerSingleton()

    bind(classOf[Sundial]).asEagerSingleton()

  }

  @Provides
  @Singleton
  def notifications(wsClient: WSClient,
                    daoFactory: SundialDaoFactory,
                    displayModels: DisplayModels,
                    sesClient: SesClient): Seq[Notification] = {
    configuration.getOptional[String]("notifications.mode") match {
      case Some("browser") =>
        Seq(
          new DevelopmentEmailNotifications(daoFactory,
                                            displayModels,
                                            sesClient))
      case Some("email") =>
        Seq(
          new EmailNotifications(
            daoFactory,
            configuration.get[String]("notifications.from"),
            displayModels,
            sesClient))
      case Some("all") =>
        Seq(
          new EmailNotifications(
            daoFactory,
            configuration.get[String]("notifications.from"),
            displayModels,
            sesClient),
          new PagerdutyNotifications(wsClient, daoFactory)
        )
      case _ => Seq.empty
    }
  }

} 
Example 26
Source File: Env.scala    From izanami   with Apache License 2.0 5 votes vote down vote up
package env

import akka.actor.ActorSystem
import com.codahale.metrics.MetricRegistry
import controllers.AssetsFinder
import libs.logs.IzanamiLogger
import play.api.libs.ws.WSClient
import play.api.{Environment, Mode}
import cats._
import cats.implicits._

import scala.util.Random

object ModeEq {
  implicit val eqMode: Eq[Mode] = Eq.fromUniversalEquals
}

case class Env(
    izanamiConfig: IzanamiConfig,
    environment: Environment,
//    actorSystem: ActorSystem,
    assetsFinder: AssetsFinder
) {

  import ModeEq._

  val env: Mode = IzanamiConfig.mode(izanamiConfig, environment.mode)

  def isPlayDevMode = environment.mode === Mode.Dev

  IzanamiLogger.info(s"Starting izanami with $env mode")
//  val sharedKey: String = izanamiConfig.claim.sharedKey

  def hash = Random.nextInt(100000)

  def getFile(path: String) = environment.getFile(path)

  val cookieName: String = Option(izanamiConfig.filter)
    .collect { case Default(config) => config.cookieClaim }
    .getOrElse("Izanami")

  val contextPath: String = if (izanamiConfig.contextPath.endsWith("/")) {
    izanamiConfig.contextPath.dropRight(1)
  } else {
    izanamiConfig.contextPath
  }
  val baseURL: String = if (izanamiConfig.baseURL.endsWith("/")) {
    izanamiConfig.baseURL.dropRight(1)
  } else {
    izanamiConfig.baseURL
  }
} 
Example 27
Source File: BaseNrsConnector.scala    From vat-api   with Apache License 2.0 5 votes vote down vote up
package v1.connectors

import java.util.concurrent.TimeoutException

import config.AppConfig
import play.api.Logger
import play.api.libs.json.{Json, Writes}
import play.api.libs.ws.WSClient
import uk.gov.hmrc.http.HeaderCarrier
import v1.connectors.httpparsers.WsReads

import scala.concurrent.{ExecutionContext, Future}

trait BaseNrsConnector {
  val ws: WSClient
  val appConfig: AppConfig

  val logger = Logger(this.getClass)

  private[connectors] def nrsHeaderCarrier(implicit hc: HeaderCarrier): HeaderCarrier =
    hc.withExtraHeaders(
      "X-API-Key" -> appConfig.nrsApiKey,
      "User-Agent" -> appConfig.appName
    )

  def nrsPost[Body: Writes, Resp](body: Body, uri: NrsUri[Resp], defaultResult: NrsOutcome[Resp])(implicit ec: ExecutionContext,
                                                                                      hc: HeaderCarrier,
                                                                                      wsReads: WsReads[NrsOutcome[Resp]]): Future[NrsOutcome[Resp]] = {

    def doPost(implicit hc: HeaderCarrier): Future[NrsOutcome[Resp]] = {

      ws.url(s"${appConfig.nrsBaseUrl}/${uri.value}")
        .withHttpHeaders(hc.headers: _*)
        .withRequestTimeout(appConfig.nrsMaxTimeout)
        .post(Json.toJson(body))
        .map(wsReads.wsRead(_, defaultResult)).recover {
        case e: TimeoutException =>
          logger.warn(s"[NrsConnector][nrsPost] - NRS Call timed out - $e")
          defaultResult
      }
    }

    doPost(nrsHeaderCarrier(hc))
  }

} 
Example 28
Source File: CirceSpec.scala    From play-circe   with Apache License 2.0 5 votes vote down vote up
package play.api.libs.circe

import io.circe.generic.auto._
import io.circe.syntax._
import org.scalatestplus.play._
import org.scalatestplus.play.guice._
import play.api._
import play.api.libs.ws.WSClient
import play.api.routing._
import play.api.routing.sird._
import play.api.mvc._
import play.api.inject.guice._
import scala.concurrent._

class CirceSpec extends PlaySpec with GuiceOneServerPerSuite {

  lazy val controllersComponent = app.injector.instanceOf[ControllerComponents]
  lazy val circeController = new CirceController(controllersComponent)
  lazy val wsClient = app.injector.instanceOf[WSClient]
  lazy val url = s"http://127.0.0.1:$port"
  lazy val fooJsonString = circeController.customPrinter.print(Data.foo.asJson)

  def await[A](f: Future[A]) = Await.result(f, duration.Duration.Inf)


  override def fakeApplication(): Application =
    new GuiceApplicationBuilder()
      .configure("play.allowGlobalApplication" -> "false")
      .router(Router.from {
        case GET(p"/get") => circeController.get
        case POST(p"/post") => circeController.post
        case POST(p"/postJson") => circeController.postJson
        case POST(p"/postTolerant") => circeController.postTolerant
        case POST(p"/postTolerantJson") => circeController.postTolerantJson
        case _ => new Handler{}
      }).build()


  "Circe trait"  must {
    "server json" in {
      val resp = await(wsClient.url(url + "/get").get())
      resp.headers("Content-Type")(0) mustEqual("application/json")
      resp.body mustEqual fooJsonString
    }
    "parse json as object" in {
      val resp = wsClient
        .url(url + "/post")
        .addHttpHeaders("Content-Type" -> "application/json")
        .post(fooJsonString)
      await(resp).body mustEqual "true"
    }
    "parse json" in {
      val resp = wsClient
        .url(url + "/postJson")
        .addHttpHeaders("Content-Type" -> "application/json")
        .post(fooJsonString)
      await(resp).body mustEqual "true"
    }
    "parse json as obj for content type `text/html`" in {
      val resp = wsClient
        .url(url + "/postTolerant")
        .post(fooJsonString)
      await(resp).body mustEqual "true"
    }
    "parse json for content type `text/html`" in {
      val resp = wsClient
        .url(url + "/postTolerantJson")
        .post(fooJsonString)
      await(resp).body mustEqual "true"
    }
    "report 415 while parsing non-json content-type" in {
      val resp = wsClient
        .url(url + "/post")
        .post(fooJsonString)
      await(resp).status mustEqual 415
    }
    "report 400 while parsing invalid json String" in {
      val resp = wsClient
        .url(url + "/post")
        .withHttpHeaders("Content-Type" -> "application/json")
        .post("invalid json string")
      await(resp).status mustEqual 400
    }
    "report 400 if decode failed" in {
      val resp = wsClient
        .url(url + "/post")
        .withHttpHeaders("Content-Type" -> "application/json")
        .post("{}")
      await(resp).status mustEqual 400
    }
  }
} 
Example 29
Source File: ApplicationSpec.scala    From Scala-Programming-Projects   with MIT License 5 votes vote down vote up
import org.scalatest.Matchers._
import org.scalatest.concurrent.PatienceConfiguration.Timeout
import org.scalatest.concurrent.ScalaFutures
import org.scalatest.time.{Seconds, Span}
import org.scalatestplus.play.PlaySpec
import org.scalatestplus.play.guice.GuiceOneServerPerSuite
import play.api.libs.ws.WSClient
import play.api.test.Helpers._
import scala.concurrent.duration._



class ApplicationSpec extends PlaySpec with ScalaFutures with GuiceOneServerPerSuite {
  "Application" should {
    val wsClient = app.injector.instanceOf[WSClient]
    val myPublicAddress = s"localhost:$port"

    "send 404 on a bad request" in {
      val testURL = s"http://$myPublicAddress/boom"

      whenReady(wsClient.url(testURL).get(), Timeout(1 second)) { response =>
        response.status mustBe NOT_FOUND
      }
    }

    "render the index page" in {
      val testURL = s"http://$myPublicAddress/"

      whenReady(wsClient.url(testURL).get(), Timeout(1 second)) { response =>
        response.status mustBe OK
        response.contentType should include("text/html")
      }
    }
  }
} 
Example 30
Source File: IntegrationSpec.scala    From Scala-Programming-Projects   with MIT License 5 votes vote down vote up
import org.scalatestplus.play.PlaySpec
import org.scalatestplus.play.guice.GuiceOneServerPerSuite
import play.api.libs.ws.WSClient

import scala.concurrent.duration.DurationInt
import scala.concurrent.Await
import org.scalatest._
import Matchers._

class IntegrationSpec extends PlaySpec with GuiceOneServerPerSuite {
  "Application" should {
    val wsClient = app.injector.instanceOf[WSClient]
    val myPublicAddress = s"localhost:$port"
    "work from within a browser" in {

      val testURL = s"http://$myPublicAddress/"

      val response = Await.result(wsClient.url(testURL).get(), 1 seconds)

      response.body should include ("<title>Shopping Page</title>")
    }
  }
} 
Example 31
Source File: AvatarService.scala    From crm-seed   with Apache License 2.0 5 votes vote down vote up
package com.dataengi.crm.profiles.services

import java.net.URLEncoder._
import java.security.MessageDigest

import cats.instances.all._
import com.dataengi.crm.profiles.services.errors.ProfileServiceErrors
import com.google.inject.{Inject, Singleton}
import com.dataengi.crm.common.context.types._
import play.api.libs.ws.WSClient

import scala.concurrent.ExecutionContext

trait AvatarService {

  def retrieveURL(email: String): Or[Option[String]]

}

@Singleton
class AvatarServiceImplementation @Inject()(wSClient: WSClient,
                                            implicit val executionContext: ExecutionContext,
                                            settings: GravatarServiceSettings)
    extends AvatarService {

  override def retrieveURL(email: String): Or[Option[String]] = hash(email) match {
    case Some(hash) =>
      val url = settings.getUrl(hash)
      gerAvatarUrl(url)
    case None => ProfileServiceErrors.avatarCreatingUrlError(email).toErrorOr
  }

  private def gerAvatarUrl(url: String): Or[Option[String]] =
    wSClient
      .url(url)
      .get()
      .toOr
      .flatMap(response =>
        response.status match {
          case 200  => Option(url).toOr
          case code => ProfileServiceErrors.avatarReceivingError(code, url).toErrorOr
      })

  private def hash(email: String): Option[String] = {
    val s = email.trim.toLowerCase
    if (s.length > 0) {
      Some(MessageDigest.getInstance(GravatarService.MD5).digest(s.getBytes).map("%02x".format(_)).mkString)
    } else {
      None
    }
  }

}

object GravatarService {
  val InsecureURL = "http://www.gravatar.com/avatar/%s%s"
  val SecureURL   = "https://secure.gravatar.com/avatar/%s%s"
  val MD5         = "MD5"
}

case class GravatarServiceSettings(secure: Boolean = true, params: Map[String, String] = Map("d" -> "identicon")) {

  def getUrl(hash: String): String = {
    val encodedParams = params.map { p =>
      encode(p._1, "UTF-8") + "=" + encode(p._2, "UTF-8")
    }
    (if (secure) GravatarService.SecureURL else GravatarService.InsecureURL)
      .format(hash, encodedParams.mkString("?", "&", ""))
  }

} 
Example 32
Source File: SilhouetteModule.scala    From crm-seed   with Apache License 2.0 5 votes vote down vote up
package com.dataengi.crm.identities.modules

import com.dataengi.crm.configurations.{CompaniesConfiguration, RolesConfiguration, RootConfiguration, SilhouetteConfiguration}
import com.dataengi.crm.identities.actions.{ActionsProvider, ActionsProviderImplementation}
import com.google.inject.name.Named
import com.google.inject.{Provides, Singleton}
import com.mohiva.play.silhouette.api.actions.SecuredErrorHandler
import com.mohiva.play.silhouette.api.crypto.{AuthenticatorEncoder, Crypter, CrypterAuthenticatorEncoder}
import com.mohiva.play.silhouette.api.repositories.AuthInfoRepository
import com.mohiva.play.silhouette.api.services._
import com.mohiva.play.silhouette.api.util._
import com.mohiva.play.silhouette.api.{Environment, EventBus, Silhouette, SilhouetteProvider}
import com.mohiva.play.silhouette.crypto.{JcaCrypter, JcaCrypterSettings}
import com.mohiva.play.silhouette.impl.authenticators._
import com.mohiva.play.silhouette.impl.providers._
import com.mohiva.play.silhouette.impl.services._
import net.ceedubs.ficus.readers.EnumerationReader._
import com.mohiva.play.silhouette.impl.util._
import com.mohiva.play.silhouette.password.BCryptPasswordHasher
import com.mohiva.play.silhouette.persistence.daos.DelegableAuthInfoDAO
import com.mohiva.play.silhouette.persistence.repositories.DelegableAuthInfoRepository
import net.ceedubs.ficus.Ficus._
import net.ceedubs.ficus.readers.ArbitraryTypeReader._
import net.codingwell.scalaguice.ScalaModule
import play.api.Configuration
import play.api.libs.ws.WSClient
import com.dataengi.crm.identities.daos._
import com.dataengi.crm.identities.repositories._
import com.dataengi.crm.identities.services._
import com.dataengi.crm.identities.slick.initiation.InitiateTables
import com.dataengi.crm.identities.utils.auth.DefaultEnv

import scala.concurrent.ExecutionContext

class SilhouetteModule extends ScalaModule {

  def configure() = {
    // Initiation ORDER make sense
    bind[InitiateTables].asEagerSingleton()
    bind[IdentitiesInitiation].asEagerSingleton()

    // Silhouette
    bind[SecuredErrorHandler].to[CustomSecuredErrorHandler]
    bind[Silhouette[DefaultEnv]].to[SilhouetteProvider[DefaultEnv]]
    bind[CacheLayer].to[PlayCacheLayer]
    bind[PasswordHasher].toInstance(new BCryptPasswordHasher)
    bind[FingerprintGenerator].toInstance(new DefaultFingerprintGenerator(false))
    bind[EventBus].toInstance(EventBus())
    bind[Clock].toInstance(Clock())

    // Replace this with the bindings to your concrete DAOs
    bind[PasswordInfoDAO].to[PasswordInfoDAOSlickImplementation]
    bind[DelegableAuthInfoDAO[PasswordInfo]].to[PasswordInfoDAO]

    // Repository
    bind[RolesRepository].to[RolesRepositoryImplementation]
    bind[CompaniesRepository].to[CompaniesRepositoryImplementation]
    bind[InvitesRepository].to[InvitesRepositoryImplementation]
    bind[JWTAuthenticatorRepository].to[JWTAuthenticatorCacheRepositoryImplementation]
    bind[UsersRepository].to[UsersRepositoryImplementation]
    bind[RecoverPasswordInfoRepository].to[RecoverPasswordInfoRepositoryImplementation]

    // DAOs
    bind[CompaniesDAO].to[CompaniesSlickDAOImplementation]
    bind[RolesDAO].to[RolesSlickDAOImplementation]
    bind[InvitesDAO].to[InvitesSlickDAOImplementation]
    bind[JWTAuthenticatorDAO].to[JWTAuthenticatorSlickDAOImplementation]
    bind[UsersDAO].to[UsersSlickDAOImplementation]
    bind[RecoverPasswordInfoDAO].to[RecoverPasswordInfoSlickDAOImplementation]

    // Services
    bind[UsersService].to[UsersServiceImplementation]
    bind[CompaniesService].to[CompaniesServiceImplementation]
    bind[RolesService].to[RolesServiceImplementation]
    bind[InvitesService].to[InvitesServiceImplementation]
    bind[AuthenticationService].to[AuthenticationServiceImplementation]
    bind[UsersManagementService].to[UsersManagementServiceImplementation]

    // Actions
    bind[ActionsProvider].to[ActionsProviderImplementation]

    // Configuration
    bind[RolesConfiguration].asEagerSingleton()
    bind[CompaniesConfiguration].asEagerSingleton()
    bind[RootConfiguration].asEagerSingleton()
    bind[SilhouetteConfiguration].asEagerSingleton()

  }

  
  @Provides
  def provideCredentialsProvider(authInfoRepository: AuthInfoRepository,
                                 passwordHasherRegistry: PasswordHasherRegistry,
                                 executionContext: ExecutionContext): CredentialsProvider = {

    new CredentialsProvider(authInfoRepository, passwordHasherRegistry)(executionContext)
  }

} 
Example 33
Source File: ClientsModule.scala    From daf-semantics   with Apache License 2.0 5 votes vote down vote up
package modules

import com.google.inject.ImplementedBy
import play.api.inject.ApplicationLifecycle
import javax.inject.Singleton
import javax.inject.Inject
import play.api.libs.ws.WSClient
import play.api.Application
import play.api.Environment
import play.api.Configuration
import scala.concurrent.ExecutionContext
import scala.concurrent.Future
import play.Logger
import clients.OntonetHubClient
import com.typesafe.config.ConfigFactory
import com.typesafe.config.ConfigRenderOptions

@ImplementedBy(classOf[ClientsModuleBase])
trait ClientsModule

@Singleton
class ClientsModuleBase @Inject() (lifecycle: ApplicationLifecycle,
                                   ws: WSClient,
                                   configuration: Configuration) extends ClientsModule {

  val conf_clients = configuration.underlying
    .getConfig("clients")

  val ontonethub_config = conf_clients.getConfig("ontonethub")

  // TODO: verify if default configurations are needed here
  val ontonethub = new OntonetHubClient(ws, ontonethub_config)

  // TESTING ................................................
  val options = ConfigRenderOptions.concise()
    .setComments(false).setOriginComments(false)
    .setFormatted(true).setJson(true)
  val json = ontonethub_config.root().render(options)
  // TESTING ................................................

  // when application starts...
  @Inject
  def onStart(
    app: Application,
    env: Environment)(implicit ec: ExecutionContext) {

    Logger.info("ClientsModuleBase START")

    println("\n\n\n\n\n\n")
    println(json)

  }

  // when application stops...
  lifecycle.addStopHook({ () =>

    Future.successful {

      Logger.info("ClientsModuleBase STOP")

    }

  })

} 
Example 34
Source File: TestingHttpApi.scala    From daf-semantics   with Apache License 2.0 5 votes vote down vote up
package it.almawave.linkeddata.kb.http

import play.api.inject.guice.GuiceApplicationBuilder
import org.junit.Test
import org.junit.After
import play.api.Application
import org.junit.Before
import it.almawave.linkeddata.kb.utils.JSONHelper
import play.api.libs.ws.WSClient
import org.asynchttpclient.DefaultAsyncHttpClient
import play.api.libs.ws.ssl.SystemConfiguration
import akka.stream.ActorMaterializer
import play.api.libs.ws.ahc.AhcWSClient
import scala.concurrent.Await
import scala.concurrent.duration.Duration
import java.net.URL
import com.typesafe.config.ConfigFactory

class TestingHttpApi {

  var app: Application = null
  var conf = ConfigFactory.empty()
  var ws: WSClient = null
  var app_url = new URL("http://localhost:8080")

  @Test
  def testing_contexts() {

    //    curl -X GET http://localhost:8999/kb/v1/prefixes/lookup?prefix=no_pref 
    //    -H  "accept: application/json" 
    //    -H  "content-type: application/json"

    val fut = ws.url(s"http://localhost:8999/kb/v1/prefixes/lookup")
      .withHeaders(("accept", "application/json"))
      .withHeaders(("content-type", "application/json"))
      .withFollowRedirects(true)
      .withQueryString(("prefix", "muapit"))
      .get()

    val results = Await.result(fut, Duration.Inf)
    println(results.body)

  }

  @Before
  def before() {

    app = GuiceApplicationBuilder()
      .build()

    conf = app.configuration.underlying

    // play.app.local.url
    // play.server.http.address
    // play.server.http.port

    println(JSONHelper.writeToString(conf.root().unwrapped()))

    app_url = new URL(conf.getString("app.local.url"))

    println(s"\n\nrunning at ${app_url}")

    val materializer = ActorMaterializer()(app.actorSystem)
    ws = AhcWSClient()(materializer)

  }

  @After
  def after() {
    ws.close()
    app.stop()
  }

} 
Example 35
Source File: SemanticRepositorySpecs.scala    From daf-semantics   with Apache License 2.0 5 votes vote down vote up
package specs

import org.junit.runner.RunWith

import scala.concurrent.{ Await, Future }
import scala.concurrent.duration.Duration

import play.api.test._
import play.api.http.Status
import play.api.Application
import play.api.inject.guice.GuiceApplicationBuilder
import play.api.libs.ws.WSResponse
import play.api.libs.ws.ahc.AhcWSClient
import org.specs2.runner.JUnitRunner
import org.specs2.mutable.Specification
import play.api.libs.json.Json
//import it.almawave.linkeddata.kb.utils.ConfigHelper

import scala.collection.JavaConversions._
import scala.collection.JavaConverters._
import play.twirl.api.Content
import play.api.test.Helpers._
import play.api.libs.json.JsObject
import java.io.File
import play.api.http.Writeable
import akka.stream.scaladsl.Source
import play.api.mvc.MultipartFormData
import play.api.libs.Files.TemporaryFile
import java.nio.file.Files
import org.asynchttpclient.AsyncHttpClient
import play.api.libs.ws.WS
import akka.util.ByteString
import play.api.mvc.MultipartFormData.DataPart
import play.api.mvc.MultipartFormData.FilePart
import akka.stream.scaladsl.FileIO
import play.api.libs.ws.WSClient

/*
 * TODO: REWRITE
 */
@RunWith(classOf[JUnitRunner])
class SemanticRepositorySpecs extends Specification {

  def application: Application = GuiceApplicationBuilder().build()

  "The semantic repository" should {

    "call kb/v1/contexts to obtain a list of contexts" in {
      new WithServer(app = application, port = 9999) {
        WsTestClient.withClient { implicit client =>

          val response: WSResponse = Await.result[WSResponse](
            client.url(s"http://localhost:${port}/kb/v1/contexts").execute,
            Duration.Inf)

          response.status must be equalTo Status.OK
          response.json.as[Seq[JsObject]].size must be equals 0
          // response.json.as[Seq[JsObject]].size must be greaterThan 0 // if pre-loaded ontologies!

        }
      }
    }

    "call kb/v1/contexts ensuring all contexts have triples" in {
      new WithServer(app = application, port = 9999) {
        WsTestClient.withClient { implicit client =>

          val response: WSResponse = Await.result[WSResponse](
            client.url(s"http://localhost:${port}/kb/v1/contexts").execute,
            Duration.Inf)

          val json_list = response.json.as[Seq[JsObject]]
          forall(json_list)((_) must not beNull)
          forall(json_list)(_.keys must contain("context", "triples"))
          forall(json_list)(item => (item \ "triples").get.as[Int] > 0)

        }
      }
    }

  }

} 
Example 36
Source File: DatasetManagerService.scala    From daf   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
package it.gov.daf.server

import it.gov.daf.catalogmanager.MetaCatalog
import it.gov.daf.catalogmanager.client.Catalog_managerClient
import it.gov.daf.datasetmanager._
import it.gov.daf.server.storage.StorageManagerClient
import play.api.libs.ws.WSClient

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

class DatasetManagerService(
  catalogUrl: String,
  storageUrl: String,
  ws: WSClient
)(implicit val ec: ExecutionContext) {

  private val catalogService = new Catalog_managerClient(ws)(catalogUrl)
  private val storageManager = new StorageManagerClient(storageUrl, ws)

  def getDatasetSchema(authorization: String, datasetId: String): Future[Dataset] = {
    val result = catalogService.datasetcatalogbyid(authorization, datasetId)
      .flatMap(c => Future.fromTry(extractParams(c)))
      .flatMap(storageManager.datasetSchema(authorization, _))

    result.map(_.as[Dataset])
  }

  def getDataset(authorization: String, datasetId: String): Future[Dataset] = {
    val result = catalogService.datasetcatalogbyid(authorization, datasetId)
      .flatMap(c => Future.fromTry(extractParams(c)))
      .flatMap(storageManager.dataset(authorization, _))

    result.map(_.as[Dataset])
  }

  def getDataset(authorization: String, datasetId: String, numRows: Int): Future[Dataset] = {
    val result = catalogService.datasetcatalogbyid(authorization, datasetId)
      .flatMap(c => Future.fromTry(extractParams(c)))
      .flatMap(storageManager.dataset(authorization, _))

    result.map(_.as[Dataset])
  }

  def searchDataset(authorization: String, datasetId: String, query: Query): Future[Dataset] = {
    val result = catalogService.datasetcatalogbyid(authorization, datasetId)
      .flatMap(c => Future.fromTry(extractParams(c)))
      .map(params => params ++ transform(query))
      .flatMap(storageManager.search(authorization, _))

    result.map(_.as[Dataset])
  }

  private def extractParams(catalog: MetaCatalog): Try[Map[String, String]] = {
    catalog.operational.storage_info match {
      case Some(storage) =>
        if (storage.hdfs.isDefined) {
          Try(
            Map(
              "protocol" -> "hdfs",
              "path" -> storage.hdfs.flatMap(_.path).get
            )
          )
        } else if (storage.kudu.isDefined) {
          Try(
            Map(
              "protocol" -> "kudu",
              "table" -> storage.kudu.flatMap(_.table_name).get
            )
          )
        } else if (storage.hbase.isDefined) {
          Try(
            Map(
              "protocol" -> "opentsdb",
              "metric" -> storage.hbase.flatMap(_.metric).get,
              //FIXME right now it encodes a list a as comma separated
              "tags" -> storage.hbase.flatMap(_.tags).get.mkString(","),
              //FIXME how to encode the interval?
              "interval" -> ""
            )
          )
        } else Failure(new IllegalArgumentException("no storage configured into catalog.operational field"))

      case None =>
        Failure(new IllegalArgumentException("no storage_info configured"))
    }
  }

  private def extractParams(catalog: MetaCatalog, numRows: Int = 100): Try[Map[String, String]] = {
    extractParams(catalog)
      .map(_ + ("limit" -> numRows.toString))
  }

  //FIXME this will be changed in the next sprint
  private def transform(query: Query): Map[String, String] = Map.empty
} 
Example 37
Source File: TestAbstractModule.scala    From daf   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
package controllers.modules

import akka.stream.Materializer
import daf.util.TestCache
import org.pac4j.play.store.{ PlayCacheSessionStore, PlaySessionStore }
import play.api.{ Configuration, Environment }
import play.api.inject.Module
import play.api.libs.ws.WSClient
import play.api.libs.ws.ahc.AhcWSClient
import play.api.mvc.BodyParsers
import play.cache.{ CacheApi, DefaultCacheApi }
import play.api.cache.{ CacheApi => ApiCacheApi }
import play.api.inject.guice.GuiceInjectorBuilder

abstract class TestAbstractModule extends Module {

  private lazy val injector = new GuiceInjectorBuilder().bindings(this).injector()

  private lazy val sessionStoreInstance: PlaySessionStore = injector.instanceOf { classOf[PlaySessionStore] }
  private lazy val wsClientInstance: WSClient = injector.instanceOf { classOf[WSClient] }
  private lazy val bodyParsersInstance: BodyParsers = BodyParsers

  final def sessionStore: PlaySessionStore = sessionStoreInstance
  final def ws: WSClient = wsClientInstance
  final def bodyParsers: BodyParsers = bodyParsersInstance

  protected implicit def materializer: Materializer

  override def bindings(environment: Environment, configuration: Configuration) = Seq(
    bind(classOf[ApiCacheApi]).to(classOf[TestCache]),
    bind(classOf[CacheApi]).to(classOf[DefaultCacheApi]),
    bind(classOf[PlaySessionStore]).to(classOf[PlayCacheSessionStore]),
    bind(classOf[BodyParsers]).to(BodyParsers),
    bind(classOf[WSClient]).toInstance(AhcWSClient())
  )
} 
Example 38
Source File: KyloApiClient.scala    From daf   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
package it.gov.daf.securitymanager.service

import com.google.inject.Inject
import it.gov.daf.securitymanager.service.utilities.ConfigReader
import play.api.Logger
import play.api.libs.json.{JsUndefined, JsValue, Json}
import play.api.libs.ws.{WSAuthScheme, WSClient}
import security_manager.yaml.{Error, Success}

import scala.concurrent.Future

class KyloApiClient @Inject()(wSClient: WSClient){

  import play.api.libs.concurrent.Execution.Implicits._

  def createCategory(name: String):Future[Either[Error,Success]]= {

    val jsonRequest: JsValue = Json.parse(
                                        s"""{
                                           "id": null,
                                           "name": "$name",
                                           "description": null,
                                           "icon": null,
                                           "iconColor": null,
                                           "userFields": [],
                                           "userProperties": [],
                                           "relatedFeedSummaries": [],
                                           "securityGroups": [],
                                           "roleMemberships": [],
                                           "feedRoleMemberships": [],
                                           "owner": null,
                                           "systemName": "$name"
                                    }""")

    Logger.logger.debug("createCategory: "+ jsonRequest.toString())

    val response = wSClient.url(ConfigReader.kyloUrl + "/proxy/v1/feedmgr/categories")
                    .withHeaders("Accept" -> "application/json")
                    .withAuth(ConfigReader.kyloUser,ConfigReader.kyloUserPwd,WSAuthScheme.BASIC)
                    .post(jsonRequest)

    response.map{response =>

      if( response.status != 200 )
        Left( Error(Option(0),Some("Error in during kylo category creation: bad http code"+response.status),None) )
      else{
        Logger.logger.debug("RESPONSE:"+response.json)
        val result = response.json \ "id"
        if( result.isInstanceOf[JsUndefined] )
          Left( Error(Option(0),Some("Error in during kylo category creation"),None) )
        else
          Right( Success(Some("Category created"), Some("ok")) )
      }

    }

  }


} 
Example 39
Source File: DirManager.scala    From daf   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
package it.gov.daf.catalogmanager.listeners



import java.net.URLEncoder

import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.{ FileIO, Source }
import net.caoticode.dirwatcher.FSListener
import play.api.libs.ws.WSClient
import play.api.libs.ws.ahc.AhcWSClient
import play.api.mvc.MultipartFormData.FilePart
import play.Logger
import scala.concurrent.Future

class DirManager() extends FSListener {

  import java.nio.file.Path
  import scala.concurrent.ExecutionContext.Implicits.global

  val logger = Logger.underlying()

  override def onCreate(ref: Path): Unit = {

    implicit val system = ActorSystem()
    implicit val materializer = ActorMaterializer()
    val wsClient = AhcWSClient()

    val name = ref.getParent.getFileName.toString
    println(name)
    val uri: Option[String] = IngestionUtils.datasetsNameUri.get(name)
    val logicalUri = URLEncoder.encode(uri.get, "UTF-8")
    logger.debug("logicalUri: " + logicalUri)

    call(wsClient)
      .andThen { case _ => wsClient.close() }
      .andThen { case _ => system.terminate() }

    def call(wsClient: WSClient): Future[Unit] = {
      wsClient.url("http://localhost:9001/ingestion-manager/v1/add-datasets/" + logicalUri)
        //.withHeaders("content-type" -> "multipart/form-data")
        .post(
          Source(FilePart("upfile", name, None, FileIO.fromPath(ref)) :: List())).map { response =>
            val statusText: String = response.statusText
            logger.debug(s"Got a response $statusText")
          }
    }
    logger.debug(s"created $ref")
  }

  override def onDelete(ref: Path): Unit = println(s"deleted $ref")
  override def onModify(ref: Path): Unit = println(s"modified $ref")
} 
Example 40
Source File: PdfGeneratorConnector.scala    From pertax-frontend   with Apache License 2.0 5 votes vote down vote up
package connectors

import com.google.inject.{ImplementedBy, Inject, Singleton}
import play.api.Mode
import play.api.libs.ws.{WSClient, WSResponse}
import play.api.{Configuration, Environment}
import uk.gov.hmrc.play.bootstrap.config.ServicesConfig

import scala.concurrent.Future

@ImplementedBy(classOf[FrontendPdfGeneratorConnector])
trait PdfGeneratorConnector {
  val serviceURL: String
  def getWsClient: WSClient

  def generatePdf(html: String): Future[WSResponse] =
    getWsClient.url(serviceURL).post(Map("html" -> Seq(html)))
}

@Singleton
class FrontendPdfGeneratorConnector @Inject()(
  environment: Environment,
  runModeConfiguration: Configuration,
  wsClient: WSClient,
  servicesConfig: ServicesConfig)
    extends PdfGeneratorConnector {
  val mode: Mode = environment.mode
  val pdfServiceUrl: String = servicesConfig.baseUrl("pdf-generator-service")
  val serviceURL = pdfServiceUrl + "/pdf-generator-service/generate"

  override def getWsClient: WSClient = wsClient
} 
Example 41
Source File: NrsConnector.scala    From vat-api   with Apache License 2.0 5 votes vote down vote up
package v1.connectors

import config.AppConfig
import javax.inject.{Inject, Singleton}
import play.api.libs.ws.WSClient
import uk.gov.hmrc.http.HeaderCarrier
import v1.models.nrs.request.NrsSubmission
import v1.models.nrs.response.NrsResponse

import scala.concurrent.{ExecutionContext, Future}

@Singleton
class NrsConnector @Inject()(val ws: WSClient,
                             val appConfig: AppConfig) extends BaseNrsConnector {

  def submitNrs(body: NrsSubmission)(
    implicit hc: HeaderCarrier,
    ec: ExecutionContext): Future[NrsOutcome[NrsResponse]] = {

    import v1.connectors.httpparsers.StandardNrsWsParser._

    nrsPost[NrsSubmission, NrsResponse](
      uri = NrsUri[NrsResponse](s"submission"),
      body = body,
      defaultResult = Right(NrsResponse.empty)
    )
  }
} 
Example 42
Source File: NRSConnector.scala    From vat-api   with Apache License 2.0 5 votes vote down vote up

package uk.gov.hmrc.vatapi.connectors

import java.util.concurrent.TimeoutException

import javax.inject.Inject
import play.api.Logger
import play.api.libs.json.{JsValue, Json, Writes}
import play.api.libs.ws.WSClient
import uk.gov.hmrc.domain.Vrn
import uk.gov.hmrc.http._
import uk.gov.hmrc.play.bootstrap.http.DefaultHttpClient
import uk.gov.hmrc.vatapi.config.AppContext
import uk.gov.hmrc.vatapi.httpparsers.EmptyNrsData
import uk.gov.hmrc.vatapi.httpparsers.NrsSubmissionHttpParser.{NrsSubmissionOutcome, NrsSubmissionOutcomeReads}
import uk.gov.hmrc.vatapi.models.NRSSubmission

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


class NRSConnector @Inject()(
                              override val http: DefaultHttpClient,
                              override val appContext: AppContext,
                              ws: WSClient
                            ) extends BaseConnector {

  val logger: Logger = Logger(this.getClass)
  val nrsSubmissionUrl: String => String = vrn => s"${appContext.nrsServiceUrl}/submission"
  val nrsMaxTimeout: Duration = appContext.nrsMaxTimeoutMillis.milliseconds

  private val xApiKeyHeader = "X-API-Key"

  def submit(vrn: Vrn, nrsSubmission: NRSSubmission)(implicit hc: HeaderCarrier, ec: ExecutionContext): Future[NrsSubmissionOutcome] = {

    logger.debug(s"[NRSConnector][submit] - Submission to NRS for 9 box vat return for VRN: $vrn")

    val nrsResponse = {
      val submitUrl = nrsSubmissionUrl(vrn.toString)
      val headers = hc.withExtraHeaders(xApiKeyHeader -> s"${appContext.xApiKey}", "User-Agent" -> appContext.appName).headers

      implicit val nrsWrites = implicitly[Writes[NRSSubmission]]

      ws.url(submitUrl)
        .withHttpHeaders(headers: _*)
        .withRequestTimeout(nrsMaxTimeout)
        .post(Json.toJson(nrsSubmission))
    }

    nrsResponse.map { res =>

      val resJson = Try(res.json) match {
        case Success(json: JsValue) => Some(json)
        case _ => None
      }

      val httpResponse = HttpResponse(
        res.status,
        resJson,
        res.headers,
        None
      )

      Logger.debug(s"[NRSConnector][submit] - NRS Call succeeded")

      NrsSubmissionOutcomeReads.read("", "", httpResponse)

    }.recover {
      case e: TimeoutException => {
        logger.warn(s"[NRSConnector][submit] - NRS Call timed out for VRN: $vrn - $e")
        Right(EmptyNrsData)
      }
    }
  }
} 
Example 43
Source File: IntegrationBaseSpec.scala    From vat-api   with Apache License 2.0 5 votes vote down vote up
package support

import org.scalatest.{BeforeAndAfterAll, BeforeAndAfterEach}
import org.scalatestplus.play.guice.GuiceOneServerPerSuite
import play.api.inject.guice.GuiceApplicationBuilder
import play.api.libs.json.{JsValue, Json}
import play.api.libs.ws.{WSClient, WSRequest, WSResponse}
import play.api.{Application, Environment, Mode}

trait IntegrationBaseSpec extends UnitSpec with WireMockHelper with GuiceOneServerPerSuite
  with BeforeAndAfterEach with BeforeAndAfterAll {

  val mockHost: String = WireMockHelper.host
  val mockPort: String = WireMockHelper.wireMockPort.toString

  lazy val client: WSClient = app.injector.instanceOf[WSClient]

  def servicesConfig: Map[String, Any] = Map(
    "microservice.services.des.host" -> mockHost,
    "microservice.services.des.port" -> mockPort,
    "microservice.services.auth.host" -> mockHost,
    "microservice.services.auth.port" -> mockPort,
    "auditing.consumer.baseUri.port" -> mockPort,
    "microservice.services.non-repudiation.host" -> mockHost,
    "microservice.services.non-repudiation.port" -> mockPort,
    "feature-switch.refactor.enabled" -> true,
    "feature-switch.refactor.prod.enabled" -> false,
    "microservice.services.non-repudiation.maxTimeout" -> 5000
  )

  override implicit lazy val app: Application = new GuiceApplicationBuilder()
    .in(Environment.simple(mode = Mode.Dev))
    .configure(servicesConfig)
    .build()

  override def beforeAll(): Unit = {
    super.beforeAll()
    startWireMock()
  }

  override def afterAll(): Unit = {
    stopWireMock()
    super.afterAll()
  }

  def buildRequest(path: String): WSRequest = client.url(s"http://localhost:$port$path").withFollowRedirects(false)

  def document(response: WSResponse): JsValue = Json.parse(response.body)
} 
Example 44
Source File: BaseFunctionalSpec.scala    From vat-api   with Apache License 2.0 5 votes vote down vote up
package uk.gov.hmrc.support

import org.scalatestplus.play.guice.GuiceOneServerPerSuite
import play.api.Application
import play.api.inject.guice.GuiceApplicationBuilder
import play.api.libs.ws.{WSClient, WSRequest}
import uk.gov.hmrc.domain.Vrn
import uk.gov.hmrc.vatapi.{TestApplication, VrnGenerator}

import scala.collection.mutable

trait BaseFunctionalSpec extends TestApplication with WireMockHelper with GuiceOneServerPerSuite{
  protected val vrn: Vrn = VrnGenerator().nextVrn()

  implicit val urlPathVariables: mutable.Map[String, String] = mutable.Map()


  lazy val client: WSClient = app.injector.instanceOf[WSClient]

  override lazy val app: Application = new GuiceApplicationBuilder().configure(Map(
    "auditing.consumer.baseUri.host" -> "localhost",
    "auditing.consumer.baseUri.port" -> 22222,
    "microservice.services.des.host" -> mockHost,
    "microservice.services.des.port" -> mockPort,
    "microservice.services.auth.host" -> mockHost,
    "microservice.services.auth.port" -> mockPort,
    "auditing.consumer.baseUri.port" -> mockPort,
    "access-keys.xApiKey" -> "dummy-api-key",
    "microservice.services.non-repudiation.host" -> mockHost,
    "microservice.services.non-repudiation.port" -> mockPort,
    "feature-switch.refactor.enabled" -> false,
    "feature-switch.refactor.prod.enabled" -> false
  )).build()

  def when() = new HttpVerbs()(urlPathVariables, timeout)
  def given() = new Givens(when())

  def buildRequest(path: String): WSRequest = client.url(s"http://localhost:$port$path").withFollowRedirects(false)
} 
Example 45
Source File: MockWsClient.scala    From vat-api   with Apache License 2.0 5 votes vote down vote up
package v1.mocks
import org.scalamock.handlers.CallHandler
import org.scalamock.scalatest.MockFactory
import play.api.libs.ws.{BodyWritable, WSClient, WSRequest, WSResponse}

import scala.concurrent.Future
import scala.concurrent.duration.Duration

trait MockWsClient extends MockFactory {

  val mockWsClient: WSClient = mock[WSClient]
  val mockWsRequest: WSRequest = mock[WSRequest]

  object MockWsClient {

    def url(url: String): CallHandler[WSRequest] = {
      (mockWsClient.url(_: String))
        .expects(url)
    }
  }

  object MockWsRequest {

    def withHttpHeaders(headers: Seq[(String, String)]): CallHandler[WSRequest] = {
      (mockWsRequest.withHttpHeaders _ ).expects(*)
    }

    def withRequestTimeout(timeout: Duration): CallHandler[WSRequest] = {
      (mockWsRequest.withRequestTimeout(_: Duration))
        .expects(timeout)
    }

    def post[I: BodyWritable](body: I): CallHandler[Future[WSResponse]] = {
      (mockWsRequest.post(_: I)(_: BodyWritable[I]))
        .expects(body, *)
    }
  }

} 
Example 46
Source File: NRSConnectorSpec.scala    From vat-api   with Apache License 2.0 5 votes vote down vote up
package uk.gov.hmrc.vatapi.connectors

import java.util.concurrent.TimeoutException

import org.scalatestplus.play.guice.GuiceOneAppPerSuite
import play.api.libs.json.{JsResultException, Json, Writes}
import play.api.libs.ws.{WSClient, WSRequest, WSResponse}
import uk.gov.hmrc.domain.Vrn
import uk.gov.hmrc.http.HeaderCarrier
import uk.gov.hmrc.vatapi.UnitSpec
import uk.gov.hmrc.vatapi.assets.TestConstants.NRSResponse._
import uk.gov.hmrc.vatapi.httpparsers.NrsSubmissionHttpParser.NrsSubmissionOutcome
import uk.gov.hmrc.vatapi.httpparsers.{EmptyNrsData, NRSData}
import uk.gov.hmrc.vatapi.mocks.MockHttp
import uk.gov.hmrc.vatapi.mocks.config.MockAppContext
import uk.gov.hmrc.vatapi.models.NRSSubmission

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future

class NRSConnectorSpec extends UnitSpec with GuiceOneAppPerSuite
  with MockHttp
  with MockAppContext {

  class Setup {
    val wsClient = mock[WSClient]
    val testNrsConnector = new NRSConnector(mockHttp, mockAppContext, wsClient)

    MockAppContext.nrsMaxTimeoutMilliseconds returns 5000

    val testUrl: String = testNrsConnector.nrsSubmissionUrl(testVrn.vrn)
    def result(requestBody: NRSSubmission): Future[NrsSubmissionOutcome] = testNrsConnector.submit(testVrn, requestBody)
  }

  implicit val hc: HeaderCarrier = HeaderCarrier(nsStamp = 2L)

  val testVrn = Vrn("123456789")

  "NRSConnector.submit" should {

    "successful responses are returned from the connector" should {
      "return the correctly formatted NRS Data model" in new Setup {

        val request = mock[WSRequest]
        val response = mock[WSResponse]
        val expectedResponse = NRSData("2dd537bc-4244-4ebf-bac9-96321be13cdc","This has been deprecated - DO NOT USE","")


        implicit val nrsWrites = implicitly[Writes[NRSSubmission]]
        val meJson = Json.toJson(nrsSubmission)

        when(wsClient.url(testUrl)).thenReturn(request)
        when(request.withHttpHeaders(any())).thenReturn(request)
        when(request.withRequestTimeout(testNrsConnector.nrsMaxTimeout)).thenReturn(request)
        when(request.post(eqTo(meJson))(any())).thenReturn(Future.successful(response))
        when(response.json).thenReturn(nrsResponseJson)
        when(response.status).thenReturn(202)

        await(result(nrsSubmission)) shouldBe Right(expectedResponse)

      }

    }

    "return EmptyNrsData" when {
      "the connection times out" in new Setup {

        val request = mock[WSRequest]

        implicit val nrsWrites = implicitly[Writes[NRSSubmission]]

        when(wsClient.url(testUrl)).thenReturn(request)
        when(request.withHttpHeaders(any())).thenReturn(request)
        when(request.withRequestTimeout(testNrsConnector.nrsMaxTimeout)).thenReturn(request)
        when(request.post(eqTo(Json.toJson(nrsSubmission)))(any())).thenReturn(Future.failed(new TimeoutException("Expected Error")))

        await(result(nrsSubmission)) shouldBe Right(EmptyNrsData)

      }

      "the response JSON cannot be parsed" in new Setup {

        val request = mock[WSRequest]
        val response = mock[WSResponse]

        implicit val nrsWrites = implicitly[Writes[NRSSubmission]]

        when(wsClient.url(testUrl)).thenReturn(request)
        when(request.withHttpHeaders(any())).thenReturn(request)
        when(request.withRequestTimeout(testNrsConnector.nrsMaxTimeout)).thenReturn(request)
        when(request.post(eqTo(Json.toJson(nrsSubmission)))(any())).thenReturn(Future.successful(response))
        when(response.json).thenThrow(JsResultException(Seq()))
        await(result(nrsSubmission)) shouldBe Right(EmptyNrsData)

      }
    }
  }
} 
Example 47
Source File: GroupUserMapper.scala    From Cortex   with GNU Affero General Public License v3.0 5 votes vote down vote up
package org.thp.cortex.services.mappers

import scala.concurrent.{ExecutionContext, Future}

import play.api.Configuration
import play.api.libs.json._
import play.api.libs.ws.WSClient

import javax.inject.Inject

import org.elastic4play.AuthenticationError
import org.elastic4play.controllers.Fields

class GroupUserMapper(
    loginAttrName: String,
    nameAttrName: String,
    rolesAttrName: Option[String],
    groupAttrName: String,
    organizationAttrName: Option[String],
    defaultRoles: Seq[String],
    defaultOrganization: Option[String],
    groupsUrl: String,
    mappings: Map[String, Seq[String]],
    ws: WSClient,
    implicit val ec: ExecutionContext
) extends UserMapper {

  @Inject() def this(configuration: Configuration, ws: WSClient, ec: ExecutionContext) =
    this(
      configuration.getOptional[String]("auth.sso.attributes.login").getOrElse("name"),
      configuration.getOptional[String]("auth.sso.attributes.name").getOrElse("username"),
      configuration.getOptional[String]("auth.sso.attributes.roles"),
      configuration.getOptional[String]("auth.sso.attributes.groups").getOrElse(""),
      configuration.getOptional[String]("auth.sso.attributes.organization"),
      configuration.getOptional[Seq[String]]("auth.sso.defaultRoles").getOrElse(Seq()),
      configuration.getOptional[String]("auth.sso.defaultOrganization"),
      configuration.getOptional[String]("auth.sso.groups.url").getOrElse(""),
      configuration.getOptional[Map[String, Seq[String]]]("auth.sso.groups.mappings").getOrElse(Map()),
      ws,
      ec
    )

  override val name: String = "group"

  override def getUserFields(jsValue: JsValue, authHeader: Option[(String, String)]): Future[Fields] = {

    val apiCall = authHeader.fold(ws.url(groupsUrl))(headers ⇒ ws.url(groupsUrl).addHttpHeaders(headers))
    apiCall.get.flatMap { r ⇒
      val jsonGroups  = (r.json \ groupAttrName).as[Seq[String]]
      val mappedRoles = jsonGroups.flatMap(mappings.get).maxBy(_.length)
      val roles       = if (mappedRoles.nonEmpty) mappedRoles else defaultRoles

      val fields = for {
        login ← (jsValue \ loginAttrName).validate[String]
        name  ← (jsValue \ nameAttrName).validate[String]
        organization ← organizationAttrName
          .flatMap(o ⇒ (jsValue \ o).asOpt[String])
          .orElse(defaultOrganization)
          .fold[JsResult[String]](JsError())(o ⇒ JsSuccess(o))
      } yield Fields(Json.obj("login" → login, "name" → name, "roles" → roles, "organization" → organization))
      fields match {
        case JsSuccess(f, _) ⇒ Future.successful(f)
        case JsError(errors) ⇒ Future.failed(AuthenticationError(s"User info fails: ${errors.map(_._1).mkString}"))
      }
    }
  }
} 
Example 48
Source File: DependencyClientProvider.scala    From dependency   with MIT License 5 votes vote down vote up
package io.flow.dependency.www.lib

import io.flow.common.v0.models.UserReference
import io.flow.dependency.v0.{Authorization, Client}
import io.flow.play.util.AuthHeaders
import play.api.libs.ws.WSClient

trait DependencyClientProvider {

  def newClient(user: Option[UserReference], requestId: Option[String]): Client

}

@javax.inject.Singleton
class DefaultDependencyClientProvider @javax.inject.Inject() (
  config: io.flow.play.util.Config,
  wsClient: WSClient,
  authHeaders: AuthHeaders
) extends DependencyClientProvider {

  def host: String = config.requiredString("dependency.api.host")

  private[this] lazy val client = new Client(wsClient, host)

  override def newClient(user: Option[UserReference], requestId: Option[String]): Client = {
    user match {
      case None => {
        client
      }
      case Some(u) => {
        val authHeaderUser = requestId match {
          case Some(rid) => AuthHeaders.user(user = u, requestId = rid)
          case None => AuthHeaders.user(user = u)
        }

        new Client(
          ws = wsClient,
          baseUrl = host,
          auth = Some(
            Authorization.Basic(
              username = u.id.toString,
              password = None
            )
          ),
          defaultHeaders = authHeaders.headers(authHeaderUser)
        )
      }
    }
  }


} 
Example 49
Source File: IAMClient.scala    From play-zhewbacca   with MIT License 5 votes vote down vote up
package org.zalando.zhewbacca

import java.util.concurrent.atomic.AtomicInteger
import javax.inject.{Inject, Singleton}

import akka.actor.ActorSystem
import akka.pattern.CircuitBreaker
import org.zalando.zhewbacca.metrics.PluggableMetrics
import play.api.http.Status._
import play.api.libs.ws.WSClient
import play.api.{Configuration, Logger}

import scala.concurrent.duration._
import scala.concurrent.{ExecutionContext, Future}
import scala.util.control.NonFatal

import atmos.dsl._
import atmos.dsl.Slf4jSupport._


@Singleton
class IAMClient @Inject() (
    config: Configuration,
    pluggableMetrics: PluggableMetrics,
    ws: WSClient,
    actorSystem: ActorSystem,
    implicit val ec: ExecutionContext) extends (OAuth2Token => Future[Option[TokenInfo]]) {

  val logger: Logger = Logger("security.IAMClient")

  val METRICS_BREAKER_CLOSED = 0
  val METRICS_BREAKER_OPEN = 1
  val circuitStatus = new AtomicInteger()

  pluggableMetrics.gauge {
    circuitStatus.get
  }

  val authEndpoint: String = config.getOptional[String]("authorisation.iam.endpoint").getOrElse(
    throw new IllegalArgumentException("Authorisation: IAM endpoint is not configured"))

  val breakerMaxFailures: Int = config.getOptional[Int]("authorisation.iam.cb.maxFailures").getOrElse(
    throw new IllegalArgumentException("Authorisation: Circuit Breaker max failures is not configured"))

  val breakerCallTimeout: FiniteDuration = config.getOptional[FiniteDuration]("authorisation.iam.cb.callTimeout").getOrElse(
    throw new IllegalArgumentException("Authorisation: Circuit Breaker call timeout is not configured"))

  val breakerResetTimeout: FiniteDuration = config.getOptional[FiniteDuration]("authorisation.iam.cb.resetTimeout").getOrElse(
    throw new IllegalArgumentException("Authorisation: Circuit Breaker reset timeout is not configured"))

  val breakerMaxRetries: TerminationPolicy = config.getOptional[Int]("authorisation.iam.maxRetries").getOrElse(
    throw new IllegalArgumentException("Authorisation: Circuit Breaker max retries is not configured")).attempts

  val breakerRetryBackoff: FiniteDuration = config.getOptional[FiniteDuration]("authorisation.iam.retry.backoff.duration").getOrElse(
    throw new IllegalArgumentException("Authorisation: Circuit Breaker the duration of exponential backoff is not configured"))

  lazy val breaker: CircuitBreaker = new CircuitBreaker(
    actorSystem.scheduler,
    breakerMaxFailures,
    breakerCallTimeout,
    breakerResetTimeout).onHalfOpen {
    circuitStatus.set(METRICS_BREAKER_OPEN)
  }.onOpen {
    circuitStatus.set(METRICS_BREAKER_OPEN)
  }.onClose {
    circuitStatus.set(METRICS_BREAKER_CLOSED)
  }

  implicit val retryRecover = retryFor { breakerMaxRetries } using {
    exponentialBackoff { breakerRetryBackoff }
  } monitorWith {
    logger.logger onRetrying logNothing onInterrupted logWarning onAborted logError
  }

  override def apply(token: OAuth2Token): Future[Option[TokenInfo]] = {
    breaker.withCircuitBreaker(
      pluggableMetrics.timing(
        retryAsync(s"Calling $authEndpoint") {
          ws.url(authEndpoint).withQueryStringParameters(("access_token", token.value)).get()
        })).map { response =>
        response.status match {
          case OK => Some(response.json.as[TokenInfo])
          case _ => None
        }
      } recover {
        case NonFatal(e) =>
          logger.error(s"Exception occurred during validation of token '${token.toSafeString}': $e")
          None // consider any exception as invalid token
      }
  }

} 
Example 50
Source File: PublicServer.scala    From endpoints4s   with MIT License 5 votes vote down vote up
package cqrs.publicserver

import cats.Traverse
import cqrs.queries._
import cqrs.commands.{AddRecord, CreateMeter, MeterCreated, StoredEvent}
import play.api.libs.ws.WSClient
import play.api.routing.{Router => PlayRouter}
import cats.instances.option._
import cats.instances.future._
import endpoints4s.play.server.{Endpoints, JsonEntitiesFromCodecs, PlayComponents}

import scala.concurrent.Future


class PublicServer(
    commandsBaseUrl: String,
    queriesBaseUrl: String,
    wsClient: WSClient,
    val playComponents: PlayComponents
) extends PublicEndpoints
    with Endpoints
    with JsonEntitiesFromCodecs {

  import playComponents.executionContext

  private val commandsClient = new CommandsClient(commandsBaseUrl, wsClient)
  //#invocation
  private val queriesClient = new QueriesClient(queriesBaseUrl, wsClient)
  //#invocation

  val routes: PlayRouter.Routes =
    routesFromEndpoints(
      listMeters.implementedByAsync { _ =>
        //#invocation
        val metersList: Future[ResourceList] = queriesClient.query(FindAll)
        //#invocation
        metersList.map(_.value)
      },
      getMeter.implementedByAsync { id =>
        queriesClient.query(FindById(id, None)).map(_.value)
      },
      createMeter.implementedByAsync { createData =>
        //#microservice-endpoint-invocation
        val eventuallyMaybeEvent: Future[Option[StoredEvent]] =
          commandsClient.command(CreateMeter(createData.label))
        //#microservice-endpoint-invocation
        for {
          maybeEvent <- eventuallyMaybeEvent
          maybeMeter <- Traverse[Option].flatSequence(
            maybeEvent.collect {
              case StoredEvent(t, MeterCreated(id, _)) =>
                //#invocation-find-by-id
                val maybeMeter: Future[MaybeResource] =
                  queriesClient.query(FindById(id, after = Some(t)))
                //#invocation-find-by-id
                maybeMeter.map(_.value)
            }
          )
          meter <- maybeMeter.fold[Future[Meter]](
            Future.failed(new NoSuchElementException)
          )(Future.successful)
        } yield meter
      },
      addRecord.implementedByAsync {
        case (id, addData) =>
          for {
            maybeEvent <- commandsClient.command(
              AddRecord(id, addData.date, addData.value)
            )
            findMeter =
              (evt: StoredEvent) =>
                queriesClient
                  .query(FindById(id, after = Some(evt.timestamp)))
                  .map(_.value)
            maybeMeter <- Traverse[Option].flatTraverse(maybeEvent)(findMeter)
            meter <- maybeMeter.fold[Future[Meter]](
              Future.failed(new NoSuchElementException)
            )(Future.successful)
          } yield meter
      }
    )

  // These aliases are probably due to a limitation of circe
  implicit private def circeEncoderReq: io.circe.Encoder[QueryReq] =
    QueryReq.queryEncoder
  implicit private def circeDecoderResp: io.circe.Decoder[QueryResp] =
    QueryResp.queryDecoder

} 
Example 51
Source File: QueriesService.scala    From endpoints4s   with MIT License 5 votes vote down vote up
package cqrs.queries

import java.util.UUID

import akka.actor.Scheduler
import play.api.libs.ws.WSClient

import scala.concurrent.ExecutionContext.Implicits.global
import cqrs.commands.{CommandsEndpoints, MeterCreated, RecordAdded, StoredEvent}

import scala.collection.immutable.SortedMap
import scala.concurrent.duration.DurationInt
import scala.concurrent.Future
import scala.concurrent.stm.{Ref, atomic}


  private def applyEvent(state: State, storedEvent: StoredEvent): State =
    storedEvent match {
      case StoredEvent(t, MeterCreated(id, label)) =>
        state.copy(
          lastEventTimestamp = Some(t),
          meters = state.meters + (id -> Meter(id, label, SortedMap.empty))
        )
      case StoredEvent(t, RecordAdded(id, date, value)) =>
        val meter = state.meters(id)
        val updatedMeter =
          meter.copy(timeSeries = meter.timeSeries + (date -> value))
        state.copy(
          lastEventTimestamp = Some(t),
          meters = state.meters + (id -> updatedMeter)
        )
    }

}