/**
* 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.");
    }
  }
}

/**
* 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 java.io.OutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;

import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

import com.altiscale.Util.ServerWithStats;

import org.apache.log4j.Logger;

public class ServerStatus implements Runnable {

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

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

  @Override
  public void run() {
    try {
      InetSocketAddress addr = new InetSocketAddress(port);
      HttpServer httpServer = HttpServer.create(addr, 0);
      httpServer.createContext("/stats", new StatsHandler(serverWithStats));
      httpServer.start();
      LOG.info("Started HttpServer accessible at localhost:" + port + "/stats");
    } catch (IOException e) {
      LOG.error("Could not start HttpServer. " + e.getMessage());
    }
  }

  class StatsHandler implements HttpHandler {
    ServerWithStats serverWithStats;

    public StatsHandler(ServerWithStats server) {
      this.serverWithStats = server;
    }

    public void handle(HttpExchange exchange) throws IOException {
      String requestMethod = exchange.getRequestMethod();
      if (requestMethod.equalsIgnoreCase("GET")) {
        String response = serverWithStats.getServerStatsHtml();
        Headers responseHeaders = exchange.getResponseHeaders();
        responseHeaders.set("Content-Type", "text/html");
        exchange.sendResponseHeaders(200, response.getBytes().length);
        OutputStream responseBody = exchange.getResponseBody();
        Headers requestHeaders = exchange.getRequestHeaders();
        responseBody.write(response.getBytes());
        responseBody.close();
      }
    }
  }
}

Change from http status from socket to httpserver implementation.