3 import java.io.BufferedInputStream;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.net.HttpURLConnection;
8 import java.net.URLConnection;
10 import javajs.api.js.J2SObjectInterface;
14 * A method to allow a JavaScript Ajax
17 public class AjaxURLConnection extends HttpURLConnection {
19 protected AjaxURLConnection(URL url) {
28 * doAjax() is where the synchronous call to AJAX is to happen. or at least
29 * where we wait for the asynchronous call to return. This method should fill
30 * the dataIn field with either a string or byte array, or null if you want to
33 * url, bytesOut, and postOut are all available for use
35 * the method is "private", but in JavaScript that can still be overloaded.
36 * Just set something to org.jmol.awtjs.JmolURLConnection.prototype.doAjax
41 * @return file data as a javajs.util.SB or byte[] depending upon the file
46 @SuppressWarnings("null")
47 private Object doAjax(boolean isBinary) {
48 J2SObjectInterface J2S = /** @j2sNative self.J2S || */ null;
49 return J2S.doAjax(url.toString(), postOut, bytesOut, isBinary);
53 public void connect() throws IOException {
54 // not expected to be used.
57 public void outputBytes(byte[] bytes) {
58 // type = "application/octet-stream;";
62 public void outputString(String post) {
64 // type = "application/x-www-form-urlencoded";
68 public InputStream getInputStream() {
69 BufferedInputStream is = getAttachedStreamData(url, false);
70 return (is == null ? attachStreamData(url, doAjax(true)) : is);
74 * J2S will attach the data (String, SB, or byte[]) to any URL that is
75 * retrieved using a ClassLoader. This improves performance by
76 * not going back to the server every time a second time, since
77 * the first time in Java is usually just to see if it exists.
80 * @return String, SB, or byte[]
82 public static BufferedInputStream getAttachedStreamData(URL url, boolean andDelete) {
88 * data = url._streamData;
89 * if (andDelete) url._streamData = null;
91 return (data == null ? null : Rdr.toBIS(data));
94 public static BufferedInputStream attachStreamData(URL url, Object o) {
98 * url._streamData = o;
101 return (o == null ? null : Rdr.toBIS(o));
105 * @return javajs.util.SB or byte[], depending upon the file type
107 public Object getContents() {
108 return doAjax(false);
112 public int getResponseCode() throws IOException {
114 * We're got the response code already
116 if (responseCode != -1) {
121 * Ensure that we have connected to the server. Record
122 * exception as we need to re-throw it if there isn't
125 Exception exc = null;
127 BufferedInputStream is = (BufferedInputStream) getInputStream();
128 if (is.available() > 40)
129 return responseCode = HTTP_OK;
131 byte[] bytes = new byte[13];
134 String s = new String(bytes);
135 if (s.startsWith("Network Error"))
136 return responseCode = HTTP_NOT_FOUND;
137 } catch (Exception e) {
140 return responseCode = HTTP_INTERNAL_ERROR;
143 public void disconnect() {
144 // TODO Auto-generated method stub
149 public boolean usingProxy() {
150 // TODO Auto-generated method stub