ko-fx/src/main/java/org/apidesign/html/kofx/LoadJSON.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 16 Dec 2013 15:00:21 +0100
branchnetbeans
changeset 358 80702021b851
parent 258 5f446d17f6fa
permissions -rw-r--r--
Changing license to standard GPLv2CPE/CDDL
jaroslav@37
     1
/**
jaroslav@358
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@37
     3
 *
jaroslav@358
     4
 * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
jaroslav@37
     5
 *
jaroslav@358
     6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
jaroslav@358
     7
 * Other names may be trademarks of their respective owners.
jaroslav@37
     8
 *
jaroslav@358
     9
 * The contents of this file are subject to the terms of either the GNU
jaroslav@358
    10
 * General Public License Version 2 only ("GPL") or the Common
jaroslav@358
    11
 * Development and Distribution License("CDDL") (collectively, the
jaroslav@358
    12
 * "License"). You may not use this file except in compliance with the
jaroslav@358
    13
 * License. You can obtain a copy of the License at
jaroslav@358
    14
 * http://www.netbeans.org/cddl-gplv2.html
jaroslav@358
    15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jaroslav@358
    16
 * specific language governing permissions and limitations under the
jaroslav@358
    17
 * License.  When distributing the software, include this License Header
jaroslav@358
    18
 * Notice in each file and include the License file at
jaroslav@358
    19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
jaroslav@358
    20
 * particular file as subject to the "Classpath" exception as provided
jaroslav@358
    21
 * by Oracle in the GPL Version 2 section of the License file that
jaroslav@358
    22
 * accompanied this code. If applicable, add the following below the
jaroslav@358
    23
 * License Header, with the fields enclosed by brackets [] replaced by
jaroslav@358
    24
 * your own identifying information:
jaroslav@358
    25
 * "Portions Copyrighted [year] [name of copyright owner]"
jaroslav@358
    26
 *
jaroslav@358
    27
 * Contributor(s):
jaroslav@358
    28
 *
jaroslav@358
    29
 * The Original Software is NetBeans. The Initial Developer of the Original
jaroslav@358
    30
 * Software is Oracle. Portions Copyright 2013-2013 Oracle. All Rights Reserved.
jaroslav@358
    31
 *
jaroslav@358
    32
 * If you wish your version of this file to be governed by only the CDDL
jaroslav@358
    33
 * or only the GPL Version 2, indicate your decision by adding
jaroslav@358
    34
 * "[Contributor] elects to include this software in this distribution
jaroslav@358
    35
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
jaroslav@358
    36
 * single choice of license, a recipient has the option to distribute
jaroslav@358
    37
 * your version of this file under either the CDDL, the GPL Version 2 or
jaroslav@358
    38
 * to extend the choice of license to its licensees as provided above.
jaroslav@358
    39
 * However, if you add GPL Version 2 code and therefore, elected the GPL
jaroslav@358
    40
 * Version 2 license, then the option applies only if the new code is
jaroslav@358
    41
 * made subject to such option by the copyright holder.
jaroslav@37
    42
 */
jaroslav@37
    43
package org.apidesign.html.kofx;
jaroslav@37
    44
jaroslav@37
    45
import java.io.IOException;
jaroslav@60
    46
import java.io.InputStream;
jaroslav@37
    47
import java.io.InputStreamReader;
jaroslav@75
    48
import java.io.OutputStream;
jaroslav@37
    49
import java.io.PushbackInputStream;
jaroslav@37
    50
import java.io.Reader;
jaroslav@74
    51
import java.net.HttpURLConnection;
jaroslav@37
    52
import java.net.MalformedURLException;
jaroslav@37
    53
import java.net.URL;
jaroslav@74
    54
import java.net.URLConnection;
jaroslav@37
    55
import java.util.Iterator;
jaroslav@37
    56
import java.util.concurrent.Executor;
jaroslav@37
    57
