ko-ws-tyrus/src/main/java/org/netbeans/html/wstyrus/LoadJSON.java
author Jaroslav Tulach <jtulach@netbeans.org>
Wed, 27 May 2015 23:37:25 +0200
changeset 940 bdec4103bdb2
parent 838 bdc3d696dd4a
child 1020 b5d5cbb44ce0
permissions -rw-r--r--
#252258: @OnReceive attribute to specify additional request headers
jaroslav@37
     1
/**
jaroslav@358
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@37
     3
 *
jaroslav@551
     4
 * Copyright 2013-2014 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@551
    30
 * Software is Oracle. Portions Copyright 2013-2014 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@446
    43
package org.netbeans.html.wstyrus;
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.URL;
jaroslav@74
    53
import java.net.URLConnection;
jtulach@760
    54
import java.util.ArrayList;
jaroslav@37
    55
import java.util.Iterator;
jtulach@760
    56
import java.util.List;
jaroslav@37
    57
import java.util.concurrent.Executor;
jaroslav@37
    58
import java.util.concurrent.Executors;
jaroslav@158
    59
import java.util.concurrent.ThreadFactory;
jaroslav@37
    60
import java.util.logging.Logger;
jaroslav@131
    61
import net.java.html.js.JavaScriptBody;
jaroslav@37
    62
import org.json.JSONArray;
jaroslav@37
    63
import org.json.JSONException;
jaroslav@37
    64
import org.json.JSONObject;
jaroslav@37
    65
import org.json.JSONTokener;
jtulach@940
    66
import org.netbeans.html.json.spi.JSONCall;
jaroslav@37
    67
jaroslav@54
    68
/** This is an implementation package - just
jaroslav@54
    69
 * include its JAR on classpath and use official {@link Context} API
jaroslav@54
    70
 * to access the functionality.
jaroslav@37
    71
 *
jtulach@790
    72
 * @author Jaroslav Tulach
jaroslav@37
    73
 */
jaroslav@37
    74
