package com.eactive.eai.inbound.filter; 

import com.eactive.eai.common.util.CommonLib;
import com.eactive.eai.common.util.Logger;

public class ZipFilter implements AdapterFilter
{ 
	private static Integer HEADER_LENGTH    = 150;
	private static Integer HEADER_LL_LENGTH = 8;
	
    protected String adapterGroupName;
    protected String adapterName;
    
    protected Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);
    

    public void setAdapterInfo(String adapterGroupName,String adapterName)
    {
        this.adapterGroupName = adapterGroupName;
        this.adapterName = adapterName;
    }

	@Override
	public Object requestPerform(Object message) throws AdapterFilterException {
       	return message;
	}

	@Override
	public Object responsePerform(Object message) throws AdapterFilterException {
       	return message;
	}
	protected byte[] comp(byte[] msg){
		byte[] result = null;
		if (msg != null && msg.length > HEADER_LENGTH ){
			byte[] header = splitHeader(msg);
			byte[] data    = splitData(msg);
			byte[] comp = ZIPUtil.compress(data);
			result = concatStdmessage(header,comp);
			return result;
		}
		return msg;		
	}
	protected byte[] decomp(byte[] msg){
		byte[] result = null;
		if (msg != null && msg.length > HEADER_LENGTH ){
			byte[] header = splitHeader(msg);
			byte[] data    = splitData(msg);
			byte[] decomp = ZIPUtil.decompress(data);
			result = concatStdmessage(header,decomp);
			return result;
		}
		return msg;
		
	}
	protected byte[] splitHeader(byte[] message){
		byte[] header = new byte[HEADER_LENGTH];
		System.arraycopy(message, 0, header, 0, HEADER_LENGTH);
		return header;
		
	}
	protected byte[] splitData(byte[] message){
		byte[] data = new byte[message.length-HEADER_LENGTH];
		System.arraycopy(message, HEADER_LENGTH, data, 0, data.length);
		return data;
	}
	protected byte[] concatStdmessage(byte[] header,byte[] data){

		int iLen = header.length+data.length;
		byte[] result = new byte[iLen];
		
		/**
		 * header 와 압축해제한 데이타를 합침
		 */
		System.arraycopy(header, 0, result, 0, HEADER_LENGTH);
		System.arraycopy(data, 0, result, HEADER_LENGTH, data.length);		
		
		
		/**
		 * 길이보정
		 */
		String len = CommonLib.getFormatString(iLen,HEADER_LL_LENGTH);
		System.arraycopy(len.getBytes(), 0, result,0, HEADER_LL_LENGTH);
		return result;
	}
	

//	   public static void main(String args[]) throws Exception {
//	   test();
//}

//	private static void test() throws Exception {
//    	ZipFilter filter = new ZipFilter();
//    	byte[] message = "00001048 000DEVCHAP_0120120509140619000002002SR10                                       EAI               000000                                      000IFWFW555555IFWCD950112TSTSYNC             A2   S Y                                       0000        0000  0000000000000000000000000000000                              KORN000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000                              0000000000000000000000000000000000000000000000000000000000000000000000000000N0000000000000000000000000000000000000000000000000000000000000000000000000000000000000FILLER                                                                                                                                                                                                                                IBS                                     IO00000036IFW                       0000100010@@".getBytes();
//    	byte[] com = (byte[]) filter.responsePerform(message);
//    	System.out.println("compress="+ new String(com));
//    	byte[] decom= (byte[]) filter.requestPerform(com);
//    	System.out.println("decompress="+new String(decom));
//    }
    
} 
