KickJava   Java API By Example, From Geeks To Geeks.

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


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 JSPServletResponseWaitable implements Waitable {
35     private String JavaDoc id = null;
36     private String JavaDoc real = null;
37     private String JavaDoc userAgent = null;
38     private String JavaDoc answer = null;
39     private int port = -1;
40     private boolean started = false;
41     public JSPServletResponseWaitable(String JavaDoc id, String JavaDoc answer, int port) {
42     this.id = id;
43     this.answer = answer;
44     this.port = port;
45
46     (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             System.out.println("Listen on " + port);
53             started = true;
54             Socket JavaDoc s = ss.accept();
55             InputStream JavaDoc is = s.getInputStream();
56             BufferedReader JavaDoc br = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(is));
57             String JavaDoc line = br.readLine();
58             while(!(line == null)) {
59                 System.out.println("READ: \"" + line + "\"");
60                 if(line.startsWith("ID")) {
61                 setId(line);
62                 }
63                 if(line.startsWith("UserAgent")) {
64                 setUserAgent(line);
65                 }
66                 line = br.readLine();
67                 if(line.equals("")) {
68                 line = null;
69                 }
70             }
71             OutputStream JavaDoc os = s.getOutputStream();
72             BufferedWriter JavaDoc bw = new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(os));
73             bw.write(answer,0,answer.length());
74             System.out.println("CL is " + answer.length());
75             bw.flush();
76             s.close();
77             ss.close();
78             }catch(Exception JavaDoc e) {
79             real = "Exception at READ/WRITE http opeartions";
80             System.out.println("ERROR!");
81             e.printStackTrace();
82             }
83         }
84         }).start();
85     while(!started);
86     }
87     
88     public Object JavaDoc actionProduced(Object JavaDoc o) {
89     if((real!=null)&&(real.indexOf(id)!=-1)) {
90         return Boolean.TRUE;
91     }
92     if((real!=null)&&(real.indexOf(id)==-1)) {
93         System.out.println("ID is " + real + " instead of " + id);
94     }
95     return null;
96     }
97
98     public String JavaDoc getDescription() {
99     return "Waiter for ID: " + id;
100     }
101
102     private int getDefaultPort() {
103     return port;
104     }
105     private String JavaDoc getDefaultAnswer() {
106     return answer;
107     }
108     private void setId(String JavaDoc id) {
109     real = id;
110     }
111
112     private void setUserAgent(String JavaDoc agent) {
113     userAgent = agent;
114     }
115     private String JavaDoc getUserAgent() {
116     return userAgent;
117     }
118     public String JavaDoc getId() {
119     return real;
120     }
121 }
122
123
Popular Tags