Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified sources/net.sf.j2s.core/dist/swingjs/SwingJS-site.zip
Binary file not shown.
2 changes: 1 addition & 1 deletion sources/net.sf.j2s.core/dist/swingjs/timestamp
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20201202180738
20201203093348
Binary file modified sources/net.sf.j2s.core/dist/swingjs/ver/3.2.9/SwingJS-site.zip
Binary file not shown.
2 changes: 1 addition & 1 deletion sources/net.sf.j2s.core/dist/swingjs/ver/3.2.9/timestamp
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20201202180738
20201203093348
Binary file modified sources/net.sf.j2s.java.core/dist/SwingJS-site.zip
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) 1995, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

package java.net;

import java.io.IOException;

/**
* Thrown to indicate that the IP address of a host could not be determined.
*
* @author Jonathan Payne
* @since JDK1.0
*/
public
class UnknownHostException extends IOException {
private static final long serialVersionUID = -4639126076052875403L;

/**
* Constructs a new {@code UnknownHostException} with the
* specified detail message.
*
* @param host the detail message.
*/
public UnknownHostException(String host) {
super(host);
}

/**
* Constructs a new {@code UnknownHostException} with no detail
* message.
*/
public UnknownHostException() {
}
}
126 changes: 89 additions & 37 deletions sources/net.sf.j2s.java.core/src/javajs/http/SimpleHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLEncoder;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -43,7 +45,7 @@
* @author Mateusz Warowny
*
*/
class SimpleHttpClient implements HttpClient {
public class SimpleHttpClient implements HttpClient {

SimpleHttpClient() {
// for reflection
Expand Down Expand Up @@ -315,6 +317,7 @@ public void executeAsync(Consumer<HttpResponse> succeed,
}

private HttpResponse executeImpl(Response r) {

Runnable runner = new Runnable() {

@Override
Expand Down Expand Up @@ -424,6 +427,8 @@ private HttpURLConnection getConnection(URL url) throws IOException {

class Response implements HttpResponse {

private final static int BAD_REQUEST = 400;

int state = 0;

/**
Expand Down Expand Up @@ -473,8 +478,8 @@ class Response implements HttpResponse {
* getResponseCode() is called.)
*
* @param conn
* @param request
* @return Response
* @param request
* @return Response
*
* @throws IOException
*/
Expand All @@ -491,34 +496,39 @@ Response getResponse(HttpURLConnection conn, Request request) throws IOException
}

new Thread(() -> {
// asynchronous methods cannot throw an exception.
IOException exception = null;
// asynchronous methods cannot throw an exception.
IOException exception = null;
try {
if (method.equals(HttpRequest.METHOD_HEAD)) {
try {
state = conn.getResponseCode();
} catch (IOException e) {
exception = e;
}
doCallback(exception);
state = conn.getResponseCode();
} else {
@SuppressWarnings("unused")
Function<byte[], Void> f = new Function<byte[], Void>() {

@Override
public Void apply(byte[] t) {
state = 400; // Bad Request?
try {
state = SimpleHttpClient.Response.this.conn.getResponseCode();
} catch (IOException e) {
}
doCallback(null);
return null;
}

};
/** @j2sNative conn.getBytesAsync$java_util_function_Function(f) */
@SuppressWarnings("unused")
Function<byte[], Void> f = new Function<byte[], Void>() {

@Override
public Void apply(byte[] t) {
state = 400; // Bad Request?
try {
state = SimpleHttpClient.Response.this.conn.getResponseCode();
doCallback(null);
} catch (IOException e) {
doCallback(e);
}
return null;
}

};
/** @j2sNative return conn.getBytesAsync$java_util_function_Function(f); */
{
f.apply(null);
return;
}
}

} catch (IOException e) {
exception = e;
}
doCallback(exception);

}).start();
return this;
}
Expand Down Expand Up @@ -550,6 +560,8 @@ protected boolean handleError(Throwable e) {
if (!(e instanceof IOException)) {
e = new IOException(e);
}
if (e instanceof UnknownHostException)
state = BAD_REQUEST;
IOException exception = (IOException) e;
// setting e = null to indicated handled.
if (isAsync) {
Expand Down Expand Up @@ -593,16 +605,33 @@ public Map<String, String> getHeaders() {

@Override
public String getText() throws IOException {
return new String( getBytes(getContent()));
return new String(getBytes(getContent()));
}

/**
* In SwingJS, this is always a ByteArrayInputStream.
* @throws IOException
*/
@Override
public InputStream getContent() throws IOException {
return (inputStream == null ? (inputStream = conn.getInputStream())
: inputStream);
if (conn == null)
throw new IOException("Connection could not be opened");
try {
return (inputStream == null ? (inputStream = conn.getInputStream())
: inputStream);
} catch (IOException e) {
if (state < 400)
state = decodeServerException(e.toString());
throw new IOException(e);
}
}

private int decodeServerException(String msg) {
try {
return Integer.parseInt(msg.substring(msg.indexOf("code:") + 5));
} catch (Exception e) {
}
return 404;
}

@Override
Expand All @@ -612,26 +641,49 @@ public void close() {

@Override
public String toString() {
return "JSHttpClient " + method + " state=" + state + " uri=" + uri;
return "SimpleHttpClient " + method + " state=" + state + " uri=" + uri;
}

}

public static String getBytes(InputStream is) throws IOException {
public static byte[] getBytes(InputStream is) throws IOException {

// Java 9 version is better:
// return new String(is.readAllBytes());
// return is.readAllBytes();

ByteArrayOutputStream bos = new ByteArrayOutputStream(0x4000);
byte[] buf = new byte[0x4000];
int n = 0, ntotal = 0;
int n = 0;
while((n = is.read(buf)) >= 0) {
ntotal += n;
bos.write(buf, 0, n);
}
is.close();
return new String(bos.toByteArray(), 0, ntotal);
return bos.toByteArray();

}

public static HttpRequest createRequest(HttpClient client, String method, String url) throws IOException {

URI uri = null;
try {
uri = new URI(url);
} catch (URISyntaxException e) {
throw new IOException(e);
}
switch (method.toUpperCase()) {
case "HEAD":
return client.head(uri);
case "GET":
return client.get(uri);
case "POST":
return client.post(uri);
case "PUT":
return client.put(uri);
case "DELETE":
return client.delete(uri);
default:
throw new IOException("Unknown method:" + method);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -146,6 +147,8 @@ private Object doAjax(boolean isBinary, Function<Object, Void> whenDone) {
* info = info || {}; if (!info.dataType) { info.isBinary =
* !!isBinary; }
*
* info.type = this.method;
*
* whenDone && (info.fWhenDone =
* function(data){whenDone.apply$O(data)});
*
Expand Down Expand Up @@ -251,8 +254,15 @@ private void setJQueryResponseCodeFromJQuery(Object result) {
* result[1] == 10); if (isEmpty) result = new Int8Array;
*/

responseCode = isEmpty ? HTTP_NOT_FOUND : /** @j2sNative info.xhr.status || */
0;
responseCode = (!isEmpty ? /** @j2sNative info.xhr.status || */
0 : getAJAXStatusError());
}

private int getAJAXStatusError() {
@SuppressWarnings("unused")
Object info = this.info;
// AJAX cannot distinguish among a network connection error, a method (PUT) error, or a file not found
return /** @j2sNative !info.xhr.statusText || (info.xhr.statusText+"").indexOf("NetworkError:") == 0 ? 400 : */HTTP_NOT_FOUND;
}

private String getFileDocumentDir() {
Expand Down Expand Up @@ -328,9 +338,10 @@ private byte[] getBytesOut() {
//

if (formData != null) {
String method = ("GET".equals(this.method) ? "POST" : this.method);
Object map = ajax = (/**
* @j2sNative 1 ? { data:new FormData(), processData:false, contentType:false,
* type:"POST", j2sNoProxy:true } :
* type:method, j2sNoProxy:true } :
*/
null);
if (formData instanceof Map<?, ?>) {
Expand Down Expand Up @@ -389,12 +400,15 @@ public OutputStream getOutputStream() throws IOException {

@SuppressWarnings({ "null", "unused" })
@Override
public InputStream getInputStream() throws FileNotFoundException {
public InputStream getInputStream() throws IOException {
BufferedInputStream is = /** @j2sNative this.is || */null;
if (is != null)
return is;
responseCode = -1;
is = getInputStreamAndResponse(false);
if (responseCode == HTTP_BAD_REQUEST) {
throw new UnknownHostException(url.toString());
}
if (is == null)
throw new FileNotFoundException("opening " + url);
return is;
Expand Down Expand Up @@ -551,10 +565,14 @@ private String getCacheKey() {

@SuppressWarnings("unused")
private boolean isNetworkError(BufferedInputStream is) {
if (is != null) {
if (responseCode > 0) {
return (responseCode >= 400);
if (responseCode > 0) {
return (responseCode >= HTTP_BAD_REQUEST);
}
if (is == null) {
if (ajax != null) {
responseCode = getAJAXStatusError();
}
} else {
responseCode = HTTP_OK;
if (/** @j2sNative is._jsonData || */
false)
Expand Down Expand Up @@ -635,7 +653,11 @@ public int getResponseCode() throws IOException {
try {
getInputStreamAndResponse(true);
} catch (Exception e) {
responseCode = HTTP_BAD_REQUEST;
}
}
if (responseCode == HTTP_BAD_REQUEST) {
throw new UnknownHostException(url.toString());
}
return responseCode;
}
Expand Down
Loading