|
24 | 24 | * Note that none of this code should ever be run in Java. Java implementations |
25 | 25 | * should use their own implementations. |
26 | 26 | * |
| 27 | + * Can be initialized either directly or via HttpClientFactory.getDefault(). |
27 | 28 | * |
28 | 29 | * For more details on HTTP methods see: |
29 | 30 | * 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 | + * |
30 | 42 | */ |
31 | 43 | public class JSHttpClient implements HttpClient { |
32 | 44 |
|
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 | | - |
60 | 45 | public JSHttpClient() { |
| 46 | + // for reflection |
61 | 47 | } |
62 | 48 |
|
63 | 49 | // no OPTIONS, no TRACE |
@@ -88,28 +74,90 @@ public HttpRequest delete(URI uri) { |
88 | 74 | return new Request(uri, "DELETE"); |
89 | 75 | } |
90 | 76 |
|
| 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 | + |
91 | 105 | public class Request implements HttpRequest { |
92 | 106 |
|
| 107 | + /** |
| 108 | + * the source URI |
| 109 | + */ |
93 | 110 | 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 | + */ |
94 | 122 | private String method; |
95 | 123 |
|
| 124 | + /** |
| 125 | + * headers, mostly ignored in SwingJS and AJAX |
| 126 | + * |
| 127 | + */ |
96 | 128 | private Map<String, String> htHeaders = new HashMap<>(); |
| 129 | + |
| 130 | + /** |
| 131 | + * GET and POST data |
| 132 | + */ |
97 | 133 | private Map<String, String> htGetParams = new HashMap<>(); |
98 | 134 | private List<Object[]> listPostFiles = new ArrayList<>(); |
99 | 135 |
|
| 136 | + /** |
| 137 | + * asynchronous callback functions |
| 138 | + * |
| 139 | + */ |
100 | 140 | private Consumer<? super HttpResponse> succeed; |
101 | 141 | private BiConsumer<? super HttpResponse, Throwable> fail; |
102 | 142 | private BiConsumer<? super HttpResponse, Throwable> always; |
103 | 143 |
|
104 | 144 | /** |
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 |
107 | 152 | */ |
108 | 153 | 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 | + */ |
109 | 159 | private boolean allowInputStream = true; |
110 | | - private boolean isAsync; |
111 | 160 |
|
112 | | - AjaxURLConnection conn; |
113 | 161 |
|
114 | 162 | public Request(URI uri, String method) { |
115 | 163 | this.uri = uri; |
@@ -171,8 +219,7 @@ public HttpResponse execute() throws IOException { |
171 | 219 |
|
172 | 220 | @Override |
173 | 221 | 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) { |
176 | 223 | isAsync = (succeed != null || fail != null || always != null); |
177 | 224 | this.succeed = succeed; |
178 | 225 | this.fail = fail; |
@@ -200,7 +247,6 @@ public void run() { |
200 | 247 | return r; |
201 | 248 | } |
202 | 249 |
|
203 | | - |
204 | 250 | @SuppressWarnings("resource") |
205 | 251 | public Response fulfillGet() throws Exception { |
206 | 252 | URI uri = getUri(); |
@@ -282,7 +328,7 @@ public class Response implements HttpResponse { |
282 | 328 | private int state = 0; |
283 | 329 |
|
284 | 330 | ByteArrayInputStream inputStream; |
285 | | - |
| 331 | + |
286 | 332 | private Throwable exception; |
287 | 333 |
|
288 | 334 | /** |
@@ -335,6 +381,7 @@ public void accept(byte[] t) { |
335 | 381 |
|
336 | 382 | /** |
337 | 383 | * Make the proper callback, depending upon response code and exception state. |
| 384 | + * |
338 | 385 | * @param ok |
339 | 386 | */ |
340 | 387 | protected void doCallback(boolean ok) { |
|
0 commit comments