[JDEV] Java jabber client.
Leon Kwan
leon at icon.com.hk
Wed Aug 16 23:53:30 CDT 2000
Actually my code is just some modifcation of jtest2 that found in jabberbeans:
package jabber;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.net.*;
import java.io.*;
import org.jabber.jabberbeans.*;
import org.jabber.jabberbeans.Extension.*;
public class JabberApplet extends Applet {
boolean isStandalone = false;
BorderLayout borderLayout2 = new BorderLayout();
Panel lowerpanel = new Panel();
ConnectionBean conn = null;
Label status = new Label(" ");
Panel upperpanel = new Panel();
BorderLayout borderLayout1 = new BorderLayout();
Panel panel1 = new Panel();
BorderLayout borderLayout3 = new BorderLayout();
List list2 = new List();
Label label1 = new Label("Offline");
Panel panel2 = new Panel();
Button logoutbtn = new Button();
Button loginbtn = new Button();
BorderLayout borderLayout4 = new BorderLayout();
Panel panel3 = new Panel();
Label label2 = new Label(" ");
//Get a parameter value
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
}
//Construct the applet
public JabberApplet() {
}
//Initialize the applet
public void init() {
try {
conn = (org.jabber.jabberbeans.ConnectionBean)
java.beans.Beans.instantiate(
this.getClass().getClassLoader(),
"org.jabber.jabberbeans.ConnectionBean");
}
catch (IOException ie)
{
System.err.println("IOException");
}
catch (ClassNotFoundException cne)
{
System.err.println("Class not found");
}
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
lowerpanel.setLayout(borderLayout1);
this.setLayout(borderLayout2);
status.setFont(new java.awt.Font("SansSerif", 1, 12));
status.setForeground(Color.red);
panel1.setLayout(borderLayout3);
logoutbtn.setLabel("Logout");
logoutbtn.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e)
{
logoutbtn_actionPerformed(e);
}
});
loginbtn.setLabel("Login");
loginbtn.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e)
{
loginbtn_actionPerformed(e);
}
});
upperpanel.setLayout(borderLayout4);
label1.setFont(new java.awt.Font("SansSerif", 1, 14));
label1.setForeground(Color.blue);
this.add(lowerpanel, BorderLayout.CENTER);
lowerpanel.add(list2, BorderLayout.CENTER);
this.add(upperpanel, BorderLayout.NORTH);
upperpanel.add(panel2, BorderLayout.CENTER);
panel2.add(loginbtn, null);
panel2.add(logoutbtn, null);
upperpanel.add(panel1, BorderLayout.EAST);
panel1.add(label1, BorderLayout.EAST);
upperpanel.add(panel3, BorderLayout.WEST);
panel3.add(label2, null);
this.add(status, BorderLayout.SOUTH);
}
//Get Applet information
public String getAppletInfo() {
return "Applet Information";
}
//Get parameter info
public String[][] getParameterInfo() {
return null;
}
void loginbtn_actionPerformed(ActionEvent e) {
Object parent = JabberApplet.this.getParent();
LoginDialog d;
d = new LoginDialog((java.awt.Frame)parent);
if (d.getGotoLogin())
{
System.err.println(d.getLoginID());
System.err.println(d.getPassword());
try {
conn.setDefaultProtocolHandler();
conn.addPacketListener(new JTestPacketListener(this));
conn.addConnectionListener(new JMStatusListener(this));
conn.connect(InetAddress.getByName("redhat"));
}
catch (UnknownHostException uhe)
{
uhe.printStackTrace();
}
catch (InstantiationException ie)
{
ie.printStackTrace();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
//now, we construct a InfoQuery packet with reg data
InfoQueryBuilder iqb=new InfoQueryBuilder();
//and the auth data builder
// IQRegisterExtensionBuilder iqRegb=new IQRegisterExtensionBuilder();
IQAuthExtensionBuilder iqAuthb = new IQAuthExtensionBuilder();
//we are setting info
iqb.setType("set");
iqAuthb.setUsername(d.getLoginID());
iqAuthb.setPassword(d.getPassword());
iqAuthb.setResource("JavaApplet");
//build the Reg data
try
{
iqb.addExtension((QueryExtension)iqAuthb.build());
}
catch (InstantiationException ie)
{
//building failed ?
System.out.println("Fatal Error on Auth object build:");
System.out.println(ie.toString());
return;
}
//build the iq packet
try
{
//build the full InfoQuery packet
InfoQuery iq=(InfoQuery)iqb.build();
conn.send(iq);
}
catch (InstantiationException ie)
{
//building failed ?
System.out.println("Fatal Error on IQ object build:");
System.out.println(ie.toString());
return ;
}
System.out.println("Send requested for IQ");
PresenceBuilder pb = new PresenceBuilder();
try {
conn.send(pb.build());
}
catch (InstantiationException ie)
{
//building failed ?
System.out.println("Fatal Error on Presence object build:");
System.out.println(ie.toString());
return ;
}
System.out.println("Build presence");
label1.setText("Online");
label1.setForeground(Color.red);
}
}
void logoutbtn_actionPerformed(ActionEvent e) {
conn.disconnect();
list2.removeAll();
label1.setText("Offline");
label1.setForeground(Color.blue);
}
public Label getStatus()
{
return status;
}
public List getList()
{
return list2;
}
}
===================================================
package jabber;
import java.awt.*;
import java.awt.event.*;
public class LoginDialog extends Dialog /*implements ActionListener*/ {
Panel panel1 = new Panel();
BorderLayout borderLayout1 = new BorderLayout();
Panel panel2 = new Panel();
Label label1 = new Label();
TextField loginid = new TextField(8);
FlowLayout flowLayout1 = new FlowLayout();
Panel panel3 = new Panel();
Label label2 = new Label();
TextField password = new TextField(8);
Panel panel4 = new Panel();
Button btn_ok = new Button();
Button btn_cancel = new Button();
boolean gotoLogin = false;
public LoginDialog(Frame owner) {
// System.err.println("TEST from LoginDialog");
super(owner, "Please enter login id and password", true);
gotoLogin = false;
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
panel1.setLayout(borderLayout1);
label1.setText("Login ID");
panel2.setLayout(flowLayout1);
loginid.setText("");
label2.setText("Password");
password.setEchoChar('*');
password.setText("");
btn_ok.setLabel("OK");
btn_ok.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
btn_ok_actionPerformed(e);
}
});
btn_cancel.setLabel("Cancel");
btn_cancel.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e) {
btn_cancel_actionPerformed(e);
}
});
panel1.add(panel2, BorderLayout.NORTH);
panel2.add(label1, null);
panel2.add(loginid, null);
panel1.add(panel3, BorderLayout.CENTER);
panel3.add(label2, null);
panel3.add(password, null);
panel1.add(panel4, BorderLayout.SOUTH);
panel4.add(btn_ok, null);
panel4.add(btn_cancel, null);
add(panel1);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(WindowEvent e) {
win_dialog_actionPerformed(e);
}
});
createFrame();
pack();
setVisible(true);
}
private void createFrame() {
Dimension d = getToolkit().getScreenSize();
setLocation(d.width/4 , d.height/3);
}
void btn_ok_actionPerformed(ActionEvent e) {
gotoLogin = true;
setVisible(false);
}
void btn_cancel_actionPerformed(ActionEvent e) {
gotoLogin = false;
setVisible(false);
}
void win_dialog_actionPerformed(WindowEvent e) {
dispose();
}
String getLoginID()
{
return loginid.getText();
}
String getPassword()
{
return password.getText();
}
boolean getGotoLogin()
{
return gotoLogin;
}
/* public void actionPerformed(ActionEvent e) {
setVisible(false);
}*/
}
===================================================
package jabber;
import org.jabber.jabberbeans.ConnectionAdapter;
public class JMStatusListener extends ConnectionAdapter
{
JabberApplet jabberApplet = null;
public JMStatusListener(JabberApplet a)
{
jabberApplet = a;
}
public void connecting()
{
jabberApplet.getStatus().setText("Connecting...");
}
public void connected()
{
jabberApplet.getStatus().setText("Connected...");
}
public void disconnected()
{
jabberApplet.getStatus().setText("Disconnected...");
}
public void connectFailed()
{
jabberApplet.getStatus().setText("Fail to connect...");
}
}
===================================================
package jabber;
import org.jabber.jabberbeans.PacketListener;
import org.jabber.jabberbeans.Packet;
public class JTestPacketListener implements PacketListener
{
JabberApplet jabberApplet = null;
public JTestPacketListener(JabberApplet a)
{
jabberApplet = a;
}
public void receivedPacket(Packet parm1)
{
jabberApplet.getList().add("Receive"+parm1.toString());
}
public void sentPacket(Packet parm1)
{
jabberApplet.getList().add("Send"+parm1.toString());
}
}
-Leon
David, Waite, mass at ufl.edu wrote:
> I can field this one :)
>
> That is a bug in the existing version of Jabberbeans, and something I am
> working on now. Apparently there is a stream:error being sent, most probably
> due to either an invalid data packet being sent, or an incorrect servername
> at connect-time - both which cause the connection to reset (killing the
> output thread unexpectedly). Send me (mass at ufl.edu) a more-complete log so I
> can see how far it is getting.
>
> -David Waite
>
> > -----Original Message-----
> > From: jdev-admin at jabber.org [mailto:jdev-admin at jabber.org]On
> > Behalf Of Leon Kwan
> > Sent: Wednesday, August 16, 2000 5:55 AM
> > To: jdev at jabber.org
> > Subject: [JDEV] Java jabber client.
> >
> >
> > Hello all, I am new to Jabber.
> > I have installed the jabber server in the linux machine.
> > and successfully connect it with WinJab.
> >
> > Now I plan to use JabberBeans and Jabber-webb to implement
> > the jabber client in java applet version.
> > However, I do not know the correct procedure of interacting
> > with jabber server.
> > a. Connecting the server.
> > b. Use IQ to send login request to the server.
> > c. Send Presence packet?
> > d. Get the roster list?
> > e. Send Message packet?
> >
> > What I can do now is that I can connect and login to the server
> > but cannot see its online status in another jabber client like
> > WinJab. I think the presence packet may not properly sent.
> > I got the exception:
> >
> > ISH: SAX: Unknown/invalid root element: stream: error
> > java.lang.RuntimeException: Death of output thread
> > at org/jabber/jabberbeans/sax/ProtocolHandler.onOutputDeath
> > at org/jabber/jabberbeans/sax/OutputStreamHandler.run
> >
> > I would like to ask if there exist any java client source code
> > that successful connect to server and send message or get the
> > roster list?
> >
> > -Leon Kwan
> >
> >
> >
> > _______________________________________________
> > jdev mailing list
> > jdev at jabber.org
> > http://mailman.jabber.org/listinfo/jdev
> >
More information about the JDev
mailing list