Skip to content

Commit b4d2530

Browse files
author
Johannes Duesing
committed
Fixed a bug in /matchingResult. Introduced error handling for POST data
1 parent b63e3a3 commit b4d2530

File tree

1 file changed

+27
-16
lines changed
  • src/main/scala/de/upb/cs/swt/delphi/instanceregistry/connection

1 file changed

+27
-16
lines changed

src/main/scala/de/upb/cs/swt/delphi/instanceregistry/connection/Server.scala

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import spray.json.JsonParser.ParsingException
1717
import spray.json._
1818

1919
import scala.concurrent.ExecutionContext
20-
import scala.util.{Failure, Success}
20+
import scala.util.{Failure, Success, Try}
2121

2222
/**
2323
* Web server configuration for Instance Registry API.
@@ -73,7 +73,7 @@ class Server(handler: RequestHandler) extends HttpApp
7373
} ~
7474
path("matchingResult") {
7575
entity(as[JsValue]) {
76-
json => matchInstance(Id, json.asJsObject.fields("MatchingSuccessful").toString(), json.asJsObject.fields("SenderId").toString())
76+
json => matchInstance(Id, json.asJsObject)
7777
}
7878
} ~
7979
path("eventList") {
@@ -349,25 +349,36 @@ class Server(handler: RequestHandler) extends HttpApp
349349
*
350350
* @return Server route that either maps to 200 OK or to the respective error codes
351351
*/
352-
def matchInstance(callerId: Long, matchingResultStr: String, matchedInstanceIdStr: String): server.Route = {
352+
def matchInstance(affectedInstanceId: Long, json: JsObject): server.Route = {
353353
authenticateOAuth2[AccessToken]("Secure Site", AuthProvider.authenticateOAuthRequire(_, userType = UserType.Component)) { token =>
354354

355-
val matchingResult: Boolean = matchingResultStr.toBoolean
356-
val matchedInstanceId: Long = matchedInstanceIdStr.toLong
357355
post {
358-
log.debug(s"POST /instances/$callerId/matchingResult has been called with parameters : matchedInstanceId=$matchedInstanceId&MatchingSuccessful=$matchingResult")
359-
360-
handler.handleMatchingResult(callerId, matchedInstanceId, matchingResult) match {
361-
case handler.OperationResult.IdUnknown =>
362-
log.warning(s"Cannot apply matching result for id $callerId to id $matchedInstanceId, at least one id could not be found")
363-
complete {
364-
HttpResponse(StatusCodes.NotFound, entity = s"One of the ids $callerId and $matchedInstanceId was not found.")
365-
}
366-
case handler.OperationResult.Ok =>
367-
complete {
368-
s"Matching result $matchingResult processed."
356+
log.debug(s"POST /instances/$affectedInstanceId/matchingResult has been called with entity $json")
357+
358+
Try[(Boolean, Long)] {
359+
val callerId = json.fields("SenderId").toString.toLong
360+
val result = json.fields("MatchingSuccessful").toString.toBoolean
361+
(result, callerId)
362+
} match {
363+
case Success((result, callerId)) =>
364+
365+
handler.handleMatchingResult(callerId, affectedInstanceId, result) match {
366+
case handler.OperationResult.IdUnknown =>
367+
log.warning(s"Cannot apply matching result for id $callerId to id $affectedInstanceId, at least one id could not be found")
368+
complete {
369+
HttpResponse(StatusCodes.NotFound, entity = s"One of the ids $callerId and $affectedInstanceId was not found.")
370+
}
371+
case handler.OperationResult.Ok =>
372+
complete {
373+
s"Matching result $result processed."
374+
}
369375
}
376+
377+
case Failure(ex) =>
378+
log.warning(s"Failed to unmarshal parameters with message ${ex.getMessage}. Data: $json")
379+
complete{HttpResponse(StatusCodes.BadRequest, entity = "Wrong data format supplied.")}
370380
}
381+
371382
}
372383
}
373384
}

0 commit comments

Comments
 (0)