
JAVA
Java Agent Server Socket Call
Paul Kim 04.28.2000
Rating: --- (out of 5)




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!
Code
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("<p>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("<head><title>TITLE</title></head>"); out.println("<FORM action='AgentToModifyDataset' method='POST' NAME='_myform'>"); out.println("<p>Decoded Results:<p>" + 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("<INPUT type='submit' value='Submit action for AgentToModifyDataset'></form>"); } else { out.println("<p>No Records Found<p>"); } 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 = "
<P>Response Size=" + size + "<p>"; 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
 |

|
Rate this Tip
|
To rate tips, you must be a member of SearchDomino.com. Register now
to start rating these tips. Log in if you are already a member.
|


');
// -->
DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.
|
 |
|
|
 |
|
 |