From 4ce2d94ccbf548f9ce5c6bd63dddeb06dcf6a956 Mon Sep 17 00:00:00 2001 From: frcroth Date: Mon, 6 Nov 2023 15:26:13 +0100 Subject: [PATCH 1/3] Add error when requesting too high API version --- app/RequestHandler.scala | 22 +++++++++++++++++-- .../util/mvc/ExtendedController.scala | 1 + 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/app/RequestHandler.scala b/app/RequestHandler.scala index 09f1b09c11d..873e1081ac7 100644 --- a/app/RequestHandler.scala +++ b/app/RequestHandler.scala @@ -1,5 +1,7 @@ +import com.scalableminds.util.mvc.ExtendedController import com.typesafe.scalalogging.LazyLogging -import controllers.{Assets, WkorgProxyController, SitemapController} +import controllers.{Assets, SitemapController, WkorgProxyController} + import javax.inject.Inject import play.api.OptionalDevContext import play.api.http.{DefaultHttpRequestHandler, HttpConfiguration, HttpErrorHandler, HttpFilters} @@ -27,9 +29,24 @@ class RequestHandler @Inject()(webCommands: WebCommands, filters ) with InjectedController + with ExtendedController with LazyLogging { - override def routeRequest(request: RequestHeader): Option[Handler] = + private def CURRENT_API_VERSION = 5 + + override def routeRequest(request: RequestHeader): Option[Handler] = { + "^/api/v(\\d+).*$".r.findFirstMatchIn(request.uri) match { + case Some(m) => + val version = m.group(1) + if (version.toInt > CURRENT_API_VERSION) { + return Some(Action { + JsonNotFound( + f"This WEBKNOSSOS instance does not yet support this API version. The requested API version $version is higher than the current API version $CURRENT_API_VERSION.") + }) + } + case None => + } + if (request.uri.matches("^(/api/|/data/|/tracings/|/swagger|/\\.well-known/).*$")) { super.routeRequest(request) } else if (request.uri.matches("^(/assets/).*$")) { @@ -42,4 +59,5 @@ class RequestHandler @Inject()(webCommands: WebCommands, } else if (request.uri == "/favicon.ico") { Some(Action { NotFound }) } else Some(wkorgProxyController.proxyPageOrMainView) + } } diff --git a/util/src/main/scala/com/scalableminds/util/mvc/ExtendedController.scala b/util/src/main/scala/com/scalableminds/util/mvc/ExtendedController.scala index 80c2ad450cf..54171c1665e 100644 --- a/util/src/main/scala/com/scalableminds/util/mvc/ExtendedController.scala +++ b/util/src/main/scala/com/scalableminds/util/mvc/ExtendedController.scala @@ -188,6 +188,7 @@ trait MimeTypes { trait JsonResults extends JsonResultAttribues { val JsonOk = new JsonResult(OK) val JsonBadRequest = new JsonResult(BAD_REQUEST) + val JsonNotFound = new JsonResult(NOT_FOUND) } trait JsonResultAttribues { From afaa0d0a62342bac594a1eb89aff105df923ffed Mon Sep 17 00:00:00 2001 From: frcroth Date: Mon, 6 Nov 2023 15:29:26 +0100 Subject: [PATCH 2/3] Update changelog --- CHANGELOG.unreleased.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.unreleased.md b/CHANGELOG.unreleased.md index 0dfeedfed29..6c9fb1b6add 100644 --- a/CHANGELOG.unreleased.md +++ b/CHANGELOG.unreleased.md @@ -14,6 +14,7 @@ For upgrade instructions, please check the [migration guide](MIGRATIONS.released - Added support for reading uint24 rgb layers in datasets with zarr2/zarr3/n5/neuroglancerPrecomputed format, as used for voxelytics predictions. [#7413](https://github.com/scalableminds/webknossos/pull/7413) ### Changed +- An appropriate error is returned when requesting an API version that is higher that the current version. [#7424](https://github.com/scalableminds/webknossos/pull/7424) ### Fixed - Searching the segments in the sidebar will highlight newly focused segments properly now. [#7406](https://github.com/scalableminds/webknossos/pull/7406) From 9c0a008db343551f11579c482ed13ba664eb84e3 Mon Sep 17 00:00:00 2001 From: frcroth Date: Wed, 8 Nov 2023 09:30:19 +0100 Subject: [PATCH 3/3] Remove return --- app/RequestHandler.scala | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/app/RequestHandler.scala b/app/RequestHandler.scala index 873e1081ac7..ed0879e5381 100644 --- a/app/RequestHandler.scala +++ b/app/RequestHandler.scala @@ -32,22 +32,13 @@ class RequestHandler @Inject()(webCommands: WebCommands, with ExtendedController with LazyLogging { - private def CURRENT_API_VERSION = 5 - - override def routeRequest(request: RequestHeader): Option[Handler] = { - "^/api/v(\\d+).*$".r.findFirstMatchIn(request.uri) match { - case Some(m) => - val version = m.group(1) - if (version.toInt > CURRENT_API_VERSION) { - return Some(Action { - JsonNotFound( - f"This WEBKNOSSOS instance does not yet support this API version. The requested API version $version is higher than the current API version $CURRENT_API_VERSION.") - }) - } - case None => - } - - if (request.uri.matches("^(/api/|/data/|/tracings/|/swagger|/\\.well-known/).*$")) { + override def routeRequest(request: RequestHeader): Option[Handler] = + if (apiVersionIsTooNew(request)) { + Some(Action { + JsonNotFound( + f"This WEBKNOSSOS instance does not yet support this API version. The requested API version is higher than the current API version $CURRENT_API_VERSION.") + }) + } else if (request.uri.matches("^(/api/|/data/|/tracings/|/swagger|/\\.well-known/).*$")) { super.routeRequest(request) } else if (request.uri.matches("^(/assets/).*$")) { val path = request.path.replaceFirst("^(/assets/)", "") @@ -59,5 +50,14 @@ class RequestHandler @Inject()(webCommands: WebCommands, } else if (request.uri == "/favicon.ico") { Some(Action { NotFound }) } else Some(wkorgProxyController.proxyPageOrMainView) - } + + private def CURRENT_API_VERSION = 5 + + private def apiVersionIsTooNew(request: RequestHeader): Boolean = + "^/api/v(\\d+).*$".r.findFirstMatchIn(request.uri) match { + case Some(m) => + val version = m.group(1) + version.toInt > CURRENT_API_VERSION + case None => false + } }