Skip to content

Commit 86a7901

Browse files
committed
JSHttpClient second draft
1 parent 409903b commit 86a7901

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

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

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,7 @@ public HttpResponse execute() throws IOException {
171171

172172
@Override
173173
public HttpResponse executeAsync(Consumer<? super HttpResponse> succeed,
174-
BiConsumer<? super HttpResponse, Throwable> fail,
175-
BiConsumer<? super HttpResponse, Throwable> always) {
174+
BiConsumer<? super HttpResponse, Throwable> fail, BiConsumer<? super HttpResponse, Throwable> always) {
176175
isAsync = (succeed != null || fail != null || always != null);
177176
this.succeed = succeed;
178177
this.fail = fail;
@@ -200,7 +199,6 @@ public void run() {
200199
return r;
201200
}
202201

203-
204202
@SuppressWarnings("resource")
205203
public Response fulfillGet() throws Exception {
206204
URI uri = getUri();
@@ -282,7 +280,7 @@ public class Response implements HttpResponse {
282280
private int state = 0;
283281

284282
ByteArrayInputStream inputStream;
285-
283+
286284
private Throwable exception;
287285

288286
/**
@@ -333,6 +331,11 @@ public void accept(byte[] t) {
333331
return this;
334332
}
335333

334+
/**
335+
* Make the proper callback, depending upon response code and exception state.
336+
*
337+
* @param ok
338+
*/
336339
protected void doCallback(boolean ok) {
337340
ok &= (exception == null);
338341
if (ok && succeed != null)
@@ -343,22 +346,13 @@ else if (!ok && fail != null)
343346
always.accept(this, exception);
344347
}
345348

346-
@Override
347-
public int getStatusCode() {
348-
try {
349-
return (state != 0 ? state : conn.getResponseCode());
350-
} catch (Throwable e) {
351-
handleError(e);
352-
return state;
353-
}
354-
}
355-
356349
/**
350+
* Handle any errors that arise in the process of processing this request.
357351
*
358352
* @param e
359353
* @return true if aSynchronous and has been handled
360354
*/
361-
private boolean handleError(Throwable e) {
355+
protected boolean handleError(Throwable e) {
362356
exception = e;
363357
// setting e = null to indicated handled.
364358
if (isAsync) {
@@ -374,6 +368,16 @@ private boolean handleError(Throwable e) {
374368
return e == null;
375369
}
376370

371+
@Override
372+
public int getStatusCode() {
373+
try {
374+
return (state != 0 ? state : conn.getResponseCode());
375+
} catch (Throwable e) {
376+
handleError(e);
377+
return state;
378+
}
379+
}
380+
377381
@Override
378382
public Map<String, String> getHeaders() {
379383
Map<String, String> headers = new LinkedHashMap<>();

0 commit comments

Comments
 (0)