Java Agent Server Socket Call

Have you ever wanted to make a server socket call? Well, wait no longer.
Initially, this agent started out as a rather simpla java agent to test
domino's ability to make server socket calls, however you see that it can be
extended to do very powerful things. Think of it as a stepping stone. For
example, I use this agent in the Domino App I developed to handle CGI requests
to retrieve SQL data through an intermediate server that guards and handles all
requests to retrieve, delete and modify data from an Oracle database. I know it
could be better, but oh well I just want the free t-shirt. It's know yours, as
is. Have fun!
import lotus.domino.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class JavaAgent extends AgentBase {

public void NotesMain() {
try {
Session session = getSession();
AgentContext agentContext = session.getAgentContext();
Document doc = agentContext.getDocumentContext();
PrintWriter out = getAgentOutput();

//Grab or Print CGI Variables
//out.println("REMOTE_USER=" + doc.getItemValue("REMOTE_USER") + "
//out.println("HTTP_USER_AGENT=" + doc.getItemValue("REMOTE_USER") + "
//out.println("SERVER_SOFTWARE=" + doc.getItemValue("SERVER_SOFTWARE") +
//Grab or Print Form Fields
out.println("&LTp>Subject=" + doc.getItemValueString("Subject") + "

// ( Socket Code goes here)
JavaAgent d = new JavaAgent(); = doc.getItemValueString("Server");
d.port = Integer.parseInt(doc.getItemValueString("Port"));
Socket sck = new Socket(, d.port);
OutputStream os = sck.getOutputStream();
InputStream is = sck.getInputStream();
DataInputStream reader = new DataInputStream(is);
DataOutputStream writer = new DataOutputStream(new BufferedOutputStream(os,
BufferedReader input = new BufferedReader(new InputStreamReader( );
int ch;
String cmd = command;
String ds=null;
d.writeCmd(writer, cmd);
ds = d.readResponse(reader);
String dataset = ds;
if ( dataset != null ) {
//Stick in any HTML code you want here for formatting and layout of
your results page
out.println("&LTFORM action='AgentToModifyDataset' method='POST'
out.println("&LTp>Decoded Results:&LTp>" + dataset);

//Decode dataset returned using javax's HTTPUtils
Hashtable ht = HttpUtils.parseQueryString ( dataset );

//Grab value from hashtable key, could be data, errorcodes, etc.
depends on what the server returns to you
String ht_data[] = (String[])ht.get("HT_KEY");

//Tokenize decoded data set so we you can manipulate the data returned
StringTokenizer st = new StringTokenizer(ht_data[0], "&");
int i = 0;
while (st.hasMoreTokens()) {
try {
String value = st.nextToken();
//Trim any whitespace
String value_1 = adid.trim();
} catch(NoSuchElementException e) {
System.out.println("Exception Caught - End of Tokens: " + e);

//You can stick any html code in here, like form input type=checkbox
tags to grab the value you want to submit to another agent to either delete or
modify the data retrieved.
out.println("&LTINPUT type='submit' value='Submit action for
} else {
out.println("&LTp>No Records Found&LTp>");
} catch(Exception e) {
System.out.println("Error: " + e);

private static void writeCmd(DataOutputStream dos, String msg)
throws IOException
int msgSize = msg.length();
byte[] str = msg.getBytes(); // the string in ascii
dos.write(str, 0, str.length);

private static String readResponse(DataInputStream dis)
throws IOException
String dataset = null;
byte[] bytes = new byte[MAX_RESPONSE_SIZE];
int size = dis.readInt(); // read the length of the message
int count = 0;
dataset = "\n&LTP>Response Size=" + size + "&LTp>";
String response = null;

for (int totalRead=0, len=0; totalRead < size; totalRead += len) {
response = new String(bytes, 0, len);
count += 1;
dataset += response;
return dataset;

private static final int MA

Dig Deeper on Java for Lotus Notes Domino

  • Favorite iSeries cheat sheets

    Here you'll find a collection of valuable cheat sheets gathered from across the iSeries/ community. These cheat ...