JAL-1807 Bob's first commit -- Applet loaded; needs image
[jalview.git] / src / javajs / util / AjaxURLConnection.java
1 package javajs.util;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.net.URL;
6 import java.net.URLConnection;
7
8 import javajs.api.JmolObjectInterface;
9
10 /**
11  * 
12  * A method to allow a JavaScript Ajax 
13  * 
14  */
15 public class AjaxURLConnection extends URLConnection {
16
17   protected AjaxURLConnection(URL url) {
18     super(url);
19   }
20
21   byte[] bytesOut;
22   String postOut = "";
23
24   /**
25    * 
26    * doAjax() is where the synchronous call to AJAX is to happen. or at least
27    * where we wait for the asynchronous call to return. This method should fill
28    * the dataIn field with either a string or byte array, or null if you want to
29    * throw an error.
30    * 
31    * url, bytesOut, and postOut are all available for use
32    * 
33    * the method is "private", but in JavaScript that can still be overloaded.
34    * Just set something to org.jmol.awtjs.JmolURLConnection.prototype.doAjax
35    * 
36    * @return file data as a javajs.util.SB or byte[] depending upon the file
37    *         type.
38    * 
39    * 
40    */
41   @SuppressWarnings("null")
42   private Object doAjax() {
43     JmolObjectInterface jmol = null;
44     /**
45      * @j2sNative
46      * 
47      *            jmol = Jmol;
48      * 
49      */
50     {
51     }
52     return jmol._doAjax(url, postOut, bytesOut);
53   }
54
55   @Override
56   public void connect() throws IOException {
57     // not expected to be used. 
58   }
59
60   public void outputBytes(byte[] bytes) {
61     //      type = "application/octet-stream;";
62     bytesOut = bytes;
63   }
64
65   public void outputString(String post) {
66     postOut = post;
67     //     type = "application/x-www-form-urlencoded";
68   }
69
70   @Override
71   public InputStream getInputStream() {
72     InputStream is = null;
73     Object o = doAjax();
74     if (AU.isAB(o))
75       is = Rdr.getBIS((byte[]) o);
76     else if (o instanceof SB) 
77       is = Rdr.getBIS(Rdr.getBytesFromSB((SB)o));
78     else if (o instanceof String)
79       is = Rdr.getBIS(((String) o).getBytes());
80     return is;
81   }
82   /**
83    * @return javajs.util.SB or byte[], depending upon the file type
84    */
85   public Object getContents() {
86     return doAjax();
87   }
88
89 }