Skip to content

Commit

Permalink
minor code clean
Browse files Browse the repository at this point in the history
  • Loading branch information
workingDog committed Feb 23, 2018
1 parent 60278b9 commit d35a299
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 28 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Change Log

### changes in 0.4-SNAPSHOT

* updated sbt to 1.1.0


### changes in 0.3
Expand Down
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version = 1.0.3
sbt.version = 1.1.0
50 changes: 23 additions & 27 deletions src/main/scala/com/kodekutters/taxii/TaxiiConnection.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import scala.concurrent.duration._
import scala.concurrent.Future
import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import play.api.libs.ws.StandaloneWSResponse

//import com.typesafe.scalalogging.Logger

import scala.language.postfixOps
Expand Down Expand Up @@ -101,7 +103,7 @@ case class TaxiiConnection(host: String,
*/
def fetch[T: TypeTag](thePath: String, theHeaders: Seq[(String, String)] = getHeaders,
filter: Option[Seq[(String, String)]] = None): Future[Either[TaxiiErrorMessage, T]] = {
// println("----> thePath="+thePath)

wsClient.url(thePath)
.withAuth(user, password, WSAuthScheme.BASIC)
.withHttpHeaders(theHeaders: _*)
Expand All @@ -111,34 +113,16 @@ case class TaxiiConnection(host: String,
.get().map { response =>
response.status match {
// partial content
case 206 =>
// todo aggregate the partial content
// val contentRangeOpt = response.header("Content-Range")
// contentRangeOpt.map(r => {
// val rangeTuple = toRangeInfo(r)
// println("----> r: " + r + " start: " + rangeTuple._1 + " end: " + rangeTuple._2 + " total: " + rangeTuple._3)
// })
val js = response.body[JsValue]
jsonToTaxii[T](js).asOpt match {
case Some(taxiiObj) =>
taxiiObj match {
case x: TaxiiErrorMessage => Left(x)
case x => Right(x.asInstanceOf[T])
}
case None => Left(TaxiiErrorMessage("fetch failed: cannot deserialize response"))
}
case 206 => getTaxiiObject[T](response)
// todo aggregate the partial content
// val contentRangeOpt = response.header("Content-Range")
// contentRangeOpt.map(r => {
// val rangeTuple = toRangeInfo(r)
// println("----> r: " + r + " start: " + rangeTuple._1 + " end: " + rangeTuple._2 + " total: " + rangeTuple._3)
// })

// all results if the server can deliver without pagination
case 200 =>
val js = response.body[JsValue]
jsonToTaxii[T](js).asOpt match {
case Some(taxiiObj) =>
taxiiObj match {
case x: TaxiiErrorMessage => Left(x)
case x => Right(x.asInstanceOf[T])
}
case None => Left(TaxiiErrorMessage("fetch failed: cannot deserialize response"))
}
case 200 => getTaxiiObject[T](response)

case _ => Left(TaxiiErrorMessage(s"fetch failed with response code ${response.status}"))
}
Expand All @@ -147,6 +131,18 @@ case class TaxiiConnection(host: String,
})
}

private def getTaxiiObject[T: TypeTag](response: StandaloneWSResponse): Either[TaxiiErrorMessage, T] = {
val js = response.body[JsValue]
jsonToTaxii[T](js).asOpt match {
case Some(taxiiObj) =>
taxiiObj match {
case x: TaxiiErrorMessage => Left(x)
case x => Right(x.asInstanceOf[T])
}
case None => Left(TaxiiErrorMessage("fetch failed: cannot deserialize response"))
}
}

/**
* convert a json value to a Taxii2 or Bundle STIX object
*/
Expand Down

0 comments on commit d35a299

Please sign in to comment.