org.gliderwiki.admin.PatchController.java Source code

Java tutorial

Introduction

Here is the source code for org.gliderwiki.admin.PatchController.java

Source

/**
 * @FileName  : PatchController.java
 * @Project   : NightHawk
 * @Date      : 2012. 8. 30. 
 * @?      : @author yion
    
 * @?    :
 * @  : GLiDER Wiki  , ?? ,  ,  (URLConnection), 
 */
package org.gliderwiki.admin;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;

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

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.gliderwiki.framework.entity.service.EntityService;
import org.gliderwiki.framework.util.StringUtil;
import org.gliderwiki.install.InstallPropertyUtil;
import org.gliderwiki.util.PropertyUtil;
import org.gliderwiki.web.domain.WeFunction;
import org.gliderwiki.web.domain.WePatch;
import org.gliderwiki.web.system.SystemConst;
import org.gliderwiki.web.vo.JsonResponse;
import org.gliderwiki.web.vo.PatchInfoVo;
import org.gliderwiki.web.vo.JsonResponse.ResponseStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author yion
 *
 */
@Controller
public class PatchController {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private EntityService entityService;

    @Autowired
    private RestTemplate restTemplate;

    public static final String REST_SERVER_URL = "http://www.gliderwiki.org";
    //public static final String REST_SERVER_URL = "http://localhost";

    /**
     * ????  . ??? ?    . 
     * @param request
     * @param response
     * @param modelAndView
     * @return
     * @throws Throwable
     */
    @RequestMapping(value = "/admin/patch", method = RequestMethod.GET)
    public ModelAndView adminPatch(HttpServletRequest request, HttpServletResponse response,
            ModelAndView modelAndView) throws Throwable {
        logger.debug("### adminLogin ");

        String svcPath = request.getSession().getServletContext()
                .getRealPath(SystemConst.PROPERTY_FULL_PATH + "version");
        boolean isServer = false;

        // ???    
        Properties props = PropertyUtil.getVersionPropertyInfo(svcPath, isServer);
        String clientVersion = props.getProperty("version.info");

        // ??? ?     config.properties ? . 
        String configPath = request.getSession().getServletContext()
                .getRealPath(SystemConst.PROPERTY_FULL_PATH + "config");
        Properties config = PropertyUtil.getPropertyInfo(configPath, SystemConst.CONFIG_NAME);

        String weDomain = config.getProperty("we.domain");
        String weEmail = config.getProperty("we.email");
        String activeKey = config.getProperty("we.active.key");
        String serverDomain = config.getProperty("server.domain");
        logger.debug("#weDomain : " + weDomain);
        logger.debug("#weEmail : " + weEmail);
        logger.debug("#activeKey : " + activeKey);

        PatchInfoVo vo = new PatchInfoVo();
        vo.setWeDomain(weDomain);
        vo.setWeEmail(weEmail);
        vo.setWeActiveKey(activeKey);
        vo.setWeVersionInfo(clientVersion);

        // RestTemplate  GLiDER ?    . 
        HttpEntity<PatchInfoVo> entity = new HttpEntity<PatchInfoVo>(vo);
        String restUrl = REST_SERVER_URL + "/service/patchService";
        ResponseEntity<PatchInfoVo> entityResponse = restTemplate.postForEntity(restUrl, entity, PatchInfoVo.class);

        PatchInfoVo patch = entityResponse.getBody();
        logger.debug("###patch : " + patch.toString());

        // version  1.1.2  ? ?? rest  parameter  ?
        // ? -  replace   
        String version = clientVersion.replaceAll("\\.", "-");

        ResponseEntity<WeFunction[]> weFunctionList = null;
        if (!clientVersion.equals(patch.getWeServerVerionInfo())) {
            String listUrl = REST_SERVER_URL + "/service/patchList/" + version;
            logger.debug("###listUrl : " + listUrl);
            weFunctionList = restTemplate.getForEntity(listUrl, WeFunction[].class);
        }

        WeFunction[] list = null;
        if (weFunctionList != null) {
            list = weFunctionList.getBody();
        }

        modelAndView.addObject("menu", "4");
        modelAndView.addObject("serverVersion", patch.getWeServerVerionInfo());
        modelAndView.addObject("clientVersion", clientVersion);
        modelAndView.addObject("list", list);
        modelAndView.setViewName("admin/extension/patchMgr");
        return modelAndView;
    }

