com.fota.firmMgt.controller.FirmUpdateCTR.java Source code

Java tutorial

Introduction

Here is the source code for com.fota.firmMgt.controller.FirmUpdateCTR.java

Source

/*
 * M2M ServiceFOTA ONM version 1.0
 *
 *  Copyright  2014 kt corp. All rights reserved.
 *
 *  This is a proprietary software of kt corp, and you may not use this file except in
 *  compliance with license agreement with kt corp. Any redistribution or use of this
 *  software, with or without modification shall be strictly prohibited without prior written
 *  approval of kt corp, and the copyright notice above does not evidence any actual or
 *  intended publication of such software.
 */
package com.fota.firmMgt.controller;

import java.io.File;
import java.util.Date;

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

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJacksonJsonView;

import com.fota.comm.util.PropUtil;
import com.fota.comm.util.VoV;
import com.fota.commMgt.vo.UserLoginVO;
import com.fota.firmMgt.service.FirmMgtSVC;
import com.fota.firmMgt.vo.FirmSearchVO;
import com.fota.firmMgt.vo.FirmUpdateVO;
import com.oreilly.servlet.MultipartRequest;

@Controller
@RequestMapping("/commonDevice/firmUpdateMgt")
public class FirmUpdateCTR {

    private static final Logger logger = LoggerFactory.getLogger(FirmMgtCTR.class);

    /**
     * @uml.property  name="mySVC"
     * @uml.associationEnd  readOnly="true"
     */
    @Autowired
    FirmMgtSVC mySVC;
    /**
     * @uml.property  name="ajaxMainView"
     * @uml.associationEnd  readOnly="true"
     */
    @Autowired
    private MappingJacksonJsonView ajaxMainView;

    private static final String DEFAULT_PATH = "/home/jboss/firmware/";
    //   private static final String DEFAULT_PATH = "/firmwareDiff_ServiceFOTA/";

    /**
     *  ? ?  view 
     * @param vo
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/updateView")
    @ResponseBody
    public ModelAndView firmUpdateView(HttpServletRequest request, HttpServletResponse response,
            @ModelAttribute("firmUpdateVO") FirmUpdateVO vo, Model model) throws Exception {
        if (!request.getServletPath().equals("/commonDevice/firmUpdateMgt/updateView")) {
            response.setStatus(403);
            return new ModelAndView("redirect:/lresources/errorPage.jsp");
            //         return null;
        }
        model.addAttribute("data", vo);

        return new ModelAndView("firmMgt/firmUpdateView");
    }

    /**
     *  
     * @param vo
     * @param model
     * @return
     */
    @RequestMapping(value = "/modifyInfo")
    @ResponseBody
    public ModelAndView modifyInfo(@ModelAttribute("firmUpdateVO") FirmUpdateVO vo, Model model) throws Exception {
        // validation check
        vo.setFirmwareVerId(VoV.valid(vo.getFirmwareVerId(), 30, 4));
        //-------------------

        vo = mySVC.searchFirmwareVerInfo(vo);
        model.addAttribute("info", vo);

        return new ModelAndView(ajaxMainView);
    }

    /**
     * ? 
     * @param vo
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/addInfo")
    @ResponseBody
    public ModelAndView addInfo(@ModelAttribute("firmUpdateVO") FirmUpdateVO vo, Model model) throws Exception {
        // validation check
        vo.setFirmwareId(VoV.valid(vo.getFirmwareId(), 30, 4));
        //-------------------

        //firmware 
        vo = mySVC.searchFirmwareInfo(vo);

        //firmwareVer 
        FirmSearchVO fvo = new FirmSearchVO();
        fvo.setDevModelId(vo.getDevModelId());
        fvo = mySVC.searchFirmwareVer(fvo);
        int firmwareVerInt = Integer.parseInt(fvo.getFirmwareVer()) + 1;

        vo.setFirmwareVer(String.valueOf(firmwareVerInt));

        model.addAttribute("info", vo);

        return new ModelAndView(ajaxMainView);
    }

    /**
     *  
     * @param vo
     * @param model
     * @return
     */
    @RequestMapping(value = "/modify")
    @ResponseBody
    public ModelAndView modify(@ModelAttribute("firmUpdateVO") FirmUpdateVO vo, ModelMap model) throws Exception {
        // validation check
        vo.setFirmwareId(VoV.valid(vo.getFirmwareId(), 30, 4));
        vo.setFirmwareMakerVer(VoV.valid(vo.getFirmwareMakerVer(), 100, 8));
        vo.setVerMemo(VoV.valid(vo.getVerMemo(), 1000, 9));
        vo.setFirmwareDesc(VoV.valid(vo.getFirmwareDesc(), 1000, 9));
        //-------------------

        mySVC.modify(vo);

        return new ModelAndView(ajaxMainView, model);
    }

