jaroslav@37
|
1 |
/**
|
jaroslav@37
|
2 |
* HTML via Java(tm) Language Bindings
|
jaroslav@37
|
3 |
* Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jaroslav@37
|
4 |
*
|
jaroslav@37
|
5 |
* This program is free software: you can redistribute it and/or modify
|
jaroslav@37
|
6 |
* it under the terms of the GNU General Public License as published by
|
jaroslav@37
|
7 |
* the Free Software Foundation, version 2 of the License.
|
jaroslav@37
|
8 |
*
|
jaroslav@37
|
9 |
* This program is distributed in the hope that it will be useful,
|
jaroslav@37
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
jaroslav@37
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
jaroslav@37
|
12 |
* GNU General Public License for more details. apidesign.org
|
jaroslav@37
|
13 |
* designates this particular file as subject to the
|
jaroslav@37
|
14 |
* "Classpath" exception as provided by apidesign.org
|
jaroslav@37
|
15 |
* in the License file that accompanied this code.
|
jaroslav@37
|
16 |
*
|
jaroslav@37
|
17 |
* You should have received a copy of the GNU General Public License
|
jaroslav@37
|
18 |
* along with this program. Look for COPYING file in the top folder.
|
jaroslav@37
|
19 |
* If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
|
jaroslav@37
|
20 |
*/
|
jaroslav@37
|
21 |
package org.apidesign.html.kofx;
|
jaroslav@37
|
22 |
|
jaroslav@37
|
23 |
import java.io.IOException;
|
jaroslav@60
|
24 |
import java.io.InputStream;
|
jaroslav@37
|
25 |
import java.io.InputStreamReader;
|
jaroslav@75
|
26 |
import java.io.OutputStream;
|
jaroslav@37
|
27 |
import java.io.PushbackInputStream;
|
jaroslav@37
|
28 |
import java.io.Reader;
|
jaroslav@74
|
29 |
import java.net.HttpURLConnection;
|
jaroslav@37
|
30 |
import java.net.MalformedURLException;
|
jaroslav@37
|
31 |
import java.net.URL;
|
jaroslav@74
|
32 |
import java.net.URLConnection;
|
jaroslav@37
|
33 |
import java.util.Iterator;
|
jaroslav@37
|
34 |
import java.util.concurrent.Executor;
|
jaroslav@37
|
35 |
import java.util.concurrent.Executors;
|
jaroslav@158
|
36 |
import java.util.concurrent.ThreadFactory;
|
jaroslav@37
|
37 |
import java.util.logging.Level;
|
jaroslav@37
|
38 |
import java.util.logging.Logger;
|
jaroslav@37
|
39 |
import javafx.application.Platform;
|
jaroslav@131
|
40 |
import net.java.html.js.JavaScriptBody;
|
jaroslav@37
|
41 |
import netscape.javascript.JSObject;
|
jaroslav@37
|
42 |
import org.apidesign.html.json.spi.JSONCall;
|
jaroslav@37
|
43 |
import org.json.JSONArray;
|
jaroslav@37
|
44 |
import org.json.JSONException;
|
jaroslav@37
|
45 |
import org.json.JSONObject;
|
jaroslav@37
|
46 |
import org.json.JSONTokener;
|
jaroslav@37
|
47 |
|
jaroslav@54
|
48 |
/** This is an implementation package - just
|
jaroslav@54
|
49 |
* include its JAR on classpath and use official {@link Context} API
|
jaroslav@54
|
50 |
* to access the functionality.
|
jaroslav@37
|
51 |
*
|
jaroslav@37
|
52 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jaroslav@37
|
53 |
*/
|
jaroslav@37
|
54 |
final class LoadJSON implements Runnable {
|
jaroslav@37
|
55 |
private static final Logger LOG = FXContext.LOG;
|
jaroslav@158
|
56 |
private static final Executor REQ = Executors.newCachedThreadPool(new ThreadFactory() {
|
jaroslav@158
|
57 |
@Override
|
jaroslav@158
|
58 |
public Thread newThread(Runnable runnable) {
|
jaroslav@158
|
59 |
Thread thread = Executors.defaultThreadFactory().newThread(runnable);
|
jaroslav@158
|
60 |
thread.setDaemon(true);
|
jaroslav@158
|
61 |
return thread;
|
jaroslav@158
|
62 |
}
|
jaroslav@158
|
63 |
});
|
jaroslav@37
|
64 |
|
jaroslav@37
|
65 |
private final JSONCall call;
|
jaroslav@37
|
66 |
private final URL base;
|
jaroslav@37
|
67 |
private Throwable error;
|
jaroslav@37
|
68 |
private Object json;
|
jaroslav@37
|
69 |
|
jaroslav@37
|
70 |
|
jaroslav@37
|
71 |
private LoadJSON(JSONCall call) {
|
jaroslav@37
|
72 |
this.call = call;
|
jaroslav@37
|
73 |
URL b;
|
jaroslav@37
|
74 |
try {
|
jaroslav@37
|
75 |
b = new URL(findBaseURL());
|
jaroslav@37
|
76 |
} catch (MalformedURLException ex) {
|
jaroslav@37
|
77 |
LOG.log(Level.SEVERE, "Can't find base url for " + call.composeURL("dummy"), ex);
|
jaroslav@37
|
78 |
b = null;
|
jaroslav@37
|
79 |
}
|
jaroslav@37
|
80 |
this.base = b;
|
jaroslav@37
|
81 |
}
|
jaroslav@37
|
82 |
|
jaroslav@37
|
83 |
public static void loadJSON(JSONCall call) {
|
jaroslav@37
|
84 |
REQ.execute(new LoadJSON((call)));
|
jaroslav@37
|
85 |
}
|
jaroslav@37
|
86 |
|
jaroslav@37
|
87 |
@Override
|
jaroslav@37
|
88 |
public void run() {
|
jaroslav@37
|
89 |
if (Platform.isFxApplicationThread()) {
|
jaroslav@37
|
90 |
if (error != null) {
|
jaroslav@37
|
91 |
call.notifyError(error);
|
jaroslav@37
|
92 |
} else {
|
jaroslav@37
|
93 |
call.notifySuccess(json);
|
jaroslav@37
|
94 |
}
|
jaroslav@37
|
95 |
return;
|
jaroslav@37
|
96 |
}
|
jaroslav@37
|
97 |
final String url;
|
jaroslav@37
|
98 |
if (call.isJSONP()) {
|
jaroslav@37
|
99 |
url = call.composeURL("dummy");
|
jaroslav@37
|
100 |
} else {
|
jaroslav@37
|
101 |
url = call.composeURL(null);
|
jaroslav@37
|
102 |
}
|
jaroslav@37
|
103 |
try {
|
jaroslav@37
|
104 |
final URL u = new URL(base, url.replace(" ", "%20"));
|
jaroslav@74
|
105 |
URLConnection conn = u.openConnection();
|
jaroslav@74
|
106 |
if (conn instanceof HttpURLConnection) {
|
jaroslav@74
|
107 |
HttpURLConnection huc = (HttpURLConnection) conn;
|
jaroslav@74
|
108 |
if (call.getMethod() != null) {
|
jaroslav@74
|
109 |
huc.setRequestMethod(call.getMethod());
|
jaroslav@74
|
110 |
}
|
jaroslav@75
|
111 |
if (call.isDoOutput()) {
|
jaroslav@75
|
112 |
huc.setDoOutput(true);
|
jaroslav@75
|
113 |
final OutputStream os = huc.getOutputStream();
|
jaroslav@75
|
114 |
call.writeData(os);
|
jaroslav@75
|
115 |
os.flush();
|
jaroslav@75
|
116 |
}
|
jaroslav@74
|
117 |
}
|
jaroslav@74
|
118 |
final PushbackInputStream is = new PushbackInputStream(
|
jaroslav@74
|
119 |
conn.getInputStream(), 1
|
jaroslav@74
|
120 |
);
|
jaroslav@37
|
121 |
boolean array = false;
|
jaroslav@73
|
122 |
boolean string = false;
|
jaroslav@37
|
123 |
if (call.isJSONP()) {
|
jaroslav@37
|
124 |
for (;;) {
|
jaroslav@37
|
125 |
int ch = is.read();
|
jaroslav@37
|
126 |
if (ch == -1) {
|
jaroslav@37
|
127 |
break;
|
jaroslav@37
|
128 |
}
|
jaroslav@37
|
129 |
if (ch == '[') {
|
jaroslav@37
|
130 |
is.unread(ch);
|
jaroslav@37
|
131 |
array = true;
|
jaroslav@37
|
132 |
break;
|
jaroslav@37
|
133 |
}
|
jaroslav@37
|
134 |
if (ch == '{') {
|
jaroslav@37
|
135 |
is.unread(ch);
|
jaroslav@37
|
136 |
break;
|
jaroslav@37
|
137 |
}
|
jaroslav@37
|
138 |
}
|
jaroslav@37
|
139 |
} else {
|
jaroslav@37
|
140 |
int ch = is.read();
|
jaroslav@138
|
141 |
if (ch == -1) {
|
jaroslav@73
|
142 |
string = true;
|
jaroslav@138
|
143 |
} else {
|
jaroslav@138
|
144 |
array = ch == '[';
|
jaroslav@138
|
145 |
is.unread(ch);
|
jaroslav@138
|
146 |
if (!array && ch != '{') {
|
jaroslav@138
|
147 |
string = true;
|
jaroslav@138
|
148 |
}
|
jaroslav@73
|
149 |
}
|
jaroslav@37
|
150 |
}
|
jaroslav@73
|
151 |
try {
|
jaroslav@73
|
152 |
if (string) {
|
jaroslav@73
|
153 |
throw new JSONException("");
|
jaroslav@73
|
154 |
}
|
jaroslav@73
|
155 |
Reader r = new InputStreamReader(is, "UTF-8");
|
jaroslav@37
|
156 |
|
jaroslav@73
|
157 |
JSONTokener tok = new JSONTokener(r);
|
jaroslav@73
|
158 |
Object obj;
|
jaroslav@73
|
159 |
obj = array ? new JSONArray(tok) : new JSONObject(tok);
|
jaroslav@73
|
160 |
json = convertToArray(obj);
|
jaroslav@73
|
161 |
} catch (JSONException ex) {
|
jaroslav@73
|
162 |
Reader r = new InputStreamReader(is, "UTF-8");
|
jaroslav@73
|
163 |
StringBuilder sb = new StringBuilder();
|
jaroslav@73
|
164 |
for (;;) {
|
jaroslav@73
|
165 |
int ch = r.read();
|
jaroslav@73
|
166 |
if (ch == -1) {
|
jaroslav@73
|
167 |
break;
|
jaroslav@73
|
168 |
}
|
jaroslav@73
|
169 |
sb.append((char)ch);
|
jaroslav@73
|
170 |
}
|
jaroslav@73
|
171 |
json = sb.toString();
|
jaroslav@73
|
172 |
}
|
jaroslav@73
|
173 |
} catch (IOException ex) {
|
jaroslav@37
|
174 |
error = ex;
|
jaroslav@37
|
175 |
LOG.log(Level.WARNING, "Cannot connect to " + url, ex);
|
jaroslav@37
|
176 |
} finally {
|
jaroslav@37
|
177 |
Platform.runLater(this);
|
jaroslav@37
|
178 |
}
|
jaroslav@37
|
179 |
}
|
jaroslav@37
|
180 |
|
jaroslav@37
|
181 |
private static Object convertToArray(Object o) throws JSONException {
|
jaroslav@37
|
182 |
if (o instanceof JSONArray) {
|
jaroslav@37
|
183 |
JSONArray ja = (JSONArray)o;
|
jaroslav@37
|
184 |
Object[] arr = new Object[ja.length()];
|
jaroslav@37
|
185 |
for (int i = 0; i < arr.length; i++) {
|
jaroslav@37
|
186 |
arr[i] = convertToArray(ja.get(i));
|
jaroslav@37
|
187 |
}
|
jaroslav@37
|
188 |
return arr;
|
jaroslav@37
|
189 |
} else if (o instanceof JSONObject) {
|
jaroslav@37
|
190 |
JSONObject obj = (JSONObject)o;
|
jaroslav@37
|
191 |
Iterator it = obj.keys();
|
jaroslav@37
|
192 |
while (it.hasNext()) {
|
jaroslav@37
|
193 |
String key = (String)it.next();
|
jaroslav@37
|
194 |
obj.put(key, convertToArray(obj.get(key)));
|
jaroslav@37
|
195 |
}
|
jaroslav@37
|
196 |
return obj;
|
jaroslav@37
|
197 |
} else {
|
jaroslav@37
|
198 |
return o;
|
jaroslav@37
|
199 |
}
|
jaroslav@37
|
200 |
}
|
jaroslav@37
|
201 |
|
jaroslav@37
|
202 |
public static void extractJSON(Object jsonObject, String[] props, Object[] values) {
|
jaroslav@37
|
203 |
if (jsonObject instanceof JSONObject) {
|
jaroslav@37
|
204 |
JSONObject obj = (JSONObject)jsonObject;
|
jaroslav@37
|
205 |
for (int i = 0; i < props.length; i++) {
|
jaroslav@37
|
206 |
try {
|
jaroslav@37
|
207 |
values[i] = obj.has(props[i]) ? obj.get(props[i]) : null;
|
jaroslav@37
|
208 |
} catch (JSONException ex) {
|
jaroslav@37
|
209 |
LoadJSON.LOG.log(Level.SEVERE, "Can't read " + props[i] + " from " + jsonObject, ex);
|
jaroslav@37
|
210 |
}
|
jaroslav@37
|
211 |
}
|
jaroslav@37
|
212 |
}
|
jaroslav@37
|
213 |
if (jsonObject instanceof JSObject) {
|
jaroslav@37
|
214 |
JSObject obj = (JSObject)jsonObject;
|
jaroslav@37
|
215 |
for (int i = 0; i < props.length; i++) {
|
jaroslav@37
|
216 |
Object val = obj.getMember(props[i]);
|
jaroslav@37
|
217 |
values[i] = isDefined(val) ? val : null;
|
jaroslav@37
|
218 |
}
|
jaroslav@37
|
219 |
}
|
jaroslav@37
|
220 |
}
|
jaroslav@37
|
221 |
|
jaroslav@60
|
222 |
public static Object parse(InputStream is) throws IOException {
|
jaroslav@60
|
223 |
try {
|
jaroslav@60
|
224 |
InputStreamReader r = new InputStreamReader(is, "UTF-8");
|
jaroslav@60
|
225 |
JSONTokener t = new JSONTokener(r);
|
jaroslav@60
|
226 |
return new JSONObject(t);
|
jaroslav@60
|
227 |
} catch (JSONException ex) {
|
jaroslav@60
|
228 |
throw new IOException(ex);
|
jaroslav@60
|
229 |
}
|
jaroslav@60
|
230 |
}
|
jaroslav@118
|
231 |
|
jaroslav@118
|
232 |
@JavaScriptBody(args = { }, body =
|
jaroslav@118
|
233 |
"var h;"
|
jaroslav@118
|
234 |
+ "if (!!window && !!window.location && !!window.location.href)\n"
|
jaroslav@118
|
235 |
+ " h = window.location.href;\n"
|
jaroslav@118
|
236 |
+ "else "
|
jaroslav@118
|
237 |
+ " h = null;"
|
jaroslav@118
|
238 |
+ "return h;\n"
|
jaroslav@118
|
239 |
)
|
jaroslav@118
|
240 |
private static native String findBaseURL();
|
jaroslav@60
|
241 |
|
jaroslav@37
|
242 |
private static boolean isDefined(Object val) {
|
jaroslav@37
|
243 |
return !"undefined".equals(val);
|
jaroslav@37
|
244 |
}
|
jaroslav@37
|
245 |
}
|