package com.eactive.eai.rms.onl.common.adapterstatus;

import java.util.Arrays;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.View;

import com.eactive.eai.rms.common.base.OnlBaseAnnotationController;
import com.eactive.eai.rms.common.interceptor.InterceptorSkipController;
import com.eactive.eai.rms.onl.server.repository.AdaptersStatusRepository;
import com.eactive.eai.rms.onl.vo.AdapterStatusVO;

/**
 * <pre>
 *     1. 기능 : 어뎁터 관리 화면의 컨트롤러
 * </pre>
 *
 * @author : music75
 * @version : $Revision: 1.26.2.5 $
 * @since : rts 1.0
 */
@SuppressWarnings("unchecked")
@Controller
public class AdapterStatusController extends OnlBaseAnnotationController implements InterceptorSkipController{
    
    @Autowired
    @Qualifier("adaptersStatusRepository")
    private AdaptersStatusRepository adapterStatusRepository;	
	


	@RequestMapping(value= "/adapterStatus.do")
	private View selectDetail(HttpServletRequest request, HttpServletResponse response,String status) throws Exception {
		/* 전문
		String adapterNameIn    = null ;  //어댑터명 인바운드    (40)
		String adapterNameOut   = null ;  //어댑터명 아웃바운드  (40)
		String status           = null ;  //상태  (1) (1: 정상,9:오류)
		String result           = null ;  //처리결과구분코드    (2)  (NR : 정상 , ER : 오류)
		 */

		if (status != null) {
			
			byte[] org = status.getBytes();
			String[] data = toArray(status);
    	
	    	boolean isAll=false;
	    	boolean isSend=true;
	    	boolean isReceive=true;
	    	if (data[0].trim().length()>0){
	    		AdapterStatusVO sendVo     = (AdapterStatusVO)adapterStatusRepository.getAdapterStatus(data[0]);
	    		if (sendVo == null){
	    			isSend = false;
	    		}else{
	    			isSend = sendVo.isStatus();
	    		}
	    	}
	    	if (data[0].trim().length()>0){
	    		AdapterStatusVO recvVo     = (AdapterStatusVO)adapterStatusRepository.getAdapterStatus(data[1]);
	    		if(recvVo==null){
	    			isReceive = false;
	    		}else{
	    			isReceive = recvVo.isStatus();
	    		}
	    	}
	    	if (isSend && isReceive){
	    		isAll = true;
	    	}
	    	byte[] result = setResult(org,isAll);
	    	
			ServletOutputStream sos = null;
	    	try {
	    		sos = response.getOutputStream();
	    		sos.write(result);
	    	} catch( Exception e ) {
	    		//취약점소스 수정 2020.08.24
	        	logger.error(" error", e); 
	    		//e.printStackTrace();  
	    	} finally {
	    		if( sos != null ) sos.close();
	    	}
		}
		
    	return null;
	}
	private String[] toArray(String status){
		
		String[] result = new String[4];
		if ( status != null ) {
			ReceiveMessage message = new ReceiveMessage(status.getBytes());
			result[0] = new String(message.getBytes(40));
			result[1] = new String(message.getBytes(40));
			result[2] = new String(message.getBytes(1));
			result[3] = new String(message.getBytes(2));
		}
		return result;
	}
	private byte[] setResult(byte[] org,boolean result){
		if (result){
			System.arraycopy("1".getBytes(), 0, org, 80, 1);
		}else{
			System.arraycopy("9".getBytes(), 0, org, 80, 1);
		}
		return org;
	}
	

	class ReceiveMessage {
		private byte[] message;
		public ReceiveMessage(byte[] message){
			this.message = message;
		}
		public byte[] getBytes(int length){
			byte[] result = new byte[length];
			result = Arrays.copyOfRange(this.message, 0, length);
			this.message = Arrays.copyOfRange(this.message, length, this.message.length );
			return result;
		}
		
	}
 	
//	public static void main(String[] args){  //취약점소스 수정 2020.08.24
//		String data  = "_CDN_IN_NET_AsS                         ";
//		       data += "_CDN_OU_NET_AsC                         ";
//		       data += " ";
//		       data += "  ";
//		
//		AdapterStatusController a = new AdapterStatusController();
//		String[] array = a.toArray(data);
//		for(int i=0;i<array.length;i++){
//			System.out.println("array["+i+"]=["+array[i]+"]");
//		}
//		
//		byte[] result = a.setResult(data.getBytes(),true);
//		
//		array = a.toArray(new String(result));
//		for(int i=0;i<array.length;i++){
//			System.out.println("array["+i+"]=["+array[i]+"]");
//		}
//		
//
//	}
	
	
	
}
