1 24 25 import java.applet.Applet ; 26 import java.applet.AppletContext ; 27 import java.io.BufferedReader ; 28 import java.io.IOException ; 29 import java.io.InputStreamReader ; 30 import java.io.PrintWriter ; 31 import java.io.UnsupportedEncodingException ; 32 import java.net.MalformedURLException ; 33 import java.net.URL ; 34 import java.net.URLConnection ; 35 import java.net.URLEncoder ; 36 import java.util.HashMap ; 37 import java.util.Iterator ; 38 import java.util.Map ; 39 40 47 public class ControlApplet extends Applet implements Runnable { 48 49 private static String pushUrl = "/commonapp/control/pushapplet"; 50 private static String pullUrl = "/commonapp/control/pullapplet"; 51 52 protected AppletContext ctx = null; 53 54 protected String sessionId = null; 55 protected String visitId = null; 56 protected String serverUrl = null; 57 protected String timeoutUrl = null; 58 protected String currentPage = null; 59 protected String debug = null; 60 61 protected int waitTime = 1500; 62 protected int timeout = 300000; 63 64 protected boolean isRunning = false; 65 protected boolean stopped = true; 66 67 protected Thread thread = null; 68 69 public void init() { 70 ctx = this.getAppletContext(); 71 this.sessionId = this.getParameter("sessionId"); 72 this.visitId = this.getParameter("visitId"); 73 this.serverUrl = this.getParameter("serverUrl"); 74 this.timeoutUrl = this.getParameter("timeoutUrl"); 75 this.currentPage = this.getParameter("currentPage"); 76 this.debug = this.getParameter("debug"); 77 78 try { 80 int waitInt = Integer.parseInt(this.getParameter("waitTime")); 81 if (waitInt > 0) 82 waitTime = waitInt; 83 } catch (NumberFormatException e) { 84 } 85 try { 86 int outInt = Integer.parseInt(this.getParameter("timeout")); 87 if (outInt > 0) 88 timeout = outInt; 89 } catch (NumberFormatException e) { 90 } 91 92 if (serverUrl != null) { 93 boolean sessionOkay = false; 94 boolean visitOkay = false; 95 boolean pageOkay = false; 96 97 if (sessionId != null && sessionId.length() > 0) 98 sessionOkay = true; 99 if (visitId != null && visitId.length() > 0) 100 visitOkay = true; 101 if (currentPage != null && currentPage.length() > 0) 102 pageOkay = true; 103 104 if (sessionOkay && visitOkay && pageOkay) { 105 this.push(); 107 } 108 109 this.isRunning = true; 111 this.stopped = false; 112 thread = new Thread (this); 113 thread.setDaemon(false); 114 thread.start(); 115 } 116 } 117 118 public void destroy() { 119 this.stopped = true; 120 } 121 122 public void run() { 124 while (isRunning && !stopped) { 125 this.pull(); 126 try { 127 Thread.sleep(waitTime); 128 } catch (InterruptedException e) { 129 this.stopped = true; 130 this.isRunning = false; 131 } 132 } 133 if (debug != null && debug.equalsIgnoreCase("true")) 134 System.out.println("Polling finished."); 135 } 136 137 protected void pull() { 138 Map params = new HashMap (); 139 params.put("sessionId", this.sessionId.trim()); 140 params.put("visitId", this.visitId.trim()); 141 142 String pullResp = null; 143 URL callPullUrl = null; 144 try { 145 callPullUrl = new URL (serverUrl + pullUrl); 146 } catch (MalformedURLException e) { 147 } 148 149 if (callPullUrl != null) { 150 try { 151 pullResp = callServer(callPullUrl, params); 152 } catch (IOException e) { 153 } 154 } 155 156 if (pullResp != null && pullResp.length() > 0) { 157 URL docUrl = null; 158 try { 159 docUrl = new URL (pullResp); 160 } catch (MalformedURLException e) { 161 } 162 if (docUrl != null) 163 ctx.showDocument(docUrl, "appletWindow"); 164 } 165 } 166 167 protected void push() { 168 Map params = new HashMap (); 169 params.put("sessionId", this.sessionId.trim()); 170 params.put("visitId", this.visitId.trim()); 171 params.put("currentPage", this.currentPage.trim()); 172 173 String pushResp = null; 174 URL callPushUrl = null; 175 try { 176 callPushUrl = new URL (serverUrl + pushUrl); 177 } catch (MalformedURLException e) { 178 } 179 180 if (callPushUrl != null) { 181 try { 182 pushResp = callServer(callPushUrl, params); 183 } catch (IOException e) { 184 } 185 } 186 } 187 188 private String callServer(URL serverUrl, Map parms) throws IOException { 189 String parameters = this.encodeArgs(parms); 191 if (debug != null && debug.equalsIgnoreCase("true")) 192 System.out.println("Sending parameters: " + parameters); 193 URLConnection uc = serverUrl.openConnection(); 194 uc.setDoOutput(true); 195 uc.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); 196 PrintWriter pw = new PrintWriter (uc.getOutputStream()); 197 pw.println(parameters); 198 pw.close(); 199 200 BufferedReader in = new BufferedReader (new InputStreamReader (uc.getInputStream())); 202 String responseStr = in.readLine(); 203 in.close(); 204 if (responseStr != null) 205 responseStr.trim(); 206 if (debug != null && debug.equalsIgnoreCase("true")) 207 System.out.println("Receive response: " + responseStr); 208 return responseStr; 209 } 210 211 public String encodeArgs(Map args) { 212 StringBuffer buf = new StringBuffer (); 213 if (args != null) { 214 Iterator i = args.entrySet().iterator(); 215 while (i.hasNext()) { 216 Map.Entry entry = (Map.Entry ) i.next(); 217 String name = (String ) entry.getKey(); 218 Object value = entry.getValue(); 219 String valueStr = null; 220 if (name != null && value != null) { 221 if (value instanceof String ) { 222 valueStr = (String ) value; 223 } else { 224 valueStr = value.toString(); 225 } 226 227 if (valueStr != null && valueStr.length() > 0) { 228 if (buf.length() > 0) buf.append('&'); 229 try { 230 buf.append(URLEncoder.encode(name, "UTF-8")); 231 } catch (UnsupportedEncodingException e) { 232 e.printStackTrace(); 233 } 234 buf.append('='); 235 try { 236 buf.append(URLEncoder.encode(valueStr, "UTF-8")); 237 } catch (UnsupportedEncodingException e) { 238 e.printStackTrace(); 239 } 240 } 241 } 242 } 243 } 244 return buf.toString(); 245 } 246 } 247 | Popular Tags |