Skip to content

Commit 755c88d

Browse files
authored
Merge pull request #215 from BobHanson/master
httpclient
2 parents cd5bd8c + a08bbdb commit 755c88d

File tree

4 files changed

+147
-6
lines changed

4 files changed

+147
-6
lines changed

README.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ java2script/java2script is the overall master of the project. However, all activ
55
These notes are for Java developers who want to convert their Java applets or Java applications to
66
JavaScript, allowing continued, simultaneous one-source development of both Java and JavaScript.
77

8+
A discussion of application limitations and features is at https://github.com/BobHanson/java2script/blob/master/sources/net.sf.j2s.core/dist/swingjs/differences.txt
9+
810
Developers of java2script/SwingJS itself should read README-developers.md
911

1012
Bob Hanson (hansonr@stolaf.edu)
@@ -56,27 +58,31 @@ In the author's own words, "There are Java APIs that are impossible to implement
5658

5759
https://www.leaningtech.com/cheerpj/ "CheerpJ converts Java applications or libraries into JavaScript. Works on bytecode, does not require access to the source code. Compatible with 100% of Java including reflection and dynamic classes. Existing Java archives can be converted to Web applications effortlessly"
5860

59-
This sounds terrific. Very straightforward -- just convert the Java class files to JavaScript. I don't doubt that most of what is written here is true. I am dubious about that "100%" claim, as there are plenty of problems in Java that would take considerably more work than just using class files directly. The Java Reporter demonstration at https://www.leaningtech.com/cheerpj/demos/ crashed both Firefox and Chrome for me, so I cannot really evaluate what I see here.
61+
This sounds terrific. And truly it is. I am very impressed! Basically run the Java byte code in JavaScript. The Java Reporter demonstration at https://www.leaningtech.com/cheerpj/demos/ crashed both Firefox and Chrome for me, so I cannot really evaluate what I see here. But I have played with the JFiddle at https://javafiddle.leaningtech.com/, and it is pretty amazing.
6062

6163
The primary differences between java2script/SwingJS and CheerpJ, to the best of my knowledge, include:
6264

63-
- implementing a true HTML5 UI rather than just painting a canvas the way Java does
65+
- fast start-up time. SwingJS applications generally start within a second or two, sometimes within 100 ms
66+
- small downloads, anywhere from about 800K for a small non-GUI program to 10 MB for a full-blown Swing application (JSmol)
67+
- class-level just-in-time dynamic class loading; no need to retrieve entire JAR files just for a few methods
68+
- leveraging features of HTML5 and modern JavaScript rather than just painting a canvas the way Java does natively
6469
- delivering an easily interpretable and debuggable JavaScript translation of Java classes, with little or no obscurification (unless that is desired)
65-
- well-designed JavaScript-friendly Java core classes that leverage the considerable power of HTML5 rather than ignoring that completely
70+
- well-designed JavaScript-friendly Java core classes that leverage the considerable power of JavaScript
6671
- open source and completely extensible
6772

6873
# History - 2019-
6974

70-
SwingJS is now more than just "Swing"-JS. AWT applets and applications are now supported. A test suite of over 500 AWT applets has been used to refine the AWT runtime classes with great success. Many thanks to Karsten Blankenagel (University of Wuppertal) for access to this source code set.
75+
SwingJS is now more than just "Swing"+JavaScript. AWT applets and applications are now supported. A test suite of over 500 AWT applets has been used to refine the AWT runtime classes with great success. Many thanks to Karsten Blankenagel (University of Wuppertal) for access to this source code set.
7176

7277
Examples include:
7378

