Skip to content

Commit 7e3fa3d

Browse files
hansonrhansonr
authored andcommitted
Ajax responseCode fixes for retrieval from cache
1 parent 5c20bd6 commit 7e3fa3d

File tree

5 files changed

+71
-27
lines changed

5 files changed

+71
-27
lines changed

sources/net.sf.j2s.java.core/src/java/io/File.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1749,7 +1749,7 @@ public boolean canExecute() {
17491749
private static File generateFile(String prefix, String suffix, File dir)
17501750
throws IOException
17511751
{
1752-
long n = new Random().nextLong();
1752+
long n = new Random().nextInt(); // was nextLong()
17531753
if (n == Long.MIN_VALUE) {
17541754
n = 0; // corner case
17551755
} else {

sources/net.sf.j2s.java.core/src/java/io/FileInputStream.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,8 @@ public FileInputStream(FileDescriptor fdObj) {
193193
*/
194194
private void open(File file) throws FileNotFoundException {
195195
byte[] bytes = JSUtil.getFileAsBytes(file);
196+
if (bytes == null)
197+
throw new FileNotFoundException("Opening file " + file);
196198
file._bytes = bytes;
197199
is = new ByteArrayInputStream(bytes);
198200
}

sources/net.sf.j2s.java.core/src/java/net/HttpURLConnection.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,8 @@ public int getResponseCode() throws IOException {
479479
try {
480480
getInputStream();
481481
} catch (Exception e) {
482-
exc = e;
482+
return responseCode = HTTP_NOT_FOUND;
483+
// exc = e;
483484
}
484485

485486

sources/net.sf.j2s.java.core/src/javajs/util/AjaxURLConnection.java

Lines changed: 65 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package javajs.util;
22

33
import java.io.BufferedInputStream;
4+
import java.io.FileNotFoundException;
45
import java.io.IOException;
56
import java.io.InputStream;
67
import java.net.HttpURLConnection;
8+
import java.net.HttpsURLConnection;
79
import java.net.URL;
10+
import java.net.URLConnection;
811
import java.util.Hashtable;
912
import java.util.Map;
1013

@@ -16,6 +19,18 @@
1619
*
1720
*/
1821
public 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
}

sources/net.sf.j2s.java.core/src/javajs/util/AjaxURLStreamHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public AjaxURLStreamHandler(String protocol) {
2323

2424
@Override
2525
protected URLConnection openConnection(URL url) throws IOException {
26-
return new AjaxURLConnection(url);
26+
return AjaxURLConnection.newConnection(url);
2727
}
2828

2929

0 commit comments

Comments
 (0)