Sample implementations  Code sample – TcpProxy

Chapter 2: Secure TCP Socket Connections

Code sample – JSSL plug-in

This sample demonstrate how to start TCPService, which forwards data from a secure socket to a non-secure socket running in an ODL AIM.

/*
* PluginApplicationSample.java
*
* Created on June 18, 2004, 10:47 AM
*/
package samples.plugin;

import com.sybase.impact.plugin.Application;
import com.sybase.impact.tcp.TcpService;
import com.sybase.impact.tcp.SSLStoreObject;
import com.sybase.impact.tcp.TcpFlowListner;
import com.sybase.impact.tcp.TcpEventListner;

/**
* This class is an example of a Java application that is powered by the e-Biz
* impact runtime engine. Any custom function that is defined public can be
* accessed by the e-Biz Impact bus. This sample demonstrate how to start a
* TcpService which will forward data from a Secured Socket to a nonsecured
* socket running in an ODL aim
*/
public class PluginSslAimSample extends Application implements TcpFlowListner,
     TcpEventListner; 
{

/**
* Private Tcp Service instance
*/
private TcpService m_TCP =null;

/**
* Creates a new instance of <code>PluginApplicationSample.</code>
*/
public PluginSslAimSample() {
     // Create and configure new TCP service
     m_TCP= new TcpService();

     // enable debug
     m_TCP.setDebugEnabled(true);

     // Willing to receive notification of openFlow and closeFlow events
     m_TCP.registerFlowListner(this);

     // Willing to receive notification runtime and debugging events
     m_TCP.registerEventListner(this);
}

/**
* Standard entry points used by Impact bus to initialize the application.
* Implementing this function is optional.
** @return Positive or null for success.
*/
public int initialize(){
     erm("Acquisition Aim Initializing");
     int i=0;
     try{

     // Compose keystore used for secured client
     SSLStoreObject oKeyStore = new SSLStoreObject("keystore.txt");
     oKeyStore.setPassword("password",false);

     // Declare trust for clients authorized to connect
     SSLStoreObject oTrustStore = new SSLStoreObject("truststore.txt");
     oTrustStore.setPassword("trustword",false);
     m_TCP.setServerSecurity(oKeyStore,oTrustStore);
     m_TCP.setServerSecurityEnabled(true);

     // Set the server TCP Port to listen for secured client connections. 
     // Warning, for secured clients, always use setServerInfo()
     // After setServerSecurity() and setServerSecurityEnabled() , otherwise,
     // The server will start listening for standard connections instead.
     // In this sample, the server listens for secured connections on port 10000
     // At this time the server will bound its socket on the specified port.
     m_TCP.setServerInfo(10000, 0);

     // This server will forward data to non secured connections on port 9999
     m_TCP.setTargetSecurityEnabled(false);
     m_TCP.setTargetInfo("localhost",9999);

     // Start the TcpService on non blocking mode to continue execution 
     //of the application
     m_TCP.setServerBlockingEnabled(false);
     m_TCP.startServer();
     i = 1;
}
catch (Exception e){
     i =0;
     erm(e);
  }
     return i;
}

/**
* Standard entry points uses by the bus to de-initialize the application.
* Implementing this function is optional.
*
* @return Positive or null for success.
*/
public void deinitialize(){
     erm("Acquisition Aim is Deinitializing");
     try{ 
        // Stop server, because it is still running on non blocking mode
        // also explicitely indicate to release the socket
        m_TCP.stopServer();
        m_TCP.stopListening();
        m_TCP.unregisterFlowListner(this);
  
        // Release reference to Tcp service instance forces garbage collect
        m_TCP=null;
     }
     catch(Exception e){
        erm(e);
     }
}

/**
* Method inherited from interface TcpFlowListner notifies when 
* flows are open with source and target protocol objects
* @param sFlowName is a unique socket name
* @return True to allow communication to be open, 
* False to prevent the communication from been opened
*/
public boolean openFlow(String sFlowName){
     erm("openFlow:" + sFlowName);
     return true;
}

/**
* Method inherited from interface TcpFlowListner notifies when 
* flows are closed with source and target protocol objects
* @param sFlowName is a unique socket name 
*/
public void closeFlow(String sFlowName){
     erm("closeFlow:" + sFlowName);
}

/**
* Method inherited from interface TcpEventListner notifies 
* of the TcpService's runtime events 
* @param sDescription sDescription Description of the event
* @param sDescription sDescription Severity of the event, 
* ex: INFO, WARNING, ERROR, etc.
*/
public void processEvent(String sDescription, String sSeverity){
     erm(sSeverity + " " + sDescription);
     }
}




Copyright © 2005. Sybase Inc. All rights reserved. Code sample – TcpProxy

View this book as PDF