01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: *
19: */
20: package org.apache.mina.example.proxy;
21:
22: import java.nio.charset.Charset;
23:
24: import org.apache.mina.common.IoBuffer;
25: import org.apache.mina.common.IoHandlerAdapter;
26: import org.apache.mina.common.IoSession;
27: import org.apache.mina.common.TrafficMask;
28: import org.slf4j.Logger;
29: import org.slf4j.LoggerFactory;
30:
31: /**
32: * Base class of {@link org.apache.mina.common.IoHandler} classes which handle
33: * proxied connections.
34: *
35: * @author The Apache MINA Project (dev@mina.apache.org)
36: * @version $Rev$, $Date$
37: *
38: */
39: public abstract class AbstractProxyIoHandler extends IoHandlerAdapter {
40: private static final Charset CHARSET = Charset.forName("iso8859-1");
41:
42: private final Logger logger = LoggerFactory.getLogger(getClass());
43:
44: @Override
45: public void sessionCreated(IoSession session) throws Exception {
46: session.setTrafficMask(TrafficMask.NONE);
47: }
48:
49: @Override
50: public void sessionClosed(IoSession session) throws Exception {
51: if (session.getAttribute("") != null) {
52: ((IoSession) session.getAttribute("")).setAttribute("",
53: null);
54: ((IoSession) session.getAttribute("")).closeOnFlush();
55: session.setAttribute("", null);
56: }
57: }
58:
59: @Override
60: public void messageReceived(IoSession session, Object message)
61: throws Exception {
62: IoBuffer rb = (IoBuffer) message;
63: IoBuffer wb = IoBuffer.allocate(rb.remaining());
64: rb.mark();
65: wb.put(rb);
66: wb.flip();
67: ((IoSession) session.getAttribute("")).write(wb);
68: rb.reset();
69: logger.info(rb.getString(CHARSET.newDecoder()));
70: }
71: }
|