final class LoadJSON implements Runnable {
jaroslav@446
    75
    private static final Logger LOG = Logger.getLogger(LoadJSON.class.getName());
jaroslav@158
    76
    private static final Executor REQ = Executors.newCachedThreadPool(new ThreadFactory() {
jaroslav@158
    77
        @Override
jaroslav@158
    78
        public Thread newThread(Runnable runnable) {
jaroslav@158
    79
            Thread thread = Executors.defaultThreadFactory().newThread(runnable);
jaroslav@158
    80
            thread.setDaemon(true);
jaroslav@158
    81
            return thread;
jaroslav@158
    82
        }
jaroslav@158
    83
    });
jaroslav@37
    84
jaroslav@37
    85
    private final JSONCall call;
jaroslav@37
    86
    private final URL base;
jaroslav@37
    87
jaroslav@37
    88
jaroslav@37
    89
    private LoadJSON(JSONCall call) {
jaroslav@37
    90
        this.call = call;
jaroslav@446
    91
        this.base = null;
jaroslav@37
    92
    }
jaroslav@37
    93
jaroslav@37
    94
    public static void loadJSON(JSONCall call) {
jaroslav@258
    95
        assert !"WebSocket".equals(call.getMethod());
jaroslav@258
    96
        REQ.execute(new LoadJSON((call)));
jaroslav@37
    97
    }
jaroslav@37
    98
jaroslav@37
    99
    @Override
jaroslav@37
   100
    public void run() {
jaroslav@37
   101
        final String url;
jaroslav@446
   102
        Throwable error = null;
jaroslav@446
   103
        Object json = null;
jtulach@940
   104
jaroslav@37
   105
        if (call.isJSONP()) {
jaroslav@37
   106
            url = call.composeURL("dummy");
jaroslav@37
   107
        } else {
jaroslav@37
   108
            url = call.composeURL(null);
jaroslav@37
   109
        }
jaroslav@37
   110
        try {
jaroslav@37
   111
            final URL u = new URL(base, url.replace(" ", "%20"));
jaroslav@74
   112
            URLConnection conn = u.openConnection();
jaroslav@527
   113
            if (call.isDoOutput()) {
jaroslav@527
   114
                conn.setDoOutput(true);
jaroslav@527
   115
            }
jtulach@940
   116
            String h = call.getHeaders();
jtulach@940
   117
            if (h != null) {
jtulach@940
   118
                int pos = 0;
jtulach@940
   119
                while (pos < h.length()) {
jtulach@940
   120
                    int tagEnd = h.indexOf(':', pos);
jtulach@940
   121
                    if (tagEnd == -1) {
jtulach@940
   122
                        break;
jtulach@940
   123
                    }
jtulach@940
   124
                    int r = h.indexOf('\r', tagEnd);
jtulach@940
   125
                    int n = h.indexOf('\n', tagEnd);
jtulach@940
   126
                    if (r == -1) {
jtulach@940
   127
                        r = h.length();
jtulach@940
   128
                    }
jtulach@940
   129
                    if (n == -1) {
jtulach@940
   130
                        n = h.length();
jtulach@940
   131
                    }
jtulach@940
   132
                    String key = h.substring(pos, tagEnd).trim();
jtulach@940
   133
                    String val = h.substring(tagEnd + 1, Math.min(r, n)).trim();
jtulach@940
   134
                    conn.setRequestProperty(key, val);;
jtulach@940
   135
                    pos = Math.max(r, n);
jtulach@940
   136
                }
jtulach@940
   137
            }
jaroslav@527
   138
            if (call.getMethod() != null && conn instanceof HttpURLConnection) {
jaroslav@527
   139
                ((HttpURLConnection) conn).setRequestMethod(call.getMethod());
jaroslav@527
   140
            }
jaroslav@527
   141
            if (call.isDoOutput()) {
jaroslav@527
   142
                final OutputStream os = conn.getOutputStream();
jaroslav@527
   143
                call.writeData(os);
jaroslav@527
   144
                os.flush();
jaroslav@74
   145
            }
jaroslav@74
   146
            final PushbackInputStream is = new PushbackInputStream(
jaroslav@74
   147
                conn.getInputStream(), 1
jaroslav@74
   148
            );
jtulach@745
   149
            boolean[] arrayOrString = { false, false };
jtulach@745
   150
            detectJSONType(call.isJSONP(), is, arrayOrString);
jaroslav@73
   151
            try {
jtulach@745
   152
                if (arrayOrString[1]) {
jaroslav@73
   153
                    throw new JSONException("");
jaroslav@73
   154
                }
jaroslav@525
   155
                JSONTokener tok = createTokener(is);
jaroslav@73
   156
                Object obj;
jtulach@745
   157
                obj = arrayOrString[0] ? new JSONArray(tok) : new JSONObject(tok);
jaroslav@73
   158
                json = convertToArray(obj);
jaroslav@73
   159
            } catch (JSONException ex) {
jaroslav@73
   160
                Reader r = new InputStreamReader(is, "UTF-8");
jaroslav@73
   161
                StringBuilder sb = new StringBuilder();
jaroslav@73
   162
                for (;;) {
jaroslav@73
   163
                    int ch = r.read();
jaroslav@73
   164
                    if (ch == -1) {
jaroslav@73
   165
                        break;
jaroslav@73
   166
                    }
jaroslav@73
   167
                    sb.append((char)ch);
jaroslav@73
   168
                }
jaroslav@73
   169
                json = sb.toString();
jaroslav@73
   170
            }
jaroslav@73
   171
        } catch (IOException ex) {
jaroslav@37
   172
            error = ex;
jaroslav@37
   173
        } finally {
jaroslav@446
   174
            if (error != null) {
jaroslav@446
   175
                call.notifyError(error);
jaroslav@446
   176
            } else {
jaroslav@446
   177
                call.notifySuccess(json);
jaroslav@446
   178
            }
jaroslav@37
   179
        }
jaroslav@37
   180
    }
jaroslav@37
   181
jtulach@745
   182
    private static void detectJSONType(boolean skipAnything, final PushbackInputStream is, boolean[] arrayOrString) throws IOException {
jtulach@745
   183
        for (;;) {
jtulach@745
   184
            int ch = is.read();
jtulach@745
   185
            if (ch == -1) {
jtulach@745
   186
                arrayOrString[1] = true;
jtulach@745
   187
                break;
jtulach@745
   188
            }
jtulach@745
   189
            if (Character.isWhitespace(ch)) {
jtulach@745
   190
                continue;
jtulach@745
   191
            }
jtulach@745
   192
jtulach@745
   193
            if (ch == '[') {
jtulach@745
   194
                is.unread(ch);
jtulach@745
   195
                arrayOrString[0] = true;
jtulach@745
   196
                break;
jtulach@745
   197
            }
jtulach@745
   198
            if (ch == '{') {
jtulach@745
   199
                is.unread(ch);
jtulach@745
   200
                break;
jtulach@745
   201
            }
jtulach@745
   202
            if (!skipAnything) {
jtulach@745
   203
                is.unread(ch);
jtulach@745
   204
                arrayOrString[1] = true;
jtulach@745
   205
                break;
jtulach@745
   206
            }
jtulach@745
   207
        }
jtulach@745
   208
    }
jtulach@745
   209
jaroslav@525
   210
    private static JSONTokener createTokener(InputStream is) throws IOException {
jaroslav@525
   211
        Reader r = new InputStreamReader(is, "UTF-8");
jaroslav@525
   212
        try {
jaroslav@525
   213
            return new JSONTokener(r);
jaroslav@525
   214
        } catch (LinkageError ex) {
jaroslav@525
   215
            // phones may carry outdated version of JSONTokener
jaroslav@525
   216
            StringBuilder sb = new StringBuilder();
jaroslav@525
   217
            for (;;) {
jaroslav@525
   218
                int ch = r.read();
jaroslav@525
   219
                if (ch == -1) {
jaroslav@525
   220
                    break;
jaroslav@525
   221
                }
jaroslav@525
   222
                sb.append((char)ch);
jaroslav@525
   223
            }
jaroslav@525
   224
            return new JSONTokener(sb.toString());
jaroslav@525
   225
        }
jaroslav@525
   226
    }
jaroslav@525
   227
jaroslav@247
   228
    static Object convertToArray(Object o) throws JSONException {
jaroslav@37
   229
        if (o instanceof JSONArray) {
jaroslav@37
   230
            JSONArray ja = (JSONArray)o;
jaroslav@37
   231
            Object[] arr = new Object[ja.length()];
jaroslav@37
   232
            for (int i = 0; i < arr.length; i++) {
jaroslav@37
   233
                arr[i] = convertToArray(ja.get(i));
jaroslav@37
   234
            }
jaroslav@37
   235
            return arr;
jaroslav@37
   236
        } else if (o instanceof JSONObject) {
jaroslav@37
   237
            JSONObject obj = (JSONObject)o;
jaroslav@37
   238
            Iterator it = obj.keys();
jtulach@760
   239
            List<Object> collect = new ArrayList<Object>();
jaroslav@37
   240
            while (it.hasNext()) {
jaroslav@37
   241
                String key = (String)it.next();
jtulach@760
   242
                final Object val = obj.get(key);
jtulach@760
   243
                final Object newVal = convertToArray(val);
jtulach@760
   244
                if (val != newVal) {
jtulach@760
   245
                    collect.add(key);
jtulach@760
   246
                    collect.add(newVal);
jtulach@760
   247
                }
jtulach@760
   248
            }
jtulach@760
   249
            int size = collect.size();
jtulach@760
   250
            for (int i = 0; i < size; i += 2) {
jtulach@760
   251
                obj.put((String) collect.get(i), collect.get(i + 1));
jaroslav@37
   252
            }
jaroslav@37
   253
            return obj;
jtulach@750
   254
        } else if (o == JSONObject.NULL) {
jtulach@750
   255
            return null;
jaroslav@37
   256
        } else {
jaroslav@37
   257
            return o;
jaroslav@37
   258
        }
jaroslav@37
   259
    }
jtulach@940
   260
jaroslav@37
   261
    public static void extractJSON(Object jsonObject, String[] props, Object[] values) {
jaroslav@37
   262
        if (jsonObject instanceof JSONObject) {
jaroslav@37
   263
            JSONObject obj = (JSONObject)jsonObject;
jaroslav@37
   264
            for (int i = 0; i < props.length; i++) {
jtulach@749
   265
                Object val = obj.opt(props[i]);
jtulach@749
   266
                if (val == JSONObject.NULL) {
jtulach@749
   267
                    val = null;
jaroslav@37
   268
                }
jtulach@749
   269
                values[i] = val;
jaroslav@37
   270
            }
jaroslav@446
   271
            return;
jaroslav@37
   272
        }
jaroslav@446
   273
        for (int i = 0; i < props.length; i++) {
jaroslav@446
   274
            values[i] = getProperty(jsonObject, props[i]);
jaroslav@37
   275
        }
jaroslav@37
   276
    }
jtulach@940
   277
jaroslav@446
   278
    @JavaScriptBody(args = {"object", "property"},
jaroslav@446
   279
            body
jaroslav@446
   280
            = "if (property === null) return object;\n"
jaroslav@446
   281
            + "if (object === null) return null;\n"
jaroslav@446
   282
            + "var p = object[property]; return p ? p : null;"
jaroslav@446
   283
    )
jaroslav@446
   284
    private static Object getProperty(Object object, String property) {
jaroslav@446
   285
        return null;
jaroslav@446
   286
    }
jtulach@940
   287
jaroslav@60
   288
    public static Object parse(InputStream is) throws IOException {
jaroslav@60
   289
        try {
jtulach@745
   290
            PushbackInputStream push = new PushbackInputStream(is, 1);
jtulach@745
   291
            boolean[] arrayOrString = { false, false };
jtulach@745
   292
            detectJSONType(false, push, arrayOrString);
jtulach@745
   293
            JSONTokener t = createTokener(push);
jtulach@745
   294
            Object obj = arrayOrString[0] ? new JSONArray(t) : new JSONObject(t);
jtulach@745
   295
            return convertToArray(obj);
jaroslav@60
   296
        } catch (JSONException ex) {
jaroslav@60
   297
            throw new IOException(ex);
jaroslav@60
   298
        }
jaroslav@60
   299
    }
jaroslav@37
   300
}