Handler & Crawler : Add traits.

This commit is contained in:
Dmitry Voronin 2024-09-23 15:44:03 +03:00
parent 359d4af5ca
commit 290ae030fa
Signed by: voronind
SSH key fingerprint: SHA256:3kBb4iV2ahufEBNq+vFbUe4QYfHt98DHQjN7QaptY9k
5 changed files with 29 additions and 9 deletions

View file

@ -0,0 +1,6 @@
package com.voronind.doublegis.test
package model.crawler
trait Crawler {
}

View file

@ -0,0 +1,6 @@
package com.voronind.doublegis.test
package model.crawler
class TitleCrawler extends Crawler {
}

View file

@ -0,0 +1,8 @@
package com.voronind.doublegis.test
package model.handler
import com.sun.net.httpserver.HttpExchange
trait Handler {
def handle(exchange: HttpExchange): Unit
}

View file

@ -5,10 +5,10 @@ import model.lib.StreamUtil
import com.sun.net.httpserver.{HttpExchange, HttpHandler} import com.sun.net.httpserver.{HttpExchange, HttpHandler}
import java.io.InputStream import java.io.InputStream
class TitleCrawlerHandler extends HttpHandler { class TitleCrawlerHandler extends HttpHandler, Handler {
def handle(t: HttpExchange): Unit = { override def handle(exchange: HttpExchange): Unit = {
log(t.getRequestBody) log(exchange.getRequestBody)
sendResponse(t) sendResponse(exchange)
} }
private def log(body: InputStream): Unit = { private def log(body: InputStream): Unit = {
@ -17,12 +17,12 @@ class TitleCrawlerHandler extends HttpHandler {
println() println()
} }
private def sendResponse(t: HttpExchange): Unit = { private def sendResponse(exchange: HttpExchange): Unit = {
val response = "Ack!" val response = "Ack!"
t.sendResponseHeaders(200, response.length()) exchange.sendResponseHeaders(200, response.length())
val os = t.getResponseBody val out = exchange.getResponseBody
os.write(response.getBytes) out.write(response.getBytes)
os.close() out.close()
} }
} }