3dcb3cdb9be2befc7fc3d59343e63514f8a676b3
[jalview.git] / srcjar / 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.HttpURLConnection;
7 import java.net.URL;
8 import java.net.URLConnection;
9
10 import javajs.api.js.J2SObjectInterface;
11
12 /**
13  * 
14  * A method to allow a JavaScript Ajax 
15  * 
16  */
17 public class AjaxURLConnection extends HttpURLConnection {
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    * 
39    * @param isBinary 
40    * 
41    * @return file data as a javajs.util.SB or byte[] depending upon the file
42    *         type.
43    * 
44    * 
45    */
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);
50   }
51
52   @Override
53   public void connect() throws IOException {
54     // not expected to be used. 
55   }
56
57   public void outputBytes(byte[] bytes) {
58     //      type = "application/octet-stream;";
59     bytesOut = bytes;
60   }
61
62   public void outputString(String post) {
63     postOut = post;
64     //     type = "application/x-www-form-urlencoded";
65   }
66
67         @Override
68         public InputStream getInputStream() {
69                 BufferedInputStream is = getAttachedStreamData(url, false);
70                 return (is == null ? attachStreamData(url, doAjax(true)) : is);
71         }
72
73         /**
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. 
78          * 
79          * @param url
80          * @return String, SB, or byte[]
81          */
82         public static BufferedInputStream getAttachedStreamData(URL url, boolean andDelete) {
83         
84                 Object data = null;
85                 /**
86                  * @j2sNative
87                  * 
88                  *       data = url._streamData;
89                  *       if (andDelete) url._streamData = null;
90                  */
91                 return (data == null ? null : Rdr.toBIS(data));
92         }
93
94    public static BufferedInputStream attachStreamData(URL url, Object o) {
95            /**
96             * @j2sNative
97             * 
98             *   url._streamData = o;
99             */
100            
101             return (o == null ? null : Rdr.toBIS(o));
102   }
103
104   /**
105    * @return javajs.util.SB or byte[], depending upon the file type
106    */
107   public Object getContents() {
108     return doAjax(false);
109   }
110
111   @Override
112   public int getResponseCode() throws IOException {
113       /*
114        * We're got the response code already
115        */
116       if (responseCode != -1) {
117           return responseCode;
118       }
119
120       /*
121        * Ensure that we have connected to the server. Record
122        * exception as we need to re-throw it if there isn't
123        * a status line.
124        */
125       Exception exc = null;
126       try {
127           BufferedInputStream is = (BufferedInputStream) getInputStream();
128           if (is.available() > 40)
129                 return responseCode = HTTP_OK;
130           is.mark(15);
131           byte[] bytes = new byte[13];
132           is.read(bytes);
133           is.reset();
134           String s = new String(bytes);
135           if (s.startsWith("Network Error"))
136                 return responseCode = HTTP_NOT_FOUND;
137       } catch (Exception e) {
138           exc = e;
139       }      
140         return responseCode = HTTP_INTERNAL_ERROR;
141   }
142 @Override
143 public void disconnect() {
144         // TODO Auto-generated method stub
145         
146 }
147
148 @Override
149 public boolean usingProxy() {
150         // TODO Auto-generated method stub
151         return false;
152 }
153
154 }