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