directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nik...@apache.org
Subject svn commit: r393153 - in /directory/trunks/mina/examples/src/main/java/org/apache/mina/examples/proxy: ./ AbstractProxyIoHandler.java ClientToProxyIoHandler.java Main.java ServerToProxyIoHandler.java
Date Tue, 11 Apr 2006 07:36:26 GMT
Author: niklas
Date: Tue Apr 11 00:36:24 2006
New Revision: 393153

URL: http://svn.apache.org/viewcvs?rev=393153&view=rev
Log:
Added example of a simple tunneling proxy.

Added:
    directory/trunks/mina/examples/src/main/java/org/apache/mina/examples/proxy/
    directory/trunks/mina/examples/src/main/java/org/apache/mina/examples/proxy/AbstractProxyIoHandler.java
  (with props)
    directory/trunks/mina/examples/src/main/java/org/apache/mina/examples/proxy/ClientToProxyIoHandler.java
  (with props)
    directory/trunks/mina/examples/src/main/java/org/apache/mina/examples/proxy/Main.java
  (with props)
    directory/trunks/mina/examples/src/main/java/org/apache/mina/examples/proxy/ServerToProxyIoHandler.java
  (with props)

Added: directory/trunks/mina/examples/src/main/java/org/apache/mina/examples/proxy/AbstractProxyIoHandler.java
URL: http://svn.apache.org/viewcvs/directory/trunks/mina/examples/src/main/java/org/apache/mina/examples/proxy/AbstractProxyIoHandler.java?rev=393153&view=auto
==============================================================================
--- directory/trunks/mina/examples/src/main/java/org/apache/mina/examples/proxy/AbstractProxyIoHandler.java
(added)
+++ directory/trunks/mina/examples/src/main/java/org/apache/mina/examples/proxy/AbstractProxyIoHandler.java
Tue Apr 11 00:36:24 2006
@@ -0,0 +1,67 @@
+/*
+ *   @(#) $Id$
+ *
+ *   Copyright 2006 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.mina.examples.proxy;
+
+import java.nio.charset.Charset;
+
+import org.apache.mina.common.ByteBuffer;
+import org.apache.mina.common.IoHandlerAdapter;
+import org.apache.mina.common.IoSession;
+import org.apache.mina.common.TrafficMask;
+import org.apache.mina.util.SessionLog;
+
+/**
+ * Base class of {@link org.apache.mina.common.IoHandler} classes which handle
+ * proxied connections.
+ *
+ * @author The Apache Directory Project (mina-dev@directory.apache.org)
+ * @version $Rev$, $Date$
+ *
+ */
+public abstract class AbstractProxyIoHandler extends IoHandlerAdapter
+{
+    private static Charset CHARSET = Charset.forName( "iso8859-1" );
+    
+    public void sessionCreated( IoSession session ) throws Exception
+    {
+        session.setTrafficMask( TrafficMask.NONE );
+    }
+
+    public void sessionClosed( IoSession session ) throws Exception 
+    {
+        if( session.getAttachment() != null )
+        {
+            ( ( IoSession ) session.getAttachment() ).setAttachment( null );
+            ( ( IoSession ) session.getAttachment() ).close();
+            session.setAttachment( null );
+        }
+    }
+    
+    public void messageReceived( IoSession session, Object message ) throws Exception
+    {
+        ByteBuffer rb = ( ByteBuffer ) message;
+        ByteBuffer wb = ByteBuffer.allocate( rb.remaining() );
+        rb.mark();
+        wb.put( rb );
+        wb.flip();
+        ( ( IoSession ) session.getAttachment() ).write( wb );
+        rb.reset();
+        SessionLog.info( session, rb.getString( CHARSET.newDecoder() ) );
+    }
+}

Propchange: directory/trunks/mina/examples/src/main/java/org/apache/mina/examples/proxy/AbstractProxyIoHandler.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: directory/trunks/mina/examples/src/main/java/org/apache/mina/examples/proxy/ClientToProxyIoHandler.java
URL: http://svn.apache.org/viewcvs/directory/trunks/mina/examples/src/main/java/org/apache/mina/examples/proxy/ClientToProxyIoHandler.java?rev=393153&view=auto
==============================================================================
--- directory/trunks/mina/examples/src/main/java/org/apache/mina/examples/proxy/ClientToProxyIoHandler.java
(added)
+++ directory/trunks/mina/examples/src/main/java/org/apache/mina/examples/proxy/ClientToProxyIoHandler.java
Tue Apr 11 00:36:24 2006
@@ -0,0 +1,77 @@
+/*
+ *   @(#) $Id$
+ *
+ *   Copyright 2006 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.mina.examples.proxy;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+
+import org.apache.mina.common.ConnectFuture;
+import org.apache.mina.common.IoConnector;
+import org.apache.mina.common.IoFuture;
+import org.apache.mina.common.IoSession;
+import org.apache.mina.common.TrafficMask;
+
+/**
+ * Handles the client to proxy part of the proxied connection.
+ *
+ * @author The Apache Directory Project (mina-dev@directory.apache.org)
+ * @version $Rev$, $Date$
+ *
+ */
+public class ClientToProxyIoHandler extends AbstractProxyIoHandler
+{
+    private final ServerToProxyIoHandler connectorHandler; 
+    private final IoConnector connector; 
+    private final InetSocketAddress address;
+
+    public ClientToProxyIoHandler( ServerToProxyIoHandler connectorHandler, 
+                        IoConnector connector, InetSocketAddress address )
+    {
+        this.connectorHandler = connectorHandler;
+        this.connector = connector;
+        this.address = address;
+    }
+
+    public void sessionOpened( final IoSession session ) throws Exception 
+    {
+        connector.connect( address, connectorHandler ).setCallback( 
+                new IoFuture.Callback()
+        {
+            public void operationComplete( IoFuture f )
+            {
+                ConnectFuture future = ( ConnectFuture ) f;
+                try
+                {
+                    future.getSession().setAttachment( session );
+                    session.setAttachment( future.getSession() );
+                    future.getSession().setTrafficMask( TrafficMask.ALL );
+                }
+                catch( IOException e )
+                {
+                    // Connect failed
+                    session.close();
+                }
+                finally
+                {
+                    session.setTrafficMask( TrafficMask.ALL );
+                }
+            }
+        } );
+    }
+}

