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