11package javajs .util ;
22
33import java .io .BufferedInputStream ;
4+ import java .io .FileNotFoundException ;
45import java .io .IOException ;
56import java .io .InputStream ;
67import java .net .HttpURLConnection ;
8+ import java .net .HttpsURLConnection ;
79import java .net .URL ;
10+ import java .net .URLConnection ;
811import java .util .Hashtable ;
912import java .util .Map ;
1013
1619 *
1720 */
1821public class AjaxURLConnection extends HttpURLConnection {
22+
23+ public static class AjaxHttpsURLConnection extends AjaxURLConnection implements HttpsURLConnection {
24+
25+ protected AjaxHttpsURLConnection (URL url ) {
26+ super (url );
27+ }
28+
29+ }
30+
31+ public static URLConnection newConnection (URL url ) {
32+ return (url .getProtocol () == "https" ? new AjaxHttpsURLConnection (url ) : new AjaxURLConnection (url ));
33+ }
1934
2035 protected AjaxURLConnection (URL url ) {
2136 super (url );
@@ -91,18 +106,25 @@ public void outputString(String post) {
91106 }
92107
93108 @ Override
94- public InputStream getInputStream () {
109+ public InputStream getInputStream () throws FileNotFoundException {
95110 responseCode = -1 ;
96- return getInputStreamAndResponse (url , false );
111+ InputStream is = getInputStreamAndResponse (url , false );
112+ if (is == null )
113+ throw new FileNotFoundException ("opening " + url );
114+ return is ;
97115 }
98116
99117 private InputStream getInputStreamAndResponse (URL url , boolean allowNWError ) {
100118 BufferedInputStream is = getAttachedStreamData (url , false );
101- if (is != null || getUseCaches () && (is = getCachedStream (url , allowNWError )) != null )
119+ if (is != null || getUseCaches () && (is = getCachedStream (url , allowNWError )) != null ) {
102120 return is ;
121+ }
103122 is = attachStreamData (url , doAjax (ajax == null ));
104- if (getUseCaches () && is != null )
123+ if (getUseCaches () && is != null ) {
124+ isNetworkError (is );
105125 setCachedStream (url );
126+ return is ;
127+ }
106128 isNetworkError (is );
107129 return is ;
108130 }
@@ -115,45 +137,54 @@ private BufferedInputStream getCachedStream(URL url, boolean allowNWError) {
115137 return null ;
116138 boolean isAjax = /** @j2sNative url.ajax || */ false ;
117139 BufferedInputStream bis = getBIS (data , isAjax );
118- return (allowNWError || !isNetworkError (bis ) ? bis : null );
140+ return (!isNetworkError (bis ) || allowNWError ? bis : null );
119141 }
120142
121- private static BufferedInputStream getBIS (Object data , boolean isAjax ) {
143+ private static BufferedInputStream getBIS (Object data , boolean isJSON ) {
122144 if (data == null )
123145 return null ;
124- if (!isAjax )
146+ if (!isJSON )
125147 return Rdr .toBIS (data );
126148 BufferedInputStream bis = Rdr .toBIS ("" );
127149 /**
128150 * @j2sNative
129151 *
130- * bis._ajaxData = data;
152+ * bis._jsonData = data;
131153 */
132154 return bis ;
133155 }
134156
135157 private void setCachedStream (URL url ) {
136158 Object data = url ._streamData ;
137- if (data != null )
159+ if (data != null ) {
160+ int code = this .responseCode ;
161+ /**
162+ * @j2sNative data._responseCode = code;
163+ */
138164 urlCache .put (url .toString (), data );
165+ }
139166 }
140167
141168 @ SuppressWarnings ("unused" )
142169 private boolean isNetworkError (BufferedInputStream is ) {
143- if (/** @j2sNative is._ajaxData || */ false )
144- return false ;
145- is .mark (15 );
146- byte [] bytes = new byte [13 ];
147- try {
148- is .read (bytes );
149- is .reset ();
150- for (int i = NETWORK_ERROR .length ; --i >= 0 ;)
151- if (bytes [i ] != NETWORK_ERROR [i ])
152- return false ;
153- } catch (IOException e ) {
170+ if (is != null ) {
171+ responseCode = HTTP_OK ;
172+ if (/** @j2sNative is._jsonData || */
173+ false )
174+ return false ;
175+ is .mark (15 );
176+ byte [] bytes = new byte [13 ];
177+ try {
178+ is .read (bytes );
179+ is .reset ();
180+ for (int i = NETWORK_ERROR .length ; --i >= 0 ;)
181+ if (bytes [i ] != NETWORK_ERROR [i ])
182+ return false ;
183+ } catch (IOException e ) {
184+ }
154185 }
155186 responseCode = HTTP_NOT_FOUND ;
156- return true ;
187+ return true ;
157188 }
158189
159190 final private static int [] NETWORK_ERROR = new int [] { 78 , 101 , 116 , 119 , 111 , 114 , 107 , 69 , 114 , 114 , 111 , 114 };
@@ -171,14 +202,14 @@ private boolean isNetworkError(BufferedInputStream is) {
171202 public static BufferedInputStream getAttachedStreamData (URL url , boolean andDelete ) {
172203
173204 Object data = null ;
174- boolean isAjax = false ;
205+ boolean isJSON = false ;
175206 /**
176207 * @j2sNative
177208 * data = url._streamData;
178209 * if (andDelete) url._streamData = null;
179- * isAjax = (data && url.ajax && url.ajax.dataType == "json")
210+ * isJSON = (data && url.ajax && url.ajax.dataType == "json")
180211 */
181- return getBIS (data , isAjax );
212+ return getBIS (data , isJSON );
182213 }
183214
184215 /**
@@ -233,4 +264,14 @@ public boolean usingProxy() {
233264 return false ;
234265}
235266
267+ @ Override
268+ public int getContentLength () {
269+ try {
270+ InputStream is = getInputStream ();
271+ return is .available ();
272+ } catch (IOException e ) {
273+ return -1 ;
274+ }
275+ }
276+
236277}
0 commit comments