/**
* Copyright Altiscale 2014
* Author: Cosmin Negruseri <cosmin@altiscale.com>
*
* ServerStatus is a Runnable that listens on a port and returns a html page with values
* from getServerStats.
*/

package com.altiscale.Util;

import org.apache.log4j.Logger;

import java.net.Socket;
import java.net.ServerSocket;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Map;

import com.altiscale.Util.ServerWithStats;

// TODO(cosmin): add http interface.

public class ServerStatus implements Runnable {

  // log4j logger.
  private static Logger LOG = Logger.getLogger("TcpProxy");
 
  private ServerWithStats server; 
  private int port;

  public ServerStatus(ServerWithStats server, int port) {
    this.port = port;
    this.server = server;
  }

  @Override
  public void run() {
    try {
      ServerSocket serverSocket = new ServerSocket(port);
      LOG.info("Started StatsServer thread on port " + port + ".");

      // Connection listening loop.
      while (true) {
        Socket clientSocket = serverSocket.accept();

        BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));

        String s;
        while ((s = in.readLine()) != null) {
          if (s.isEmpty()) {
            break;
          }
        }

        out.write(server.getServerStatsHtml());

        out.flush();
        out.close();
        in.close();
        clientSocket.close();
      }  
    } catch (java.io.IOException e) {
      LOG.error("ServerStats thread died.");
    }
  }
}

@@ -231,117 +231,131 @@
 ort 
-org.apache.log4j.Logger;%0A%0Aimport java.net.Socket;%0Aimport java.net.ServerSocket;%0Aimport java.io.BufferedR
+java.io.OutputStream;%0Aimport java.io.IOException;%0Aimport java.net.InetSocketAddress;%0A%0Aimport com.sun.net.httpserver.H
 eader
+s
 ;%0Aim
@@ -363,121 +363,128 @@
 ort 
-java.io.BufferedWriter;%0Aimport java.io.InputStreamReader;%0Aimport java.io.OutputStreamWriter;%0Aimport java.util.Map
+com.sun.net.httpserver.HttpExchange;%0Aimport com.sun.net.httpserver.HttpHandler;%0Aimport com.sun.net.httpserver.HttpServer
 ;%0A%0Ai
@@ -530,44 +530,39 @@
 s;%0A%0A
-// TODO(cosmin): add http interf
+import org.ap
 ac
+h
 e.
+log4j.Logger;
 %0A%0Apu
@@ -721,16 +721,25 @@
 s server
+WithStats
 ; %0A  pri
@@ -849,16 +849,25 @@
 s.server
+WithStats
  = serve
@@ -928,57 +928,220 @@
     
-ServerSocket serverSocket = new ServerSocket(port
+InetSocketAddress addr = new InetSocketAddress(port);%0A      HttpServer httpServer = HttpServer.create(addr, 0);%0A      httpServer.createContext(%22/stats%22, new StatsHandler(serverWithStats));%0A      httpServer.start(
 );%0A 
@@ -1167,35 +1167,43 @@
 ted 
-Stats
+Http
 Server 
-thread on port 
+accessible at localhost:
 %22 + 
@@ -1214,492 +1214,520 @@
  + %22
-.
+/stats
 %22);%0A
-%0A
     
-  // Connection listening loop.%0A      while (true) %7B%0A        Socket clientSocket = serverSocket.accept(
+%7D catch (IOException e) %7B%0A      LOG.error(%22Could not start HttpServer. %22 + e.getMessage()
 );%0A
-%0A
     
-    BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));%0A        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));%0A%0A        String s;%0A        while ((s = in.readLine()) != null) %7B%0A          if (s.isEmpty()) %7B%0A            break;%0A          %7D%0A        %7D%0A%0A        out.write(server
+%7D%0A  %7D%0A%0A  class StatsHandler implements HttpHandler %7B%0A    ServerWithStats serverWithStats;%0A%0A    public StatsHandler(ServerWithStats server) %7B%0A      this.serverWithStats = server;%0A    %7D%0A%0A    public void handle(HttpExchange exchange) throws IOException %7B%0A      String requestMethod = exchange.getRequestMethod();%0A      if (requestMethod.equalsIgnoreCase(%22GET%22)) %7B%0A        String response = serverWithStats
 .get
@@ -1743,19 +1743,17 @@
 tsHtml()
-);%0A
+;
 %0A       
@@ -1757,88 +1757,254 @@
     
-out.flush();%0A        out.close();%0A        in.close();%0A        clientSocket.close
+Headers responseHeaders = exchange.getResponseHeaders();%0A        responseHeaders.set(%22Content-Type%22, %22text/html%22);%0A        exchange.sendResponseHeaders(200, response.getBytes().length);%0A        OutputStream responseBody = exchange.getResponseBody
 ();%0A
@@ -2013,94 +2013,151 @@
     
-%7D
   
-%0A    %7D catch (java.io.IOException e) %7B%0A      LOG.error(%22ServerStats thread died.%22);
+Headers requestHeaders = exchange.getRequestHeaders();%0A        responseBody.write(response.getBytes());%0A        responseBody.close();%0A      %7D
 %0A   

Change from http status from socket to httpserver implementation.