package com.dayou.interceptor;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.*;

/**
 * requtes包装类
 */
public class RequestWrapper extends HttpServletRequestWrapper {

    private String body;


    public RequestWrapper(HttpServletRequest request) throws IOException {
        super(request);
        StringBuilder stringBuilder = new StringBuilder();
        BufferedReader bufferedReader = null;
        try {
            InputStream inputStream = request.getInputStream();
            if (inputStream != null) {
                bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                char[] charBuffer = new char[128];
                int bytesRead = -1;
                while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
                    stringBuilder.append(charBuffer, 0, bytesRead);
                }
            } else {
                stringBuilder.append("");
            }

        } catch (IOException ex) {
            throw ex;
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (Exception ex) {
                    throw ex;
                }
            }
        }

        body = stringBuilder.toString();
//        handleParam();
    }


    /**
     * 删除首尾空格(包括中文空格) 若是空串返回 ""
     *
     * @param input " s asasa "
     * @return "s asasa"
     */
    public static String delSpace(String input) {
        if (StringUtils.isBlank(input)) {
            return input;
        }
        input = input.trim();
        int end = input.length();
        int start = 0;

        byte[] val = input.getBytes();
        while (start < end && (start + 1 < val.length - 1) && ((val[start + 1] == 32) || Character.isSpaceChar((char) val[start + 1]))) {
            start++;
        }
        while (start < end && (end - 2 >= 0) && ((val[end - 2] == 32 || Character.isSpaceChar((char) val[end - 2])))) {
            end--;
        }
        String substring = "";
        if (start + 1 <= input.length() && end - 1 < input.length()) {
            if (start + 1 <= end - 1 && end - 1 > 0) {
                substring = input.substring(start + 1, end - 1);
            }
        }
        substring = substring.trim();
        if (!substring.startsWith("\"")) {
            substring = "\"" + substring;
        }
        if (!substring.endsWith("\"")) {
            substring = substring + "\"";
        }
        if (StringUtils.isBlank(substring) || "\"".equals(substring)) {
            substring = "\"\"";
        }
        return start > 0 || end < input.length() ? substring : input;
    }

    @Override
    public ServletInputStream getInputStream() throws IOException {
        final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(body.getBytes());
        ServletInputStream servletInputStream = new ServletInputStream() {
            @Override
            public boolean isFinished() {
                return false;
            }

            @Override
            public boolean isReady() {
                return false;
            }

            @Override
            public void setReadListener(ReadListener readListener) {
            }

            @Override
            public int read() throws IOException {
                return byteArrayInputStream.read();
            }
        };
        return servletInputStream;

    }

    @Override
    public BufferedReader getReader() throws IOException {
        return new BufferedReader(new InputStreamReader(this.getInputStream()));
    }

    public String getBody() {
        return this.body;
    }


}
