Re: [Asterisk-java-users] FastAgi Simulate
Brought to you by:
srt
From: Martin S. <ma...@be...> - 2007-10-12 14:50:48
|
Very cool -- thanks for sharing! Martin Smith, Systems Developer ma...@be... Bureau of Economic and Business Research University of Florida (352) 392-0171 Ext. 221=20 =20 > -----Original Message----- > From: ast...@li...=20 > [mailto:ast...@li...] On=20 > Behalf Of S. Brett Sutton > Sent: Friday, October 12, 2007 2:40 AM > To: ast...@li... > Subject: [Asterisk-java-users] FastAgi Simulate >=20 > Stefan, > I don't know if anyone will find this useful, but I've created a=20 > little simulator for Fast AGI. >=20 > The idea is that you can use it in a test enviroment to simulate fast=20 > agi calls FROM an asterisk server. >=20 > Its by no means complete and has had only basic testing, but=20 > I found it=20 > useful and thought the community might get some benefit from it. >=20 > On word of warning, it tampers with the URL protocol=20 > factories so don't=20 > use this inside any application which intends to use the URL=20 > class. It=20 > is only intended for use as a test driver in something like junit. >=20 > If you want it, I'm happy to put it into the public domain. >=20 > To use the script do the following: >=20 > FastAgiSimulator fast =3D new FastAgiSimulator(); > =20 > fast.setVariable("somekey", "somevalue"); > = fast.execute("agi://localhost/Auth?Amount=3D10.00&CC=3D1234"); >=20 >=20 > Regards, > S. Brett Sutton >=20 >=20 > /* > * 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; >=20 > 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; >=20 > import org.apache.log4j.Logger; >=20 > public class FastAgiSimulator > { > static final Logger s_logger =3D=20 > Logger.getLogger(FastAgiSimulator.class); > static final int FASTAGI_PORT =3D 4573; > =20 > HashMap<String, String> vars =3D new HashMap<String, String>(); > private boolean initialized =3D false;; > =20 > /** > * 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); > } > =20 > void execute(String urlString) throws IOException > { > init(); > URL url =3D new URL(urlString); > =20 > =20 > Socket client =3D new Socket(url.getHost(),=20 > (url.getPort() =3D=3D -1 ?=20 > FASTAGI_PORT > : url.getPort())); >=20 > OutputStream os =3D client.getOutputStream(); > =20 > writeAgiVar(os, "network", "yes"); > writeAgiVar(os, "network_script", > url.getPath() + (url.getQuery() =3D=3D null ? "" : ("?" +=20 > url.getQuery()))); > =20 > for (String key : vars.keySet()) > { > String value =3D vars.get(key); > writeAgiVar(os, key, value); > } > =20 > os.write("\n".getBytes()); > os.flush(); >=20 > // Wait for responses and just Ack > BufferedWriter br =3D new BufferedWriter(new=20 > OutputStreamWriter(client.getOutputStream())); > InputStream is =3D client.getInputStream(); > BufferedReader lr =3D new BufferedReader(new=20 > InputStreamReader(is)); > String line =3D null; > while ((line =3D lr.readLine()) !=3D null) > { > s_logger.debug(line); > // Just reply back with a 'status ok'. > br.write("200 result=3DOK\n"); > br.flush(); > } >=20 > } > =20 > =20 > private void init() > { > if (!initialized ) > URL.setURLStreamHandlerFactory(new=20 > AgiStreamHandlerFactory()); > initialized =3D true; > =20 > } >=20 > private void writeAgiVar(OutputStream os, > String argName, String argValue) throws IOException > { > if (s_logger.isDebugEnabled()) > s_logger.debug("Writing agi var: " + argName + ":=20 > " + argValue); > =20 > os.write(("agi_" + argName + ": " + argValue +=20 > "\n").getBytes()); > } > =20 > =20 > // Add a fake stream handler so that the agi: protocol is=20 > handled by > // the URL class > class AgiStreamHandlerFactory implements URLStreamHandlerFactory > { >=20 > AgiStreamHandlerFactory() > { > =20 > } > =20 > public URLStreamHandler createURLStreamHandler(String=20 > protocol) > { > // TODO Auto-generated method stub > return new AgiURLStreamHandler(); > } > =20 > } > =20 > class AgiURLStreamHandler extends URLStreamHandler > { >=20 > @Override > protected URLConnection openConnection(URL u) throws=20 > IOException > { > // Not used > return null; > } > =20 > } >=20 > } >=20 > -------------------------------------------------------------- > ----------- > This SF.net email is sponsored by: Splunk Inc. > Still grepping through log files to find problems? Stop. > Now Search log events and configuration files using AJAX and=20 > a browser. > Download your FREE copy of Splunk now >> http://get.splunk.com/ > _______________________________________________ > Asterisk-java-users mailing list > Ast...@li... > https://lists.sourceforge.net/lists/listinfo/asterisk-java-users >=20 |