2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2013-2014 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-2014 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.xhr4j;
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.io.UnsupportedEncodingException;
52 import java.net.HttpURLConnection;
54 import java.net.URLConnection;
55 import java.util.concurrent.Callable;
56 import java.util.concurrent.Executor;
57 import java.util.concurrent.Executors;
58 import java.util.concurrent.ThreadFactory;
59 import java.util.logging.Logger;
60 import net.java.html.js.JavaScriptBody;
61 import org.netbeans.html.json.spi.JSONCall;
63 /** This is an implementation package - just
64 * include its JAR on classpath and use official {@link Context} API
65 * to access the functionality.
67 * @author Jaroslav Tulach
69 final class LoadJSON implements Runnable {
70 private static final Logger LOG = Logger.getLogger(LoadJSON.class.getName());
71 private static final Executor REQ = Executors.newCachedThreadPool(new ThreadFactory() {
73 public Thread newThread(Runnable runnable) {
74 Thread thread = Executors.defaultThreadFactory().newThread(runnable);
75 thread.setDaemon(true);
76 thread.setName("xhr4j daemon");
81 private final JSONCall call;
82 private final URL base;
85 private LoadJSON(JSONCall call) {
90 public static void loadJSON(JSONCall call) {
91 assert !"WebSocket".equals(call.getMethod());
92 REQ.execute(new LoadJSON((call)));
98 Throwable error = null;
101 if (call.isJSONP()) {
102 url = call.composeURL("dummy");
104 url = call.composeURL(null);
107 final URL u = new URL(base, url.replace(" ", "%20"));
108 URLConnection conn = u.openConnection();
109 if (call.isDoOutput()) {
110 conn.setDoOutput(true);
112 String h = call.getHeaders();
115 while (pos < h.length()) {
116 int tagEnd = h.indexOf(':', pos);
120 int r = h.indexOf('\r', tagEnd);
121 int n = h.indexOf('\n', tagEnd);
128 String key = h.substring(pos, tagEnd).trim();
129 String val = h.substring(tagEnd + 1, Math.min(r, n)).trim();
130 conn.setRequestProperty(key, val);;
131 pos = Math.max(r, n);
134 if (call.getMethod() != null && conn instanceof HttpURLConnection) {
135 ((HttpURLConnection) conn).setRequestMethod(call.getMethod());
137 if (call.isDoOutput()) {
138 final OutputStream os = conn.getOutputStream();
142 final PushbackInputStream is = new PushbackInputStream(
143 conn.getInputStream(), 1
145 boolean[] arrayOrString = { false, false };
146 detectJSONType(call.isJSONP(), is, arrayOrString);
147 String response = readStream(is);
148 if (call.isJSONP()) {
149 response = '(' + response;
151 json = new Result(response, arrayOrString[0], arrayOrString[1]);
152 } catch (IOException ex) {
156 call.notifyError(error);
158 call.notifySuccess(json);
163 private static final class Result implements Callable<Object> {
164 private final String response;
165 private final boolean array;
166 private final boolean plain;
168 Result(String response, boolean array, boolean plain) {
169 this.response = response;
175 public Object call() throws Exception {
180 Object r = parse(response);
181 Object[] arr = r instanceof Object[] ? (Object[])r : new Object[] { r };
182 for (int i = 0; i < arr.length; i++) {
183 arr[i] = new JSObjToStr(response, arr[i]);
187 return new JSObjToStr(response, parse(response));
192 private static final class JSObjToStr {
196 public JSObjToStr(Object str, Object obj) {
197 this.str = str == null ? "" : str.toString();
202 public String toString() {
207 static String readStream(InputStream is) throws IOException, UnsupportedEncodingException {
208 Reader r = new InputStreamReader(is, "UTF-8");
209 StringBuilder sb = new StringBuilder();
210 char[] arr = new char[4096];
212 int len = r.read(arr);
216 sb.append(arr, 0, len);
218 return sb.toString();
221 private static void detectJSONType(boolean skipAnything, final PushbackInputStream is, boolean[] arrayOrString) throws IOException {
225 arrayOrString[1] = true;
228 if (Character.isWhitespace(ch)) {
234 arrayOrString[0] = true;
243 arrayOrString[1] = true;
249 @JavaScriptBody(args = {"object", "property"}, body =
251 "if (property === null) ret = object;\n" +
252 "else if (object === null) ret = null;\n" +
253 "else ret = object[property];\n" +
254 "return ret ? (typeof ko === 'undefined' ? ret : ko.utils.unwrapObservable(ret)) : null;"
256 private static Object getProperty(Object object, String property) {
260 @JavaScriptBody(args = {"s"}, body = "return eval('(' + s + ')');")
261 static Object parse(String s) {
262 throw new IllegalStateException("No parser context for " + s);
265 static void extractJSON(Object js, String[] props, Object[] values) {
266 if (js instanceof JSObjToStr) {
267 js = ((JSObjToStr)js).obj;
269 for (int i = 0; i < props.length; i++) {
270 values[i] = getProperty(js, props[i]);