Permalink
Switch branches/tags
Nothing to show
Find file Copy path
b3dc5ab Dec 6, 2014
1 contributor

Users who have contributed to this file

58 lines (43 sloc) 1.26 KB
package com.softwaremill.httpserver
import java.io.{InputStream, OutputStream}
import java.net.InetSocketAddress
import com.sun.net.httpserver.{HttpExchange, HttpHandler, HttpServer}
object SimpleHttpServer {
def main(args: Array[String]) {
val server = HttpServer.create(new InetSocketAddress(8000), 0)
server.createContext("/", new RootHandler())
server.setExecutor(null)
server.start()
println("Hit any key to exit...")
System.in.read()
server.stop(0)
}
}
class RootHandler extends HttpHandler {
def handle(t: HttpExchange) {
displayPayload(t.getRequestBody)
sendResponse(t)
}
private def displayPayload(body: InputStream): Unit ={
println()
println("******************** REQUEST START ********************")
println()
copyStream(body, System.out)
println()
println("********************* REQUEST END *********************")
println()
}
private def copyStream(in: InputStream, out: OutputStream) {
Iterator
.continually(in.read)
.takeWhile(-1 !=)
.foreach(out.write)
}
private def sendResponse(t: HttpExchange) {
val response = "Ack!"
t.sendResponseHeaders(200, response.length())
val os = t.getResponseBody
os.write(response.getBytes)
os.close()
}
}