Skip to content

Commit 9c89675

Browse files
committed
Redesigning API using JsValue and ServerTests for instances/{id}/assignInstance
1 parent c315cc8 commit 9c89675

File tree

2 files changed

+120
-123
lines changed
  • src
    • main/scala/de/upb/cs/swt/delphi/instanceregistry/connection
    • test/scala/de/upb/cs/swt/delphi/instanceregistry/connection

2 files changed

+120
-123
lines changed

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

Lines changed: 57 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ class Server(handler: RequestHandler) extends HttpApp
5353
fetchInstancesOfType()
5454
} ~
5555
path("register") {
56-
entity(as[String]) { jsonString => register(jsonString) }
56+
entity(as[String]) {
57+
jsonString => register(jsonString)
58+
}
5759
} ~
5860
path("network") {
5961
network()
@@ -72,6 +74,9 @@ class Server(handler: RequestHandler) extends HttpApp
7274
path("matchingInstance") {
7375
matchingInstance(Id)
7476
} ~
77+
path("matchingResult") {
78+
entity(as[JsValue]) { json => matchInstance(Id, json.asJsObject.fields("MatchingSuccessful").asInstanceOf[Boolean], json.asJsObject.fields("SenderId").asInstanceOf[Long]) }
79+
} ~
7580
path("eventList") {
7681
eventList(Id)
7782
} ~
@@ -106,16 +111,15 @@ class Server(handler: RequestHandler) extends HttpApp
106111
deleteContainer(Id)
107112
} ~
108113
path("assignInstance") {
109-
entity(as[String]) { jsonString => assignInstance(Id, jsonString) }
114+
entity(as[JsValue]) {
115+
json => assignInstance(Id, json.asJsObject.fields("AssignedInstanceId").toString())
116+
}
110117
} ~
111118
path("label") {
112119
entity(as[JsValue]) { json => addLabel(Id, json.asJsObject.fields("Label").toString()) }
113120
}
114121
}
115122
} ~
116-
path("matchingResult") {
117-
matchInstance()
118-
} ~
119123
path("command") {
120124
runCommandInContainer()
121125
} ~
@@ -337,7 +341,7 @@ class Server(handler: RequestHandler) extends HttpApp
337341
*
338342
* @return Server route that either maps to 200 OK or to the respective error codes
339343
*/
340-
def matchInstance(): server.Route = parameters('CallerId.as[Long], 'MatchedInstanceId.as[Long], 'MatchingSuccessful.as[Boolean]) { (callerId, matchedInstanceId, matchingResult) =>
344+
def matchInstance(callerId: Long, matchingResult: Boolean, matchedInstanceId: Long): server.Route = {
341345
authenticateOAuth2[AccessToken]("Secure Site", AuthProvider.authenticateOAuthRequire(_, userType = UserType.Component)) { token =>
342346
post {
343347
log.debug(s"POST /matchingResult?callerId=$callerId&matchedInstanceId=$matchedInstanceId&MatchingSuccessful=$matchingResult has been called")
@@ -728,53 +732,41 @@ class Server(handler: RequestHandler) extends HttpApp
728732
*
729733
* @return Server route that either maps to 202 ACCEPTED or the respective error codes
730734
*/
731-
def assignInstance(id: Long, assignedInstanceId: String): server.Route = {
735+
def assignInstance(id: Long, assignedInstanceIdStr: String): server.Route = {
732736
authenticateOAuth2[AccessToken]("Secure Site", AuthProvider.authenticateOAuthRequire(_, userType = UserType.Admin)) { token =>
733737

734-
try {
735-
val assignedInstanceIdLng: Long = assignedInstanceId.parseJson.convertTo[Long]
736738

737-
post {
738-
log.debug(s"POST /instances/$id/assignInstance has been called with parameter : $assignedInstanceId ")
739+
val assignedInstanceId: Long = assignedInstanceIdStr.toLong
739740

740-
handler.handleInstanceAssignment(id, assignedInstanceIdLng) match {
741-
case handler.OperationResult.IdUnknown =>
742-
log.warning(s"Cannot assign $assignedInstanceId to $id, one or more ids not found.")
743-
complete {
744-
HttpResponse(StatusCodes.NotFound, entity = s"Cannot assign instance, at least one of the ids $id / $assignedInstanceId was not found.")
745-
}
746-
case handler.OperationResult.NoDockerContainer =>
747-
log.warning(s"Cannot assign $assignedInstanceId to $id, $id is no docker container.")
748-
complete {
749-
HttpResponse(StatusCodes.BadRequest, entity = s"Cannot assign instance, $id is no docker container.")
750-
}
751-
case handler.OperationResult.InvalidTypeForOperation =>
752-
log.warning(s"Cannot assign $assignedInstanceId to $id, incompatible types.")
753-
complete {
754-
HttpResponse(StatusCodes.BadRequest, entity = s"Cannot assign $assignedInstanceId to $id, incompatible types.")
755-
}
756-
case handler.OperationResult.Ok =>
757-
complete {
758-
HttpResponse(StatusCodes.Accepted, entity = "Operation accepted.")
759-
}
760-
case x =>
761-
complete {
762-
HttpResponse(StatusCodes.InternalServerError, entity = s"Unexpected operation result $x")
763-
}
764-
}
741+
post {
742+
log.debug(s"POST /instances/$id/assignInstance has been called with parameter : $assignedInstanceId ")
743+
744+
handler.handleInstanceAssignment(id, assignedInstanceId) match {
745+
case handler.OperationResult.IdUnknown =>
746+
log.warning(s"Cannot assign $assignedInstanceId to $id, one or more ids not found.")
747+
complete {
748+
HttpResponse(StatusCodes.NotFound, entity = s"Cannot assign instance, at least one of the ids $id / $assignedInstanceId was not found.")
749+
}
750+
case handler.OperationResult.NoDockerContainer =>
751+
log.warning(s"Cannot assign $assignedInstanceId to $id, $id is no docker container.")
752+
complete {
753+
HttpResponse(StatusCodes.BadRequest, entity = s"Cannot assign instance, $id is no docker container.")
754+
}
755+
case handler.OperationResult.InvalidTypeForOperation =>
756+
log.warning(s"Cannot assign $assignedInstanceId to $id, incompatible types.")
757+
complete {
758+
HttpResponse(StatusCodes.BadRequest, entity = s"Cannot assign $assignedInstanceId to $id, incompatible types.")
759+
}
760+
case handler.OperationResult.Ok =>
761+
complete {
762+
HttpResponse(StatusCodes.Accepted, entity = "Operation accepted.")
763+
}
764+
case x =>
765+
complete {
766+
HttpResponse(StatusCodes.InternalServerError, entity = s"Unexpected operation result $x")
767+
}
765768
}
766769
}
767-
catch {
768-
case dx: DeserializationException =>
769-
log.error(dx, "Deserialization exception")
770-
complete(HttpResponse(StatusCodes.BadRequest, entity = s"Could not deserialize parameter instance with message ${dx.getMessage}."))
771-
case px: ParsingException =>
772-
log.error(px, "Failed to parse JSON while assigning instance")
773-
complete(HttpResponse(StatusCodes.BadRequest, entity = s"Failed to parse JSON entity with message ${px.getMessage}"))
774-
case x: Exception =>
775-
log.error(x, "Uncaught exception while deserializing.")
776-
complete(HttpResponse(StatusCodes.InternalServerError, entity = "An internal server error occurred."))
777-
}
778770
}
779771
}
780772

@@ -857,26 +849,25 @@ class Server(handler: RequestHandler) extends HttpApp
857849
def addLabel(id: Long, label: String): server.Route = {
858850
authenticateOAuth2[AccessToken]("Secure Site", AuthProvider.authenticateOAuthRequire(_, userType = UserType.Admin)) { token =>
859851

860-
861-
post {
862-
log.debug(s"POST /instances/$id/label with parameter label=$label has been called.")
863-
handler.handleAddLabel(id, label) match {
864-
case handler.OperationResult.IdUnknown =>
865-
log.warning(s"Cannot add label $label to $id, id not found.")
866-
complete {
867-
HttpResponse(StatusCodes.NotFound, entity = s"Cannot add label, id $id not found.")
868-
}
869-
case handler.OperationResult.InternalError =>
870-
log.warning(s"Error while adding label $label to $id: Label exceeds character limit.")
871-
complete {
872-
HttpResponse(StatusCodes.BadRequest,
873-
entity = s"Cannot add label to $id, label exceeds character limit of ${Registry.configuration.maxLabelLength}")
874-
}
875-
case handler.OperationResult.Ok =>
876-
log.info(s"Successfully added label $label to instance with id $id.")
877-
complete("Successfully added label")
878-
}
852+
post {
853+
log.debug(s"POST /instances/$id/label with parameter label=$label has been called.")
854+
handler.handleAddLabel(id, label) match {
855+
case handler.OperationResult.IdUnknown =>
856+
log.warning(s"Cannot add label $label to $id, id not found.")
857+
complete {
858+
HttpResponse(StatusCodes.NotFound, entity = s"Cannot add label, id $id not found.")
859+
}
860+
case handler.OperationResult.InternalError =>
861+
log.warning(s"Error while adding label $label to $id: Label exceeds character limit.")
862+
complete {
863+
HttpResponse(StatusCodes.BadRequest,
864+
entity = s"Cannot add label to $id, label exceeds character limit of ${Registry.configuration.maxLabelLength}")
865+
}
866+
case handler.OperationResult.Ok =>
867+
log.info(s"Successfully added label $label to instance with id $id.")
868+
complete("Successfully added label")
879869
}
870+
}
880871
}
881872
}
882873

0 commit comments

Comments
 (0)