/***********************************************************************************
 * @@ Program Name : HttpFormContentModify.java Description : 양식 내용 변경, 문서 내용 변경(텍스트 변경) Author : 조용준 Create Date :
 *    2004.12.13 History :
 * @@
 ***********************************************************************************/

package kr.co.kihyun.beans.totsys.doc;

import java.io.IOException;
import java.io.PrintWriter;

import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import kr.co.kihyun.beans.entity.TotDoc;
import kr.co.kihyun.beans.entity.util.*;
import kr.co.kihyun.beans.user.HttpSSOLogin;
import kr.co.kihyun.lang.Encoder;
import kr.co.kihyun.lang.MInteger;
import kr.co.kihyun.moumi.MoumiConfig;
import kr.co.kihyun.text.html.HtmlUtil;
import kr.co.kihyun.text.html.ServletUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@WebServlet("/servlet/kr.co.kihyun.beans.totsys.doc.HttpFormContentModify")
public class HttpFormContentModify extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
        private static final Logger LOG = LoggerFactory.getLogger(HttpDocRegister.class);

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		HttpSession session = request.getSession(true);

		/*********** User HttpLogin check ***********/
		if (!HttpSSOLogin.isLogin(request)) {
			String connURL = "location='/totsys/login/login.jsp';";
			out.println(ServletUtil.getJavaScript(connURL));
			return;
		}

		/******** get Parameter ************/
		String docForm = HtmlUtil.toLowerCase(Encoder.toKR(request.getParameter("docFCM")));
		Long docID = new Long(MInteger.parseInt(request.getParameter("docID")));

		String formConTagetURL = (String) session.getAttribute("formConTagetURL");

		PersistenceManager pm = new MPersistenceManager(PMF.get().getPersistenceManager());
		try {
			TotDoc totDoc = pm.getObjectById(TotDoc.class, docID);
			totDoc.setForm(docForm);
			out.println("<script> alert(\"" + MoumiConfig.getMessageBundle().getString("moumi.message.popup.formDocChangeSuccess") + " \")</script>");
			out.println("<script> location.href = '" + formConTagetURL + "?docID=" + docID + "';</script>");
		} catch (Exception ex) {
			ex.printStackTrace();
			out.println("<script> alert(\"" + MoumiConfig.getMessageBundle().getString("moumi.message.popup.formDocChangeFail") + " \")</script>");
                        //v2. 9.크로스사이트 스크립트 (PrintWrier) : Update by KWON,HAN   
//			out.println("<script> location.href = '" + formConTagetURL + "?docID=" + docID + "';</script>");
                        
                       // 수정 : 외부 입력값 필터링
                        String callbackFunc = "<script> location.href = '" + formConTagetURL + "?docID=" + docID + "';</script>";
                        String filtered_callbackFunc = callbackFunc.replaceAll("<","").replaceAll(">","").replaceAll("&","").replaceAll(",","");
                        LOG.debug("v2 9.크로스사이트 스크립트 (PrintWrier) : HttpFormContentModify.doPost() filtered_callbackFunc={}, Not Test", filtered_callbackFunc);
                        out.println(filtered_callbackFunc);
                        //============================================================ 
                        
		} finally {
			pm.close();
		}
	}
}
