package com.speedment.beacon;

import com.speedment.beacon.resource.Resource;
import com.speedment.beacon.resource.Resources;
import fi.iki.elonen.NanoHTTPD;
import fi.iki.elonen.NanoHTTPD.Response.Status;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import static java.util.stream.Collectors.joining;

/**
 *
 * @author pemi
 */
public class BeaconServer extends NanoHTTPD {

    public BeaconServer() {
        super(8081);
    }

    @Override
    public Response serve(IHTTPSession session) {
        final Map<String, String> files = new HashMap<>();
        final Method method = session.getMethod();
        final String uri = session.getUri();
        final String command = uri.substring(uri.indexOf("/") + 1);

        if (Method.PUT.equals(method) 
        ||  Method.POST.equals(method)) {
            
            try {
                session.parseBody(files);
            } catch (IOException ex) {
                return new Response(Response.Status.INTERNAL_ERROR, MIME_PLAINTEXT,
                        "SERVER INTERNAL ERROR: IOException: " + ex.getMessage()
                );
            } catch (ResponseException ex) {
                return new Response(ex.getStatus(), MIME_PLAINTEXT, ex.getMessage());
            }
        }

        final Map<String, String> params = session.getParms();

        System.out.print(method + " '" + uri + "' "
                + params.entrySet().stream()
                .map(e -> "\"" + e.getKey() + "\" = \"" + e.getValue() + "\"")
                .collect(joining(", ", "(", ")"))
                + " -> "
        );

        final Response resp;
        
        switch (command) {
            case "Cats"     : resp = Helper.success(Resources.CATS_JPG);  break;
            case "Mario"    : resp = Helper.success(Resources.MARIO_PNG); break;
            case "Beacon"   : resp = Helper.success(Resources.ONE_PNG);   break;
            case "one.gif"  : resp = Helper.success(Resources.ONE_GIF);   break;
            default         : resp = Helper.fail();
        }
        
        try {
            System.out.println(resp.getStatus().getDescription() + " (" + resp.getData().available() + " bytes)");
        } catch (IOException ex) {
            System.err.println(ex.getMessage());
        }
        
        return resp;
    }
    
    private static class Helper {
        
        private Helper() {}
        
        public static Response fail() {
            return response(Resources.NOT_FOUND_404, Status.NOT_FOUND);
        }

        public static Response success(Resource resource) {
            return response(resource, Status.OK);
        }

        private static Response response(Resource resource, Status status) {
            final NanoHTTPD.Response response = new NanoHTTPD.Response(
                status, 
                resource.getMimeType().toText(), 
                resource.newInputStream()
            );

            response.addHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
            response.addHeader("Pragma", "no-cache"); // HTTP 1.0.
            response.addHeader("Expires", "0"); // Proxies

            return response;
        }
    }
}
@@ -231,34 +231,8 @@
 on;%0A
-import java.util.HashMap;%0A
 impo
@@ -249,16 +249,16 @@
 il.Map;%0A
+
 import s
@@ -499,67 +499,8 @@
 ) %7B%0A
-        final Map%3CString, String%3E files = new HashMap%3C%3E();%0A
     
@@ -587,24 +587,24 @@
 n.getUri();%0A
+
         fina
@@ -664,541 +664,8 @@
 );%0A%0A
-        if (Method.PUT.equals(method) %0A        %7C%7C  Method.POST.equals(method)) %7B%0A            %0A            try %7B%0A                session.parseBody(files);%0A            %7D catch (IOException ex) %7B%0A                return new Response(Response.Status.INTERNAL_ERROR, MIME_PLAINTEXT,%0A                        %22SERVER INTERNAL ERROR: IOException: %22 + ex.getMessage()%0A                );%0A            %7D catch (ResponseException ex) %7B%0A                return new Response(ex.getStatus(), MIME_PLAINTEXT, ex.getMessage());%0A            %7D%0A        %7D%0A%0A
     

Remove unused param code