1 package org.lobobrowser.html.js; 2 3 import org.w3c.dom.Document ; 4 import org.lobobrowser.html.*; 5 import org.lobobrowser.js.*; 6 import org.mozilla.javascript.*; 7 import java.util.logging.*; 8 9 public class XMLHttpRequest extends AbstractScriptableDelegate { 10 private static final Logger logger = Logger.getLogger(XMLHttpRequest.class.getName()); 11 private final HttpRequest request; 12 private final UserAgentContext pcontext; 13 private final Scriptable scope; 14 private final java.net.URL codeSource; 15 16 public XMLHttpRequest(UserAgentContext pcontext, java.net.URL codeSource, Scriptable scope) { 17 this.request = pcontext.createHttpRequest(); 18 this.pcontext = pcontext; 19 this.scope = scope; 20 this.codeSource = codeSource; 21 } 22 23 public void abort() { 24 request.abort(); 25 } 26 27 public String getAllResponseHeaders() { 28 return request.getAllResponseHeaders(); 29 } 30 31 public int getReadyState() { 32 return request.getReadyState(); 33 } 34 35 public byte[] getResponseBytes() { 36 return request.getResponseBytes(); 37 } 38 39 public String getResponseHeader(String headerName) { 40 return request.getResponseHeader(headerName); 41 } 42 43 public String getResponseText() { 44 return request.getResponseText(); 45 } 46 47 public Document getResponseXML() { 48 return request.getResponseXML(); 49 } 50 51 public int getStatus() { 52 return request.getStatus(); 53 } 54 55 public String getStatusText() { 56 return request.getStatusText(); 57 } 58 59 public void open(String method, String url, boolean asyncFlag, String userName, String password) { 60 request.open(method, url, asyncFlag, userName, password); 61 } 62 63 public void open(String method, String url, boolean asyncFlag, String userName) { 64 request.open(method, url, asyncFlag, userName); 65 } 66 67 public void open(String method, String url, boolean asyncFlag) { 68 request.open(method, url, asyncFlag); 69 } 70 71 public void open(String method, String url) { 72 request.open(method, url); 73 } 74 75 private Function onreadystatechange; 76 private boolean listenerAdded; 77 78 public Function getOnreadystatechange() { 79 synchronized(this) { 80 return this.onreadystatechange; 81 } 82 } 83 84 public void setOnreadystatechange(final Function value) { 85 synchronized(this) { 86 this.onreadystatechange = value; 87 if(value != null && !this.listenerAdded) { 88 this.request.addReadyStateChangeListener(new ReadyStateChangeListener() { 89 public void readyStateChanged() { 90 java.awt.EventQueue.invokeLater(new Runnable () { 91 public void run() { 92 executeReadyStateChange(); 93 } 94 }); 95 } 96 }); 97 this.listenerAdded = true; 98 } 99 } 100 } 101 102 private void executeReadyStateChange() { 103 try { 105 Function f = XMLHttpRequest.this.getOnreadystatechange(); 106 if(f != null) { 107 Context ctx = Executor.createContext(this.codeSource, this.pcontext); 108 try { 109 Scriptable newScope = (Scriptable) JavaScript.getInstance().getJavascriptObject(XMLHttpRequest.this, this.scope); 110 f.call(ctx, newScope, newScope, new Object [0]); 111 } finally { 112 Context.exit(); 113 } 114 } 115 } catch(Exception err) { 116 logger.log(Level.WARNING, "Error processing ready state change.", err); 117 } 118 } 119 120 } 121 122 | Popular Tags |