[Asterisk-java-users] FastAgi Simulate
Brought to you by:
srt
From: S. B. S. <bs...@no...> - 2007-10-12 06:39:37
|
Stefan, I don't know if anyone will find this useful, but I've created a little simulator for Fast AGI. The idea is that you can use it in a test enviroment to simulate fast agi calls FROM an asterisk server. Its by no means complete and has had only basic testing, but I found it useful and thought the community might get some benefit from it. On word of warning, it tampers with the URL protocol factories so don't use this inside any application which intends to use the URL class. It is only intended for use as a test driver in something like junit. If you want it, I'm happy to put it into the public domain. To use the script do the following: FastAgiSimulator fast = new FastAgiSimulator(); fast.setVariable("somekey", "somevalue"); fast.execute("agi://localhost/Auth?Amount=10.00&CC=1234"); Regards, S. Brett Sutton /* * This class is designed to simulate a fast agi call originating * from an asterisk server. * * The idea is that when developing a fast agi server you can use * this class to simulate the AGI() command for testing purposes. * */ package au.com.asteriskit.fastagi.paymentgateway.test; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.Socket; import java.net.URL; import java.net.URLConnection; import java.net.URLStreamHandler; import java.net.URLStreamHandlerFactory; import java.util.HashMap; import org.apache.log4j.Logger; public class FastAgiSimulator { static final Logger s_logger = Logger.getLogger(FastAgiSimulator.class); static final int FASTAGI_PORT = 4573; HashMap<String, String> vars = new HashMap<String, String>(); private boolean initialized = false;; /** * Allows you to simulating setting an asterisk variable. * Set all variables before calling execute. * @param key * @param value */ void setVariable(String key, String value) { vars.put(key, value); } void execute(String urlString) throws IOException { init(); URL url = new URL(urlString); Socket client = new Socket(url.getHost(), (url.getPort() == -1 ? FASTAGI_PORT : url.getPort())); OutputStream os = client.getOutputStream(); writeAgiVar(os, "network", "yes"); writeAgiVar(os, "network_script", url.getPath() + (url.getQuery() == null ? "" : ("?" + url.getQuery()))); for (String key : vars.keySet()) { String value = vars.get(key); writeAgiVar(os, key, value); } os.write("\n".getBytes()); os.flush(); // Wait for responses and just Ack BufferedWriter br = new BufferedWriter(new OutputStreamWriter(client.getOutputStream())); InputStream is = client.getInputStream(); BufferedReader lr = new BufferedReader(new InputStreamReader(is)); String line = null; while ((line = lr.readLine()) != null) { s_logger.debug(line); // Just reply back with a 'status ok'. br.write("200 result=OK\n"); br.flush(); } } private void init() { if (!initialized ) URL.setURLStreamHandlerFactory(new AgiStreamHandlerFactory()); initialized = true; } private void writeAgiVar(OutputStream os, String argName, String argValue) throws IOException { if (s_logger.isDebugEnabled()) s_logger.debug("Writing agi var: " + argName + ": " + argValue); os.write(("agi_" + argName + ": " + argValue + "\n").getBytes()); } // Add a fake stream handler so that the agi: protocol is handled by // the URL class class AgiStreamHandlerFactory implements URLStreamHandlerFactory { AgiStreamHandlerFactory() { } public URLStreamHandler createURLStreamHandler(String protocol) { // TODO Auto-generated method stub return new AgiURLStreamHandler(); } } class AgiURLStreamHandler extends URLStreamHandler { @Override protected URLConnection openConnection(URL u) throws IOException { // Not used return null; } } } |