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.net.*;
import java.io.*;
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();
d.host = doc.getItemValueString("Server");
d.port = Integer.parseInt(doc.getItemValueString("Port"));
Socket sck = new Socket(d.host, d.port);
OutputStream os = sck.getOutputStream();
InputStream is = sck.getInputStream();
DataInputStream reader = new DataInputStream(is);
DataOutputStream writer = new DataOutputStream(new BufferedOutputStream(os,
2048));
BufferedReader input = new BufferedReader(new InputStreamReader(System.in) );
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("&LThead>&LTtitle>TITLE</title></head>");
out.println("&LTFORM action='AgentToModifyDataset' method='POST'
NAME='_myform'>");
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();
out.println(value_1);
} catch(NoSuchElementException e) {
e.printStackTrace();
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
AgentToModifyDataset'></form>");
} else {
out.println("&LTp>No Records Found&LTp>");
}
os.close();
is.close();
} catch(Exception e) {
e.printStackTrace();
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.writeInt(msgSize);
dos.write(str, 0, str.length);
dos.flush();
}

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) {
len = dis.read(bytes, 0, MAX_RESPONSE_SIZE);
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

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.

-ADS BY GOOGLE

SearchWindowsServer

Search400

  • iSeries tutorials

    Search400.com's tutorials provide in-depth information on the iSeries. Our iSeries tutorials address areas you need to know about...

  • V6R1 upgrade planning checklist

    When upgrading to V6R1, make sure your software will be supported, your programs will function and the correct PTFs have been ...

  • Connecting multiple iSeries systems through DDM

    Working with databases over multiple iSeries systems can be simple when remotely connecting logical partitions with distributed ...

SearchDataCenter

SearchContentManagement

Close