2 * HTML via Java(tm) Language Bindings
3 * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. apidesign.org
13 * designates this particular file as subject to the
14 * "Classpath" exception as provided by apidesign.org
15 * in the License file that accompanied this code.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. Look for COPYING file in the top folder.
19 * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
21 package org.apidesign.html.kofx;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.InputStreamReader;
26 import java.io.OutputStream;
27 import java.io.PushbackInputStream;
28 import java.io.Reader;
29 import java.net.HttpURLConnection;
30 import java.net.MalformedURLException;
32 import java.net.URLConnection;
33 import java.util.Iterator;
34 import java.util.concurrent.Executor;
35 import java.util.concurrent.Executors;
36 import java.util.logging.Level;
37 import java.util.logging.Logger;
38 import javafx.application.Platform;
39 import net.java.html.js.JavaScriptBody;
40 import netscape.javascript.JSObject;
41 import org.apidesign.html.json.spi.JSONCall;
42 import org.json.JSONArray;
43 import org.json.JSONException;
44 import org.json.JSONObject;
45 import org.json.JSONTokener;
47 /** This is an implementation package - just
48 * include its JAR on classpath and use official {@link Context} API
49 * to access the functionality.
51 * @author Jaroslav Tulach <jtulach@netbeans.org>
53 final class LoadJSON implements Runnable {
54 private static final Logger LOG = FXContext.LOG;
55 private static final Executor REQ = Executors.newCachedThreadPool();
57 private final JSONCall call;
58 private final URL base;
59 private Throwable error;
63 private LoadJSON(JSONCall call) {
67 b = new URL(findBaseURL());
68 } catch (MalformedURLException ex) {
69 LOG.log(Level.SEVERE, "Can't find base url for " + call.composeURL("dummy"), ex);
75 public static void loadJSON(JSONCall call) {
76 REQ.execute(new LoadJSON((call)));
81 if (Platform.isFxApplicationThread()) {
83 call.notifyError(error);
85 call.notifySuccess(json);
91 url = call.composeURL("dummy");
93 url = call.composeURL(null);
96 final URL u = new URL(base, url.replace(" ", "%20"));
97 URLConnection conn = u.openConnection();
98 if (conn instanceof HttpURLConnection) {
99 HttpURLConnection huc = (HttpURLConnection) conn;
100 if (call.getMethod() != null) {
101 huc.setRequestMethod(call.getMethod());
103 if (call.isDoOutput()) {
104 huc.setDoOutput(true);
105 final OutputStream os = huc.getOutputStream();
110 final PushbackInputStream is = new PushbackInputStream(
111 conn.getInputStream(), 1
113 boolean array = false;
114 boolean string = false;
115 if (call.isJSONP()) {
135 if (!array && ch != '{') {
141 throw new JSONException("");
143 Reader r = new InputStreamReader(is, "UTF-8");
145 JSONTokener tok = new JSONTokener(r);
147 obj = array ? new JSONArray(tok) : new JSONObject(tok);
148 json = convertToArray(obj);
149 } catch (JSONException ex) {
150 Reader r = new InputStreamReader(is, "UTF-8");
151 StringBuilder sb = new StringBuilder();
159 json = sb.toString();
161 } catch (IOException ex) {
163 LOG.log(Level.WARNING, "Cannot connect to " + url, ex);
165 Platform.runLater(this);
169 private static Object convertToArray(Object o) throws JSONException {
170 if (o instanceof JSONArray) {
171 JSONArray ja = (JSONArray)o;
172 Object[] arr = new Object[ja.length()];
173 for (int i = 0; i < arr.length; i++) {
174 arr[i] = convertToArray(ja.get(i));
177 } else if (o instanceof JSONObject) {
178 JSONObject obj = (JSONObject)o;
179 Iterator it = obj.keys();
180 while (it.hasNext()) {
181 String key = (String)it.next();
182 obj.put(key, convertToArray(obj.get(key)));
190 public static void extractJSON(Object jsonObject, String[] props, Object[] values) {
191 if (jsonObject instanceof JSONObject) {
192 JSONObject obj = (JSONObject)jsonObject;
193 for (int i = 0; i < props.length; i++) {
195 values[i] = obj.has(props[i]) ? obj.get(props[i]) : null;
196 } catch (JSONException ex) {
197 LoadJSON.LOG.log(Level.SEVERE, "Can't read " + props[i] + " from " + jsonObject, ex);
201 if (jsonObject instanceof JSObject) {
202 JSObject obj = (JSObject)jsonObject;
203 for (int i = 0; i < props.length; i++) {
204 Object val = obj.getMember(props[i]);
205 values[i] = isDefined(val) ? val : null;
210 public static Object parse(InputStream is) throws IOException {
212 InputStreamReader r = new InputStreamReader(is, "UTF-8");
213 JSONTokener t = new JSONTokener(r);
214 return new JSONObject(t);
215 } catch (JSONException ex) {
216 throw new IOException(ex);
220 @JavaScriptBody(args = { }, body =
222 + "if (!!window && !!window.location && !!window.location.href)\n"
223 + " h = window.location.href;\n"
228 private static native String findBaseURL();
230 private static boolean isDefined(Object val) {
231 return !"undefined".equals(val);