package org.voovan.vestful;

import org.voovan.http.server.HttpServer;
import org.voovan.vestful.dto.ClassElement;
import org.voovan.vestful.dto.MethodElement;
import org.voovan.vestful.dto.ParamElement;
import org.voovan.vestful.handler.ClassDescRouter;
import org.voovan.vestful.handler.RestfulRouter;
import org.voovan.vestful.handler.MethodDescRouter;
import org.voovan.tools.TFile;
import org.voovan.tools.TObject;
import org.voovan.tools.TReflect;
import org.voovan.tools.json.JSON;
import org.voovan.tools.log.Logger;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *  Restful 上下文类
 *
 * @author helyho
 * <p>
 * Vestful Framework.
 * WebSite: https://github.com/helyho/Vestful
 * Licence: Apache v2 License
 */
public class RestfulContext {

    /**
     * 读取 Restful 接口的配置文件
     * @return List<ClassElement> IntefaceConfig对象的列表
     */
    private static List<ClassElement> loadConfig() {

        List<ClassElement> classElements = new ArrayList<ClassElement>();
        try {
            byte[] fileContent = TFile.loadFileFromContextPath("/conf/vestful.json");
            List<Map<String, Object>> classConfigs = TObject.cast(JSON.parse(new String(fileContent, "UTF-8")));
            for (Map<String, Object> classConfig : classConfigs) {
                //通过反射构造ClassElement 元素
                ClassElement classElement = TObject.cast(TReflect.getObjectFromMap(ClassElement.class, classConfig, true));
                //填充方法信息
                classElement.fillMethodInfo();
                //增加类元素到 List
                classElements.add(classElement);
            }
        }catch(UnsupportedEncodingException | ParseException | ReflectiveOperationException e){
            Logger.error("Load /conf/vestful.json file error.",e);
        }
        return classElements;
    }

    /**
     * 初始化 Restful 的接口到 HttpServer 对象
     * @param server HttpServer 对象
     */
    public static void installRestful(HttpServer server) {
        List<ClassElement> classElemenets = loadConfig();
        for (ClassElement classElemenet : classElemenets) {
            String route = classElemenet.getRoute();
            for(MethodElement methodElement : classElemenet.getMethodElements()) {
                //增加路由控制
                String httpRoutePath = route+"/"+methodElement.getName();
                server.otherMethod(methodElement.getHttpMethod(), httpRoutePath, new RestfulRouter(httpRoutePath,methodElement));
                server.otherMethod(methodElement.getHttpMethod(), getParamPath(httpRoutePath,methodElement), new RestfulRouter(httpRoutePath,methodElement));
                server.otherMethod("GET", httpRoutePath+"!", new MethodDescRouter(methodElement));
            }

            server.otherMethod("GET", route+"!", new ClassDescRouter(classElemenet));
        }
    }

    /**
     * 获取参数路径
     * @param routePath 路由路径
     * @return  参数路径
     */
    public static String getParamPath(String routePath,MethodElement methodElement){
        StringBuilder matchRoute = new StringBuilder(routePath);
        for (ParamElement paramElement : methodElement.getParamElements()) {
            matchRoute.append("/:");
            matchRoute.append(paramElement.getName());
        }
        return matchRoute.toString();
    }
}

@@ -22,16 +22,58 @@
 stful;%0A%0A
+import org.voovan.http.server.HttpModule;%0A
 import o
@@ -102,24 +102,24 @@
 HttpServer;%0A
-
 import org.v
@@ -892,16 +892,34 @@
 Context 
+extends HttpModule
 %7B%0A%0A    /
@@ -1215,17 +1215,16 @@
 xtPath(%22
-/
 conf/ves
@@ -1837,18 +1837,20 @@
 ception 
+ 
 e)
+ 
 %7B%0A      
@@ -1872,46 +1872,8 @@
 ror(
-%22Load /conf/vestful.json file error.%22,
 e);%0A
@@ -3326,32 +3326,32 @@
 e());%0A        %7D%0A
-
         return m
@@ -3377,11 +3377,883 @@
 );%0A    %7D
+%0A%0A    @Override%0A    public void install() %7B%0A        List%3CClassElement%3E classElemenets = loadConfig();%0A        for (ClassElement classElemenet : classElemenets) %7B%0A            String route = classElemenet.getRoute();%0A            for(MethodElement methodElement : classElemenet.getMethodElements()) %7B%0A                //%E5%A2%9E%E5%8A%A0%E8%B7%AF%E7%94%B1%E6%8E%A7%E5%88%B6%0A                String httpRoutePath = route+%22/%22+methodElement.getName();%0A                otherMethod(methodElement.getHttpMethod(), httpRoutePath, new RestfulRouter(httpRoutePath,methodElement));%0A                otherMethod(methodElement.getHttpMethod(), getParamPath(httpRoutePath,methodElement), new RestfulRouter(httpRoutePath,methodElement));%0A                otherMethod(%22GET%22, httpRoutePath+%22!%22, new MethodDescRouter(methodElement));%0A            %7D%0A%0A            otherMethod(%22GET%22, route+%22!%22, new ClassDescRouter(classElemenet));%0A        %7D%0A    %7D
 %0A%7D%0A

修改成 HttpModule 的形式