andrew_zeon065198

Connecting to a Socket Application

Blog Post created by andrew_zeon065198 Employee on May 15, 2017

In this post I wanted to share the steps I took to connect to a TCP/IP sockets based application from an AtomSphere integration process.

 

 

The Socket Application

During a POC I was asked to connect to an Electronic Locker system. Whenever an employee is onboarded or offboarded a request needs to be sent to this system to activate or deactivate a locker. This system only supports JSON over a socket connection. The authentication sequence for connecting to the socket application was as follows:

 

 

This article will show only the first request/response sequence. Decrypting the authentication string and sending request B will not be covered.

 

The Solution

 

Process

 

In order to get this use case working it required me to use the Data Process Shape and some custom Groovy script since Boomi does not have a socket connector. The following is the process:

 

 

The "Auth Req A JSON" shape has the following JSON:

 


This JSON is sent to the Groovy script outlined below.

 

Groovy Script

 

The "Send Authentication Req A" shape has the following groovy script

 

import java.util.Properties;
import java.io.InputStream;

for( int i = 0; i < dataContext.getDataCount(); i++ ) {
  InputStream is = dataContext.getStream(i);
  Properties props = dataContext.getProperties(i);

  //Convert input stream (i.e. incoming document) to string
  StringBuilder sb = new StringBuilder();
  String content;
                               
  InputStreamReader isr = null;
  BufferedReader ibr = null;
                               
  try {
     isr = new InputStreamReader(is);
     ibr = new BufferedReader(isr);
   
     while ((content = ibr.readLine()) != null) {
        sb.append(content);
     }
  } catch (IOException ioe) {
     System.out.println("IO Exception occurred");
     ioe.printStackTrace();
  } finally {
     isr.close();
      ibr.close();
  }
                               
  String mystring = sb.toString();

  //SOCKET CODE
         
  Socket s = new Socket("localhost", 8237);
         
  int STX = 2;
  int ETX = 3;
         
  String stx = new Character((char)STX).toString();
  String etx = new Character((char)ETX).toString();
         
  String auth_A_str = stx + mystring + etx;
         
  //Write to Socket
  PrintWriter out = new PrintWriter(s.getOutputStream(), true);
  out.println(auth_A_str);
         
  //Read response_char from Socket
  BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
  int response_char;
         
  String auth_response_str = "";
         
  while ((response_char = br.read()) != null) {
     if (response_char == 3) {
          break;
     } else if(response_char == 2) {
          //skip
     } else {
          //println("Gantner response char: " + response_char);
          auth_response_str = auth_response_str + (char)response_char;
     }              
  }

  s.close();
  println "auth_response_str: " + auth_response_str;

  //Put string back into stream for next shape
  is = new ByteArrayInputStream(auth_response_str.getBytes());
  dataContext.storeStream(is, props);
}

 

This particular socket application requires a start-of-text (STX) and end-of-text (ETX) character before and after sending the JSON. The response also comes back starting with a STX and ends with an ETX. Hence, the while loop collects and appends every character that is returned between STX and ETX.

 

The Response

 

The response is a JSON containing the authentication string:

 

 

A JSON Profile can be used to extract the string, decrypt and send Auth Request B.

 

Andrew Zeon is a Senior Solution Consultant at Dell Boomi with over 13 years experience in all things integration. Programming is not his strength but luckily he hasn't had to do too much of it since joining Boomi.

Outcomes