package io.teiler.api.endpoint;

import static spark.Spark.before;
import static spark.Spark.exception;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.google.gson.Gson;

import io.teiler.server.util.Error;
import io.teiler.server.util.exceptions.NotAuthorizedException;

/**
 * Controller for global things such as authorisation-checks or exception handling.
 * 
 * @author lroellin
 */
@Component
public class GlobalEndpointController implements EndpointController {

    private static final Logger LOGGER = LoggerFactory.getLogger(GlobalEndpointController.class);

    private Gson gson = new Gson();
    public static final String URL_VERSION = "/v1/";

    @Override
    public void register() {
        before((req, res) -> LOGGER.debug("API call to '" + req.pathInfo() + "'"));
        before((req, res) -> res.type("application/json"));

        exception(NotAuthorizedException.class, (e, request, response) -> {
            response.status(401);
            Error error = new Error(e.getMessage());
            response.body(gson.toJson(error));
        });

        exception(Exception.class, (e, request, response) -> {
            response.status(500);
            Error error = new Error("GENERAL_SERVER_ERROR");
            e.printStackTrace();
            response.body(gson.toJson(error));
        });
    }

}

@@ -24,17 +24,19 @@
 ndpoint;
-%0A
+%0D%0A%0D
 %0Aimport 
@@ -61,16 +61,17 @@
 .before;
+%0D
 %0Aimport 
@@ -99,17 +99,19 @@
 ception;
-%0A
+%0D%0A%0D
 %0Aimport 
@@ -127,16 +127,17 @@
 .Logger;
+%0D
 %0Aimport 
@@ -160,16 +160,17 @@
 Factory;
+%0D
 %0Aimport 
@@ -210,17 +210,19 @@
 mponent;
-%0A
+%0D%0A%0D
 %0Aimport 
@@ -242,17 +242,19 @@
 on.Gson;
-%0A
+%0D%0A%0D
 %0Aimport 
@@ -281,16 +281,17 @@
 l.Error;
+%0D
 %0Aimport 
@@ -350,13 +350,16 @@
 ion;
-%0A
+%0D%0A%0D
 %0A/**
+%0D
 %0A * 
@@ -438,20 +438,22 @@
 andling.
+%0D
 %0A * 
+%0D
 %0A * @aut
@@ -468,12 +468,14 @@
 llin
+%0D
 %0A */
+%0D
 %0A@Co
@@ -481,16 +481,17 @@
 omponent
+%0D
 %0Apublic 
@@ -552,17 +552,19 @@
 roller %7B
-%0A
+%0D%0A%0D
 %0A    pri
@@ -653,17 +653,19 @@
 .class);
-%0A
+%0D%0A%0D
 %0A    pri
@@ -692,16 +692,17 @@
  Gson();
+%0D
 %0A    pub
@@ -746,17 +746,19 @@
  %22/v1/%22;
-%0A
+%0D%0A%0D
 %0A    @Ov
@@ -763,16 +763,17 @@
 Override
+%0D
 %0A    pub
@@ -793,16 +793,17 @@
 ster() %7B
+%0D
 %0A       
@@ -846,16 +846,46 @@
 API call
+ %5B%22 + req.requestMethod() + %22%5D
  to '%22 +
@@ -908,16 +908,17 @@
 + %22'%22));
+%0D
 %0A       
@@ -969,17 +969,19 @@
 json%22));
-%0A
+%0D%0A%0D
 %0A       
@@ -1040,32 +1040,33 @@
 , response) -%3E %7B
+%0D
 %0A            res
@@ -1083,16 +1083,17 @@
 us(401);
+%0D
 %0A       
@@ -1137,16 +1137,17 @@
 sage());
+%0D
 %0A       
@@ -1177,32 +1177,33 @@
 .toJson(error));
+%0D
 %0A        %7D);%0A%0A  
@@ -1198,17 +1198,19 @@
      %7D);
-%0A
+%0D%0A%0D
 %0A       
@@ -1264,16 +1264,17 @@
 se) -%3E %7B
+%0D
 %0A       
@@ -1299,16 +1299,17 @@
 us(500);
+%0D
 %0A       
@@ -1361,16 +1361,17 @@
 ERROR%22);
+%0D
 %0A       
@@ -1395,16 +1395,17 @@
 Trace();
+%0D
 %0A       
@@ -1443,16 +1443,17 @@
 error));
+%0D
 %0A       
@@ -1460,14 +1460,18 @@
  %7D);
+%0D
 %0A    %7D
-%0A
+%0D%0A%0D
 %0A%7D
+%0D
 %0A

add request method to api-call log