1 20 package org.apache.mina.example.proxy; 21 22 import java.nio.charset.Charset ; 23 24 import org.apache.mina.common.ByteBuffer; 25 import org.apache.mina.common.IoHandlerAdapter; 26 import org.apache.mina.common.IoSession; 27 import org.apache.mina.common.TrafficMask; 28 import org.apache.mina.util.SessionLog; 29 30 38 public abstract class AbstractProxyIoHandler extends IoHandlerAdapter { 39 private static Charset CHARSET = Charset.forName("iso8859-1"); 40 41 public void sessionCreated(IoSession session) throws Exception { 42 session.setTrafficMask(TrafficMask.NONE); 43 } 44 45 public void sessionClosed(IoSession session) throws Exception { 46 if (session.getAttachment() != null) { 47 ((IoSession) session.getAttachment()).setAttachment(null); 48 ((IoSession) session.getAttachment()).close(); 49 session.setAttachment(null); 50 } 51 } 52 53 public void messageReceived(IoSession session, Object message) 54 throws Exception { 55 ByteBuffer rb = (ByteBuffer) message; 56 ByteBuffer wb = ByteBuffer.allocate(rb.remaining()); 57 rb.mark(); 58 wb.put(rb); 59 wb.flip(); 60 ((IoSession) session.getAttachment()).write(wb); 61 rb.reset(); 62 SessionLog.info(session, rb.getString(CHARSET.newDecoder())); 63 } 64 } 65 | Popular Tags |