All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.jiangzeyin.controller.base.AbstractController Maven / Gradle / Ivy

package cn.jiangzeyin.controller.base;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.extra.servlet.ServletUtil;
import cn.hutool.http.HtmlUtil;
import cn.hutool.http.HttpUtil;
import cn.jiangzeyin.common.interceptor.BaseCallbackController;
import cn.jiangzeyin.common.request.XssFilter;
import cn.jiangzeyin.controller.multipart.MultipartFileBuilder;
import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;
import org.springframework.http.HttpHeaders;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * base
 * 公共的获取参数
 *
 * @author jiangzeyin
 * @date 2017/1/12.
 */
public abstract class AbstractController extends BaseCallbackController {

    /**
     * 拦截器注入
     */
    @Override
    public void resetInfo() {

    }

    /**
     * 获取请求的ip 地址
     *
     * @return ip
     */
    protected String getIp() {
        return ServletUtil.getClientIP(getRequest());
    }

    /**
     * 获取指定header
     *
     * @param name name
     * @return value
     */
    protected String getHeader(String name) {
        return getRequest().getHeader(name);
    }

    /**
     * 获取cookie 值
     *
     * @param name name
     * @return value
     */
    protected String getCookieValue(String name) {
        Cookie cookie = ServletUtil.getCookie(getRequest(), name);
        if (cookie == null) {
            return "";
        }
        return cookie.getValue();
    }

    protected String getParameter(String name) {
        return getParameter(name, null);
    }

    protected String[] getParameters(String name) {
        return getRequest().getParameterValues(name);
    }

    /**
     * 获取指定参数名的值
     *
     * @param name 参数名
     * @param def  默认值
     * @return str
     */
    protected String getParameter(String name, String def) {
        String value = getRequest().getParameter(name);
        return value == null ? def : value;
    }

    //----------------------------------------------  xss start

    protected String getXssParameter(String name) {
        return getXssParameter(name, null);
    }

    protected String[] getXssParameters(String name) {
        String[] values = getParameters(name);
        if (values == null) {
            return null;
        }
        if (XssFilter.isXSS()) {
            return values;
        }
        for (int i = 0, len = values.length; i < len; i++) {
            values[i] = XssFilter.xss(values[i]);
        }
        return values;
    }

    /**
     * 获取指定参数名的值,自动xss标签过滤
     *
     * @param name 参数名
     * @param def  默认值
     * @return str
     */
    protected String getXssParameter(String name, String def) {
        String value = getParameter(name);
        if (value == null) {
            return def;
        }
        if (XssFilter.isXSS()) {
            return value;
        }
        return XssFilter.xss(value);
    }

    //----------------------------------------------  xss end

    //----------------------------------------------  unescape start

    protected String getUnescapeParameter(String name) {
        return getUnescapeParameter(name, null);
    }

    protected String[] getUnescapeParameters(String name) {
        String[] values = getParameters(name);
        if (values == null) {
            return null;
        }
        if (XssFilter.isXSS()) {
            for (int i = 0, len = values.length; i < len; i++) {
                values[i] = HtmlUtil.unescape(values[i]);
            }
        }
        return values;
    }

    //----------------------------------------------  unescape end

    /**
     * 获取指定参数名的值,自动还原标签过滤
     *
     * @param name 参数名
     * @param def  默认值
     * @return str
     */
    protected String getUnescapeParameter(String name, String def) {
        String value = getParameter(name);
        if (value == null) {
            return def;
        }
        if (XssFilter.isXSS()) {
            return HtmlUtil.unescape(value);
        }
        return value;
    }

    protected int getParameterInt(String name, int def) {
        return Convert.toInt(getParameter(name), def);
    }

    protected int getParameterInt(String name) {
        return getParameterInt(name, 0);
    }

    protected long getParameterLong(String name, long def) {
        String value = getParameter(name);
        return Convert.toLong(value, def);
    }

    protected long getParameterLong(String name) {
        return getParameterLong(name, 0L);
    }

    /**
     * 获取来源的url 参数
     *
     * @return map
     */
    protected Map getRefererParameter() {
        String referer = getHeader(HttpHeaders.REFERER);
        return HttpUtil.decodeParamMap(referer, CharsetUtil.CHARSET_UTF_8);
    }

    /**
     * 获取表单数据到实体中
     *
     * @param tClass class
     * @param     t
     * @return t
     */
    protected  T getObject(Class tClass) {
        return ServletUtil.toBean(getRequest(), tClass, true);
    }

    /**
     * 获取所有请求头
     *
     * @return map
     */
    protected Map getHeaders() {
        return getHeaderMapValues(getRequest());
    }

    /**
     * 所有参数
     *
     * @return map 值为数组类型
     */
    protected Map getParametersMap() {
        return getRequest().getParameterMap();
    }

    // ----------------文件上传
    /**
     * cache
     */
    private static final ThreadLocal THREAD_LOCAL_MULTIPART_HTTP_SERVLET_REQUEST = new ThreadLocal<>();

    /**
     * 释放资源
     */
    public static void clearResources() {
        THREAD_LOCAL_MULTIPART_HTTP_SERVLET_REQUEST.remove();
    }

    /**
     * 获取文件上传请求对象
     *
     * @return multipart
     */
    protected MultipartHttpServletRequest getMultiRequest() {
        HttpServletRequest request = getRequest();
        if (request instanceof MultipartHttpServletRequest) {
            return (MultipartHttpServletRequest) request;
        }
        if (ServletFileUpload.isMultipartContent(request)) {
            MultipartHttpServletRequest multipartHttpServletRequest = THREAD_LOCAL_MULTIPART_HTTP_SERVLET_REQUEST.get();
            if (multipartHttpServletRequest != null) {
                return multipartHttpServletRequest;
            }
            multipartHttpServletRequest = new StandardMultipartHttpServletRequest(request);
            THREAD_LOCAL_MULTIPART_HTTP_SERVLET_REQUEST.set(multipartHttpServletRequest);
            return multipartHttpServletRequest;
        }
        throw new IllegalArgumentException("not MultipartHttpServletRequest");
    }

    /**
     * 判断是否存在文件
     *
     * @return true 存在文件
     */
    protected boolean hasFile() {
        Map fileMap = getMultiRequest().getFileMap();
        return fileMap != null && fileMap.size() > 0;
    }

    /**
     * 创建多文件上传对象
     *
     * @return MultipartFileBuilder
     */
    protected MultipartFileBuilder createMultipart() {
        return new MultipartFileBuilder(getMultiRequest());
    }
    // ------------------------文件上传结束
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy