package cn.cerc.mis.core;

import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;

@Slf4j
public class RequestData {

    // FIXME: 2019/12/7 sid 应该改为 token
    public static final String TOKEN = "sid";
    public static final String WEBCLIENT = "webclient";

    private String token;
    private String param;
    private String serviceCode;

    public RequestData() {
    }

    public RequestData(HttpServletRequest request) {
        this.token = request.getParameter(RequestData.TOKEN);
        this.serviceCode = request.getParameter("class");
        if (this.serviceCode == null) {
            try {
                this.serviceCode = request.getPathInfo().substring(1);
            } catch (Exception e) {
                log.error(e.getMessage(), e);
                throw new RuntimeException("服务名不能为空！");
            }
        }
        BufferedReader reader;
        try {
            reader = request.getReader();
            StringBuffer params = new StringBuffer();
            String line = null;
            while ((line = reader.readLine()) != null) {
                params.append(line);
            }

            this.param = params.toString();
            if (this.param != null && this.param.length() > 1 && this.param.startsWith("[")) {
                if (this.param.endsWith("]\r\n")) {
                    this.param = this.param.substring(1, this.param.length() - 3);
                } else if (this.param.endsWith("]")) {
                    this.param = this.param.substring(1, this.param.length() - 1);
                }
            }

            if (this.param != null && "".equals(this.param)) {
                this.param = null;
            }
        } catch (IOException e) {
            log.error(e.getMessage(), e);
            this.param = null;
        }
    }

    public String getToken() {
        return token;
    }

    public String getParam() {
        return param;
    }

    public void setParam(String param) {
        this.param = param;
    }

    public String getService() {
        return serviceCode;
    }

    public void setService(String service) {
        this.serviceCode = service;
    }

    @Override
    public String toString() {
        Gson gson = new Gson();
        return gson.toJson(this);
    }

    public String getMethod() {
        String[] sl = serviceCode.split("\\.");
        if (sl.length > 1) {
            return sl[1];
        } else {
            return "execute";
        }
    }

    // private static String base64Decode(String param)
    // {
    // if (param == null)
    // {
    // return null;
    // }
    // byte[] bs = null;
    // String result = null;
    // BASE64Decoder decoder = new BASE64Decoder();
    // try
    // {
    // bs = decoder.decodeBuffer(param);
    // result = new String(bs, "UTF-8");
    // }
    // catch (IOException e)
    // {
    // e.printStackTrace();
    // }
    // return result;
    // }

    // public String urlDecode(String text)
    // {
    // if (text == null)
    // {
    // return null;
    // }
    // try
    // {
    // return java.net.URLDecoder.decode(text, "UTF-8");
    // }
    // catch (Exception e)
    // {
    // e.printStackTrace();
    // return text;
    // }
    // }

}

@@ -456,42 +456,8 @@
 e;%0A%0A
-    public RequestData() %7B%0A    %7D%0A%0A
     
@@ -501,24 +501,24 @@
  request) %7B%0A
+
         this
@@ -1023,18 +1023,19 @@
 StringBu
-ff
+ild
 er param
@@ -1050,18 +1050,19 @@
 StringBu
-ff
+ild
 er();%0A  
@@ -1082,23 +1082,16 @@
 ing line
- = null
 ;%0A      
@@ -1267,30 +1267,8 @@
 aram
- != null && this.param
 .len
@@ -1636,30 +1636,8 @@
 if (
-this.param != null && 
 %22%22.e
@@ -2496,771 +2496,13 @@
   %7D%0A
+
     %7D%0A%0A
-    // private static String base64Decode(String param)%0A    // %7B%0A    // if (param == null)%0A    // %7B%0A    // return null;%0A    // %7D%0A    // byte%5B%5D bs = null;%0A    // String result = null;%0A    // BASE64Decoder decoder = new BASE64Decoder();%0A    // try%0A    // %7B%0A    // bs = decoder.decodeBuffer(param);%0A    // result = new String(bs, %22UTF-8%22);%0A    // %7D%0A    // catch (IOException e)%0A    // %7B%0A    // e.printStackTrace();%0A    // %7D%0A    // return result;%0A    // %7D%0A%0A    // public String urlDecode(String text)%0A    // %7B%0A    // if (text == null)%0A    // %7B%0A    // return null;%0A    // %7D%0A    // try%0A    // %7B%0A    // return java.net.URLDecoder.decode(text, %22UTF-8%22);%0A    // %7D%0A    // catch (Exception e)%0A    // %7B%0A    // e.printStackTrace();%0A    // return text;%0A    // %7D%0A    // %7D%0A%0A
 %7D%0A

Update RequestData.java