package i5.las2peer.connectors.nodeAdminConnector.handler;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.logging.Level;

import com.sun.net.httpserver.HttpExchange;

import i5.las2peer.connectors.nodeAdminConnector.NodeAdminConnector;
import i5.las2peer.p2p.Node;
import i5.las2peer.tools.SimpleTools;

public class DefaultHandler extends AbstractHandler {

	public DefaultHandler(NodeAdminConnector connector) {
		super(connector);
	}

	@Override
	public void handle(HttpExchange exchange) throws IOException {
		try {
			logger.info("Handler: " + getClass().getCanonicalName() + " Request-Path: "
					+ exchange.getRequestURI().getPath());
			exchange.getResponseHeaders().set("Server-Name", "las2peer " + getClass().getSimpleName());
			final Node node = connector.getNode();
			final Path path = Paths.get(exchange.getRequestURI().getPath());
			if (path.getNameCount() > 0) {
				String path0 = path.getName(0).toString();
				if (path0.equalsIgnoreCase("version")) {
					sendPlainResponse(exchange, getCoreVersion());
				} else if (path0.equalsIgnoreCase("nodeinfo")) {
					sendPlainResponse(exchange, node.getNodeInformation().toString());
				} else if (path0.equalsIgnoreCase("cpuload")) {
					sendPlainResponse(exchange, Integer.toString(getCPULoad(node)));
				} else if (path0.equalsIgnoreCase("netinfo")) {
					sendPlainResponse(exchange, SimpleTools.join(node.getOtherKnownNodes(), "\n"));
				} else {
					sendStringResponse(exchange, HttpURLConnection.HTTP_NOT_FOUND, "text/plain", "404 (Not Found)\n");
				}
			} else {
				// perm redirect to frontend
				sendRedirect(exchange, FrontendHandler.STATUS_PATH, true);
			}
		} catch (Exception e) {
			logger.log(Level.SEVERE, "Unknown connector error", e);
			sendPlainResponse(exchange, e.toString());
		}
	}

}

@@ -68,27 +68,154 @@
 java.io.
-IOException
+FileInputStream;%0Aimport java.io.FileNotFoundException;%0Aimport java.io.IOException;%0Aimport java.io.InputStream;%0Aimport java.io.OutputStream
 ;%0Aimport
@@ -1616,24 +1616,116 @@
 (), %22%5Cn%22));%0A
+%09%09%09%09%7D else if (path0.equalsIgnoreCase(%22favicon.ico%22)) %7B%0A%09%09%09%09%09sendFaviconResponse(exchange);%0A
 %09%09%09%09%7D else %7B
@@ -2087,10 +2087,837 @@
 %09%09%7D%0A%09%7D%0A%0A
+%09private void sendFaviconResponse(HttpExchange exchange) %7B%0A%09%09try %7B%0A%09%09%09byte%5B%5D bytes = null;%0A%09%09%09try %7B%0A%09%09%09%09FileInputStream fis = new FileInputStream(%22etc/favicon.ico%22);%0A%09%09%09%09bytes = SimpleTools.toByteArray(fis);%0A%09%09%09%09fis.close();%0A%09%09%09%7D catch (FileNotFoundException e) %7B%0A%09%09%09%09// use fallback from classpath%0A%09%09%09%09InputStream is = getClass().getResourceAsStream(%22/favicon.ico%22);%0A%09%09%09%09if (is != null) %7B%0A%09%09%09%09%09bytes = SimpleTools.toByteArray(is);%0A%09%09%09%09%09is.close();%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%09OutputStream os = exchange.getResponseBody();%0A%09%09%09if (bytes != null) %7B%0A%09%09%09%09exchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, bytes.length);%0A%09%09%09%09os.write(bytes);%0A%09%09%09%7D else %7B%0A%09%09%09%09exchange.sendResponseHeaders(HttpURLConnection.HTTP_NOT_FOUND, NO_RESPONSE_BODY);%0A%09%09%09%7D%0A%09%09%09os.close();%0A%09%09%7D catch (IOException e) %7B%0A%09%09%09logger.log(Level.WARNING, e.toString());%0A%09%09%7D%0A%09%7D%0A%0A
 %7D%0A

add favicon handling to NodeAdminConnector