Main : Split handler to separate models.
This commit is contained in:
parent
e74dbb0bec
commit
359d4af5ca
|
@ -1,52 +1,17 @@
|
||||||
package com.voronind.doublegis.test
|
package com.voronind.doublegis.test
|
||||||
|
|
||||||
import com.sun.net.httpserver.{HttpExchange, HttpHandler, HttpServer}
|
import model.handler.TitleCrawlerHandler
|
||||||
|
import com.sun.net.httpserver.HttpServer
|
||||||
import java.io.{InputStream, OutputStream}
|
|
||||||
import java.net.InetSocketAddress
|
import java.net.InetSocketAddress
|
||||||
import scala.language.postfixOps
|
|
||||||
|
|
||||||
@main def main(): Unit = {
|
@main def main(): Unit = {
|
||||||
println("Hello world!")
|
|
||||||
val server = HttpServer.create(new InetSocketAddress(8000), 0)
|
val server = HttpServer.create(new InetSocketAddress(8000), 0)
|
||||||
server.createContext("/", new RootHandler())
|
server.createContext(TitleCrawlerHandler.context, new TitleCrawlerHandler())
|
||||||
server.setExecutor(null)
|
server.setExecutor(null)
|
||||||
|
|
||||||
server.start()
|
server.start()
|
||||||
|
|
||||||
println("Hit any key to exit...")
|
println("Server started! Input any key to exit.")
|
||||||
System.in.read()
|
System.in.read()
|
||||||
server.stop(0)
|
server.stop(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
class RootHandler extends HttpHandler {
|
|
||||||
def handle(t: HttpExchange): Unit = {
|
|
||||||
displayPayload(t.getRequestBody)
|
|
||||||
sendResponse(t)
|
|
||||||
}
|
|
||||||
|
|
||||||
private def displayPayload(body: InputStream): Unit = {
|
|
||||||
println()
|
|
||||||
println("**** Start ****")
|
|
||||||
println()
|
|
||||||
copyStream(body, System.out)
|
|
||||||
println()
|
|
||||||
println("**** End ****")
|
|
||||||
println()
|
|
||||||
}
|
|
||||||
|
|
||||||
private def copyStream(in: InputStream, out: OutputStream): Unit = {
|
|
||||||
Iterator
|
|
||||||
.continually(in.read)
|
|
||||||
.takeWhile(-1 !=)
|
|
||||||
.foreach(out.write)
|
|
||||||
}
|
|
||||||
|
|
||||||
private def sendResponse(t: HttpExchange): Unit = {
|
|
||||||
val response = "Ack!"
|
|
||||||
t.sendResponseHeaders(200, response.length())
|
|
||||||
val os = t.getResponseBody
|
|
||||||
os.write(response.getBytes)
|
|
||||||
os.close()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
31
src/main/scala/model/handler/TitleCrawlerHandler.scala
Normal file
31
src/main/scala/model/handler/TitleCrawlerHandler.scala
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
package com.voronind.doublegis.test
|
||||||
|
package model.handler
|
||||||
|
|
||||||
|
import model.lib.StreamUtil
|
||||||
|
import com.sun.net.httpserver.{HttpExchange, HttpHandler}
|
||||||
|
import java.io.InputStream
|
||||||
|
|
||||||
|
class TitleCrawlerHandler extends HttpHandler {
|
||||||
|
def handle(t: HttpExchange): Unit = {
|
||||||
|
log(t.getRequestBody)
|
||||||
|
sendResponse(t)
|
||||||
|
}
|
||||||
|
|
||||||
|
private def log(body: InputStream): Unit = {
|
||||||
|
println(f"${TitleCrawlerHandler.context}%s : Got payload.")
|
||||||
|
StreamUtil.copyStream(body, System.out)
|
||||||
|
println()
|
||||||
|
}
|
||||||
|
|
||||||
|
private def sendResponse(t: HttpExchange): Unit = {
|
||||||
|
val response = "Ack!"
|
||||||
|
t.sendResponseHeaders(200, response.length())
|
||||||
|
val os = t.getResponseBody
|
||||||
|
os.write(response.getBytes)
|
||||||
|
os.close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
object TitleCrawlerHandler {
|
||||||
|
val context = "/crawl_title"
|
||||||
|
}
|
14
src/main/scala/model/lib/StreamUtil.scala
Normal file
14
src/main/scala/model/lib/StreamUtil.scala
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
package com.voronind.doublegis.test
|
||||||
|
package model.lib
|
||||||
|
|
||||||
|
import java.io.{InputStream, OutputStream}
|
||||||
|
import scala.language.postfixOps
|
||||||
|
|
||||||
|
object StreamUtil {
|
||||||
|
def copyStream(in: InputStream, out: OutputStream): Unit = {
|
||||||
|
Iterator
|
||||||
|
.continually(in.read)
|
||||||
|
.takeWhile(-1 !=)
|
||||||
|
.foreach(out.write)
|
||||||
|
}
|
||||||
|
}
|
Reference in a new issue