Skip to content

Commit e82dee1

Browse files
author
Johannes Duesing
committed
Data validation of posted entity for /addLabel and /assignInstance
1 parent b4d2530 commit e82dee1

File tree

1 file changed

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

1 file changed

+58
-46
lines changed

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

Lines changed: 58 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,11 @@ class Server(handler: RequestHandler) extends HttpApp
111111
} ~
112112
path("assignInstance") {
113113
entity(as[JsValue]) {
114-
json => assignInstance(Id, json.asJsObject.fields("AssignedInstanceId").toString())
114+
json => assignInstance(Id, json.asJsObject)
115115
}
116116
} ~
117117
path("label") {
118-
entity(as[JsValue]) { json => addLabel(Id, json.asJsObject.fields("Label").toString()) }
118+
entity(as[JsValue]) { json => addLabel(Id, json.asJsObject) }
119119
}
120120
}
121121
} ~
@@ -759,39 +759,44 @@ class Server(handler: RequestHandler) extends HttpApp
759759
*
760760
* @return Server route that either maps to 202 ACCEPTED or the respective error codes
761761
*/
762-
def assignInstance(id: Long, assignedInstanceIdStr: String): server.Route = {
762+
def assignInstance(id: Long, json: JsObject): server.Route = {
763763
authenticateOAuth2[AccessToken]("Secure Site", AuthProvider.authenticateOAuthRequire(_, userType = UserType.Admin)) { token =>
764764

765-
766-
val assignedInstanceId: Long = assignedInstanceIdStr.toLong
767-
768765
post {
769-
log.debug(s"POST /instances/$id/assignInstance has been called with parameter : $assignedInstanceId ")
766+
log.debug(s"POST /instances/$id/assignInstance has been called with data : $json ")
770767

771-
handler.handleInstanceAssignment(id, assignedInstanceId) match {
772-
case handler.OperationResult.IdUnknown =>
773-
log.warning(s"Cannot assign $assignedInstanceId to $id, one or more ids not found.")
774-
complete {
775-
HttpResponse(StatusCodes.NotFound, entity = s"Cannot assign instance, at least one of the ids $id / $assignedInstanceId was not found.")
776-
}
777-
case handler.OperationResult.NoDockerContainer =>
778-
log.warning(s"Cannot assign $assignedInstanceId to $id, $id is no docker container.")
779-
complete {
780-
HttpResponse(StatusCodes.BadRequest, entity = s"Cannot assign instance, $id is no docker container.")
781-
}
782-
case handler.OperationResult.InvalidTypeForOperation =>
783-
log.warning(s"Cannot assign $assignedInstanceId to $id, incompatible types.")
784-
complete {
785-
HttpResponse(StatusCodes.BadRequest, entity = s"Cannot assign $assignedInstanceId to $id, incompatible types.")
786-
}
787-
case handler.OperationResult.Ok =>
788-
complete {
789-
HttpResponse(StatusCodes.Accepted, entity = "Operation accepted.")
790-
}
791-
case x =>
792-
complete {
793-
HttpResponse(StatusCodes.InternalServerError, entity = s"Unexpected operation result $x")
768+
Try[Long] {
769+
json.fields("AssignedInstanceId").toString.toLong
770+
} match {
771+
case Success(assignedInstanceId) =>
772+
handler.handleInstanceAssignment(id, assignedInstanceId) match {
773+
case handler.OperationResult.IdUnknown =>
774+
log.warning(s"Cannot assign $assignedInstanceId to $id, one or more ids not found.")
775+
complete {
776+
HttpResponse(StatusCodes.NotFound, entity = s"Cannot assign instance, at least one of the ids $id / $assignedInstanceId was not found.")
777+
}
778+
case handler.OperationResult.NoDockerContainer =>
779+
log.warning(s"Cannot assign $assignedInstanceId to $id, $id is no docker container.")
780+
complete {
781+
HttpResponse(StatusCodes.BadRequest, entity = s"Cannot assign instance, $id is no docker container.")
782+
}
783+
case handler.OperationResult.InvalidTypeForOperation =>
784+
log.warning(s"Cannot assign $assignedInstanceId to $id, incompatible types.")
785+
complete {
786+
HttpResponse(StatusCodes.BadRequest, entity = s"Cannot assign $assignedInstanceId to $id, incompatible types.")
787+
}
788+
case handler.OperationResult.Ok =>
789+
complete {
790+
HttpResponse(StatusCodes.Accepted, entity = "Operation accepted.")
791+
}
792+
case x =>
793+
complete {
794+
HttpResponse(StatusCodes.InternalServerError, entity = s"Unexpected operation result $x")
795+
}
794796
}
797+
case Failure(ex) =>
798+
log.warning(s"Failed to unmarshal parameters with message ${ex.getMessage}. Data: $json")
799+
complete{HttpResponse(StatusCodes.BadRequest, entity = "Wrong data format supplied.")}
795800
}
796801
}
797802
}
@@ -873,26 +878,33 @@ class Server(handler: RequestHandler) extends HttpApp
873878
*
874879
* @return Server route that either maps to 200 OK or the respective error codes.
875880
*/
876-
def addLabel(id: Long, label: String): server.Route = {
881+
def addLabel(id: Long, json: JsObject): server.Route = {
877882
authenticateOAuth2[AccessToken]("Secure Site", AuthProvider.authenticateOAuthRequire(_, userType = UserType.Admin)) { token =>
878883

879884
post {
880-
log.debug(s"POST /instances/$id/label with parameter label=$label has been called.")
881-
handler.handleAddLabel(id, label) match {
882-
case handler.OperationResult.IdUnknown =>
883-
log.warning(s"Cannot add label $label to $id, id not found.")
884-
complete {
885-
HttpResponse(StatusCodes.NotFound, entity = s"Cannot add label, id $id not found.")
886-
}
887-
case handler.OperationResult.InternalError =>
888-
log.warning(s"Error while adding label $label to $id: Label exceeds character limit.")
889-
complete {
890-
HttpResponse(StatusCodes.BadRequest,
891-
entity = s"Cannot add label to $id, label exceeds character limit of ${Registry.configuration.maxLabelLength}")
885+
log.debug(s"POST /instances/$id/label has been called with data $json.")
886+
887+
Try[String](json.fields("Label").toString) match {
888+
case Success(label) =>
889+
handler.handleAddLabel(id, label) match {
890+
case handler.OperationResult.IdUnknown =>
891+
log.warning(s"Cannot add label $label to $id, id not found.")
892+
complete {
893+
HttpResponse(StatusCodes.NotFound, entity = s"Cannot add label, id $id not found.")
894+
}
895+
case handler.OperationResult.InternalError =>
896+
log.warning(s"Error while adding label $label to $id: Label exceeds character limit.")
897+
complete {
898+
HttpResponse(StatusCodes.BadRequest,
899+
entity = s"Cannot add label to $id, label exceeds character limit of ${Registry.configuration.maxLabelLength}")
900+
}
901+
case handler.OperationResult.Ok =>
902+
log.info(s"Successfully added label $label to instance with id $id.")
903+
complete("Successfully added label")
892904
}
893-
case handler.OperationResult.Ok =>
894-
log.info(s"Successfully added label $label to instance with id $id.")
895-
complete("Successfully added label")
905+
case Failure(ex) =>
906+
log.warning(s"Failed to unmarshal parameters with message ${ex.getMessage}. Data: $json")
907+
complete{HttpResponse(StatusCodes.BadRequest, entity = "Wrong data format supplied.")}
896908
}
897909
}
898910
}

0 commit comments

Comments
 (0)