import java.util.concurrent.Executors;
jaroslav@158
    58
import java.util.concurrent.ThreadFactory;
jaroslav@37
    59
import java.util.logging.Level;
jaroslav@37
    60
import java.util.logging.Logger;
jaroslav@37
    61
import javafx.application.Platform;
jaroslav@131
    62
import net.java.html.js.JavaScriptBody;
jaroslav@37
    63
import netscape.javascript.JSObject;
jaroslav@37
    64
import org.apidesign.html.json.spi.JSONCall;
jaroslav@37
    65
import org.json.JSONArray;
jaroslav@37
    66
import org.json.JSONException;
jaroslav@37
    67
import org.json.JSONObject;
jaroslav@37
    68
import org.json.JSONTokener;
jaroslav@37
    69
jaroslav@54
    70
/** This is an implementation package - just
jaroslav@54
    71
 * include its JAR on classpath and use official {@link Context} API
jaroslav@54
    72
 * to access the functionality.
jaroslav@37
    73
 *
jaroslav@37
    74
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@37
    75
 */
jaroslav@37
    76
final class LoadJSON implements Runnable {
jaroslav@37
    77
    private static final Logger LOG = FXContext.LOG;
jaroslav@158
    78
    private static final Executor REQ = Executors.newCachedThreadPool(new ThreadFactory() {
jaroslav@158
    79
        @Override
jaroslav@158
    80
        public Thread newThread(Runnable runnable) {
jaroslav@158
    81
            Thread thread = Executors.defaultThreadFactory().newThread(runnable);
jaroslav@158
    82
            thread.setDaemon(true);
jaroslav@158
    83
            return thread;
jaroslav@158
    84
        }
jaroslav@158
    85
    });
jaroslav@37
    86
jaroslav@37
    87
    private final JSONCall call;
jaroslav@37
    88
    private final URL base;
jaroslav@37
    89
    private Throwable error;
jaroslav@37
    90
    private Object json;
jaroslav@37
    91
jaroslav@37
    92
jaroslav@37
    93
    private LoadJSON(JSONCall call) {
jaroslav@37
    94
        this.call = call;
jaroslav@37
    95
        URL b;
jaroslav@37
    96
        try {
jaroslav@37
    97
            b = new URL(findBaseURL());
jaroslav@37
    98
        } catch (MalformedURLException ex) {
jaroslav@37
    99
            LOG.log(Level.SEVERE, "Can't find base url for " + call.composeURL("dummy"), ex);
jaroslav@37
   100
            b = null;
jaroslav@37
   101
        }
jaroslav@37
   102
        this.base = b;
jaroslav@37
   103
    }
jaroslav@37
   104
jaroslav@37
   105
    public static void loadJSON(JSONCall call) {
jaroslav@258
   106
        assert !"WebSocket".equals(call.getMethod());
jaroslav@258
   107
        REQ.execute(new LoadJSON((call)));
jaroslav@37
   108
    }
jaroslav@37
   109
jaroslav@37
   110
    @Override
jaroslav@37
   111
    public void run() {
jaroslav@37
   112
        if (Platform.isFxApplicationThread()) {
jaroslav@37
   113
            if (error != null) {
jaroslav@37
   114
                call.notifyError(error);
jaroslav@37
   115
            } else {
jaroslav@37
   116
                call.notifySuccess(json);
jaroslav@37
   117
            }
jaroslav@37
   118
            return;
jaroslav@37
   119
        }
jaroslav@37
   120
        final String url;
jaroslav@37
   121
        if (call.isJSONP()) {
jaroslav@37
   122
            url = call.composeURL("dummy");
jaroslav@37
   123
        } else {
jaroslav@37
   124
            url = call.composeURL(null);
jaroslav@37
   125
        }
jaroslav@37
   126
        try {
jaroslav@37
   127
            final URL u = new URL(base, url.replace(" ", "%20"));
jaroslav@74
   128
            URLConnection conn = u.openConnection();
jaroslav@74
   129
            if (conn instanceof HttpURLConnection) {
jaroslav@74
   130
                HttpURLConnection huc = (HttpURLConnection) conn;
jaroslav@74
   131
                if (call.getMethod() != null) {
jaroslav@74
   132
                    huc.setRequestMethod(call.getMethod());
jaroslav@74
   133
                }
jaroslav@75
   134
                if (call.isDoOutput()) {
jaroslav@75
   135
                    huc.setDoOutput(true);
jaroslav@75
   136
                    final OutputStream os = huc.getOutputStream();
jaroslav@75
   137
                    call.writeData(os);
jaroslav@75
   138
                    os.flush();
jaroslav@75
   139
                }
jaroslav@74
   140
            }
jaroslav@74
   141
            final PushbackInputStream is = new PushbackInputStream(
jaroslav@74
   142
                conn.getInputStream(), 1
jaroslav@74
   143
            );
jaroslav@37
   144
            boolean array = false;
jaroslav@73
   145
            boolean string = false;
jaroslav@37
   146
            if (call.isJSONP()) {
jaroslav@37
   147
                for (;;) {
jaroslav@37
   148
                    int ch = is.read();
jaroslav@37
   149
                    if (ch == -1) {
jaroslav@37
   150
                        break;
jaroslav@37
   151
                    }
jaroslav@37
   152
                    if (ch == '[') {
jaroslav@37
   153
                        is.unread(ch);
jaroslav@37
   154
                        array = true;
jaroslav@37
   155
                        break;
jaroslav@37
   156
                    }
jaroslav@37
   157
                    if (ch == '{') {
jaroslav@37
   158
                        is.unread(ch);
jaroslav@37
   159
                        break;
jaroslav@37
   160
                    }
jaroslav@37
   161
                }
jaroslav@37
   162
            } else {
jaroslav@37
   163
                int ch = is.read();
jaroslav@138
   164
                if (ch == -1) {
jaroslav@73
   165
                    string = true;
jaroslav@138
   166
                } else {
jaroslav@138
   167
                    array = ch == '[';
jaroslav@138
   168
                    is.unread(ch);
jaroslav@138
   169
                    if (!array && ch != '{') {
jaroslav@138
   170
                        string = true;
jaroslav@138
   171
                    }
jaroslav@73
   172
                }
jaroslav@37
   173
            }
jaroslav@73
   174
            try {
jaroslav@73
   175
                if (string) {
jaroslav@73
   176
                    throw new JSONException("");
jaroslav@73
   177
                }
jaroslav@73
   178
                Reader r = new InputStreamReader(is, "UTF-8");
jaroslav@37
   179
jaroslav@73
   180
                JSONTokener tok = new JSONTokener(r);
jaroslav@73
   181
                Object obj;
jaroslav@73
   182
                obj = array ? new JSONArray(tok) : new JSONObject(tok);
jaroslav@73
   183
                json = convertToArray(obj);
jaroslav@73
   184
            } catch (JSONException ex) {
jaroslav@73
   185
                Reader r = new InputStreamReader(is, "UTF-8");
jaroslav@73
   186
                StringBuilder sb = new StringBuilder();
jaroslav@73
   187
                for (;;) {
jaroslav@73
   188
                    int ch = r.read();
jaroslav@73
   189
                    if (ch == -1) {
jaroslav@73
   190
                        break;
jaroslav@73
   191
                    }
jaroslav@73
   192
                    sb.append((char)ch);
jaroslav@73
   193
                }
jaroslav@73
   194
                json = sb.toString();
jaroslav@73
   195
            }
jaroslav@73
   196
        } catch (IOException ex) {
jaroslav@37
   197
            error = ex;
jaroslav@37
   198
        } finally {
jaroslav@37
   199
            Platform.runLater(this);
jaroslav@37
   200
        }
jaroslav@37
   201
    }
jaroslav@37
   202
jaroslav@247
   203
    static Object convertToArray(Object o) throws JSONException {
jaroslav@37
   204
        if (o instanceof JSONArray) {
jaroslav@37
   205
            JSONArray ja = (JSONArray)o;
jaroslav@37
   206
            Object[] arr = new Object[ja.length()];
jaroslav@37
   207
            for (int i = 0; i < arr.length; i++) {
jaroslav@37
   208
                arr[i] = convertToArray(ja.get(i));
jaroslav@37
   209
            }
jaroslav@37
   210
            return arr;
jaroslav@37
   211
        } else if (o instanceof JSONObject) {
jaroslav@37
   212
            JSONObject obj = (JSONObject)o;
jaroslav@37
   213
            Iterator it = obj.keys();
jaroslav@37
   214
            while (it.hasNext()) {
jaroslav@37
   215
                String key = (String)it.next();
jaroslav@37
   216
                obj.put(key, convertToArray(obj.get(key)));
jaroslav@37
   217
            }
jaroslav@37
   218
            return obj;
jaroslav@37
   219
        } else {
jaroslav@37
   220
            return o;
jaroslav@37
   221
        }
jaroslav@37
   222
    }
jaroslav@37
   223
    
jaroslav@37
   224
    public static void extractJSON(Object jsonObject, String[] props, Object[] values) {
jaroslav@37
   225
        if (jsonObject instanceof JSONObject) {
jaroslav@37
   226
            JSONObject obj = (JSONObject)jsonObject;
jaroslav@37
   227
            for (int i = 0; i < props.length; i++) {
jaroslav@37
   228
                try {
jaroslav@37
   229
                    values[i] = obj.has(props[i]) ? obj.get(props[i]) : null;
jaroslav@37
   230
                } catch (JSONException ex) {
jaroslav@37
   231
                    LoadJSON.LOG.log(Level.SEVERE, "Can't read " + props[i] + " from " + jsonObject, ex);
jaroslav@37
   232
                }
jaroslav@37
   233
            }
jaroslav@37
   234
        }
jaroslav@37
   235
        if (jsonObject instanceof JSObject) {
jaroslav@37
   236
            JSObject obj = (JSObject)jsonObject;
jaroslav@37
   237
            for (int i = 0; i < props.length; i++) {
jaroslav@37
   238
                Object val = obj.getMember(props[i]);
jaroslav@37
   239
                values[i] = isDefined(val) ? val : null;
jaroslav@37
   240
            }
jaroslav@37
   241
        }
jaroslav@37
   242
    }
jaroslav@37
   243
    
jaroslav@60
   244
    public static Object parse(InputStream is) throws IOException {
jaroslav@60
   245
        try {
jaroslav@60
   246
            InputStreamReader r = new InputStreamReader(is, "UTF-8");
jaroslav@60
   247
            JSONTokener t = new JSONTokener(r);
jaroslav@60
   248
            return new JSONObject(t);
jaroslav@60
   249
        } catch (JSONException ex) {
jaroslav@60
   250
            throw new IOException(ex);
jaroslav@60
   251
        }
jaroslav@60
   252
    }
jaroslav@118
   253
jaroslav@118
   254
    @JavaScriptBody(args = {  }, body = 
jaroslav@118
   255
          "var h;"
jaroslav@118
   256
        + "if (!!window && !!window.location && !!window.location.href)\n"
jaroslav@118
   257
        + "  h = window.location.href;\n"
jaroslav@118
   258
        + "else "
jaroslav@118
   259
        + "  h = null;"
jaroslav@118
   260
        + "return h;\n"
jaroslav@118
   261
    )
jaroslav@118
   262
    private static native String findBaseURL();
jaroslav@60
   263
    
jaroslav@37
   264
    private static boolean isDefined(Object val) {
jaroslav@37
   265
        return !"undefined".equals(val);
jaroslav@37
   266
    }
jaroslav@37
   267
}