Home > Domino Tips > Developer > Java > Java Agent Server Socket Call
Domino Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

JAVA

Java Agent Server Socket Call


Paul Kim
04.28.2000
Rating: --- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


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.




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED CONTENT
Java
Java code inserts data from Notes documents into a SQL table
Java code shortens strings in a SQL table
How to execute a stored procedure in Lotus Notes Domino using Java
How to return an HTML representation of a Lotus Notes rich-text field
Shrink Lotus Notes databases with many attachments
Converting Lotus Notes Domino Web pages to PDF files with a Java agent
A bevy of Notes/Domino development tips
Converting Web pages to images using Java
Creating Microsoft Word documents from Lotus Notes
FAQ: Java for Lotus Notes and Domino

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

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.



Domino & Lotus Notes Security Solutions: Authentication, Antispam, Encryption and Antivirus
HomeTopicsITKnowledge ExchangeTipsAsk the ExpertsMultimediaWhite PapersDomino IT Downloads
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 1999 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts