2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
6 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7 * Other names may be trademarks of their respective owners.
9 * The contents of this file are subject to the terms of either the GNU
10 * General Public License Version 2 only ("GPL") or the Common
11 * Development and Distribution License("CDDL") (collectively, the
12 * "License"). You may not use this file except in compliance with the
13 * License. You can obtain a copy of the License at
14 * http://www.netbeans.org/cddl-gplv2.html
15 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16 * specific language governing permissions and limitations under the
17 * License. When distributing the software, include this License Header
18 * Notice in each file and include the License file at
19 * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
20 * particular file as subject to the "Classpath" exception as provided
21 * by Oracle in the GPL Version 2 section of the License file that
22 * accompanied this code. If applicable, add the following below the
23 * License Header, with the fields enclosed by brackets [] replaced by
24 * your own identifying information:
25 * "Portions Copyrighted [year] [name of copyright owner]"
29 * The Original Software is NetBeans. The Initial Developer of the Original
30 * Software is Oracle. Portions Copyright 2013-2013 Oracle. All Rights Reserved.
32 * If you wish your version of this file to be governed by only the CDDL
33 * or only the GPL Version 2, indicate your decision by adding
34 * "[Contributor] elects to include this software in this distribution
35 * under the [CDDL or GPL Version 2] license." If you do not indicate a
36 * single choice of license, a recipient has the option to distribute
37 * your version of this file under either the CDDL, the GPL Version 2 or
38 * to extend the choice of license to its licensees as provided above.
39 * However, if you add GPL Version 2 code and therefore, elected the GPL
40 * Version 2 license, then the option applies only if the new code is
41 * made subject to such option by the copyright holder.
43 package org.netbeans.html.kofx;
45 import java.io.IOException;
46 import java.io.InputStream;
47 import java.io.InputStreamReader;
48 import java.io.OutputStream;
49 import java.io.PushbackInputStream;
50 import java.io.Reader;
51 import java.net.HttpURLConnection;
52 import java.net.MalformedURLException;
54 import java.net.URLConnection;
55 import java.util.Iterator;
56 import java.util.concurrent.Executor;
57 import java.util.concurrent.Executors;
58 import java.util.concurrent.ThreadFactory;
59 import java.util.logging.Level;
60 import java.util.logging.Logger;
61 import javafx.application.Platform;
62 import net.java.html.js.JavaScriptBody;
63 import netscape.javascript.JSObject;
64 import org.apidesign.html.json.spi.JSONCall;
65 import org.json.JSONArray;
66 import org.json.JSONException;
67 import org.json.JSONObject;
68 import org.json.JSONTokener;
70 /** This is an implementation package - just
71 * include its JAR on classpath and use official {@link Context} API
72 * to access the functionality.
74 * @author Jaroslav Tulach <jtulach@netbeans.org>
76 final class LoadJSON implements Runnable {
77 private static final Logger LOG = FXContext.LOG;
78 private static final Executor REQ = Executors.newCachedThreadPool(new ThreadFactory() {
80 public Thread newThread(Runnable runnable) {
81 Thread thread = Executors.defaultThreadFactory().newThread(runnable);
82 thread.setDaemon(true);
87 private final JSONCall call;
88 private final URL base;
89 private Throwable error;
93 private LoadJSON(JSONCall call) {
97 b = new URL(findBaseURL());
98 } catch (MalformedURLException ex) {
99 LOG.log(Level.SEVERE, "Can't find base url for " + call.composeURL("dummy"), ex);
105 public static void loadJSON(JSONCall call) {
106 assert !"WebSocket".equals(call.getMethod());
107 REQ.execute(new LoadJSON((call)));
112 if (Platform.isFxApplicationThread()) {
114 call.notifyError(error);
116 call.notifySuccess(json);
121 if (call.isJSONP()) {
122 url = call.composeURL("dummy");
124 url = call.composeURL(null);
127 final URL u = new URL(base, url.replace(" ", "%20"));
128 URLConnection conn = u.openConnection();
129 if (conn instanceof HttpURLConnection) {
130 HttpURLConnection huc = (HttpURLConnection) conn;
131 if (call.getMethod() != null) {
132 huc.setRequestMethod(call.getMethod());
134 if (call.isDoOutput()) {
135 huc.setDoOutput(true);
136 final OutputStream os = huc.getOutputStream();
141 final PushbackInputStream is = new PushbackInputStream(
142 conn.getInputStream(), 1
144 boolean array = false;
145 boolean string = false;
146 if (call.isJSONP()) {
169 if (!array && ch != '{') {
176 throw new JSONException("");
178 Reader r = new InputStreamReader(is, "UTF-8");
180 JSONTokener tok = new JSONTokener(r);
182 obj = array ? new JSONArray(tok) : new JSONObject(tok);
183 json = convertToArray(obj);
184 } catch (JSONException ex) {
185 Reader r = new InputStreamReader(is, "UTF-8");
186 StringBuilder sb = new StringBuilder();
194 json = sb.toString();
196 } catch (IOException ex) {
199 Platform.runLater(this);
203 static Object convertToArray(Object o) throws JSONException {
204 if (o instanceof JSONArray) {
205 JSONArray ja = (JSONArray)o;
206 Object[] arr = new Object[ja.length()];
207 for (int i = 0; i < arr.length; i++) {
208 arr[i] = convertToArray(ja.get(i));
211 } else if (o instanceof JSONObject) {
212 JSONObject obj = (JSONObject)o;
213 Iterator it = obj.keys();
214 while (it.hasNext()) {
215 String key = (String)it.next();
216 obj.put(key, convertToArray(obj.get(key)));
224 public static void extractJSON(Object jsonObject, String[] props, Object[] values) {
225 if (jsonObject instanceof JSONObject) {
226 JSONObject obj = (JSONObject)jsonObject;
227 for (int i = 0; i < props.length; i++) {
229 values[i] = obj.has(props[i]) ? obj.get(props[i]) : null;
230 } catch (JSONException ex) {
231 LoadJSON.LOG.log(Level.SEVERE, "Can't read " + props[i] + " from " + jsonObject, ex);
235 if (jsonObject instanceof JSObject) {
236 JSObject obj = (JSObject)jsonObject;
237 for (int i = 0; i < props.length; i++) {
238 Object val = obj.getMember(props[i]);
239 values[i] = isDefined(val) ? val : null;
244 public static Object parse(InputStream is) throws IOException {
246 InputStreamReader r = new InputStreamReader(is, "UTF-8");
247 JSONTokener t = new JSONTokener(r);
248 return new JSONObject(t);
249 } catch (JSONException ex) {
250 throw new IOException(ex);
254 @JavaScriptBody(args = { }, body =
256 + "if (!!window && !!window.location && !!window.location.href)\n"
257 + " h = window.location.href;\n"
262 private static native String findBaseURL();
264 private static boolean isDefined(Object val) {
265 return !"undefined".equals(val);