1 19 20 package org.netbeans.test.gui.web.util; 21 import java.io.InputStream ; 22 import java.io.InputStreamReader ; 23 import java.io.BufferedReader ; 24 import java.io.OutputStream ; 25 import java.io.OutputStreamWriter ; 26 import java.io.BufferedWriter ; 27 import java.net.Socket ; 28 import java.net.ServerSocket ; 29 30 31 import org.netbeans.jemmy.Waitable; 32 import org.netbeans.core.NbTopManager; 33 34 public class HttpRequestWaitable implements Waitable { 35 private String url = null; 36 private String real = null; 37 private String userAgent = null; 38 private String answer = null; 39 private Thread t = null; 40 private int port = -1; 41 public HttpRequestWaitable(String url, String answer, int port) { 42 this.url = url; 43 this.answer = answer; 44 this.port = port; 45 46 t = new Thread () { 47 public void run() { 48 try { 49 String answer = getDefaultAnswer(); 50 int port = getDefaultPort(); 51 ServerSocket ss = new ServerSocket (port); 52 Socket s = ss.accept(); 53 InputStream is = s.getInputStream(); 54 BufferedReader br = new BufferedReader (new InputStreamReader (is)); 55 String line = br.readLine(); 56 while(!(line == null)) { 57 System.out.println("READ: \"" + line + "\""); 58 if(line.startsWith("GET")) { 59 setRealURL(line); 60 } 61 if(line.startsWith("User-Agent:")) { 62 setUserAgent(line); 63 } 64 line = br.readLine(); 65 if(line.equals("")) { 66 line = null; 67 } 68 } 69 OutputStream os = s.getOutputStream(); 70 BufferedWriter bw = new BufferedWriter (new OutputStreamWriter (os)); 71 bw.write(answer,0,answer.length()); 72 System.out.println("CL is " + answer.length()); 73 bw.flush(); 74 s.close(); 75 ss.close(); 76 }catch(Exception e) { 77 real = "Exception at READ/WRITE http opeartions"; 78 System.out.println("ERROR!"); 79 e.printStackTrace(); 80 } 81 } 82 }; 83 t.start(); 84 } 85 86 public Object actionProduced(Object o) { 87 if((real!=null)&&(real.indexOf(url)!=-1)) { 88 return Boolean.TRUE; 89 } 90 return null; 91 } 92 93 public String getDescription() { 94 return "Waiter for URL: " + url; 95 } 96 97 private int getDefaultPort() { 98 return port; 99 } 100 private String getDefaultAnswer() { 101 return answer; 102 } 103 private void setRealURL(String url) { 104 real = url; 105 } 106 107 private void setUserAgent(String agent) { 108 userAgent = agent; 109 } 110 public String getUserAgent() { 111 return userAgent; 112 } 113 public String getRequestedURL() { 114 return real; 115 } 116 public void stop() { 117 t.stop(); 118 } 119 } 120 121 | Popular Tags |