cef98c8511feb5ddb2bb3d698a12da2d5ac5c3bb
[jalview.git] / src / javajs / util / AjaxURLConnection.java
1 package javajs.util;
2
3 import java.io.BufferedInputStream;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.net.URL;
7 import java.net.URLConnection;
8
9 import javajs.api.ResettableStream;
10 import javajs.api.js.J2SObjectInterface;
11
12 /**
13  * 
14  * A method to allow a JavaScript Ajax 
15  * 
16  */
17 public class AjaxURLConnection extends URLConnection {
18
19   protected AjaxURLConnection(URL url) {
20     super(url);
21   }
22
23   byte[] bytesOut;
24   String postOut = "";
25
26   /**
27    * 
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
31    * throw an error.
32    * 
33    * url, bytesOut, and postOut are all available for use
34    * 
35    * the method is "private", but in JavaScript that can still be overloaded.
36    * Just set something to org.jmol.awtjs.JmolURLConnection.prototype.doAjax
37    * 
38    * @return file data as a javajs.util.SB or byte[] depending upon the file
39    *         type.
40    * 
41    * 
42    */
43   @SuppressWarnings("null")
44   private Object doAjax(boolean isBinary) {
45     J2SObjectInterface jmol = null;
46     /**
47      * @j2sNative
48      * 
49      *            jmol = J2S || Jmol;
50      * 
51      */
52     {
53     }
54     return jmol._doAjax(url, postOut, bytesOut, isBinary);
55   }
56
57   @Override
58   public void connect() throws IOException {
59     // not expected to be used. 
60   }
61
62   public void outputBytes(byte[] bytes) {
63     //      type = "application/octet-stream;";
64     bytesOut = bytes;
65   }
66
67   public void outputString(String post) {
68     postOut = post;
69     //     type = "application/x-www-form-urlencoded";
70   }
71
72         @Override
73         public InputStream getInputStream() {
74                 BufferedInputStream bis = getAttachedStreamData(url);
75                 if (bis != null)
76                         return bis;
77                 Object o = doAjax(true);
78                 return (
79                                 AU.isAB(o) ? Rdr.getBIS((byte[]) o) 
80                                 : o instanceof SB ? Rdr.getBIS(Rdr.getBytesFromSB((SB) o)) 
81                                 : o instanceof String ? Rdr.getBIS(((String) o).getBytes()) 
82                                 : bis
83                 );
84         }
85   @SuppressWarnings("unused")
86         /**
87          * J2S will attach a BufferedInputStream to any URL that is 
88          * retrieved using a ClassLoader. This improves performance by
89          * not going back to the server every time a second time, since
90          * the first time in Java is usually just to see if it exists. 
91          * 
92          * This stream can be re-used, but it has to be reset. Java for some 
93          * reason does not allow  a BufferedInputStream to fully reset its 
94          * inner streams. We enable that by force-casting the stream as a 
95          * javax.io stream and then applying resetStream() to that. 
96          * 
97          * 
98          * @param url
99          * @return
100          */
101         public static BufferedInputStream getAttachedStreamData(URL url) {
102                 BufferedInputStream bis = null;
103                 /**
104                  * @j2sNative
105                  * 
106                  *            bis = url._streamData;
107                  */
108                 {
109                 }
110                 if (bis != null)
111                         ((ResettableStream) (Object) bis).resetStream();
112                 return bis;
113         }
114
115         /**
116    * @return javajs.util.SB or byte[], depending upon the file type
117    */
118   public Object getContents() {
119     return doAjax(false);
120   }
121
122 }