KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > gui > web > util > HttpRequestWaitable


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.test.gui.web.util;
21 import java.io.InputStream JavaDoc;
22 import java.io.InputStreamReader JavaDoc;
23 import java.io.BufferedReader JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import java.io.OutputStreamWriter JavaDoc;
26 import java.io.BufferedWriter JavaDoc;
27 import java.net.Socket JavaDoc;
28 import java.net.ServerSocket JavaDoc;
29
30
31 import org.netbeans.jemmy.Waitable;
32 import org.netbeans.core.NbTopManager;
33
34 public class HttpRequestWaitable implements Waitable {
35     private String JavaDoc url = null;
36     private String JavaDoc real = null;
37     private String JavaDoc userAgent = null;
38     private String JavaDoc answer = null;
39     private Thread JavaDoc t = null;
40     private int port = -1;
41     public HttpRequestWaitable(String JavaDoc url, String JavaDoc answer, int port) {
42     this.url = url;
43     this.answer = answer;
44     this.port = port;
45     
46     t = new Thread JavaDoc () {
47         public void run() {
48             try {
49             String JavaDoc answer = getDefaultAnswer();
50             int port = getDefaultPort();
51             ServerSocket JavaDoc ss = new ServerSocket JavaDoc(port);
52             Socket JavaDoc s = ss.accept();
53             InputStream JavaDoc is = s.getInputStream();
54             BufferedReader JavaDoc br = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(is));
55             String JavaDoc 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 JavaDoc os = s.getOutputStream();
70             BufferedWriter JavaDoc bw = new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(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 JavaDoc 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 JavaDoc actionProduced(Object JavaDoc o) {
87     if((real!=null)&&(real.indexOf(url)!=-1)) {
88         return Boolean.TRUE;
89     }
90     return null;
91     }
92
93     public String JavaDoc getDescription() {
94     return "Waiter for URL: " + url;
95     }
96
97     private int getDefaultPort() {
98     return port;
99     }
100     private String JavaDoc getDefaultAnswer() {
101     return answer;
102     }
103     private void setRealURL(String JavaDoc url) {
104     real = url;
105     }
106
107     private void setUserAgent(String JavaDoc agent) {
108     userAgent = agent;
109     }
110     public String JavaDoc getUserAgent() {
111     return userAgent;
112     }
113     public String JavaDoc getRequestedURL() {
114     return real;
115     }
116     public void stop() {
117     t.stop();
118     }
119 }
120
121
Popular Tags