    /**
     *   ? ? ?? . 
     * @param request
     * @param response
     * @return
     * @throws Throwable
     */
    @RequestMapping(value = "/admin/getPatch", method = RequestMethod.POST)
    @ResponseBody
    public JsonResponse getPatch(HttpServletRequest request, HttpServletResponse response) throws Throwable {
        JsonResponse res = new JsonResponse();

        String functionIdx = request.getParameter("idx");
        String callUrl = request.getParameter("url");

        logger.debug("#### functionIdx : " + functionIdx);
        logger.debug("#### callUrl : " + callUrl);

        // Call URL  RestClient   . - ? we_patch  
        WePatch restVo = new WePatch();
        restVo.setWe_function_idx(Integer.parseInt(functionIdx));

        HttpEntity<WePatch> entity = new HttpEntity<WePatch>(restVo);
        String restUrl = REST_SERVER_URL + callUrl;
        ResponseEntity<WePatch> entityResponse = restTemplate.postForEntity(restUrl, entity, WePatch.class);

        //  ?  ? WePatch  . 
        // ? ,  ?,  ,  ??    . 
        WePatch patchVo = entityResponse.getBody();

        String filePath = patchVo.getWe_file_path();
        String fileName = patchVo.getWe_file_name();
        String localPath = patchVo.getWe_patch_path();
        String patchType = patchVo.getWe_patch_type(); //  ? ? ? Biz Logic ? . 

        String fullPath = filePath + "/" + fileName;

        //  ?   
        String listUrl = REST_SERVER_URL + fullPath;

        HttpClient httpClient = new DefaultHttpClient();

        //  ?  (jar?  WEB-INF / lib  ?.  ?, jsp? ?  
        ServletContext context = request.getServletContext();
        String destination = context.getRealPath(localPath) + "/" + fileName;

        logger.debug("##destination : " + destination);

        logger.debug("##listUrl : " + listUrl);

        // GliderWiki ?  ?  HttpClient   Stream ?. 
        HttpGet httpGet = new HttpGet(listUrl);

        HttpResponse httpResponse = httpClient.execute(httpGet);

        // httpEntity  Spring ??   full import . 
        org.apache.http.HttpEntity httpEntity = httpResponse.getEntity();

        // ? ? ?. 
        if (httpEntity != null) {

            try {
                FileOutputStream fos = new FileOutputStream(destination);
                httpEntity.writeTo(fos);
                fos.close();

                res.setResult(fileName);
                res.setStatus(ResponseStatus.SUCCESS);
            } catch (Exception e) {
                res.setResult(fileName);
                res.setStatus(ResponseStatus.FAIL);
                e.printStackTrace();
            }
        }
        logger.debug("#### res : " + res.toString());
        return res;
    }

    /**
     *  ?   ? ? . 
     * @param request
     * @param response
     * @return
     * @throws Throwable
     */
    @RequestMapping(value = "/admin/getExtention", method = RequestMethod.POST)
    @ResponseBody
    public JsonResponse getExtention(HttpServletRequest request, HttpServletResponse response) throws Throwable {
        JsonResponse res = new JsonResponse();

        String functionIdx = request.getParameter("idx");
        String callUrl = request.getParameter("url");

        logger.debug("#### functionIdx : " + functionIdx);
        logger.debug("#### callUrl : " + callUrl);

        /*
        // Call URL  RestClient   . - ? we_patch  
        WePatch restVo = new WePatch();
        restVo.setWe_function_idx(Integer.parseInt(functionIdx));
            
        HttpEntity<WePatch> entity = new HttpEntity<WePatch>(restVo);
        String restUrl = REST_SERVER_URL + callUrl;
        ResponseEntity<WePatch> entityResponse = restTemplate.postForEntity(restUrl, entity, WePatch.class);
            
        //  ?  ? WePatch  . 
        // ? ,  ?,  ,  ??    . 
        WePatch patchVo = entityResponse.getBody();
            
            
        String filePath = patchVo.getWe_file_path();      //???   ?     
        String fileName = patchVo.getWe_file_name();      //???   ? ? 
        String localPath = patchVo.getWe_patch_path();      //  - ??? ?  ? ?? . 
        String patchType = patchVo.getWe_patch_type();      // ? ? ? Biz Logic ? . 
            
            
            
        String fullPath = filePath + "/" + fileName;
            
        //  ?   
        String listUrl = REST_SERVER_URL+fullPath;
            
        HttpClient httpClient = new DefaultHttpClient() ;
            
        //  ?  (jar?  WEB-INF / lib  ?.  ?, jsp? ?  
        ServletContext context = request.getServletContext();
        String destination = context.getRealPath(localPath) +"/" + fileName;
            
        logger.debug("##destination : " + destination);
            
        logger.debug("##listUrl : " + listUrl);
            
        // GliderWiki ?  ?  HttpClient   Stream ?. 
        HttpGet httpGet = new HttpGet(listUrl);
            
        HttpResponse httpResponse = httpClient.execute(httpGet);
            
        // httpEntity  Spring ??   full import . 
        org.apache.http.HttpEntity httpEntity = httpResponse.getEntity();
            
        // ? ? ?. 
        if(httpEntity != null) {
               
           try {
        FileOutputStream fos = new FileOutputStream(destination);
        httpEntity.writeTo(fos);
        fos.close();
            
        res.setResult(fileName);
        res.setStatus(ResponseStatus.SUCCESS);
           } catch (Exception e) {
        res.setResult(fileName);
        res.setStatus(ResponseStatus.FAIL);
        e.printStackTrace();
           }
        }
            
        */
        String outPath = request.getSession().getServletContext().getRealPath("/WEB-INF/views/common/include/");
        String inPath = request.getSession().getServletContext().getRealPath("/resource/data/patch/v102/");

        String fileName = "default_header.jsp";

        File in = new File(inPath, fileName);
        File out = new File(outPath, fileName);

        logger.debug("### in : " + in.toString());
        logger.debug("### out : " + out.toString());

        try {
            FileCopyUtils.copy(in, out);

            res.setResult(fileName);
            res.setStatus(ResponseStatus.SUCCESS);
        } catch (Exception e) {

            res.setResult(fileName);
            res.setStatus(ResponseStatus.FAIL);
        }

        logger.debug("#### res : " + res.toString());
        return res;
    }