    /**
     *  ?
     * @param vo
     * @param model
     * @return
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public ModelAndView add(HttpServletRequest request, HttpServletResponse response,
            @ModelAttribute("firmUpdateVO") FirmUpdateVO vo, ModelMap model) throws Exception {
        if (PropUtil.getPropValue("onm.mode").equals("P")) {
            Runtime.getRuntime()
                    .exec("sudo mount -t nfs 10.220.250.11:/vol/vol2/c-dms-pd1-w01 /home/jboss/firmware");
            logger.warn("Firmware Modify - File Upload Start... "
                    + "( Mount On : sudo mount -t nfs 10.220.250.11:/vol/vol2/c-dms-pd1-w01 /home/jboss/firmware )");
            // ?? ?  ?  2~3     sleep 3 
            Thread.sleep(3000);
        }

        Date now = new Date();
        logger.info("File update start : " + now.toString());

        //    ? ?
        File temp_folder = new File(DEFAULT_PATH);
        if (!temp_folder.exists() || !temp_folder.isDirectory()) {
            temp_folder.mkdirs();
        }

        // ?? : 500M
        int maxfilesize = 500 * 1024 * 1024;
        MultipartRequest multi = null;
        File upfile = null;

        try {
            //  ?
            multi = new MultipartRequest(request, DEFAULT_PATH, maxfilesize, "utf-8");
            upfile = new File(DEFAULT_PATH + multi.getFilesystemName("file"));

            vo.setFileName(VoV.valid(upfile.getName(), 200, 8));
            vo.setFileSize(Integer.parseInt(String.valueOf(upfile.length())));

            //vo.setBizTypeId(VoV.valid(multi.getParameter("bizTypeId"), 30, 4));
            //vo.setSvcId(VoV.valid(multi.getParameter("svcId"), 30, 4));
            //vo.setMakerId(VoV.valid(multi.getParameter("makerId"), 30, 4));
            vo.setDevModelId(VoV.valid(multi.getParameter("devModelId"), 30, 4));
            vo.setFotaType(VoV.valid(multi.getParameter("fotaType"), 50));
            if (!vo.getFotaType().equals("") && !vo.getFotaType().equals("PUSH")
                    && !vo.getFotaType().equals("POLLING") && !vo.getFotaType().equals("PUSHPOLLING"))
                throw new Exception();

            vo.setFirmwareId(VoV.valid(multi.getParameter("firmwareId"), 30, 4));
            //vo.setFirmwareDesc(VoV.valid(multi.getParameter("firmwareDesc"), 1000, 9));
            vo.setFirmwareVer(VoV.valid(multi.getParameter("firmwareVer"), 100, 4));
            vo.setFirmwareMakerVer(VoV.valid(multi.getParameter("firmwareMakerVer"), 100, 8));
            vo.setVerMemo(VoV.valid(multi.getParameter("verMemo"), 1000, 9));

            //   ? ?
            String path = DEFAULT_PATH + vo.getDevModelId() + "/";
            File pysicalfolder = new File(path);
            if (!pysicalfolder.exists() || !pysicalfolder.isDirectory()) {
                pysicalfolder.mkdirs();
            }
            vo.setFilePath(VoV.valid(path, 200));

            //  ???
            File destFile = new File(path + upfile.getName());
            if (destFile.exists()) {
                destFile.delete();
            }
            FileUtils.moveFile(upfile, destFile);

            //CRC, '0' 
            vo.setCrc("0");
            vo.setCretNm(((UserLoginVO) request.getSession().getAttribute("userInfo")).getUserNm());

        } catch (Exception e) { //  ?? 
            // ??? ? ? 
            if (upfile != null && upfile.exists()) {
                upfile.delete();
            }

            String errorMSG = e.getMessage();
            if (errorMSG.indexOf("exceeds") != 0) {
                model.addAttribute("error", "?? 500MB ?.");
            }

            logger.info("File update error : " + errorMSG);
            response.setStatus(500);
            return new ModelAndView(ajaxMainView, model);
        }

        mySVC.add(vo);

        now = new Date();
        logger.info("File update end : " + now.toString());

        //      Runtime.getRuntime().exec("sudo umount /home/jboss/firmware");
        //      logger.warn("Firmware Modify - File Upload end... "
        //            + "( Mount Off : sudo umount /home/jboss/firmware )");

        return new ModelAndView(ajaxMainView, model);
    }

}