Add javadoc before it's too late.
This commit is contained in:
parent
f79582b86e
commit
3b9d6070b6
|
@ -3,6 +3,7 @@ package com.voronind.doublegis.test
|
|||
import model.server.TitleCrawlerServer
|
||||
|
||||
@main def main(): Unit = {
|
||||
// Start the server.
|
||||
val server = new TitleCrawlerServer()
|
||||
server.listen(8000)
|
||||
}
|
||||
|
|
|
@ -1,6 +1,15 @@
|
|||
package com.voronind.doublegis.test
|
||||
package model.crawler
|
||||
|
||||
/**
|
||||
* Web crawler base interface.
|
||||
* Crawler is used to scrape the required data from a specified url.
|
||||
*/
|
||||
trait Crawler {
|
||||
|
||||
/**
|
||||
* Fetch data from url.
|
||||
* @param url Target to extract from.
|
||||
* @return Result data.
|
||||
*/
|
||||
def crawl(url: String): String
|
||||
}
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
package com.voronind.doublegis.test
|
||||
package model.crawler
|
||||
|
||||
/**
|
||||
* A crawler that extracts Http Head Title.
|
||||
*/
|
||||
class TitleCrawler extends Crawler {
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -3,6 +3,17 @@ package model.handler
|
|||
|
||||
import com.sun.net.httpserver.HttpExchange
|
||||
|
||||
/**
|
||||
* Basic trait for a Http handler that handles http connections to a specific context.
|
||||
*/
|
||||
trait Handler {
|
||||
/**
|
||||
* Process incoming connection.
|
||||
* @param exchange Object used to talk with http server.
|
||||
*/
|
||||
def handle(exchange: HttpExchange): Unit
|
||||
}
|
||||
|
||||
trait HandlerCompanion {
|
||||
val CONTEXT: String
|
||||
}
|
|
@ -5,6 +5,9 @@ import model.lib.StreamUtil
|
|||
import com.sun.net.httpserver.{HttpExchange, HttpHandler}
|
||||
import java.io.InputStream
|
||||
|
||||
/**
|
||||
* Handle Http Title crawling.
|
||||
*/
|
||||
class TitleCrawlerHandler extends HttpHandler, Handler {
|
||||
override def handle(exchange: HttpExchange): Unit = {
|
||||
log(exchange.getRequestBody)
|
||||
|
@ -12,7 +15,7 @@ class TitleCrawlerHandler extends HttpHandler, Handler {
|
|||
}
|
||||
|
||||
private def log(body: InputStream): Unit = {
|
||||
println(f"${TitleCrawlerHandler.context}%s : Got payload.")
|
||||
println(f"${TitleCrawlerHandler.CONTEXT}%s : Got payload.")
|
||||
StreamUtil.copyStream(body, System.out)
|
||||
println()
|
||||
}
|
||||
|
@ -26,6 +29,6 @@ class TitleCrawlerHandler extends HttpHandler, Handler {
|
|||
}
|
||||
}
|
||||
|
||||
object TitleCrawlerHandler {
|
||||
val context = "/crawl_title"
|
||||
object TitleCrawlerHandler extends HandlerCompanion {
|
||||
override val CONTEXT = "/crawl_title"
|
||||
}
|
||||
|
|
|
@ -4,7 +4,15 @@ package model.lib
|
|||
import java.io.{InputStream, OutputStream}
|
||||
import scala.language.postfixOps
|
||||
|
||||
/**
|
||||
* Utils for Java IO streams.
|
||||
*/
|
||||
object StreamUtil {
|
||||
/**
|
||||
* Copy one stream into another.
|
||||
* @param in Source stream.
|
||||
* @param out Destination stream.
|
||||
*/
|
||||
def copyStream(in: InputStream, out: OutputStream): Unit = {
|
||||
Iterator
|
||||
.continually(in.read)
|
||||
|
|
|
@ -1,6 +1,13 @@
|
|||
package com.voronind.doublegis.test
|
||||
package model.server
|
||||
|
||||
/**
|
||||
* Http server.
|
||||
*/
|
||||
trait Server {
|
||||
/**
|
||||
* Bind to specified port and listen for inputs.
|
||||
* @param port Port to bind the server to. Ranged 0-65535. Ports bellow 1024 require privileged access.
|
||||
*/
|
||||
def listen(port: Int): Unit
|
||||
}
|
||||
|
|
|
@ -8,7 +8,7 @@ import java.net.InetSocketAddress
|
|||
class TitleCrawlerServer extends Server {
|
||||
override def listen(port: Int): Unit = {
|
||||
val server = HttpServer.create(new InetSocketAddress(port), 0)
|
||||
server.createContext(TitleCrawlerHandler.context, new TitleCrawlerHandler())
|
||||
server.createContext(TitleCrawlerHandler.CONTEXT, new TitleCrawlerHandler())
|
||||
server.setExecutor(null)
|
||||
|
||||
server.start()
|
||||
|
|
Reference in a new issue