CommandLine.java 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import java.io.PrintStream;
  4. import java.net.InetAddress;
  5. import javax.net.ssl.SSLSocket;
  6. import javax.net.ssl.SSLSocketFactory;
  7. /**
  8. * @author joseph mcverry a test program but usable with the
  9. * SocketCommandProcessor which in turns passes to command off to the
  10. * OpenAS2Server.
  11. *
  12. * uses TLS_DH_anon_WITH_AES_256_CBC_SHA cipher for the secure socket
  13. * layer;
  14. *
  15. */
  16. public class CommandLine {
  17. public static void main(String args[]) {
  18. try {
  19. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  20. SSLSocket s = null;
  21. String host, port, name, pwd;
  22. if (args.length == 0) {
  23. host = "localhost";
  24. port = "14321";
  25. name = "userID";
  26. pwd = "pWd";
  27. } else if (args.length != 4) {
  28. System.out.println("format: java org.openas2.remote.CommandLine ipaddresss portnumber userid password");
  29. return;
  30. } else {
  31. host = args[0];
  32. port = args[1];
  33. name = args[2];
  34. pwd = args[3];
  35. }
  36. int iport = Integer.parseInt(port);
  37. while (true) {
  38. System.out.print("Enter command: ");
  39. String icmd = br.readLine().trim();
  40. System.out.print("");
  41. if (icmd.length() < 1) {
  42. System.out.println("adios");
  43. return;
  44. }
  45. s = (SSLSocket) SSLSocketFactory.getDefault().createSocket(InetAddress.getByName(host), iport);
  46. String cipherSuites = System.getProperty("CmdProcessorSocketCipher",
  47. "TLS_DH_anon_WITH_AES_256_CBC_SHA");
  48. final String[] enabledCipherSuites = { cipherSuites };
  49. try {
  50. s.setEnabledCipherSuites(enabledCipherSuites);
  51. } catch (IllegalArgumentException e) {
  52. e.printStackTrace();
  53. System.out.println(
  54. "Cipher is not supported. Try using the command line switch -DCmdProcessorSocketCipher=<some cipher suite> to use one supported by your version of java security.");
  55. }
  56. String cmd = "<command id=\"" + name + "\" password=\"" + pwd + "\">" + icmd + "</command>";
  57. PrintStream ps = new PrintStream(s.getOutputStream(), true);
  58. ps.println(cmd);
  59. BufferedReader rdr = new BufferedReader(new InputStreamReader(s.getInputStream()));
  60. String r;
  61. while ((r = rdr.readLine()) != null) {
  62. System.out.println(r);
  63. }
  64. s.close();
  65. }
  66. } catch (Exception e) {
  67. e.printStackTrace();
  68. }
  69. }
  70. }