ConfigCenterCtrl.java 4.75 KB
package com.ui.ctrl;

import com.ui.contants.HttpUriContants;
import com.ui.http.HttpRestClient;
import com.ui.model.BaseResponse;
import com.ui.model.req.ZkDetailReq;
import com.ui.model.req.ZkTreeReq;
import org.apache.http.HttpResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;

/**
 * Created by liyl on 2017/12/4.
 */
@Controller
@RequestMapping("configCenter")
public class ConfigCenterCtrl {
    Logger log = LoggerFactory.getLogger(TypeCtrl.class);

    @Autowired
    private HttpRestClient httpClient;

    @RequestMapping("/tozkConfigCenterList")
    public ModelAndView tozkConfigCenter() {
        return new  ModelAndView("zkConfigCenter/zkConfigCenterList");
    }


    @RequestMapping("/zkTreeLeaf")
    @ResponseBody
    public String zkTreeLeaf() {
        String strType = "";

        log.debug("get zkTreeLeaf");

        strType = httpClient.defaultGet(HttpUriContants.TYPE_GET_ZKTREE_LEAF, String.class);

        log.info("zkTreeLeaf: {}", strType);

        return strType;
    }

    @RequestMapping("/getRootConfigCenter")
    @ResponseBody
    public BaseResponse getRootConfigCenter(ZkTreeReq req){
        log.info("getRootConfigCenter with req is {}",req);
        BaseResponse response=httpClient.defaultPost(HttpUriContants.GET_ZK_TREE_CONFIGCENTER, req, BaseResponse.class);
        return response;
    }


    @RequestMapping("/getZkConfigList")
    @ResponseBody
    public BaseResponse getZkConfigList(ZkDetailReq req){
        log.info("getZkConfigList with req is {}",req);
        BaseResponse response=httpClient.defaultPost(HttpUriContants.GET_ZK_LIST, req, BaseResponse.class);
        return response;
    }

    @RequestMapping("/export")
    @ResponseBody
    public void export(ZkDetailReq req, HttpServletResponse response){
        log.info("export with req is {}", req);
        Properties res = httpClient.defaultPost(HttpUriContants.GET_ZK_LIST_EXPORT, req, Properties.class);
        if (null == res) {
            res = new Properties();
        }
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/x-download");
        response.addHeader("Content-Disposition", "attachment;filename=config.properties");

        OutputStream out = null;
        try {
            out = response.getOutputStream();
            res.store(out, "export config info from zk!");
            out.flush();
        } catch (IOException e) {
            log.error("export with req is {}", req, e);
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (Exception e) {

                }
            }
        }
    }

    @RequestMapping("/editDetail")
    @ResponseBody
    public BaseResponse editDetail(ZkDetailReq req){
        log.info("editDetail with req is {}",req);
        BaseResponse response=httpClient.defaultPost(HttpUriContants.EDIT_ZK_CONFIG_DETAIL, req, BaseResponse.class);
        return response;
    }

    @RequestMapping("/addRoot")
    @ResponseBody
    public BaseResponse addRoot(ZkTreeReq req){
        log.info("addRoot with req is {}",req);
        BaseResponse response=httpClient.defaultPost(HttpUriContants.ADD_ZK_ROOT, req, BaseResponse.class);
        return response;
    }

    @RequestMapping("/batchImport")
    @ResponseBody
    public BaseResponse batchImport(ZkTreeReq req, @RequestParam("file") MultipartFile file){
        log.info("batchImport with req is {}", req);

        Properties properties = new Properties();
        try {
            properties.load(file.getInputStream());
            req.setProperties(properties);
        } catch (Exception e) {
            log.error("get zk batchImport file info failed", e);
        }
        BaseResponse response=httpClient.defaultPost(HttpUriContants.BATCHIMPORT_ZK_ROOT_CONFIGCENTER, req, BaseResponse.class);
        return response;
    }

    /**
     * 新增一个配置项到多个环境
     * @param req
     * @return
     */
    @RequestMapping("/addZkCongfigMulti")
    @ResponseBody
    public BaseResponse addZkCongfigMulti(ZkDetailReq req){
        log.info("addZkCongfigMulti with req is {}",req);
        BaseResponse response=httpClient.defaultPost(HttpUriContants.ADD_ZKCONFIG_MULTI, req, BaseResponse.class);
        return response;
    }
}