Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto Sitemap with HTTP-Server #41

Merged
merged 8 commits into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
JPRO_PLATFORM_VERSION = 0.5.1-SNAPSHOT


JPRO_VERSION = 2024.4.1
JAVAFX_BUILD_VERSION = 17.0.12
JAVAFX_EXAMPLES_VERSION = 23.0.1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,10 @@ class TestAppCrawler {

@Test
def testCrawlApp(): Unit = {
def app = new RouteNode(null) {
setRoute(Route.empty()
def route = Route.empty()
.and(Route.get("/", r => Response.view(new Page1)))
.and(Route.get("/page2", r => Response.view(new Page2))))
}
val result = AppCrawler.crawlApp("http://localhost", () => app)
.and(Route.get("/page2", r => Response.view(new Page2)))
val result = AppCrawler.crawlRoute("http://localhost", () => route)

assert(result.pages.contains("/"), result.pages)
assert(result.pages.contains("/page2"), result.pages)
Expand All @@ -48,17 +46,15 @@ class TestAppCrawler {

@Test
def testEmptyImage(): Unit = {
def app = new RouteNode(null) {
setRoute(Route.empty()
def route = Route.empty()
.and(Route.get("/", r => Response.view(new View {
override def title: String = ""

override def description: String = ""

override def content: all.Node = new ImageView(null: Image)
}))))
}
val result = AppCrawler.crawlApp("http://localhost", () => app)
})))
val result = AppCrawler.crawlRoute("http://localhost", () => route)
}

@Test
Expand Down Expand Up @@ -99,16 +95,19 @@ class TestAppCrawler {
}

@Test
def testImageInStyle (): Unit = inFX {
val view = new View {
def testImageInStyle (): Unit = {
def view = new View {
override def title: String = ""
override def description: String = ""
val content: Node = new Region() {
style = "-fx-background-image: url('/testfiles/test.jpg');"
}
}
val r = AppCrawler.crawlPage(view)
assert(r.pictures.nonEmpty)
val r = AppCrawler.crawlRoute("http://localhost", () =>
Route.empty().and(Route.get("/", r => Response.view(view)))
)
assert(r.pages.length == 1)
assert(r.reports.head.pictures.nonEmpty)
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,53 +11,48 @@ class TestMemoryTester {

@Test
def simpleTest(): Unit = {
def app = new RouteNode(null) {
setRoute(Route.empty()
def route = Route.empty()
.and(Route.get("/", r => Response.view(new Page1)))
.and(Route.get("/page2", r => Response.view(new Page2)))
.and(Route.get("/page4", r => Response.view(new Page2))))
}
val result = AppCrawler.crawlApp("http://localhost", () => app)
MemoryTester.testForLeaks(result, () => app)
.and(Route.get("/page4", r => Response.view(new Page2)))
val result = AppCrawler.crawlRoute("http://localhost", () => route)
MemoryTester.testForLeaks(result, () => route)
}

@Test
def simpleFailingTest(): Unit = {
val page2 = new Page2
def app = new RouteNode(null) {
setRoute(Route.empty()
def route = Route.empty()
.and(Route.get("/", r => Response.view(new Page1)))
.and(Route.get("/page2", r => Response.view(page2)))
.and(Route.get("/page4", r => Response.view(new Page2))))
}
val result = AppCrawler.crawlApp("http://localhost", () => app)
intercept[Throwable](MemoryTester.testForLeaks(result, () => app))
.and(Route.get("/page4", r => Response.view(new Page2)))
val result = AppCrawler.crawlRoute("http://localhost", () => route)
intercept[Throwable](MemoryTester.testForLeaks(result, () => route))
}

@Test
def simpleFailingTest2(): Unit = {
var node2 = new Label()

def app = new RouteNode(null) {
setRoute(Route.empty()
def route = Route.empty()
.and(Route.get("/", r => Response.view(new Page1)))
.and(Route.get("/page2", r => Response.node(node2)))
.and(Route.get("/page4", r => Response.view(new Page2))))
}
.and(Route.get("/page4", r => Response.view(new Page2)))

val result = AppCrawler.crawlApp("http://localhost", () => app)
intercept[Throwable](MemoryTester.testForLeaks(result, () => app))
val result = AppCrawler.crawlRoute("http://localhost", () => route)
intercept[Throwable](MemoryTester.testForLeaks(result, () => route))
}

/*
@Test
def simpleFailingTest3(): Unit = {
val app = inFX(new RouteNode(null) {
setRoute(Route.empty()
val route = inFX(Route.empty()
.and(Route.get("/", r => Response.view(new Page1)))
.and(Route.get("/page2", r => Response.view(new Page2)))
.and(Route.get("/page4", r => Response.view(new Page2))))
})
val result = AppCrawler.crawlApp("http://localhost", () => app)
intercept[Throwable](MemoryTester.testForLeaks(result, () => app)) // fails because the webapp is not collectable
.and(Route.get("/page4", r => Response.view(new Page2)))
)
val result = AppCrawler.crawlApp("http://localhost", () => route)
intercept[Throwable](MemoryTester.testForLeaks(result, () => route)) // fails because the webapp is not collectable
}
*/
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,27 @@ import simplefx.experimental._
class TestSitemapGenerator {
@Test
def test(): Unit = {
def app = new RouteNode(null) {
setRoute(Route.empty()
def route = Route.empty()
.and(get("/", r => Response.view(new Page1)))
.and(get("/page2", r => Response.view(new Page2)))
.and(get("/page4", r => Response.view(new Page2)))
.and(r => Response.view(new Page1)))
}
val result = AppCrawler.crawlApp("http://localhost", () => app)
.and(r => Response.view(new Page1))
val result = AppCrawler.crawlRoute("http://localhost", () => route)
val sm = SitemapGenerator.createSitemap("http://localhost", result)
println("Crawl Report: " + result)
println("SiteMap: " + sm)
assert(sm.contains("<loc>http://localhost/page4</loc>"))
assert(!sm.contains("<loc>http://external/link</loc>"))

assert(sm.contains("<loc>http://localhost/page4</loc>"), "sitemap did not contain page4")
assert(!sm.contains("<loc>http://external/link</loc>"), "sitemap contained external link")
assert(!sm.contains("mailto"))
}

@Test
def testMailToRedirect(): Unit = {
def app = new RouteNode(null) {
setRoute(Route.empty()
.and(get("/", r => Response.view(pageWithLink(List("/page2", "/page3", "mailto:something")))))
.and(get("/page2", r => Response.redirect("mailto:something-2"))))
}
val result = AppCrawler.crawlApp("http://localhost", () => app)
def route = Route.empty()
.and(get("/", r => Response.view(pageWithLink(List("/page2", "/page3", "mailto:something")))))
.and(get("/page2", r => Response.redirect("mailto:something-2")))
val result = AppCrawler.crawlRoute("http://localhost", () => route)
println("got result: " + result)
val sm = SitemapGenerator.createSitemap("http://localhost", result)
println("SiteMap2: " + sm)
Expand Down
1 change: 1 addition & 0 deletions jpro-routing/core/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@
exports one.jpro.platform.routing.filter.container;
exports one.jpro.platform.routing.sessionmanager;
exports one.jpro.platform.routing.extensions.linkheader;
exports one.jpro.platform.routing.server;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package one.jpro.platform.routing.server;

/**
* I only exist to allow the creation of a module-info.java file.
*/
public class IgnoreMe {
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ object LinkUtil {
}

def getSessionManager(node: Node): SessionManager = {
SessionManagerContext.getContext(node)
val sm = SessionManagerContext.getContext(node)
assert(sm != null, "SessionManager was null")
sm
}

def setLink(node: Node, url: String): Unit = {
Expand Down Expand Up @@ -86,7 +88,7 @@ object LinkUtil {
def refresh(node: Node): Unit = {
val man = LinkUtil.getSessionManager(node)
assert(man.url != null, "current url was null")
man.gotoURL(man.url, false, true)
man.gotoURL(man.url, false)
}

private object LinkDesktop {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,28 @@ object Request {
private lazy val logger: Logger = LoggerFactory.getLogger(getClass.getName)

private var wref_null = new WeakReference[Node](null)
def fromString(x: String): Request = {
if(!isValidLink(x)) {
logger.warn("Warning - Invalid Link: " + x)

def fromString(s: String, oldView: Node): Request = {
val oldViewW = new WeakReference(oldView)
Request.fromString(s).copy(oldContent = oldViewW, origOldContent = oldViewW)
}
def fromString(s: String): Request = {
try {
if(!isValidLink(s)) {
logger.warn("Warning - Invalid Link: " + s)
}
val uri = new URI(s)
val rawQuery = uri.getRawQuery
val query: Map[String,String] = if(rawQuery == null || rawQuery == "") Map() else rawQuery.split("&").map(x => {
val Array(a,b) = x.split("=")
a -> b
}).toMap
val path = uri.getPath
val res = Request(s, uri.getScheme, uri.getHost, uri.getPort, path,path,"/", query,wref_null,wref_null)
res
} catch {
case e: Exception =>
throw new RuntimeException("Could not parse Request from string: " + s, e)
}
val uri = new URI(x)
val rawQuery = uri.getRawQuery
val query: Map[String,String] = if(rawQuery == null || rawQuery == "") Map() else rawQuery.split("&").map(x => {
val Array(a,b) = x.split("=")
a -> b
}).toMap
val path = uri.getPath
val res = Request(x, uri.getScheme, uri.getHost, uri.getPort, path,path,"/", query,wref_null,wref_null)
res
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,9 @@ class RouteNode(stage: Stage, route: Route) extends StackPane { THIS =>
lazy val webAPI: WebAPI = if(WebAPI.isBrowser) com.jpro.webapi.WebAPI.getWebAPI(stage) else null

var newRoute: Route = route
def getRoute: Route = newRoute
def getRoute(): Route = newRoute
def setRoute(x: Route): Unit = newRoute = x

def route(s: String, oldView: Node) = {
val oldViewW = new WeakReference(oldView)
newRoute(Request.fromString(s).copy(oldContent = oldViewW, origOldContent = oldViewW))
}
def route = {
(s: String) => newRoute(Request.fromString(s))
}


def start(sessionManager: SessionManager) = {
SessionManagerContext.setContext(this, sessionManager)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ abstract class View extends ResponseResult { THIS =>
* @param x path
* @return whether the view handles the url change
*/
def handleURL(x: String): Boolean = false
def handleRequest(x: Request): Boolean = false
def mapContent(f: Node => Node): View = new View {
override def title: String = THIS.title

Expand Down
Loading
Loading