package com.eactive.eai.adapter.socket2.protocol.tcseed.handshake;

import com.eactive.eai.adapter.socket2.protocol.ByteMessage;
import com.eactive.eai.adapter.socket2.protocol.ISocketAdapter;
import org.apache.mina.common.IoSession;


public class TcseedClientFilter extends TcseedFilter {
	public TcseedClientFilter(ISocketAdapter adapter) {
		super(adapter);		
	}	
		
	@Override
	public void messageReceived(NextFilter nextFilter, IoSession session, Object message) throws Exception {

		FilterState filterState = (FilterState) session.getAttribute(FILTER_STATE_KEY);
		ByteMessage m = (ByteMessage) message;

		switch (filterState.state) {		

		case STATE_MESSAGE:
			
			byte[] decrypted = ts.Decrypt(m.getBytes());
	        if (decrypted == null || decrypted.length == 0) {
	        	throw new Exception("TcseedClientFilter received message is invalid - check if message is encrypted : " + m.hexDump());
	        }
			m.setBytes(decrypted);
			
			nextFilter.messageReceived(session, m);
			
			break;
			
		default:
			throw new Exception("<RECV> invalid message during handshake .. : " + m);
		}
	}
}
