Java Common - Library - xml protocol (Connection Manager) Usage

Submitted by code_admin on Fri, 07/20/2018 - 16:05

This page explains how to use the Connection Manager to add xml protocol support to a java app

Main java library usage notes

Using without Notification Acceptor

Create a command processor class

  1. import metcarob.com.common.network.xmlprotocol.XMLMessage;
  2. import metcarob.com.common.network.xmlprotocol.XMLMessageProcessorInterface;
  3.  
  4. public class CommandProcessor implements XMLMessageProcessorInterface {
  5.     private MainApp m_App = null;
  6.    
  7.     public CommandProcessor(MainApp p_App) throws Exception {
  8.         super();
  9.         m_App = p_App;
  10.     }
  11.  
  12.     @Override
  13.     public void processMessage(XMLMessage p_msg) {
  14.         if (!m_App.isRunning()) return; //Don't run any commands if we are quitting
  15.        
  16.         String cmdName = p_msg.getCmdName();
  17.         String cmdGUID = p_msg.getGuid();
  18.         String cmdREQGUID = p_msg.getReqGuid();
  19.        
  20.         try {
  21.  
  22.             if (cmdName==null) throw new Exception("ERROR null comand node value");
  23.             if (cmdName.equals("Quit")) {
  24.                 m_App.cmdQuit();
  25.             } else {
  26.                 p_msg.replyCmdNotRecognised();
  27.             }
  28.         } catch (Exception e) {
  29.             try {
  30.                 e.printStackTrace();
  31.                 p_msg.replyException(e);
  32.             } catch (Exception f) {
  33.                 System.out.println("ERROR - was not able to reply with exceptoin");
  34.                 System.out.println("*****************************");
  35.                 System.out.println(f.toString());
  36.                 f.printStackTrace();
  37.                 System.out.println("*****************************");
  38.             }
  39.         }
  40.     }
  41.    
  42. }

Main App Class

  1.       ConnectionManager m_comms = null;
  2.       int serverListenPort = 3564;
  3.  
  4.       m_comms = new ConnectionManager(m_cmd,serverListenPort,null);
  5.  
  6.       while(true) {
  7.             m_comms.LoopIteration();
  8.       }
  9.  
  10.       m_comms.close();

Using with Notification Acceptor

Example Program to send a single command

  1. import java.net.Socket;
  2. import metcarob.com.common.network.xmlprotocol.Connection;
  3. import metcarob.com.common.network.xmlprotocol.XMLMessage;
  4. import metcarob.com.common.network.xmlprotocol.XMLMessageProcessorInterface;
  5. import metcarob.com.common.network.xmlprotocol.XMLMessageReciever;
  6.  
  7. public class Main {
  8.     private static class Command implements XMLMessageProcessorInterface {
  9.  
  10.         @Override
  11.         public void processMessage(XMLMessage p_msg) throws Exception {
  12.             if (p_msg.isRespCmdNotRecognised()) {
  13.                 System.out.println("Command not recognised");
  14.                 return;
  15.             }
  16.             if (p_msg.isRespOK()) {
  17.                 System.out.println("Resp=OK");
  18.                 return;
  19.             }
  20.             if (p_msg.isRespException()) {
  21.                 System.out.println("Resp=ERROR");
  22.                 System.out.println(p_msg.getXml());
  23.                 return;
  24.             }
  25.             System.out.println("Something wrong with response??");
  26.         }
  27.        
  28.     }
  29.    
  30.     public static void main(String[] args) {
  31.         try {
  32.             System.out.println("Start");
  33.    
  34.             String host = "127.0.0.1";
  35.             int port = 7854;
  36.            
  37.             System.out.println("Connecting " + host + ":" + port);
  38.    
  39.             Command cmd = new Command();
  40.             Socket clientSocket = new Socket(host, port);
  41.             XMLMessageReciever rec = new XMLMessageReciever(cmd);
  42.             Connection conn = new Connection(clientSocket,rec,null);
  43.             conn.start();
  44.  
  45.             //Execute command
  46.             XMLMessage msg = new XMLMessage("Quit","");    
  47.             conn.Transmit(msg);
  48.            
  49.             Thread.sleep(300);
  50.             try {
  51.                 if (conn!=null) {
  52.                     System.out.println("Closing connection");
  53.                     conn.close(true);
  54.                 };
  55.             } catch (Exception e) {
  56.                 e.printStackTrace();
  57.             }
  58.            
  59.            
  60.             System.out.println("End");
  61.         } catch (Exception e) {
  62.             e.printStackTrace();
  63.         }
  64.     }
  65. }

Authorise a connection

Authorisation allows the verification of a client identity to the server. The connection does not necessarily have to be encrypted for this to be secure.

  1. When server creates the ConnectionManager it passes a Map which contains users and public keys
  2. Establish a normal connection
  3. Client Call conn.AuthConnection passing a Username and private key
  4. Internal three Message Authorization handshake (see diagram)
  5. Server Connection sends internal message to message processor to confirm the authorisation (See Code Snipit)

Three Message Handshake

Code to receive internal private message

This must check the message is internal to prevent client spoofing.

  1. String cmdName = p_msg.getCmdName();
  2. String cmdGUID = p_msg.getGuid();
  3. String cmdREQGUID = p_msg.getReqGuid();
  4.  
  5. if (cmdName.equals(Constants.getMsgConnectionAuth()) && p_msg.isSecureInternal()) {
  6.     String usr = p_msg.getParams().getChildNodes().item(0).getTextContent();
  7.     System.out.println("Just authorised " + usr + ":" + p_msg.getSrcConnection().getAuthUser());
  8. }

Encryption

Connections can be encrypted using pre-shared public and private keys. Encryption is set up in each direction independently.
To setup in one direction:

  1. Destination: Must call Connection.setEncryptionIdentity either directly or setup using the Connection Manager
  2. Source: Call Connection.EncryptFor
RJM Article Type
Work Notes