7479
MathePrisma (http://www.matheprisma.uni-wuppertal.de/) This site is still using the Java applets as of 2019.03.12; JavaScript versions still in development.
7580

7681
# History - 2017-
7782

83+
https://github.com/BobHanson/java2script/tree/hanson1 (development branch)
7884

79-
https://github.com/BobHanson/java2script (development master)
85+
https://github.com/BobHanson/java2script (master)
8086

8187
Current development "Version 3 development master" involves a completely rewritten transpiler (2017) “CompilationParticipant” that follows the Eclipse Java compiler. The implementation nearly perfectly emulates the Java Virtual Machine. It includes fully qualified methods, compile-time method binding, generic methods and classes, Java 8 lambda functions and streams, Java reflection and dynamic class loading for efficient modular just-in-time performance, Java Swing components, modal and nonmodel dialogs, audio, jpdf, the AWT event thread, and many other added packages. Java applications and applets can both be run in JavaScript in any browser.
8288

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package javajs.http;
2+
3+
import java.io.IOException;
4+
5+
/**
6+
* A generic class for exceptions resulting from the HTTP exchange.
7+
*/
8+
public class ClientProtocolException extends IOException {
9+
10+
private static final long serialVersionUID = 2500725227517025772L;
11+
12+
public ClientProtocolException() {
13+
super();
14+
}
15+
16+
public ClientProtocolException(String message) {
17+
super(message);
18+
}
19+
20+
public ClientProtocolException(Throwable cause) {
21+
super(cause);
22+
}
23+
24+
public ClientProtocolException(String message, Throwable cause) {
25+
super(message, cause);
26+
}
27+
28+
}

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

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package javajs.http;
22

3+
import java.io.ByteArrayInputStream;
34
import java.io.Closeable;
45
import java.io.File;
56
import java.io.IOException;
@@ -88,7 +89,10 @@ public interface HttpRequest {
8889
* @param fileName server-side fileName to associate with this file
8990
* @return
9091
*/
91-
public HttpRequest addFilePart(String name, String data, String contentType, String fileName);
92+
public default HttpRequest addFilePart(String name, String data, String contentType, String fileName) {
93+
var stream = new ByteArrayInputStream(data.getBytes());
94+
return addFilePart(name, stream, contentType, fileName);
95+
}
9296

9397
/**
9498
* Add a file-type multipart/form-data derived from a File object, using
@@ -179,6 +183,64 @@ public interface HttpResponse extends Closeable {
179183
*/
180184
public int getStatusCode();
181185

186+
/**
187+
* Get the reason phrase for the response code.
188+
* Implementations should use the reason returned by the server.
189+
* Default implementation uses phrases defined by RFC 2616 and RFC 4918
190+
*
191+
* @return status line reason phrase
192+
*/
193+
public default String getReasonPhrase() {
194+
switch (getStatusCode()) {
195+
case 100: return "Continue";
196+
case 101: return "Switching Protocols";
197+
case 200: return "OK";
198+
case 201: return "Created";
199+
case 202: return "Accepted";
200+
case 203: return "Non-Authoritative Information";
201+
case 204: return "No Content";
202+
case 205: return "Reset Content";
203+
case 206: return "Partial Content";
204+
case 207: return "Multi-status";
205+
case 300: return "Multiple Choices";
206+
case 301: return "Moved Permanently";
207+
case 302: return "Found";
208+
case 303: return "See Other";
209+
case 304: return "Not Modified";
210+
case 305: return "Use Proxy";
211+
case 307: return "Temporary Redirect";
212+
case 400: return "Bad Request";
213+
case 401: return "Unauthorized";
214+
case 402: return "Payment Required";
215+
case 403: return "Forbidden";
216+
case 404: return "Not Found";
217+
case 405: return "Method Not Allowed";
218+
case 406: return "Not Acceptable";
219+
case 407: return "Proxy Authentication Required";
220+
case 408: return "Request Timeout";
221+
case 409: return "Conflict";
222+
case 410: return "Gone";
223+
case 411: return "Length Required";
224+
case 412: return "Precondition Failed";
225+
case 413: return "Request Entity Too Large";
226+
case 414: return "Request-URI Too Long";
227+
case 415: return "Unsupported Media Type";
228+
case 416: return "Requested Range Not Satisfiable";
229+
case 417: return "Expectation Failed";
230+
case 422: return "Unprocessable Entity";
231+
case 423: return "Locked";
232+
case 424: return "Failed Dependency";
233+
case 500: return "Internal Server Error";
234+
case 501: return "Not Implemented";
235+
case 502: return "Bad Gateway";
236+
case 503: return "Service Unavailable";
237+
case 504: return "Gateway Timeout";
238+
case 505: return "HTTP Version Not Supported";
239+
case 507: return "Insufficient Storage";
240+
default: return "";
241+
}
242+
}
243+
182244
/**
183245
* Get the response headers, combining same-name headers with commas, preserving
184246
* order, as per RFC 2616
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package javajs.http;
2+
3+
/**
4+
* Exception that can be raised by the application to indicate
5+
* a non 2xx server response. The http client will not raise
6+
* this exception implicitly. A typical use case is to throw
7+
* this exception if the response status code indicate failure.
8+
* The status code and the reason phrase should be populated from
9+
* the {@link HttpClient.HttpResponse#getStatusCode()} and
10+
* {@link HttpClient.HttpResponse#getReasonPhrase()} respectively.
11+
*/
12+
public class HttpResponseException extends ClientProtocolException {
13+
14+
private static final long serialVersionUID = -8481868921105838666L;
15+
16+
private final int statusCode;
17+
private final String reasonPhrase;
18+
19+
public HttpResponseException(int statusCode, String reasonPhrase, String message) {
20+
super(message);
21+
this.statusCode = statusCode;
22+
this.reasonPhrase = reasonPhrase;
23+
}
24+
25+
public HttpResponseException(int statusCode, String reasonPhrase) {
26+
this(statusCode, reasonPhrase, String.format("%d %s", statusCode, reasonPhrase));
27+
}
28+
29+
/**
30+
* Get status code of the response which caused this exception.
31+
*
32+
* @return status code
33+
*/
34+
public int getStatusCode() {
35+
return statusCode;
36+
}
37+
38+
/**
39+
* Get reason phrase complementing the error's status code.
40+
*/
41+
public String getReasonPhrase() {
42+
return reasonPhrase;
43+
}
44+
45+
}

0 commit comments

Comments
 (0)