1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/xhr4j/src/main/java/org/netbeans/html/xhr4j/LoadJSON.java Thu Jul 21 11:21:58 2016 +0200
1.3 @@ -0,0 +1,274 @@
1.4 +/**
1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
1.6 + *
1.7 + * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
1.8 + *
1.9 + * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
1.10 + * Other names may be trademarks of their respective owners.
1.11 + *
1.12 + * The contents of this file are subject to the terms of either the GNU
1.13 + * General Public License Version 2 only ("GPL") or the Common
1.14 + * Development and Distribution License("CDDL") (collectively, the
1.15 + * "License"). You may not use this file except in compliance with the
1.16 + * License. You can obtain a copy of the License at
1.17 + * http://www.netbeans.org/cddl-gplv2.html
1.18 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
1.19 + * specific language governing permissions and limitations under the
1.20 + * License. When distributing the software, include this License Header
1.21 + * Notice in each file and include the License file at
1.22 + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
1.23 + * particular file as subject to the "Classpath" exception as provided
1.24 + * by Oracle in the GPL Version 2 section of the License file that
1.25 + * accompanied this code. If applicable, add the following below the
1.26 + * License Header, with the fields enclosed by brackets [] replaced by
1.27 + * your own identifying information:
1.28 + * "Portions Copyrighted [year] [name of copyright owner]"
1.29 + *
1.30 + * Contributor(s):
1.31 + *
1.32 + * The Original Software is NetBeans. The Initial Developer of the Original
1.33 + * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
1.34 + *
1.35 + * If you wish your version of this file to be governed by only the CDDL
1.36 + * or only the GPL Version 2, indicate your decision by adding
1.37 + * "[Contributor] elects to include this software in this distribution
1.38 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
1.39 + * single choice of license, a recipient has the option to distribute
1.40 + * your version of this file under either the CDDL, the GPL Version 2 or
1.41 + * to extend the choice of license to its licensees as provided above.
1.42 + * However, if you add GPL Version 2 code and therefore, elected the GPL
1.43 + * Version 2 license, then the option applies only if the new code is
1.44 + * made subject to such option by the copyright holder.
1.45 + */
1.46 +package org.netbeans.html.xhr4j;
1.47 +
1.48 +import java.io.IOException;
1.49 +import java.io.InputStream;
1.50 +import java.io.InputStreamReader;
1.51 +import java.io.OutputStream;
1.52 +import java.io.PushbackInputStream;
1.53 +import java.io.Reader;
1.54 +import java.io.UnsupportedEncodingException;
1.55 +import java.net.HttpURLConnection;
1.56 +import java.net.URL;
1.57 +import java.net.URLConnection;
1.58 +import java.util.concurrent.Callable;
1.59 +import java.util.concurrent.Executor;
1.60 +import java.util.concurrent.Executors;
1.61 +import java.util.concurrent.ThreadFactory;
1.62 +import java.util.logging.Logger;
1.63 +import net.java.html.js.JavaScriptBody;
1.64 +import org.netbeans.html.json.spi.JSONCall;
1.65 +
1.66 +/** This is an implementation package - just
1.67 + * include its JAR on classpath and use official {@link Context} API
1.68 + * to access the functionality.
1.69 + *
1.70 + * @author Jaroslav Tulach
1.71 + */
1.72 +final class LoadJSON implements Runnable {
1.73 + private static final Logger LOG = Logger.getLogger(LoadJSON.class.getName());
1.74 + private static final Executor REQ = Executors.newCachedThreadPool(new ThreadFactory() {
1.75 + @Override
1.76 + public Thread newThread(Runnable runnable) {
1.77 + Thread thread = Executors.defaultThreadFactory().newThread(runnable);
1.78 + thread.setDaemon(true);
1.79 + thread.setName("xhr4j daemon");
1.80 + return thread;
1.81 + }
1.82 + });
1.83 +
1.84 + private final JSONCall call;
1.85 + private final URL base;
1.86 +
1.87 +
1.88 + private LoadJSON(JSONCall call) {
1.89 + this.call = call;
1.90 + this.base = null;
1.91 + }
1.92 +
1.93 + public static void loadJSON(JSONCall call) {
1.94 + assert !"WebSocket".equals(call.getMethod());
1.95 + REQ.execute(new LoadJSON((call)));
1.96 + }
1.97 +
1.98 + @Override
1.99 + public void run() {
1.100 + final String url;
1.101 + Throwable error = null;
1.102 + Object json = null;
1.103 +
1.104 + if (call.isJSONP()) {
1.105 + url = call.composeURL("dummy");
1.106 + } else {
1.107 + url = call.composeURL(null);
1.108 + }
1.109 + try {
1.110 + final URL u = new URL(base, url.replace(" ", "%20"));
1.111 + URLConnection conn = u.openConnection();
1.112 + if (call.isDoOutput()) {
1.113 + conn.setDoOutput(true);
1.114 + }
1.115 + String h = call.getHeaders();
1.116 + if (h != null) {
1.117 + int pos = 0;
1.118 + while (pos < h.length()) {
1.119 + int tagEnd = h.indexOf(':', pos);
1.120 + if (tagEnd == -1) {
1.121 + break;
1.122 + }
1.123 + int r = h.indexOf('\r', tagEnd);
1.124 + int n = h.indexOf('\n', tagEnd);
1.125 + if (r == -1) {
1.126 + r = h.length();
1.127 + }
1.128 + if (n == -1) {
1.129 + n = h.length();
1.130 + }
1.131 + String key = h.substring(pos, tagEnd).trim();
1.132 + String val = h.substring(tagEnd + 1, Math.min(r, n)).trim();
1.133 + conn.setRequestProperty(key, val);;
1.134 + pos = Math.max(r, n);
1.135 + }
1.136 + }
1.137 + if (call.getMethod() != null && conn instanceof HttpURLConnection) {
1.138 + ((HttpURLConnection) conn).setRequestMethod(call.getMethod());
1.139 + }
1.140 + if (call.isDoOutput()) {
1.141 + final OutputStream os = conn.getOutputStream();
1.142 + call.writeData(os);
1.143 + os.flush();
1.144 + }
1.145 + final PushbackInputStream is = new PushbackInputStream(
1.146 + conn.getInputStream(), 1
1.147 + );
1.148 + boolean[] arrayOrString = { false, false };
1.149 + detectJSONType(call.isJSONP(), is, arrayOrString);
1.150 + String response = readStream(is);
1.151 + if (call.isJSONP()) {
1.152 + response = '(' + response;
1.153 + }
1.154 + json = new Result(response, arrayOrString[0], arrayOrString[1]);
1.155 + } catch (IOException ex) {
1.156 + error = ex;
1.157 + } finally {
1.158 + if (error != null) {
1.159 + call.notifyError(error);
1.160 + } else {
1.161 + call.notifySuccess(json);
1.162 + }
1.163 + }
1.164 + }
1.165 +
1.166 + private static final class Result implements Callable<Object> {
1.167 + private final String response;
1.168 + private final boolean array;
1.169 + private final boolean plain;
1.170 +
1.171 + Result(String response, boolean array, boolean plain) {
1.172 + this.response = response;
1.173 + this.array = array;
1.174 + this.plain = plain;
1.175 + }
1.176 +
1.177 + @Override
1.178 + public Object call() throws Exception {
1.179 + if (plain) {
1.180 + return response;
1.181 + } else {
1.182 + if (array) {
1.183 + Object r = parse(response);
1.184 + Object[] arr = r instanceof Object[] ? (Object[])r : new Object[] { r };
1.185 + for (int i = 0; i < arr.length; i++) {
1.186 + arr[i] = new JSObjToStr(response, arr[i]);
1.187 + }
1.188 + return arr;
1.189 + } else {
1.190 + return new JSObjToStr(response, parse(response));
1.191 + }
1.192 + }
1.193 + }
1.194 + }
1.195 + private static final class JSObjToStr {
1.196 + final String str;
1.197 + final Object obj;
1.198 +
1.199 + public JSObjToStr(Object str, Object obj) {
1.200 + this.str = str == null ? "" : str.toString();
1.201 + this.obj = obj;
1.202 + }
1.203 +
1.204 + @Override
1.205 + public String toString() {
1.206 + return str;
1.207 + }
1.208 + }
1.209 +
1.210 + static String readStream(InputStream is) throws IOException, UnsupportedEncodingException {
1.211 + Reader r = new InputStreamReader(is, "UTF-8");
1.212 + StringBuilder sb = new StringBuilder();
1.213 + char[] arr = new char[4096];
1.214 + for (;;) {
1.215 + int len = r.read(arr);
1.216 + if (len == -1) {
1.217 + break;
1.218 + }
1.219 + sb.append(arr, 0, len);
1.220 + }
1.221 + return sb.toString();
1.222 + }
1.223 +
1.224 + private static void detectJSONType(boolean skipAnything, final PushbackInputStream is, boolean[] arrayOrString) throws IOException {
1.225 + for (;;) {
1.226 + int ch = is.read();
1.227 + if (ch == -1) {
1.228 + arrayOrString[1] = true;
1.229 + break;
1.230 + }
1.231 + if (Character.isWhitespace(ch)) {
1.232 + continue;
1.233 + }
1.234 +
1.235 + if (ch == '[') {
1.236 + is.unread(ch);
1.237 + arrayOrString[0] = true;
1.238 + break;
1.239 + }
1.240 + if (ch == '{') {
1.241 + is.unread(ch);
1.242 + break;
1.243 + }
1.244 + if (!skipAnything) {
1.245 + is.unread(ch);
1.246 + arrayOrString[1] = true;
1.247 + break;
1.248 + }
1.249 + }
1.250 + }
1.251 +
1.252 + @JavaScriptBody(args = {"object", "property"}, body =
1.253 + "var ret;\n" +
1.254 + "if (property === null) ret = object;\n" +
1.255 + "else if (object === null) ret = null;\n" +
1.256 + "else ret = object[property];\n" +
1.257 + "return ret ? (typeof ko === 'undefined' ? ret : ko.utils.unwrapObservable(ret)) : null;"
1.258 + )
1.259 + private static Object getProperty(Object object, String property) {
1.260 + return null;
1.261 + }
1.262 +
1.263 + @JavaScriptBody(args = {"s"}, body = "return eval('(' + s + ')');")
1.264 + static Object parse(String s) {
1.265 + throw new IllegalStateException("No parser context for " + s);
1.266 + }
1.267 +
1.268 + static void extractJSON(Object js, String[] props, Object[] values) {
1.269 + if (js instanceof JSObjToStr) {
1.270 + js = ((JSObjToStr)js).obj;
1.271 + }
1.272 + for (int i = 0; i < props.length; i++) {
1.273 + values[i] = getProperty(js, props[i]);
1.274 + }
1.275 + }
1.276 +
1.277 +}