1 18 19 package sync4j.test.tools; 20 21 import java.net.*; 22 import java.io.*; 23 import java.util.List ; 24 import java.util.Iterator ; 25 import java.util.logging.Logger ; 26 import java.util.logging.Level ; 27 28 import sync4j.framework.core.*; 29 import sync4j.framework.logging.Sync4jLogger; 30 import sync4j.framework.protocol.*; 31 import sync4j.framework.tools.IOTools; 32 33 import sync4j.test.TestFailedException; 34 import sync4j.test.tools.HttpClientConnection; 35 36 37 44 public class SimplePostSyncML { 45 46 48 public static String LOG_NAME = "test"; 49 50 52 private String nextURL = null; 53 private String [] msgs = null; 54 private String [] msgFiles = null; 55 56 private static final Logger log = Sync4jLogger.getLogger(LOG_NAME); 57 58 60 public SimplePostSyncML(String initialURL , 61 String [] msgFiles ) 62 throws IOException { 63 if ((msgFiles == null) || (msgFiles.length == 0)) { 64 msgs = new String [0]; 65 } 66 67 msgs = new String [msgFiles.length]; 68 69 this.msgFiles = msgFiles; 70 for (int i=0; i<msgFiles.length; ++i) { 71 msgs[i] = IOTools.readFileString(new File(msgFiles[i])); 72 } 73 74 nextURL = initialURL; 75 } 76 77 79 public void start() throws IOException, TestFailedException { 80 SyncML response = null; 81 String respURI = null; 82 83 File responseFile = null; 84 for (int i=0; i<msgs.length; ++i) { 85 86 log.info("Sending " + msgFiles[i]); 87 88 try { 89 response = postRequest(msgs[i]); 90 } catch (RepresentationException e) { 91 throw new TestFailedException ("XML syntax error: " + e.getMessage(), e); 92 } catch (Sync4jException e) { 93 throw new TestFailedException ("XML syntax error: " + e.getMessage(), e); 94 } 95 96 responseFile = new File(msgFiles[i] + ".out"); 102 log.info("Writing the response into " + responseFile); 103 104 try { 105 106 IOTools.writeFile(Util.toXML(response), responseFile); 107 108 } catch(Exception e) { 109 throw new TestFailedException ("XML syntax error: " + e.getMessage(), e); 110 } 111 112 respURI = response.getSyncHdr().getRespURI(); 113 114 if (respURI != null) { 115 nextURL = respURI; 116 } 117 } 118 } 119 120 122 private SyncML postRequest(String request) 123 throws IOException, Sync4jException, RepresentationException { 124 HttpClientConnection syncMLConnection = new HttpClientConnection(nextURL); 125 return syncMLConnection.sendMessage(request); 126 } 127 128 private static void syntax() { 129 System.out.println("Syntax: " + PostSyncML.class.getName() + " <initial URL> <msg1> ... <msgN>"); 130 } 131 132 134 public static void main(String args[]) 135 throws Exception { 136 if(args.length < 2) { 137 syntax(); 138 return; 139 } 140 141 String [] msgFiles = new String [args.length-1]; 142 143 System.arraycopy(args, 1, msgFiles, 0, msgFiles.length); 144 145 SimplePostSyncML postsyncml = new SimplePostSyncML(args[0], msgFiles); 146 postsyncml.start(); 147 } 148 } 149 | Popular Tags |