Skip to content

Commit 15afab0

Browse files
hansonrhansonr
authored andcommitted
Proper return of HTTP response codes
1 parent 2f26ce2 commit 15afab0

File tree

4 files changed

+91
-73
lines changed

4 files changed

+91
-73
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ public String getRequestMethod() {
465465
*/
466466
public int getResponseCode() throws IOException {
467467
/*
468-
* We're got the response code already
468+
* We've got the response code already
469469
*/
470470
if (responseCode != -1) {
471471
return responseCode;

sources/net.sf.j2s.java.core/src/javajs/http/SimpleHttpClient.java

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -490,10 +490,7 @@ Response getResponse(HttpURLConnection conn, Request request) throws IOException
490490
return this;
491491
}
492492

493-
new Thread(new Runnable() {
494-
495-
@Override
496-
public void run() {
493+
new Thread(() -> {
497494
// asynchronous methods cannot throw an exception.
498495
IOException exception = null;
499496
if (method.equals(HttpRequest.METHOD_HEAD)) {
@@ -502,21 +499,26 @@ public void run() {
502499
} catch (IOException e) {
503500
exception = e;
504501
}
505-
doCallback(state == 0 || state >= 400, exception);
502+
doCallback(exception);
506503
} else {
507-
@SuppressWarnings("unused")
508-
Function<byte[], Void> f = new Function<byte[], Void>() {
509-
510-
@Override
511-
public Void apply(byte[] t) {
512-
doCallback(t != null, null);
513-
return null;
514-
}
515-
516-
};
517-
/** @j2sNative conn.getBytesAsync$(); */
504+
@SuppressWarnings("unused")
505+
Function<byte[], Void> f = new Function<byte[], Void>() {
506+
507+
@Override
508+
public Void apply(byte[] t) {
509+
state = 400; // Bad Request?
510+
try {
511+
state = SimpleHttpClient.Response.this.conn.getResponseCode();
512+
} catch (IOException e) {
513+
}
514+
doCallback(null);
515+
return null;
516+
}
517+
518+
};
519+
/** @j2sNative conn.getBytesAsync$java_util_function_Function(f) */
518520
}
519-
}
521+
520522
}).start();
521523
return this;
522524
}
@@ -527,8 +529,8 @@ public Void apply(byte[] t) {
527529
* @param ok
528530
* @param e
529531
*/
530-
protected void doCallback(boolean ok, IOException e) {
531-
ok &= (e == null);
532+
protected void doCallback(IOException e) {
533+
boolean ok = (e == null && state < 400);
532534
if (ok && succeed != null)
533535
succeed.accept(this);
534536
else if (!ok && fail != null)

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

Lines changed: 57 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -196,61 +196,65 @@ private Object doAjax(boolean isBinary, Function<Object, Void> whenDone) {
196196

197197
Object result;
198198
String myURL = url.toString();
199-
boolean isEmpty = false;
200199
if (myURL.startsWith("file:/TEMP/")) {
201-
202200
result = jsutil.getCachedBytes(myURL);
203-
isEmpty = (result == null);
201+
boolean isEmpty = (result == null);
204202
if (whenDone != null) {
205203
whenDone.apply(isEmpty ? null : result);
206204
return null;
207205
}
208206
responseCode = (isEmpty ? HTTP_NOT_FOUND : HTTP_ACCEPTED);
209-
} else {
210-
if (myURL.startsWith("file:")) {
211-
String j2s = /** @j2sNative Clazz._Loader.getJ2SLibBase() || */
212-
null;
213-
if (myURL.startsWith("file:/./")) {
214-
// file:/./xxxx
215-
myURL = j2s + myURL.substring(7);
216-
} else if (myURL.startsWith("file:/" + j2s)) {
217-
// from classLoader
218-
myURL = myURL.substring(6);
207+
return result;
208+
}
209+
if (myURL.startsWith("file:")) {
210+
String j2s = /** @j2sNative Clazz._Loader.getJ2SLibBase() || */
211+
null;
212+
if (myURL.startsWith("file:/./")) {
213+
// file:/./xxxx
214+
myURL = j2s + myURL.substring(7);
215+
} else if (myURL.startsWith("file:/" + j2s)) {
216+
// from classLoader
217+
myURL = myURL.substring(6);
218+
} else {
219+
String base = getFileDocumentDir();
220+
if (base != null && myURL.indexOf(base) == 0) {
221+
myURL = myURL.substring(base.length());
219222
} else {
220-
String base = getFileDocumentDir();
221-
if (base != null && myURL.indexOf(base) == 0) {
222-
myURL = myURL.substring(base.length());
223-
} else {
224-
URL path = jsutil.getCodeBase();
225-
if (path != null) {
226-
j2s = path.toString();
227-
if (myURL.indexOf(j2s) >= 0)
228-
myURL = path + myURL.split(j2s)[1];
229-
else
230-
myURL = path + myURL.substring(5);
231-
}
223+
URL path = jsutil.getCodeBase();
224+
if (path != null) {
225+
j2s = path.toString();
226+
if (myURL.indexOf(j2s) >= 0)
227+
myURL = path + myURL.split(j2s)[1];
228+
else
229+
myURL = path + myURL.substring(5);
232230
}
233231
}
234232
}
235-
result = J2S.doAjax(myURL, postOut, bytesOut, info);
236-
if (whenDone != null)
237-
return null;
238-
// the problem is that jsmol.php is still returning crlf even if output is 0
239-
// bytes
240-
// and it is not passing through the not-found state, just 200
241-
/**
242-
* @j2sNative
243-
*
244-
* isEmpty = (!result || result.length == 2 && result[0] == 13 &&
245-
* result[1] == 10); if (isEmpty) result = new Int8Array;
246-
*/
247-
248-
responseCode = isEmpty ? HTTP_NOT_FOUND : /** @j2sNative info.xhr.status || */
249-
0;
250233
}
234+
result = J2S.doAjax(myURL, postOut, bytesOut, info);
235+
if (whenDone != null)
236+
return null;
237+
setJQueryResponseCodeFromJQuery(result);
251238
return result;
252239
}
253240

241+
private void setJQueryResponseCodeFromJQuery(Object result) {
242+
// the problem is that jsmol.php is still returning crlf even if output is 0
243+
// bytes
244+
// and it is not passing through the not-found state, just 200
245+
Object info = this.info;
246+
boolean isEmpty = false;
247+
/**
248+
* @j2sNative
249+
*
250+
* isEmpty = (!result || result.length == 2 && result[0] == 13 &&
251+
* result[1] == 10); if (isEmpty) result = new Int8Array;
252+
*/
253+
254+
responseCode = isEmpty ? HTTP_NOT_FOUND : /** @j2sNative info.xhr.status || */
255+
0;
256+
}
257+
254258
private String getFileDocumentDir() {
255259
String base = jsutil.getDocumentBase().getPath();
256260
int pt = base.lastIndexOf("/");
@@ -386,7 +390,7 @@ public OutputStream getOutputStream() throws IOException {
386390
@SuppressWarnings({ "null", "unused" })
387391
@Override
388392
public InputStream getInputStream() throws FileNotFoundException {
389-
InputStream is = /** @j2sNative this.is || */null;
393+
BufferedInputStream is = /** @j2sNative this.is || */null;
390394
if (is != null)
391395
return is;
392396
responseCode = -1;
@@ -404,8 +408,7 @@ public void getBytesAsync(Function<byte[], Void> whenDone) {
404408
public Void apply(InputStream is) {
405409
try {
406410
if (is != null) {
407-
byte[] bytes = /** @j2sNative is.readAllBytes$() || */null;
408-
whenDone.apply(bytes);
411+
whenDone.apply(/** @j2sNative is.readAllBytes$() || */null);
409412
return null;
410413
}
411414
} catch (Exception e) {
@@ -420,8 +423,9 @@ public Void apply(InputStream is) {
420423

421424
@SuppressWarnings({ "null", "unused" })
422425
private void getInputStreamAsync(Function<InputStream, Void> whenDone) {
423-
InputStream is = /** @j2sNative is = this.is || */null;
426+
BufferedInputStream is = /** @j2sNative is = this.is || */null;
424427
if (is != null) {
428+
isNetworkError(is);
425429
whenDone.apply(is);
426430
return;
427431
}
@@ -438,12 +442,13 @@ private void getInputStreamAndResponseAsync(Function<InputStream, Void> whenDone
438442
doAjax(true, new Function<Object, Void>() {
439443

440444
@Override
441-
public Void apply(Object data) {
442-
if (data instanceof String) {
445+
public Void apply(Object response) {
446+
if (response instanceof String) {
443447
whenDone.apply(null);
444448
return null;
445449
}
446-
BufferedInputStream is = attachStreamData(url, data);
450+
setJQueryResponseCodeFromJQuery(response);
451+
BufferedInputStream is = attachStreamData(url, response);
447452
if (doCache() && is != null) {
448453
isNetworkError(is);
449454
setCachedStream();
@@ -457,7 +462,7 @@ public Void apply(Object data) {
457462
});
458463
}
459464

460-
private InputStream getInputStreamAndResponse(boolean allowNWError) {
465+
private BufferedInputStream getInputStreamAndResponse(boolean allowNWError) {
461466
BufferedInputStream is = getAttachedStreamData(url, false);
462467
if (is != null || doCache() && (is = getCachedStream(allowNWError)) != null) {
463468
return is;
@@ -468,8 +473,7 @@ private InputStream getInputStreamAndResponse(boolean allowNWError) {
468473
setCachedStream();
469474
return is;
470475
}
471-
if (!isNetworkError(is)) {
472-
}
476+
isNetworkError(is);
473477
return is;
474478
}
475479

@@ -548,6 +552,9 @@ private String getCacheKey() {
548552
@SuppressWarnings("unused")
549553
private boolean isNetworkError(BufferedInputStream is) {
550554
if (is != null) {
555+
if (responseCode > 0) {
556+
return (responseCode >= 400);
557+
}
551558
responseCode = HTTP_OK;
552559
if (/** @j2sNative is._jsonData || */
553560
false)

sources/net.sf.j2s.java.core/src/test/Test_HTTP.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ public static void main(String[] args) {
2828
HttpResponse resp = req.execute();
2929

3030
System.out.println(resp);
31-
System.out.println(resp.getText());
31+
String res = resp.getText();
32+
System.out.println(res);
33+
System.out.println(resp.getStatusCode());
3234

3335
req = client.put(new URI("https://www.compbio.dundee.ac.uk/slivka/api/services/example"));
3436

@@ -39,22 +41,29 @@ public static void main(String[] args) {
3941
resp = req.execute();
4042

4143
System.out.println(resp);
42-
System.out.println(resp.getText());
44+
res = resp.getText();
45+
System.out.println(res);
46+
System.out.println(resp.getStatusCode());
4347

48+
System.out.println("Testing async");
4449
req.addFormPart("testing", "here");
4550
req.executeAsync(new Consumer<HttpResponse>() {
4651

4752
@Override
4853
public void accept(HttpResponse t) {
4954
try {
50-
System.out.println(t.getText());
55+
String res = t.getText();
56+
System.out.println("Testing async...returned:");
57+
System.out.println(res);
58+
System.out.println(t.getStatusCode());
5159
} catch (IOException e) {
5260
// TODO Auto-generated catch block
5361
e.printStackTrace();
5462
}
5563
}
5664

5765
}, null, null);
66+
System.out.println("Testing async...submitted...");
5867

5968

6069
} catch (Exception e3) {

0 commit comments

Comments
 (0)