1 6 7 package com.quikj.application.web.talk.feature.proactive.applet; 8 9 import java.util.*; 10 import java.net.*; 11 import java.awt.*; 12 import java.awt.event.*; 13 14 import com.quikj.client.framework.*; 15 import com.quikj.client.beans.*; 16 17 21 public class ProactiveApplet extends java.applet.Applet 22 implements HTTPMessageListenerInterface, HTTPConnectionClosedInterface, ActionListener 23 { 24 private URL codeBase = null; 25 private int port = 80; 26 private int applicationId = 4; 27 private ServerCommunications com = null; 28 private String imageURL = null; 29 private String groupName = "operator"; 30 private ImageButton imageButton = null; 31 private URL onContactURL = null; 32 private URL onClickURL = null; 33 private String operator = null; 34 35 36 public void init() 37 { 38 initAppletParams(); 39 40 initComponents(); 41 42 if (imageURL != null) 43 { 44 drawImageButton(); 45 } 46 47 } 49 50 private void initAppletParams() 51 { 52 while (PersistentObjects.running == true) 53 { 54 try 55 { 56 Thread.sleep(10); 57 } 58 catch (InterruptedException ex) 59 { 60 } 62 } 63 64 PersistentObjects.running = true; 65 66 codeBase = getCodeBase(); 68 69 if (codeBase.getPort() >= 0) { 71 port = codeBase.getPort(); 72 } 73 74 String parm = getParameter("port"); 75 if (parm != null) 76 { 77 try 78 { 79 port = Integer.parseInt(parm); 80 } 81 catch (NumberFormatException ex) 82 { 83 ; 84 } 85 } 86 87 parm = getParameter("plugin"); 88 if (parm != null) 89 { 90 try 91 { 92 applicationId = Integer.parseInt(parm); 93 } 94 catch (NumberFormatException ex) 95 { 96 ; 97 } 98 } 99 100 parm = getParameter("image"); 101 if (parm != null) 102 { 103 imageURL = parm; 104 } 105 106 parm = getParameter("group"); 107 if (parm != null) 108 { 109 groupName = parm; 110 } 111 112 parm = getParameter("on-contact"); 113 if (parm != null) 114 { 115 onContactURL = URLUtils.formatURL(this, parm); 116 } 117 118 parm = getParameter("on-click"); 119 if (parm != null) 120 { 121 onClickURL = URLUtils.formatURL(this, parm); 122 } 123 } 124 125 130 private void initComponents() 131 { 133 setLayout(new java.awt.BorderLayout ()); 134 135 } 137 public void destroy() 138 { 139 } 141 142 public void start() 143 { 144 146 disconnect(); connect(); 148 149 if (com == null) 151 { 152 return; 153 } 154 155 if (com.isConnected() == true) 156 { 157 StringBuffer buffer = new StringBuffer (); 158 159 if (PersistentObjects.userIdentifier != null) 160 { 161 buffer.append("Identifier:" + PersistentObjects.userIdentifier + "\n"); 162 } 163 else 164 { 165 buffer.append("Identifier:NA\n"); 166 } 167 buffer.append("Group:" + groupName + "\n"); 168 buffer.append("Document:" + getDocumentBase().toString() + "\n"); 169 170 int req_id = com.sendRequestMessage("text/plain", 171 buffer.toString()); 172 if (req_id == -1) 173 { 174 } 176 } 177 } 178 179 public void stop() 180 { 181 disconnect(); 183 184 try 185 { 186 Thread.sleep(1000); 187 } 188 catch (InterruptedException ex) 189 { 190 191 } 192 PersistentObjects.running = false; 193 } 194 195 private void disconnect() 196 { 197 if (com == null) 198 { 199 return; 200 } 201 202 if (com.isConnected() == true) 203 { 204 com.disconnect(); 205 } 206 } 207 208 private void connect() 209 { 210 try 212 { 213 com = new ServerCommunications(codeBase.getHost(), port, applicationId); 214 } 215 catch (UnknownHostException ex) 216 { 217 return; 219 } 220 221 if (com.connect() == false) 222 { 223 return; 225 } 226 227 com.setRequestListener(this); 228 com.setClosedListener(this); 229 230 com.start(); 232 } 233 234 public void messageReceived(int req_id, int status, String content_type, 235 int http_status, String reason, String message) 236 { 237 if (message != null) 238 { 239 parseMessage(message); 241 242 if (operator != null) 243 { 244 if (onContactURL != null) 245 { 246 try 247 { 248 String info = URLEncoder.encode("full-name=" 249 + URLEncoder.encode(PersistentObjects.userIdentifier)); 250 251 URL url = new URL(onContactURL.toString() 252 + "?called=" 253 + operator + "&info=" + info); 254 255 getAppletContext().showDocument(url, 256 "_blank"); 257 } 258 catch (MalformedURLException ex) 259 { 260 ; 261 } 262 } 263 } 264 } 265 } 266 267 private void parseMessage(String message) 268 { 269 StringTokenizer tokens = new StringTokenizer(message, "\n"); 270 int num_lines = tokens.countTokens(); 271 272 for (int i = 0; i < num_lines; i++) 273 { 274 String line = tokens.nextToken().trim(); 275 if (line.length() == 0) 276 { 277 continue; 278 } 279 280 StringTokenizer t = new StringTokenizer(line, ":"); 281 int n = t.countTokens(); 282 if (n < 2) 283 { 284 continue; 285 } 286 287 String key = t.nextToken().trim(); 288 String value = ""; 289 StringBuffer buffer = new StringBuffer (); 290 for (int j = 1; j < n; j++) 291 { 292 buffer.append(t.nextToken()); 293 } 294 value = buffer.toString(); 295 296 if ((key.length() == 0) || (value.length() == 0)) 297 { 298 continue; 299 } 300 301 if (key.equals("Identifier") == true) 302 { 303 if (value.equals("NA") == false) 304 { 305 PersistentObjects.userIdentifier = value; 306 } 307 } 308 else if (key.equals("Initiate-Call") == true) 309 { 310 operator = value; 311 } 312 } 313 } 314 315 public void connectionClosed(String host, int port, int appl_id) 316 { 317 } 318 319 private void drawImageButton() 320 { 321 URL url = URLUtils.formatURL(this, imageURL); 322 Image image = getImage(url); if (image == null) 324 { 325 System.err.println("The specified URL does not exist"); 326 return; 327 } 328 329 imageButton = new ImageButton(); 331 imageButton.setImage(image); 332 imageButton.addActionListener(this); 333 add(imageButton, BorderLayout.CENTER); 335 imageButton.invalidate(); 336 } 337 338 public void actionPerformed(java.awt.event.ActionEvent actionEvent) 339 { 340 if (onClickURL != null) 341 { 342 getAppletContext().showDocument(onClickURL, "_blank"); 343 } 344 else if (onContactURL != null) 345 { 346 try 347 { 348 URL url = new URL(onContactURL.toString() 349 + "?called=" 350 + groupName); 351 getAppletContext().showDocument(url, "_blank"); 352 } 353 catch (MalformedURLException ex) 354 { 355 } 356 } 357 } 358 359 362 } 363 | Popular Tags |