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
|
import model.server.TitleCrawlerServer
|
||||||
|
|
||||||
@main def main(): Unit = {
|
@main def main(): Unit = {
|
||||||
|
// Start the server.
|
||||||
val server = new TitleCrawlerServer()
|
val server = new TitleCrawlerServer()
|
||||||
server.listen(8000)
|
server.listen(8000)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,15 @@
|
||||||
package com.voronind.doublegis.test
|
package com.voronind.doublegis.test
|
||||||
package model.crawler
|
package model.crawler
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Web crawler base interface.
|
||||||
|
* Crawler is used to scrape the required data from a specified url.
|
||||||
|
*/
|
||||||
trait Crawler {
|
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 com.voronind.doublegis.test
|
||||||
package model.crawler
|
package model.crawler
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A crawler that extracts Http Head Title.
|
||||||
|
*/
|
||||||
class TitleCrawler extends Crawler {
|
class TitleCrawler extends Crawler {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,17 @@ package model.handler
|
||||||
|
|
||||||
import com.sun.net.httpserver.HttpExchange
|
import com.sun.net.httpserver.HttpExchange
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Basic trait for a Http handler that handles http connections to a specific context.
|
||||||
|
*/
|
||||||
trait Handler {
|
trait Handler {
|
||||||
|
/**
|
||||||
|
* Process incoming connection.
|
||||||
|
* @param exchange Object used to talk with http server.
|
||||||
|
*/
|
||||||
def handle(exchange: HttpExchange): Unit
|
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 com.sun.net.httpserver.{HttpExchange, HttpHandler}
|
||||||
import java.io.InputStream
|
import java.io.InputStream
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle Http Title crawling.
|
||||||
|
*/
|
||||||
class TitleCrawlerHandler extends HttpHandler, Handler {
|
class TitleCrawlerHandler extends HttpHandler, Handler {
|
||||||
override def handle(exchange: HttpExchange): Unit = {
|
override def handle(exchange: HttpExchange): Unit = {
|
||||||
log(exchange.getRequestBody)
|
log(exchange.getRequestBody)
|
||||||
|
@ -12,7 +15,7 @@ class TitleCrawlerHandler extends HttpHandler, Handler {
|
||||||
}
|
}
|
||||||
|
|
||||||
private def log(body: InputStream): Unit = {
|
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)
|
StreamUtil.copyStream(body, System.out)
|
||||||
println()
|
println()
|
||||||
}
|
}
|
||||||
|
@ -26,6 +29,6 @@ class TitleCrawlerHandler extends HttpHandler, Handler {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
object TitleCrawlerHandler {
|
object TitleCrawlerHandler extends HandlerCompanion {
|
||||||
val context = "/crawl_title"
|
override val CONTEXT = "/crawl_title"
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,15 @@ package model.lib
|
||||||
import java.io.{InputStream, OutputStream}
|
import java.io.{InputStream, OutputStream}
|
||||||
import scala.language.postfixOps
|
import scala.language.postfixOps
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Utils for Java IO streams.
|
||||||
|
*/
|
||||||
object StreamUtil {
|
object StreamUtil {
|
||||||
|
/**
|
||||||
|
* Copy one stream into another.
|
||||||
|
* @param in Source stream.
|
||||||
|
* @param out Destination stream.
|
||||||
|
*/
|
||||||
def copyStream(in: InputStream, out: OutputStream): Unit = {
|
def copyStream(in: InputStream, out: OutputStream): Unit = {
|
||||||
Iterator
|
Iterator
|
||||||
.continually(in.read)
|
.continually(in.read)
|
||||||
|
|
|
@ -1,6 +1,13 @@
|
||||||
package com.voronind.doublegis.test
|
package com.voronind.doublegis.test
|
||||||
package model.server
|
package model.server
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Http server.
|
||||||
|
*/
|
||||||
trait 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
|
def listen(port: Int): Unit
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,7 +8,7 @@ import java.net.InetSocketAddress
|
||||||
class TitleCrawlerServer extends Server {
|
class TitleCrawlerServer extends Server {
|
||||||
override def listen(port: Int): Unit = {
|
override def listen(port: Int): Unit = {
|
||||||
val server = HttpServer.create(new InetSocketAddress(port), 0)
|
val server = HttpServer.create(new InetSocketAddress(port), 0)
|
||||||
server.createContext(TitleCrawlerHandler.context, new TitleCrawlerHandler())
|
server.createContext(TitleCrawlerHandler.CONTEXT, new TitleCrawlerHandler())
|
||||||
server.setExecutor(null)
|
server.setExecutor(null)
|
||||||
|
|
||||||
server.start()
|
server.start()
|
||||||
|
|
Reference in a new issue