ko-fx/src/main/java/org/netbeans/html/kofx/LoadJSON.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 16 Dec 2013 16:59:43 +0100
branchnetbeans
changeset 362 92fb71afdc0e
parent 358 ko-fx/src/main/java/org/apidesign/html/kofx/LoadJSON.java@80702021b851
child 365 5c93ad8c7a15
permissions -rw-r--r--
Moving implementation classes into org.netbeans.html namespace
     1 /**
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
     5  *
     6  * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
     7  * Other names may be trademarks of their respective owners.
     8  *
     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]"
    26  *
    27  * Contributor(s):
    28  *
    29  * The Original Software is NetBeans. The Initial Developer of the Original
    30  * Software is Oracle. Portions Copyright 2013-2013 Oracle. All Rights Reserved.
    31  *
    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.
    42  */
    43 package org.netbeans.html.kofx;
    44 
    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;
    53 import java.net.URL;
    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;
    69 
    70 /** This is an implementation package - just
    71  * include its JAR on classpath and use official {@link Context} API
    72  * to access the functionality.
    73  *
    74  * @author Jaroslav Tulach <jtulach@netbeans.org>
    75  */
    76 final class LoadJSON implements Runnable {
    77     private static final Logger LOG = FXContext.LOG;
    78     private static final Executor REQ = Executors.newCachedThreadPool(new ThreadFactory() {
    79         @Override
    80         public Thread newThread(Runnable runnable) {
    81             Thread thread = Executors.defaultThreadFactory().newThread(runnable);
    82             thread.setDaemon(true);
    83             return thread;
    84         }
    85     });
    86 
    87     private final JSONCall call;
    88     private final URL base;
    89     private Throwable error;
    90     private Object json;
    91 
    92 
    93     private LoadJSON(JSONCall call) {
    94         this.call = call;
    95         URL b;
    96         try {
    97             b = new URL(findBaseURL());
    98         } catch (MalformedURLException ex) {
    99             LOG.log(Level.SEVERE, "Can't find base url for " + call.composeURL("dummy"), ex);
   100             b = null;
   101         }
   102         this.base = b;
   103     }
   104 
   105     public static void loadJSON(JSONCall call) {
   106         assert !"WebSocket".equals(call.getMethod());
   107         REQ.execute(new LoadJSON((call)));
   108     }
   109 
   110     @Override
   111     public void run() {
   112         if (Platform.isFxApplicationThread()) {
   113             if (error != null) {
   114                 call.notifyError(error);
   115             } else {
   116                 call.notifySuccess(json);
   117             }
   118             return;
   119         }
   120         final String url;
   121         if (call.isJSONP()) {
   122             url = call.composeURL("dummy");
   123         } else {
   124             url = call.composeURL(null);
   125         }
   126         try {
   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());
   133                 }
   134                 if (call.isDoOutput()) {
   135                     huc.setDoOutput(true);
   136                     final OutputStream os = huc.getOutputStream();
   137                     call.writeData(os);
   138                     os.flush();
   139                 }
   140             }
   141             final PushbackInputStream is = new PushbackInputStream(
   142                 conn.getInputStream(), 1
   143             );
   144             boolean array = false;
   145             boolean string = false;
   146             if (call.isJSONP()) {
   147                 for (;;) {
   148                     int ch = is.read();
   149                     if (ch == -1) {
   150                         break;
   151                     }
   152                     if (ch == '[') {
   153                         is.unread(ch);
   154                         array = true;
   155                         break;
   156                     }
   157                     if (ch == '{') {
   158                         is.unread(ch);
   159                         break;
   160                     }
   161                 }
   162             } else {
   163                 int ch = is.read();
   164                 if (ch == -1) {
   165                     string = true;
   166                 } else {
   167                     array = ch == '[';
   168                     is.unread(ch);
   169                     if (!array && ch != '{') {
   170                         string = true;
   171                     }
   172                 }
   173             }
   174             try {
   175                 if (string) {
   176                     throw new JSONException("");
   177                 }
   178                 Reader r = new InputStreamReader(is, "UTF-8");
   179 
   180                 JSONTokener tok = new JSONTokener(r);
   181                 Object obj;
   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();
   187                 for (;;) {
   188                     int ch = r.read();
   189                     if (ch == -1) {
   190                         break;
   191                     }
   192                     sb.append((char)ch);
   193                 }
   194                 json = sb.toString();
   195             }
   196         } catch (IOException ex) {
   197             error = ex;
   198         } finally {
   199             Platform.runLater(this);
   200         }
   201     }
   202 
   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));
   209             }
   210             return arr;
   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)));
   217             }
   218             return obj;
   219         } else {
   220             return o;
   221         }
   222     }
   223     
   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++) {
   228                 try {
   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);
   232                 }
   233             }
   234         }
   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;
   240             }
   241         }
   242     }
   243     
   244     public static Object parse(InputStream is) throws IOException {
   245         try {
   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);
   251         }
   252     }
   253 
   254     @JavaScriptBody(args = {  }, body = 
   255           "var h;"
   256         + "if (!!window && !!window.location && !!window.location.href)\n"
   257         + "  h = window.location.href;\n"
   258         + "else "
   259         + "  h = null;"
   260         + "return h;\n"
   261     )
   262     private static native String findBaseURL();
   263     
   264     private static boolean isDefined(Object val) {
   265         return !"undefined".equals(val);
   266     }
   267 }