    @RequestMapping(value = "/admin/license", method = { RequestMethod.POST, RequestMethod.GET })
    public ModelAndView adminLicense(HttpServletRequest request, HttpServletResponse response,
            ModelAndView modelAndView) throws Throwable {
        String lisence_key = StringUtil.strNull(request.getParameter("lisence_key"));

        String configPath = request.getSession().getServletContext()
                .getRealPath(SystemConst.PROPERTY_FULL_PATH + "config");
        Properties config = PropertyUtil.getPropertyInfo(configPath, SystemConst.CONFIG_NAME);

        String weDomain = config.getProperty("we.domain");
        String weEmail = config.getProperty("we.email");
        String activeKey = lisence_key; //config.getProperty("we.active.key");
        String serverDomain = config.getProperty("server.domain");
        logger.debug("#weDomain : " + weDomain);
        logger.debug("#weEmail : " + weEmail);
        logger.debug("#activeKey : " + activeKey);

        if (lisence_key != null && !"".equals(lisence_key)) {
            InstallPropertyUtil property = new InstallPropertyUtil();

            // config ? ?. 
            property.CreateCionfigProperties(weEmail, lisence_key, weDomain, configPath);

        } else {
            activeKey = config.getProperty("we.active.key");
        }

        logger.debug("### activeKey: " + activeKey);
        modelAndView.addObject("menu", "4");
        modelAndView.addObject("activeKey", activeKey);
        modelAndView.setViewName("admin/extension/licenseMgr");
        return modelAndView;
    }

    @RequestMapping(value = "/admin/extension", method = RequestMethod.GET)
    public ModelAndView adminExtension(HttpServletRequest request, HttpServletResponse response,
            ModelAndView modelAndView) throws Throwable {
        logger.debug("### adminExtension ");

        String svcPath = request.getSession().getServletContext()
                .getRealPath(SystemConst.PROPERTY_FULL_PATH + "version");
        boolean isServer = false;

        // ???    
        Properties props = PropertyUtil.getVersionPropertyInfo(svcPath, isServer);
        String clientVersion = props.getProperty("version.info");

        // ??? ?     config.properties ? . 
        String configPath = request.getSession().getServletContext()
                .getRealPath(SystemConst.PROPERTY_FULL_PATH + "config");
        Properties config = PropertyUtil.getPropertyInfo(configPath, SystemConst.CONFIG_NAME);

        String weDomain = config.getProperty("we.domain");
        String weEmail = config.getProperty("we.email");
        String activeKey = config.getProperty("we.active.key");
        String serverDomain = config.getProperty("server.domain");
        logger.debug("#weDomain : " + weDomain);
        logger.debug("#weEmail : " + weEmail);
        logger.debug("#activeKey : " + activeKey);

        PatchInfoVo vo = new PatchInfoVo();
        vo.setWeDomain(weDomain);
        vo.setWeEmail(weEmail);
        vo.setWeActiveKey(activeKey);
        vo.setWeVersionInfo(clientVersion);

        // RestTemplate  GLiDER ?    . 
        HttpEntity<PatchInfoVo> entity = new HttpEntity<PatchInfoVo>(vo);
        String restUrl = REST_SERVER_URL + "/service/patchService";
        ResponseEntity<PatchInfoVo> entityResponse = restTemplate.postForEntity(restUrl, entity, PatchInfoVo.class);

        PatchInfoVo patch = entityResponse.getBody();
        logger.debug("###patch : " + patch.toString());

        // version  1.1.2  ? ?? rest  parameter  ?
        // ? -  replace   
        String version = clientVersion.replaceAll("\\.", "-");

        ResponseEntity<WeFunction[]> weFunctionList = null;
        if (!clientVersion.equals(patch.getWeServerVerionInfo())) {
            String listUrl = REST_SERVER_URL + "/service/extentionList/" + version;
            logger.debug("###listUrl : " + listUrl);
            weFunctionList = restTemplate.getForEntity(listUrl, WeFunction[].class);
        }

        WeFunction[] list = null;
        if (weFunctionList != null) {
            list = weFunctionList.getBody();
        }

        modelAndView.addObject("menu", "4");
        modelAndView.addObject("serverVersion", patch.getWeServerVerionInfo());
        modelAndView.addObject("clientVersion", clientVersion);
        modelAndView.addObject("list", list);

        modelAndView.addObject("menu", "4");
        modelAndView.setViewName("admin/extension/extensionMgr");
        return modelAndView;
    }

}