Skip to content

Commit

Permalink
#293 include object in webhook body
Browse files Browse the repository at this point in the history
  • Loading branch information
To-om committed Sep 5, 2017
1 parent f766f58 commit cef3e18
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions thehive-backend/app/services/WebHook.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@ package services
import java.net.ConnectException
import javax.inject.Inject

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

import play.api.{ Configuration, Logger }
import play.api.libs.json.JsObject
import play.api.libs.ws.WSRequest

import org.elastic4play.services.AuxSrv

case class WebHook(name: String, ws: WSRequest)(implicit ec: ExecutionContext) {
private[WebHook] lazy val logger = Logger(getClass.getName + "." + name)

def send(obj: JsObject) = ws.post(obj).onComplete {
def send(obj: JsObject): Unit = ws.post(obj).onComplete {
case Success(resp) if resp.status / 100 != 2 logger.error(s"WebHook returns status ${resp.status} ${resp.statusText}")
case Failure(ce: ConnectException) logger.error(s"Connection to WebHook $name error", ce)
case Failure(error) logger.error("WebHook call error", error)
Expand All @@ -22,10 +24,13 @@ case class WebHook(name: String, ws: WSRequest)(implicit ec: ExecutionContext) {
}

class WebHooks(
webhooks: Seq[WebHook]) {
webhooks: Seq[WebHook],
auxSrv: AuxSrv,
implicit val ec: ExecutionContext) {
@Inject() def this(
configuration: Configuration,
globalWS: CustomWSAPI,
auxSrv: AuxSrv,
ec: ExecutionContext) = {
this(
for {
Expand All @@ -35,8 +40,19 @@ class WebHooks(
whConfig Try(cfg.get[Configuration](name)).toOption
url whConfig.getOptional[String]("url")
instanceWS = whWS.withConfig(whConfig).url(url)
} yield WebHook(name, instanceWS)(ec))
} yield WebHook(name, instanceWS)(ec),
auxSrv,
ec)
}

def send(obj: JsObject): Unit = webhooks.foreach(_.send(obj))
def send(obj: JsObject): Unit = {
(for {
objectType (obj \ "objectType").asOpt[String]
objectId (obj \ "objectId").asOpt[String]
} yield auxSrv(objectType, objectId, nparent = 0, withStats = false, removeUnaudited = false))
.getOrElse(Future.successful(JsObject(Nil)))
.map(o obj + ("object" o))
.fallbackTo(Future.successful(obj))
.map(o webhooks.foreach(_.send(o)))
}
}

0 comments on commit cef3e18

Please sign in to comment.