Skip to content

Commit db66db4

Browse files
committed
JSHttpClient second draft
1 parent f6e44ba commit db66db4

File tree

1 file changed

+82
-35
lines changed

1 file changed

+82
-35
lines changed

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

Lines changed: 82 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -24,40 +24,26 @@
2424
* Note that none of this code should ever be run in Java. Java implementations
2525
* should use their own implementations.
2626
*
27+
* Can be initialized either directly or via HttpClientFactory.getDefault().
2728
*
2829
* For more details on HTTP methods see:
2930
* https://www.w3schools.com/tags/ref_httpmethods.asp
31+
*
32+
* For more information about FormData, see:
33+
* https://developer.mozilla.org/en-US/docs/Web/API/FormData/Using_FormData_Objects
34+
*
35+
* The JSHttpClient is very light -- just a constructor and five methods,
36+
* providing limited access to HttpRequest and HttpResponse implementations.
37+
*
38+
*
39+
* @author Bob Hanson
40+
* @author Mateusz Warowny
41+
*
3042
*/
3143
public class JSHttpClient implements HttpClient {
3244

33-
/**
34-
* Template class implementing addFormData. Added here to allow this class to be
35-
* placed in Java projects without There is no need to maintain the actual
36-
* javajs.util.AjaxURLConnection class. Basically an interface with all the body
37-
* methods of an HttpURLConnection or HttpsURLConnection.
38-
*
39-
* Can be initialized either directly or via HttpClientFactory.getDefault().
40-
*
41-
* @author hansonr
42-
*
43-
*/
44-
public abstract class AjaxURLConnection extends HttpURLConnection {
45-
46-
protected AjaxURLConnection(URL u) {
47-
super(u);
48-
}
49-
50-
public abstract void addFormData(String name, Object value, String contentType, String fileName);
51-
52-
public abstract void getBytesAsync(Consumer<byte[]> whenDone);
53-
54-
}
55-
56-
public static HttpClient create() {
57-
return new JSHttpClient();
58-
}
59-
6045
public JSHttpClient() {
46+
// for reflection
6147
}
6248

6349
// no OPTIONS, no TRACE
@@ -88,28 +74,90 @@ public HttpRequest delete(URI uri) {
8874
return new Request(uri, "DELETE");
8975
}
9076

77+
/**
78+
* Template class implementing just two abstract methods.
79+
*
80+
* Added here to allow this class to be placed in Java projects without the need
81+
* to maintain the actual javajs.util.AjaxURLConnection class itself.
82+
*
83+
* Basically an interface with all the body methods of an HttpURLConnection or
84+
* HttpsURLConnection.
85+
*
86+
* The AjaxURLConnection class handles all the necessary work of creating blobs,
87+
* adding them to a FormData object, and firing off a synchronous or
88+
* asynchronous jQuery.ajax() call.
89+
*
90+
* @author hansonr
91+
*
92+
*/
93+
public abstract class AjaxURLConnection extends HttpURLConnection {
94+
95+
protected AjaxURLConnection(URL u) {
96+
super(u);
97+
}
98+
99+
public abstract void addFormData(String name, Object value, String contentType, String fileName);
100+
101+
public abstract void getBytesAsync(Consumer<byte[]> whenDone);
102+
103+
}
104+
91105
public class Request implements HttpRequest {
92106

107+
/**
108+
* the source URI
109+
*/
93110
private URI uri;
111+
112+
/**
113+
* the HTTP(S)URLConnection that will handle this request, actually
114+
* javajs.util.AjaxURLConnection
115+
*/
116+
117+
AjaxURLConnection conn;
118+
119+
/**
120+
* GET, HEAD, POST, PUT, or DELETE
121+
*/
94122
private String method;
95123

124+
/**
125+
* headers, mostly ignored in SwingJS and AJAX
126+
*
127+
*/
96128
private Map<String, String> htHeaders = new HashMap<>();
129+
130+
/**
131+
* GET and POST data
132+
*/
97133
private Map<String, String> htGetParams = new HashMap<>();
98134
private List<Object[]> listPostFiles = new ArrayList<>();
99135

136+
/**
137+
* asynchronous callback functions
138+
*
139+
*/
100140
private Consumer<? super HttpResponse> succeed;
101141
private BiConsumer<? super HttpResponse, Throwable> fail;
102142
private BiConsumer<? super HttpResponse, Throwable> always;
103143

104144
/**
105-
* When TRUE, all parameters and files are transmitted as binary data in
106-
* multipart/form-data format.
145+
* set TRUE whenever at least one of succeed, fail, or always, is non-null
146+
*/
147+
private boolean isAsync;
148+
149+
/**
150+
* when TRUE, all parameters and files are transmitted as binary data in
151+
* multipart/form-data format
107152
*/
108153
private boolean hasFormBody = false;
154+
155+
/**
156+
* when FALSE (for HEAD only), no input stream may be opened. The only valid
157+
* call will be getResponseCode()
158+
*/
109159
private boolean allowInputStream = true;
110-
private boolean isAsync;
111160

112-
AjaxURLConnection conn;
113161

114162
public Request(URI uri, String method) {
115163
this.uri = uri;
@@ -171,8 +219,7 @@ public HttpResponse execute() throws IOException {
171219

172220
@Override
173221
public HttpResponse executeAsync(Consumer<? super HttpResponse> succeed,
174-
BiConsumer<? super HttpResponse, Throwable> fail,
175-
BiConsumer<? super HttpResponse, Throwable> always) {
222+
BiConsumer<? super HttpResponse, Throwable> fail, BiConsumer<? super HttpResponse, Throwable> always) {
176223
isAsync = (succeed != null || fail != null || always != null);
177224
this.succeed = succeed;
178225
this.fail = fail;
@@ -200,7 +247,6 @@ public void run() {
200247
return r;
201248
}
202249

203-
204250
@SuppressWarnings("resource")
205251
public Response fulfillGet() throws Exception {
206252
URI uri = getUri();
@@ -282,7 +328,7 @@ public class Response implements HttpResponse {
282328
private int state = 0;
283329

284330
ByteArrayInputStream inputStream;
285-
331+
286332
private Throwable exception;
287333

288334
/**
@@ -335,6 +381,7 @@ public void accept(byte[] t) {
335381

336382
/**
337383
* Make the proper callback, depending upon response code and exception state.
384+
*
338385
* @param ok
339386
*/
340387
protected void doCallback(boolean ok) {

0 commit comments

Comments
 (0)