FileDocCategorySizeDatePackage
JabberRPCResponder.javaAPI DocExample3250Thu Feb 28 05:31:04 GMT 2002None

JabberRPCResponder

public class JabberRPCResponder extends Object implements PacketListener

Fields Summary
private String
server
private String
user
private String
pass
private String
resource
private XmlRpcServer
responder
private ConnectionBean
cb
Constructors Summary
public JabberRPCResponder()



  // Constructor
   
  
    responder = new XmlRpcServer();
    responder.addHandler("examples", new RPCHandler());
  
Methods Summary
public static voidmain(java.lang.String[] args)

    JabberRPCResponder server = new JabberRPCResponder();
    try
    {
      server.start();
    }
    catch (Exception e)
    {
      System.out.println("Cannot start server: " + e.toString());
    }
  
public voidreceivedPacket(PacketEvent pe)

    Packet packet = pe.getPacket();
    System.out.println("RECV:" + packet.toString());

    if (packet instanceof InfoQuery)
    {
      Enumeration e = ((InfoQuery)packet).Extensions();
      if (e.hasMoreElements())
      {
        Extension ext = (Extension)e.nextElement();

        String request = ext.toString();
        String id = ((InfoQuery)packet).getIdentifier();
        JID from = ((InfoQuery)packet).getFromAddress();

        ByteArrayInputStream bis =
            new ByteArrayInputStream(request.getBytes());
        String result = new String(responder.execute(bis));

        String response = result;
        int pos = result.lastIndexOf("?>");
        if (pos >= 0)
        {
          response = result.substring(pos + 2);
        }

        IQRPCBuilder iqrpcb = new IQRPCBuilder();
        iqrpcb.setPayload(response);

        InfoQueryBuilder iqb = new InfoQueryBuilder();

        iqb.setType("result");
        iqb.setIdentifier(id);
        iqb.setToAddress(from);
        iqb.addExtension(iqrpcb.build());

        InfoQuery iq;
        try {
	        iq = (InfoQuery)iqb.build();
        }
        catch (InstantiationException ie)
        {
          System.out.println("Build failed: " + ie.toString());
          return;
        }

        cb.send(iq);

      }
    }
  
public voidsendFailed(PacketEvent pe)

    Packet packet = pe.getPacket();
    System.out.println("failed to send:" + packet.toString());
  
public voidsentPacket(PacketEvent pe)

    Packet packet = pe.getPacket();
    System.out.println("SEND:" + packet.toString());
  
public voidstart()

    cb = new ConnectionBean();
    InetAddress addr;

    cb.addPacketListener(this);

    // Connect
    cb.connect(addr=InetAddress.getByName(server));

    // Authenticate
    InfoQueryBuilder iqb = new InfoQueryBuilder();
	  IQAuthBuilder iqAuthb = new IQAuthBuilder();

    iqb.setType("set");

    iqAuthb.setUsername(user);
    iqAuthb.setPassword(pass);
    iqAuthb.setResource(resource);

    iqb.addExtension(iqAuthb.build());

	  InfoQuery iq = (InfoQuery)iqb.build();

    cb.send(iq);

    // Send presence
    PresenceBuilder pb = new PresenceBuilder();
    cb.send(pb.build());