Skip to content

Commit

Permalink
small routing bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianKirmaier committed Jan 3, 2025
1 parent 6d94d5e commit 99251b4
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package one.jpro.platform.routing.sessionmanager

import javafx.scene.control.Label
import one.jpro.platform.routing.{Response, Route, RouteApp}
import org.junit.jupiter.api.Test
import simplefx.core._

class TestSessionManager {
@Test
Expand All @@ -11,4 +14,26 @@ class TestSessionManager {
assert(SessionManager.mergeURLs("http://a.com/b/c", "../d") == "http://a.com/d")
assert(SessionManager.mergeURLs("http://a/b", "/") == "http://a/")
}

@Test
def testRedirects(): Unit = {
val route = Route.empty()
.and(Route.get("/",r => Response.node(new Label("Empty"))))
.path("/test", Route.empty()
.and(Route.redirect("/","./test2"))
.and(Route.get("/test2", r => Response.node(new Label("Yes"))))
)

val app = new RouteApp {
override def createRoute(): Route = route
}
val stage = inFX(new javafx.stage.Stage())
inFX(app.start(stage))

val res = inFX(app.getSessionManager().gotoURL("/test/").future).await
inFX {
val url = app.getSessionManager().getURL()
assert(url == "/test/test2")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ case class Response(future: FXFuture[ResponseResult]) {
object Response {
def empty(): Response = Response(FXFuture.unit(null))
def redirect(to: String): Response = Response(FXFuture.unit(Redirect(to)))
def error(ex: Exception): Response = Response(FXFuture.error(ex))

def view(view: View): Response = Response(FXFuture.unit(view))
def node(node: javafx.scene.Node): Response = Response(FXFuture.unit(View.fromNode(node)))
def fromFuture(future: FXFuture[Response]): Response = Response(future.flatMap(_.future))
def fromResult(result: ResponseResult): Response = Response(FXFuture.unit(result))
def fromFutureResult(future: FXFuture[ResponseResult]): Response = Response(future)
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,39 +32,43 @@ trait SessionManager { THIS =>
@Bind var url: String = null
@Bind var view: View = null

def getURL(): String = url

def goBack(): Unit
def goForward(): Unit
def isExternal(x: String): Boolean = x.startsWith("http")
def gotoURL(url: String): Unit = {
def gotoURL(url: String): Response = {
if(isExternal(url)) {
if(WebAPI.isBrowser) {
this.asInstanceOf[SessionManagerWeb].webAPI.executeScript(s"""window.location.href = "$url";""")
} else {
SessionManager.externalLinkImpl.accept(url)
}
Response.redirect(url)
} else {
gotoURL(url,true)
}
}
def gotoURL(url: String, pushState: Boolean = true): Unit = {
def gotoURL(url: String, pushState: Boolean = true): Response = {
val url2 = SessionManager.mergeURLs(THIS.url, url)
try {
logger.debug(s"goto: $url")
val request = getRequest(url)
logger.debug(s"goto: $url2")
val request = getRequest(url2)
val newView = if(view != null && view.handleRequest(request)) Response(FXFuture(view)) else {
webApp.getRoute()(request)
}
newView.future.map { response =>
Response.fromFuture(newView.future.map { response =>
assert(response != null, s"Response for $url2 was null")
this.url = url2
gotoURL(url2, response, pushState)
}
})
} catch {
case ex: Exception =>
logger.error(s"Error while loading the path $url2", ex)
Response.error(ex)
}
}
def gotoURL(_url: String, x: ResponseResult, pushState: Boolean): Unit
def gotoURL(_url: String, x: ResponseResult, pushState: Boolean): Response

def getRequest(url: String): Request = {
val node = if(view == null) null else view.realContent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class SessionManagerDesktop(val webApp: RouteNode) extends SessionManager { THIS
gotoURL(historyCurrent.path, false)
}

def gotoURL(_url: String, x: ResponseResult, pushState: Boolean): Unit = {
def gotoURL(_url: String, x: ResponseResult, pushState: Boolean): Response = {
x match {
case Redirect(url) =>
logger.debug(s"redirect: ${_url} -> $url")
Expand Down Expand Up @@ -54,6 +54,7 @@ class SessionManagerDesktop(val webApp: RouteNode) extends SessionManager { THIS
}
historyCurrent = HistoryEntry(url, view.title)
}
Response.view(view)
}
}
val container = new StackPane()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ class SessionManagerDummy(val webApp: RouteNode) extends SessionManager {

override def goForward(): Unit = ()

override def gotoURL(_url: String, x: ResponseResult, pushState: Boolean): Unit = {

override def gotoURL(_url: String, x: ResponseResult, pushState: Boolean): Response = {
Response.fromResult(x)
}

override def start(): Unit = ()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@ class SessionManagerWeb(val webApp: RouteNode, val webAPI: WebAPI) extends Sessi
})
}

def gotoURL(_url: String, x: ResponseResult, pushState: Boolean): Unit = {
def gotoURL(_url: String, x: ResponseResult, pushState: Boolean): Response = {
assert(x != null, "Response was null for url: " + _url)
val url = _url
x match {
case Redirect(url) =>
if(isExternal(url)) {
this.asInstanceOf[SessionManagerWeb].webAPI.executeScript(s"""window.location.href = "$url";""")
Response.fromResult(x)
} else {
gotoURL(url)
}
Expand Down Expand Up @@ -81,6 +82,7 @@ class SessionManagerWeb(val webApp: RouteNode, val webAPI: WebAPI) extends Sessi
webAPI.executeScript(s"""document.title = "${view.title.replace("\"","\\\"")}";""")
webAPI.executeScript(s"""document.querySelector('meta[name="description"]').setAttribute("content", "${view.description.replace("\"","\\\"")}");""")
webAPI.executeScript(s"history.replaceState($initialState, null, null)")
Response.fromResult(x)
}
}

Expand Down

0 comments on commit 99251b4

Please sign in to comment.