package com.hibegin.http.server.web;

import com.hibegin.common.util.LoggerUtil;
import com.hibegin.http.server.api.HttpErrorHandle;
import com.hibegin.http.server.api.HttpRequest;
import com.hibegin.http.server.api.HttpResponse;
import com.hibegin.http.server.api.Interceptor;
import com.hibegin.http.server.config.StaticResourceLoader;
import com.hibegin.http.server.util.MimeTypeUtil;

import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

public class MethodInterceptor implements Interceptor {

    private static final Logger LOGGER = LoggerUtil.getLogger(MethodInterceptor.class);

    private void handleByStaticResource(HttpRequest request, HttpResponse response) {
        for (Map.Entry<String, Map.Entry<String, StaticResourceLoader>> entry :
                request.getServerConfig().getStaticResourceMapper().entrySet()) {
            if (request.getUri().startsWith(entry.getKey())) {
                String path = request.getUri().substring(entry.getKey().length());
                if (request.getUri().endsWith("/")) {
                    path += request.getServerConfig().getWelcomeFile();
                }
                InputStream inputStream = entry.getValue().getValue().getInputStream(entry.getValue().getKey() + path);
                if (inputStream != null) {
                    if (path.contains(".")) {
                        response.addHeader("Content-Type",
                                MimeTypeUtil.getMimeStrByExt(path.substring(path.lastIndexOf(".") + 1)));
                    }
                    response.write(inputStream);
                    return;
                }
            }
        }
        HttpErrorHandle errorHandle = request.getServerConfig().getErrorHandle(404);
        if (errorHandle == null) {
            response.renderCode(404);
        } else {
            errorHandle.doHandle(request, response);
        }
    }

    @Override
    public boolean doInterceptor(HttpRequest request, HttpResponse response) throws Exception {
        Router router = request.getRequestConfig().getRouter();
        Method method = router.getMethod(request.getUri());
        if (method == null) {
            handleByStaticResource(request, response);
            return false;
        }
        Controller controller = null;
        Constructor[] constructors = method.getDeclaringClass().getConstructors();
        boolean haveDefaultConstructor = false;
        for (Constructor constructor : constructors) {
            if (constructor.getParameterTypes().length == 2) {
                if (constructor.getParameterTypes()[0].getName().equals(HttpRequest.class.getName()) && constructor.getParameterTypes()[1].getName().equals(HttpResponse.class.getName())) {
                    controller = (Controller) constructor.newInstance(request, response);
                }
            }
            if (constructor.getParameterTypes().length == 0) {
                haveDefaultConstructor = true;
            }
        }
        if (controller == null) {
            if (haveDefaultConstructor) {
                controller = (Controller) method.getDeclaringClass().getDeclaredConstructor().newInstance();
                controller.request = request;
                controller.response = response;
            } else {
                LOGGER.log(Level.WARNING, method.getDeclaringClass().getSimpleName() + " not find default " +
                        "constructor");
                return false;
            }
        }
        method.invoke(controller);
        return true;
    }
}

@@ -381,16 +381,153 @@
 peUtil;%0A
+import com.hibegin.http.server.util.PathUtil;%0A%0Aimport java.io.File;%0Aimport java.io.FileInputStream;%0Aimport java.io.FileNotFoundException;
 %0Aimport 
@@ -936,17 +936,86 @@
 sponse) 
-%7B
+throws FileNotFoundException %7B%0A        InputStream inputStream = null;
 %0A       
@@ -1471,36 +1471,24 @@
             
-InputStream 
 inputStream 
@@ -1634,42 +1634,376 @@
     
-if (path.contains(%22.%22)) %7B%0A        
+break;%0A                %7D%0A            %7D%0A        %7D%0A        if (inputStream == null) %7B%0A            File file = new File(PathUtil.getStaticPath() + %22/%22 + request.getUri());%0A            if (file.exists()) %7B%0A                inputStream = new FileInputStream(file);%0A            %7D%0A        %7D%0A        if (inputStream != null) %7B%0A            if (request.getUri().contains(%22.%22)) %7B%0A
     
@@ -2073,24 +2073,16 @@
         
-        
 MimeType
@@ -2106,27 +2106,51 @@
 Ext(
-path.substring(path
+request.getUri().substring(request.getUri()
 .las
@@ -2183,32 +2183,18 @@
         
-        %7D%0A      
+%7D%0A
         
@@ -2189,34 +2189,32 @@
   %7D%0A            
-  
 response.write(i
@@ -2242,55 +2242,15 @@
     
-        return;%0A                %7D%0A            %7D
+return;
 %0A   

Fix missing read external file