package net.acomputerdog.picam.web.handler;

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

import java.io.*;
import java.util.stream.Collectors;

public abstract class WebHandler implements HttpHandler {
    @Override
    public void handle(HttpExchange e) throws IOException {
        if (acceptRequest(e)) {
            try {
                String query = e.getRequestURI().getQuery();
                String data = new BufferedReader(new InputStreamReader(e.getRequestBody())).lines().collect(Collectors.joining());
                try {
                    handleExchange(e, query, data);
                } catch (Exception ex) {
                    System.err.println("Exception handling request");
                    ex.printStackTrace();
                    sendResponse("500 Internal Server Error: " + ex.toString(), 500, e);
                }
            } catch (IOException ex) {
                System.err.println("IO error handling request");
                ex.printStackTrace();
                sendResponse("500 Internal I/O Error: " + ex.toString(), 500, e);
            }
        } else {
            e.sendResponseHeaders(405, 0);
        }
        e.close();
    }

    /*
        Override to change type
     */
    public boolean acceptRequest(HttpExchange e) {
        return "GET".equals(e.getRequestMethod()) || "POST".equals(e.getRequestMethod());
    }

    public abstract void handleExchange(HttpExchange e, String getData, String postData) throws Exception;

    protected void sendResponse(String response, int code, HttpExchange e) throws IOException {
        byte[] bytes = response.getBytes();
        e.sendResponseHeaders(code, bytes.length);
        try {
            e.getResponseBody().write(bytes);
        } finally {
            e.getResponseBody().close();
            e.close();
        }
    }

    protected void sendFile(HttpExchange exchange, InputStream in) throws IOException {
        exchange.sendResponseHeaders(200, 0);
        byte[] buff = new byte[128];

        // autocloses out
        try (OutputStream out = exchange.getResponseBody();) {

            while (in.available() > 0) {
                int count = in.read(buff);
                out.write(buff, 0, count);
            }
        } catch (IOException e) {
            System.err.println("IO error sending file: " + e.toString());
        } finally {
            exchange.close();
        }
    }

    protected void redirect(String path, HttpExchange exchange) throws IOException {
        exchange.getResponseHeaders().add("Location", path);
        sendResponse("308 Permanent Redirect", 308, exchange);
    }
}

@@ -239,16 +239,59 @@
 ndler %7B%0A
+    private boolean responseSent = false;%0A%0A
     @Ove
@@ -296,16 +296,16 @@
 verride%0A
-
     publ
@@ -348,32 +348,62 @@
 s IOException %7B%0A
+        responseSent = false;%0A
         if (acce
@@ -847,32 +847,77 @@
 ntStackTrace();%0A
+                    if (!responseSent) %7B%0A    
                 
@@ -981,32 +981,54 @@
 ing(), 500, e);%0A
+                    %7D%0A
                 
@@ -1163,32 +1163,73 @@
 ntStackTrace();%0A
+                if (!responseSent) %7B%0A    
                 
@@ -1286,32 +1286,50 @@
 ing(), 500, e);%0A
+                %7D%0A
             %7D%0A  
@@ -1813,32 +1813,87 @@
 s IOException %7B%0A
+        if (!responseSent) %7B%0A            try %7B%0A        
         byte%5B%5D b
@@ -1920,32 +1920,40 @@
 ytes();%0A        
+    
+    
 e.sendResponseHe
@@ -1979,34 +1979,24 @@
 ength);%0A    
-    try %7B%0A
             
@@ -2029,32 +2029,36 @@
 bytes);%0A        
+ 
+   
 %7D finally %7B%0A    
@@ -2045,32 +2045,73 @@
     %7D finally %7B%0A
+                responseSent = true;%0A    
             e.ge
@@ -2139,32 +2139,36 @@
 ();%0A            
+    
 e.close();%0A     
@@ -2154,32 +2154,143 @@
      e.close();%0A
+            %7D%0A        %7D else %7B%0A            System.err.println(%22Warning: attempted to send multiple resonses%22);%0A
         %7D%0A    %7D%0A

Fix exception while handing exception in handler