jmsnell 2002/12/31 12:38:34
Added: proposals/jabber/src/com/snellspace/axis/jabber
JabberHandler.java JabberTransport.java
JabberSender.java JabberConnection.java
JabberServer.java
proposals/jabber/src TestClient.java TestServer.java
Test.java
proposals/jabber/src/com/snellspace/axis/jabber/bootstrap
Server.java
Log:
Adding sources
Revision Changes Path
1.1
xml-axis/proposals/jabber/src/com/snellspace/axis/jabber/JabberHandler.java
Index: JabberHandler.java
===================================================================
package com.snellspace.axis.jabber;
import org.jabber.jabberbeans.Packet;
/**
* @author James M Snell <[EMAIL PROTECTED]>
*/
public interface JabberHandler {
public void receivedPacket(
Packet packet);
}
1.1
xml-axis/proposals/jabber/src/com/snellspace/axis/jabber/JabberTransport.java
Index: JabberTransport.java
===================================================================
package com.snellspace.axis.jabber;
import org.apache.axis.AxisEngine;
import org.apache.axis.AxisFault;
import org.apache.axis.MessageContext;
import org.apache.axis.client.Call;
import org.apache.axis.client.Transport;
/**
* @author James M Snell <[EMAIL PROTECTED]>
*/
public class JabberTransport
extends Transport {
public static final String DEFAULT_TRANSPORT_NAME = "JabberSender";
private String to;
public JabberTransport() {
transportName = DEFAULT_TRANSPORT_NAME;
}
public JabberTransport(
String transportName) {
this.transportName = transportName;
}
public String getTo() {
return to;
}
public void setTo(
String to) {
this.to = to;
}
public void setupMessageContextImpl(
MessageContext context,
Call call,
AxisEngine engine)
throws AxisFault {
context.setProperty(
MessageContext.TRANS_URL,
getTo());
}
}
1.1
xml-axis/proposals/jabber/src/com/snellspace/axis/jabber/JabberSender.java
Index: JabberSender.java
===================================================================
package com.snellspace.axis.jabber;
import java.util.Enumeration;
import javax.xml.soap.SOAPException;
import org.apache.axis.AxisFault;
import org.apache.axis.Constants;
import org.apache.axis.Message;
import org.apache.axis.MessageContext;
import org.apache.axis.components.uuid.UUIDGenFactory;
import org.apache.axis.handlers.BasicHandler;
import org.jabber.jabberbeans.InfoQuery;
import org.jabber.jabberbeans.InfoQueryBuilder;
import org.jabber.jabberbeans.Packet;
import org.jabber.jabberbeans.XMLData;
import org.jabber.jabberbeans.Extension.MessageExtension;
import org.jabber.jabberbeans.Extension.QueryExtension;
import org.jabber.jabberbeans.sax.Extension.DefaultExtension;
import org.jabber.jabberbeans.util.JID;
/**
* @author James M Snell <[EMAIL PROTECTED]>
*/
public class JabberSender
extends BasicHandler {
public void invoke(
MessageContext context)
throws AxisFault {
JabberConnection connection =
initializeConnection(context);
Holder holder = new Holder();
Packet request =
createRequestPacketFromMessage(
context.getRequestMessage(),
context);
connection.send(request, holder);
try {
holder.waitForPacket();
} catch (InterruptedException e) {}
Packet response = holder.getPacket();
context.setResponseMessage(
createResponseMessageFromPacket(
response,
context));
connection.disconnect();
connection = null;
}
private JabberConnection initializeConnection(
MessageContext context) {
JID clientID = JID.fromString(context.getUsername());
String password = context.getPassword();
JabberConnection connection = new JabberConnection();
connection.login(clientID, password);
return connection;
}
private Packet createRequestPacketFromMessage(
Message message,
MessageContext context) {
Packet packet = null;
try {
InfoQueryBuilder iqb = new InfoQueryBuilder();
iqb.setIdentifier(getID());
iqb.setType("set");
iqb.setToAddress(
JID.fromString(
context.getStrProp(
MessageContext.TRANS_URL)));
iqb.addExtension(new AxisExtension(message));
packet = iqb.build();
} catch (InstantiationException ie) {}
return packet;
}
private String getID() {
return UUIDGenFactory.
getUUIDGen("org.apache.axis.components.uuid.SimpleUUIDGen").nextUUID();
}
private Message createResponseMessageFromPacket(
Packet packet,
MessageContext context) {
Message message = null;
if (packet instanceof InfoQuery) {
InfoQuery iq = (InfoQuery)packet;
for (
Enumeration e = iq.Extensions();
e.hasMoreElements();) {
DefaultExtension ext = (DefaultExtension)e.nextElement();
StringBuffer buf = new StringBuffer(ext.toString());
if (buf.indexOf(Constants.URI_DEFAULT_SOAP_ENV) > 0) {
message = new Message(ext.toString());
} else {
// ignore non soap stuff for now
}
}
}
return message;
}
private class Holder
implements JabberHandler {
private Packet packet;
public Packet getPacket() {
return this.packet;
}
public synchronized void waitForPacket()
throws InterruptedException {
if (packet != null) return;
wait();
}
public synchronized void receivedPacket(
Packet packet) {
this.packet = packet;
notifyAll();
}
}
public static class AxisExtension
extends XMLData
implements MessageExtension, QueryExtension {
private Message message;
public AxisExtension(
Message message) {
this.message = message;
}
public void appendItem(
StringBuffer buffer) {
try {
String env =
message.getSOAPPart().
getEnvelope().toString();
buffer.append(env);
} catch (SOAPException e) {}
}
}
}
1.1
xml-axis/proposals/jabber/src/com/snellspace/axis/jabber/JabberConnection.java
Index: JabberConnection.java
===================================================================
package com.snellspace.axis.jabber;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import org.jabber.jabberbeans.ConnectionBean;
import org.jabber.jabberbeans.ConnectionEvent;
import org.jabber.jabberbeans.ConnectionListener;
import org.jabber.jabberbeans.ContentPacket;
import org.jabber.jabberbeans.InfoQuery;
import org.jabber.jabberbeans.InfoQueryBuilder;
import org.jabber.jabberbeans.Packet;
import org.jabber.jabberbeans.PacketEvent;
import org.jabber.jabberbeans.PacketListener;
import org.jabber.jabberbeans.PresenceBuilder;
import org.jabber.jabberbeans.Extension.IQAuthBuilder;
import org.jabber.jabberbeans.util.JID;
/**
* @author James M Snell <[EMAIL PROTECTED]>
*/
public class JabberConnection
implements ConnectionListener, PacketListener {
protected ConnectionBean cb;
protected String loginid;
protected boolean connected;
protected HashMap requests = new HashMap();
public void disconnect() {
cb.disconnect();
loginid = null;
connected = false;
}
public void send(
Packet packet) {
send(packet, null);
}
public void send(
Packet packet,
JabberHandler handler) {
if (packet instanceof InfoQuery) {
InfoQuery iq = (InfoQuery)packet;
requests.put(
iq.getIdentifier(),
handler);
}
cb.send(packet);
}
public void login(
String clientID,
String password) {
login(JID.fromString(clientID), password);
}
public void login(
JID clientID,
String password) {
cb = new ConnectionBean();
try {
cb.connect(InetAddress.getByName(clientID.getServer()));
} catch (UnknownHostException e) {
throw new IllegalArgumentException(); // fix it
} catch (IOException e) {
throw new IllegalArgumentException(); // fix it
}
cb.addConnectionListener(this);
cb.addPacketListener(this);
InfoQueryBuilder iqb = new InfoQueryBuilder();
IQAuthBuilder iqAuthb = new IQAuthBuilder();
iqb.setType("set");
iqAuthb.setUsername(clientID.getUsername());
iqAuthb.setPassword(password);
iqAuthb.setResource(clientID.getResource());
InfoQuery iq = null;
try {
iqb.addExtension(iqAuthb.build());
iq = (InfoQuery)iqb.build();
} catch (InstantiationException ie) {
throw new IllegalArgumentException(); // fix it
}
loginid = iq.getIdentifier();
cb.send(iq);
}
public void receivedPacket(
PacketEvent event) {
Packet packet = event.getPacket();
if (!connected) {
if (processLogin(packet)) {
connected = true;
} else {
connected = false;
}
return;
}
processPacket(packet);
}
protected void processPacket(
Packet packet) {
try {
ContentPacket cp = (ContentPacket)packet;
JabberHandler handler =
(JabberHandler)requests.get(
cp.getIdentifier());
if (handler != null) {
handler.receivedPacket(packet);
}
} catch (Exception e) {
// ignore everything else
}
}
public void connectionChanged(
ConnectionEvent event) {
// ignore for now
}
public void sentPacket(
PacketEvent event) {
// ignore for now
}
public void sendFailed(
PacketEvent event) {
// ignore for now
}
private boolean processLogin(
Packet packet) {
if( !(packet instanceof InfoQuery))
return false;
InfoQuery iq=(InfoQuery)packet;
if (!iq.getIdentifier().equals(loginid))
return false;
if(iq.getType().equals("result")) {
try {
PresenceBuilder pb = new PresenceBuilder();
cb.send(pb.build());
} catch (InstantiationException e) {
return false;
}
} else {
if(iq.getType().equals("error")) {
return false;
} else {
return false;
}
}
return true;
}
}
1.1
xml-axis/proposals/jabber/src/com/snellspace/axis/jabber/JabberServer.java
Index: JabberServer.java
===================================================================
package com.snellspace.axis.jabber;
import java.util.Enumeration;
import org.apache.axis.AxisEngine;
import org.apache.axis.AxisFault;
import org.apache.axis.Constants;
import org.apache.axis.Message;
import org.apache.axis.MessageContext;
import org.apache.axis.server.AxisServer;
import org.jabber.jabberbeans.InfoQuery;
import org.jabber.jabberbeans.InfoQueryBuilder;
import org.jabber.jabberbeans.Packet;
import org.jabber.jabberbeans.sax.Extension.DefaultExtension;
import org.jabber.jabberbeans.util.JID;
/**
* @author James M Snell <[EMAIL PROTECTED]>
*/
public class JabberServer
extends JabberConnection {
private static AxisEngine engine;
private String resourceID;
private String transportName = JabberTransport.DEFAULT_TRANSPORT_NAME;
public static final String JABBER_FROM = "JABBER.FROM";
public static final String JABBER_TO = "JABBER.TO";
public static final String JABBER_ID = "JABBER.ID";
public JabberServer(
String identity,
String password) {
this(JID.fromString(identity), password);
}
public JabberServer(
JID identity,
String password) {
resourceID = identity.getResource();
login(identity, password);
}
public String getTransportName() {
return this.transportName;
}
public void setTransportName(
String transportName) {
this.transportName = transportName;
}
public String getServerID() {
return this.resourceID;
}
public void setServerID(
String id) {
this.resourceID = id;
}
protected void processPacket(
Packet packet) {
if (packet instanceof InfoQuery) {
InfoQuery iq = (InfoQuery)packet;
if ("set".equals(iq.getType())) {
for (
Enumeration e = iq.Extensions();
e.hasMoreElements();) {
DefaultExtension ext = (DefaultExtension)e.nextElement();
StringBuffer buffer = new StringBuffer(ext.toString());
if (buffer.indexOf(Constants.URI_DEFAULT_SOAP_ENV) > 0) {
JID from = iq.getFromAddress();
JID to = iq.getToAddress();
String id = iq.getIdentifier();
Message soapMessage = new Message(buffer.toString());
try {
processMessage(from,to,id,soapMessage);
} catch (AxisFault ex) {}
} else {
// ignore for now
}
}
} else {
// ignore for now
}
}
}
public static synchronized AxisEngine getAxisEngine() {
if (engine == null) {
engine = new AxisServer();
}
return engine;
}
private void processMessage(
JID from,
JID to,
String id,
Message soapMessage)
throws AxisFault {
AxisEngine engine = JabberServer.getAxisEngine();
MessageContext context =
new MessageContext(engine);
context.setProperty(JABBER_FROM, from.toString());
context.setProperty(JABBER_TO, to.toString());
context.setProperty(JABBER_ID, id);
context.setRequestMessage(soapMessage);
context.setTransportName(getTransportName());
context.setTargetService(resourceID);
engine.invoke(context);
Message response = context.getResponseMessage();
InfoQueryBuilder iqb = new InfoQueryBuilder();
iqb.setIdentifier(id);
iqb.setToAddress(from);
iqb.setFromAddress(to);
iqb.setType("result");
iqb.addExtension(
new JabberSender.AxisExtension(
response));
try {
send(iqb.build());
} catch (InstantiationException e) {}
}
}
1.1 xml-axis/proposals/jabber/src/TestClient.java
Index: TestClient.java
===================================================================
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.encoding.XMLType;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.configuration.XMLStringProvider;
import com.snellspace.axis.jabber.JabberTransport;
/**
* @author James M Snell <[EMAIL PROTECTED]>
*/
public class TestClient {
private static final String dep =
"<deployment name=\"defaultClientConfig\"" +
" xmlns=\"http://xml.apache.org/axis/wsdd/\"" +
" xmlns:java=\"http://xml.apache.org/axis/wsdd/providers/java\">" +
" <transport name=\"JabberSender\"
pivot=\"java:com.snellspace.axis.jabber.JabberSender\" />" +
"</deployment>";
private static void client() throws Exception {
JabberTransport transport = new JabberTransport();
transport.setTo("<someid>@jabber.org/service1");
Service service = new Service(new XMLStringProvider(dep));
Call call = (Call)service.createCall();
call.setOperationName(new QName("urn:test", "echo"));
call.addParameter("a", XMLType.XSD_STRING, ParameterMode.IN);
call.setReturnType(XMLType.XSD_STRING);
call.setTransport(transport);
call.setUsername("<someid>[EMAIL PROTECTED]/service2");
call.setPassword("<somepassword>");
String s = (String)call.invoke(new String[] {"test"});
System.out.println(s);
}
public static void main(
String[] args)
throws Exception {
client();
}
}
1.1 xml-axis/proposals/jabber/src/TestServer.java
Index: TestServer.java
===================================================================
public class TestServer {
public String echo(String arg) {
StringBuffer buf = new StringBuffer(arg);
return buf.reverse().toString();
}
}
1.1 xml-axis/proposals/jabber/src/Test.java
Index: Test.java
===================================================================
import java.io.ByteArrayInputStream;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.encoding.XMLType;
import org.apache.axis.client.AdminClient;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.configuration.XMLStringProvider;
import org.apache.axis.server.AxisServer;
import org.apache.axis.transport.local.LocalTransport;
import com.snellspace.axis.jabber.*;
public class Test {
private static final String dep =
"<deployment name=\"defaultClientConfig\"" +
" xmlns=\"http://xml.apache.org/axis/wsdd/\"" +
" xmlns:java=\"http://xml.apache.org/axis/wsdd/providers/java\">" +
" <transport name=\"JabberSender\"
pivot=\"java:com.snellspace.axis.jabber.JabberSender\" />" +
"</deployment>";
private static final String serv =
"<deployment xmlns=\"http://xml.apache.org/axis/wsdd/\""+
" xmlns:java=\"http://xml.apache.org/axis/wsdd/providers/java\">"+
" <service name=\"service1\" provider=\"java:RPC\">"+
" <namespace>urn:test</namespace>" +
" <parameter name=\"className\" value=\"TestServer\"/>"+
" <parameter name=\"allowedMethods\" value=\"*\"/>"+
" </service>"+
"</deployment>";
private static final String userv =
"<undeployment xmlns=\"http://xml.apache.org/axis/wsdd/\">"+
" <service name=\"service1\" />"+
"</undeployment>";
public static void main(String[] args) throws Exception {
deploy();
JabberServer service1 = server();
client();
undeploy();
service1.disconnect();
}
public static JabberServer server() {
return
new JabberServer(
"<someid>@jabber.org/service1", "knight12");
}
public static void deploy()
throws Exception {
AxisServer server = (AxisServer)JabberServer.getAxisEngine();
LocalTransport transport = new LocalTransport(server);
transport.setRemoteService("AdminService");
AdminClient client = new AdminClient();
Call call = client.getCall();
call.setTransport(transport);
client.process(new ByteArrayInputStream(serv.getBytes()));
server.refreshGlobalOptions();
}
public static void undeploy()
throws Exception {
AxisServer server = (AxisServer)JabberServer.getAxisEngine();
LocalTransport transport = new LocalTransport(server);
transport.setRemoteService("AdminService");
AdminClient client = new AdminClient();
Call call = client.getCall();
call.setTransport(transport);
client.process(new ByteArrayInputStream(userv.getBytes()));
server.refreshGlobalOptions();
}
private static void client() throws Exception {
JabberTransport transport = new JabberTransport();
transport.setTo("<someid>@jabber.org/service1");
Service service = new Service(new XMLStringProvider(dep));
Call call = (Call)service.createCall();
call.setOperationName(new QName("urn:test", "echo"));
call.addParameter("a", XMLType.XSD_STRING, ParameterMode.IN);
call.setReturnType(XMLType.XSD_STRING);
//call.setReturnQName(new QName("arg", "urn:test"));
call.setTransport(transport);
call.setUsername("<someid>[EMAIL PROTECTED]/service2");
call.setPassword("<somepassword>");
String s = (String)call.invoke(new String[] {"test"});
System.out.println(s);
}
}
1.1
xml-axis/proposals/jabber/src/com/snellspace/axis/jabber/bootstrap/Server.java
Index: Server.java
===================================================================
package com.snellspace.axis.jabber.bootstrap;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.MessageFormat;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Iterator;
import org.apache.axis.client.AdminClient;
import org.apache.axis.client.Call;
import org.apache.axis.server.AxisServer;
import org.apache.axis.transport.local.LocalTransport;
import com.snellspace.axis.jabber.JabberServer;
public class Server {
private static HashMap services = new HashMap();
private static final String START = "start ";
private static final String STOP = "stop ";
private static final String DEPLOY = "deploy ";
private static final String EXIT = "exit";
private static final String LIST = "list";
private static final String HELP = "help";
private static final String HELP_ = "?";
private static final String START_PATTERN = "start {0} {1}";
private static final String STOP_PATTERN = "stop {0}";
private static final String DEPLOY_PATTERN = "deploy {0}";
public static void main(
String[] args)
throws Exception {
System.out.println("Axis Jabber Server");
System.out.println("By James Snell <[EMAIL PROTECTED]>");
System.out.println("type <help> or <?> for usage help");
menuHandler();
}
private static void menuHandler()
throws IOException, ParseException {
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
for (;;) {
System.out.print("> ");
String in = br.readLine();
if (in.startsWith(HELP) ||
in.startsWith(HELP_)) {
showHelp();
} else if (in.startsWith(START)) {
try {
MessageFormat mf = new MessageFormat(START_PATTERN);
Object[] o = mf.parse(in);
startService((String)o[0],(String)o[1]);
} catch (ParseException pe) {
showHelp();
}
} else if (in.startsWith(STOP)) {
try {
MessageFormat mf = new MessageFormat(STOP_PATTERN);
Object[] o = mf.parse(in);
stopService((String)o[0]);
} catch (ParseException pe) {
showHelp();
}
} else if (in.startsWith(DEPLOY)) {
try {
MessageFormat mf = new MessageFormat(DEPLOY_PATTERN);
Object[] o = mf.parse(in);
deploy((String)o[0]);
} catch (ParseException pe) {
showHelp();
}
} else if (in.startsWith(LIST)) {
list();
} else if (in.startsWith(EXIT)) {
exit();
} else {
showHelp();
}
}
}
private static void exit() {
shutDown();
System.exit(0);
}
private static void showHelp() {
System.out.println("Usage:");
System.out.println("\tstart [service id] [password]");
System.out.println("\tstop [service id]");
System.out.println("\texit ");
System.out.println("\tlist ");
System.out.println("\tdeploy [wsdd url]");
System.out.println("");
}
private static void showError(
String message,
Exception e) {
System.out.println(message);
System.out.println("\t" + e);
}
private static void startService(
String identity,
String password) {
try {
System.out.println("Starting <" + identity + ">");
services.put(
identity,
new JabberServer(
identity,
password));
} catch (Exception e) {
showError(
"CANNOT START SERVICE",e);
}
}
private static void stopService(
String identity) {
try {
System.out.println("Stopping <" + identity + ">");
JabberServer s = (JabberServer)services.get(identity);
s.disconnect();
services.remove(identity);
} catch (Exception e) {
showError(
"CANNOT STOP SERVICE",e);
}
}
private static void shutDown() {
try {
System.out.println("Shutting down...");
for (
Iterator i = services.keySet().iterator();
i.hasNext();) {
stopService((String)i.next());
}
} catch (Exception e) {
showError("PROBLEM SHUTTING DOWN... FORCING EXIT", e);
System.exit(0);
}
}
private static void list() {
try {
System.out.println("Currently Running Services:");
if (services.size() == 0)
System.out.println("\tNo Running Services");
for (
Iterator i = services.keySet().iterator();
i.hasNext();) {
System.out.println("\t" + i.next() + "");
}
System.out.println("");
} catch (Exception e) {
showError(
"PROBLEM LISTING SERVICES", e);
}
}
private static void deploy(
String wsdd) {
try {
AxisServer server = (AxisServer)JabberServer.getAxisEngine();
LocalTransport transport = new LocalTransport(server);
transport.setRemoteService("AdminService");
AdminClient client = new AdminClient();
Call call = client.getCall();
call.setTransport(transport);
client.process(wsdd);
server.refreshGlobalOptions();
} catch (Exception e) {
showError(
"PROBLEM DEPLOYING SERVICE", e);
}
}
}