Propchange: directory/trunks/mina/examples/src/main/java/org/apache/mina/examples/proxy/ClientToProxyIoHandler.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: directory/trunks/mina/examples/src/main/java/org/apache/mina/examples/proxy/Main.java
URL: http://svn.apache.org/viewcvs/directory/trunks/mina/examples/src/main/java/org/apache/mina/examples/proxy/Main.java?rev=393153&view=auto
==============================================================================
--- directory/trunks/mina/examples/src/main/java/org/apache/mina/examples/proxy/Main.java
(added)
+++ directory/trunks/mina/examples/src/main/java/org/apache/mina/examples/proxy/Main.java
Tue Apr 11 00:36:24 2006
@@ -0,0 +1,77 @@
+/*
+ *   @(#) $Id$
+ *
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.mina.examples.proxy;
+
+import java.net.InetSocketAddress;
+
+import org.apache.mina.common.IoAcceptor;
+import org.apache.mina.common.IoConnector;
+import org.apache.mina.common.IoConnectorConfig;
+import org.apache.mina.transport.socket.nio.SocketAcceptor;
+import org.apache.mina.transport.socket.nio.SocketAcceptorConfig;
+import org.apache.mina.transport.socket.nio.SocketConnector;
+
+/**
+ * (<b>Entry point</b>) Demonstrates how to write a very simple tunneling proxy

+ * using MINA. The proxy only logs all data passing through it. This is only 
+ * suitable for text based protocols since received data will be converted into 
+ * strings before being logged.
+ * <p>
+ * Start a proxy like this:<br/>
+ * <code>org.apache.mina.examples.proxy.Main 12345 www.google.com 80</code><br/>
+ * and open <a href="http://localhost:12345">http://localhost:12345</a> in a
+ * browser window.
+ * </p>
+ * 
+ * @author The Apache Directory Project (mina-dev@directory.apache.org)
+ * @version $Rev$, $Date$
+ */
+public class Main
+{
+    
+    public static void main( String[] args ) throws Exception
+    {
+        if( args.length != 3 )
+        {
+            System.out.println( Main.class.getName() + " <proxy-port> <server-hostname>
<server-port>" );
+            return;
+        }
+
+        // Create TCP/IP acceptor.
+        IoAcceptor acceptor = new SocketAcceptor();
+        ( ( SocketAcceptorConfig ) acceptor.getDefaultConfig() ).setReuseAddress( true );
+        
+        // Create TCP/IP connector.
+        IoConnector connector = new SocketConnector();
+
+        // Set connect timeout.
+        ( ( IoConnectorConfig ) connector.getDefaultConfig()).setConnectTimeout( 30 );
+        
+        ClientToProxyIoHandler handler = new ClientToProxyIoHandler( 
+                new ServerToProxyIoHandler(), connector,
+                new InetSocketAddress( args[ 1 ],
+                        Integer.parseInt( args[ 2 ] ) ) );
+        
+        // Start proxy.
+        acceptor.bind( new InetSocketAddress( Integer.parseInt( args[ 0 ] ) ), handler );
+
+        System.out.println( "Listening on port " + Integer.parseInt( args[ 0 ] ) );
+    }
+    
+}

Propchange: directory/trunks/mina/examples/src/main/java/org/apache/mina/examples/proxy/Main.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: directory/trunks/mina/examples/src/main/java/org/apache/mina/examples/proxy/ServerToProxyIoHandler.java
URL: http://svn.apache.org/viewcvs/directory/trunks/mina/examples/src/main/java/org/apache/mina/examples/proxy/ServerToProxyIoHandler.java?rev=393153&view=auto
==============================================================================
--- directory/trunks/mina/examples/src/main/java/org/apache/mina/examples/proxy/ServerToProxyIoHandler.java
(added)
+++ directory/trunks/mina/examples/src/main/java/org/apache/mina/examples/proxy/ServerToProxyIoHandler.java
Tue Apr 11 00:36:24 2006
@@ -0,0 +1,30 @@
+/*
+ *   @(#) $Id$
+ *
+ *   Copyright 2006 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.mina.examples.proxy;
+
+/**
+ * Handles the server to proxy part of the proxied connection.
+ *
+ * @author The Apache Directory Project (mina-dev@directory.apache.org)
+ * @version $Rev$, $Date$
+ *
+ */
+public class ServerToProxyIoHandler extends AbstractProxyIoHandler
+{
+}

Propchange: directory/trunks/mina/examples/src/main/java/org/apache/mina/examples/proxy/ServerToProxyIoHandler.java
------------------------------------------------------------------------------
    svn:keywords = Id



Mime
View raw message