com.sun.net.httpserver.HttpExchange Scala Examples
The following examples show how to use com.sun.net.httpserver.HttpExchange.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
Example 1
Source File: CalculatorServer.scala From sbt-docker-compose with BSD 3-Clause "New" or "Revised" License | 5 votes |
package example import java.net.InetSocketAddress import com.sun.net.httpserver.{HttpExchange, HttpHandler, HttpServer} object CalculatorServer extends App { def start(port: Int) = { val server = HttpServer.create(new InetSocketAddress(port), 0) server.createContext("/", Handler) server.start() server } object Handler extends HttpHandler { val AddInput = """/add/([-0-9]+)/([-0-9]+)/?""".r val SubtractInput = """/subtract/([-0-9]+)/([-0-9]+)/?""".r override def handle(ex: HttpExchange): Unit = { val path = ex.getRequestURI.toString val resultOpt = path match { case AddInput(a, b) => Option(Calculator.add(a.toInt, b.toInt)) case SubtractInput(a, b) => Option(Calculator.subtract(a.toInt, b.toInt)) case _ => None } val replyString = resultOpt match { case Some(x) => val response = x.toString ex.sendResponseHeaders(200, response.length) response case None => val response = s"Unknown path: $path" ex.sendResponseHeaders(404, response.length) response } ex.getResponseBody.write(replyString.getBytes) } } val port = args.headOption.map(_.toInt).getOrElse(8080) println(s"Starting calculator server on port $port w/ user args ${args.mkString(": [", ",", "]")}") start(port) }
Example 2
Source File: SunServerImpl.scala From c4proto with Apache License 2.0 | 5 votes |
package ee.cone.c4gate import java.lang.Math.toIntExact import java.net.InetSocketAddress import java.util.concurrent.TimeUnit import com.sun.net.httpserver.{HttpExchange, HttpHandler, HttpServer} import ee.cone.c4actor.{Executable, Execution, FinallyClose, Observer, Trace} import ee.cone.c4gate.HttpProtocol.N_Header import okio.ByteString import scala.concurrent.{Await, ExecutionContext, Future} import scala.concurrent.duration.{Duration, SECONDS} import scala.collection.JavaConverters.mapAsScalaMapConverter import scala.collection.JavaConverters.iterableAsScalaIterableConverter class SunReqHandler(handler: FHttpHandler, executionContext: ExecutionContext) extends HttpHandler { def handle(httpExchange: HttpExchange) = Trace{ FinallyClose[HttpExchange,Unit](_.close())(httpExchange) { ex => val method = httpExchange.getRequestMethod val path = httpExchange.getRequestURI.getPath val reqHeaders: List[N_Header] = httpExchange.getRequestHeaders.asScala .flatMap{ case(k,l)=>l.asScala.map(v=>N_Header(k,v)) }.toList val buffer = (new okio.Buffer).readFrom(httpExchange.getRequestBody) val body = buffer.readByteString() val request = FHttpRequest(method, path, reqHeaders, body) val responseF = handler.handle(request)(executionContext) val response = Await.result(responseF,Duration(600,SECONDS)) val headers = httpExchange.getResponseHeaders response.headers.foreach(header=>headers.add(header.key,header.value)) val bytes = response.body.toByteArray httpExchange.sendResponseHeaders(toIntExact(response.status), bytes.length) if(bytes.nonEmpty) httpExchange.getResponseBody.write(bytes) } } } class SunHttpServer(port: Int, handler: FHttpHandler, execution: Execution) extends Executable { def run(): Unit = concurrent.blocking{ val pool = execution.newExecutorService("http-",None) //newWorkStealingPool execution.onShutdown("Pool",()=>{ val tasks = pool.shutdownNow() pool.awaitTermination(Long.MaxValue,TimeUnit.SECONDS) }) val executionContext: ExecutionContext = ExecutionContext.fromExecutor(pool) val server: HttpServer = HttpServer.create(new InetSocketAddress(port),0) execution.onShutdown("HttpServer",()=>server.stop(Int.MaxValue)) server.setExecutor(pool) server.createContext("/", new SunReqHandler(handler,executionContext)) server.start() } } class MutableStatefulReceiver[Message](execution: Execution, inner: List[Observer[Message]]) extends StatefulReceiver[Message] { var state: Future[List[Observer[Message]]] = Future.successful(inner) def send(message: Message): Unit = execution.fatal{ implicit ec => synchronized{ state = state.map(_.flatMap(_.activate(message))) state } } } class MutableStatefulReceiverFactory(execution: Execution) extends StatefulReceiverFactory { def create[Message](inner: List[Observer[Message]])(implicit executionContext: ExecutionContext): Future[StatefulReceiver[Message]] = Future.successful(new MutableStatefulReceiver[Message](execution,inner)) }