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

top.wboost.common.boost.handler.PropertiesSqlBoostHandler Maven / Gradle / Ivy

There is a newer version: 4.0.1
Show newest version
package top.wboost.common.boost.handler;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import top.wboost.common.system.exception.SystemException;
import top.wboost.common.util.Checker;
import top.wboost.common.util.ClassPathDataUtil;
import top.wboost.common.utils.web.utils.HtmlUtil;
import top.wboost.common.utils.web.utils.PropertiesUtil;

//@AutoWebApplicationConfig("propertiesSqlBoostHandler")
public class PropertiesSqlBoostHandler extends AbstractSqlBoostHandler implements InitializingBean {

    private String propertiesName;
    private Map sqlMap = new ConcurrentHashMap();
    private String boostName = "/boost/prosql/**";

    public PropertiesSqlBoostHandler(String propertiesName) {
        super();
        this.propertiesName = propertiesName;
    }

    @Override
    public ModelAndView handleInternal(HttpServletRequest request, HttpServletResponse response) {
        Map paramMap = request.getParameterMap();
        String requestURI = request.getRequestURI();
        String propertiesName = pathMatcher.extractPathWithinPattern(getUrlMapping(), requestURI);
        String sql = sqlMap.get(propertiesName);
        Checker.notNull(sql, propertiesName + "对应的sql");
        Map params = new HashMap<>();
        for (Entry entry : paramMap.entrySet()) {
            params.put(entry.getKey(), entry.getValue()[0]);
        }
        List> result = executeSqlAuto(params, sqlMap.get(propertiesName));
        HtmlUtil.writerJson(response, JSONObject.toJSONString(result));
        return null;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        Properties properties = PropertiesUtil.loadProperties(propertiesName);
        if (properties == null)
            throw new SystemException("cant load properties:" + propertiesName + ", null");
        for (Entry entry : properties.entrySet()) {
            String sql = ClassPathDataUtil.loadData(entry.getValue().toString());
            sqlMap.put(entry.getKey().toString(), sql);
        }
        log.info("load sql properties:{}", sqlMap);
    }

    @Override
    public String getUrlMapping() {
        return this.boostName;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy