ko-ws-tyrus/src/main/java/org/netbeans/html/wstyrus/TyrusContext.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 16 Dec 2013 16:59:43 +0100
branchnetbeans
changeset 362 92fb71afdc0e
parent 358 ko-ws-tyrus/src/main/java/org/apidesign/html/wstyrus/TyrusContext.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.wstyrus;
    44 
    45 import java.io.IOException;
    46 import java.net.URI;
    47 import java.net.URISyntaxException;
    48 import java.util.Iterator;
    49 import javax.websocket.ClientEndpoint;
    50 import javax.websocket.ContainerProvider;
    51 import javax.websocket.DeploymentException;
    52 import javax.websocket.OnClose;
    53 import javax.websocket.OnError;
    54 import javax.websocket.OnMessage;
    55 import javax.websocket.OnOpen;
    56 import javax.websocket.Session;
    57 import javax.websocket.WebSocketContainer;
    58 import net.java.html.json.OnReceive;
    59 import org.apidesign.html.context.spi.Contexts;
    60 import org.apidesign.html.json.spi.JSONCall;
    61 import org.apidesign.html.json.spi.WSTransfer;
    62 import org.netbeans.html.wstyrus.TyrusContext.Comm;
    63 import org.json.JSONArray;
    64 import org.json.JSONException;
    65 import org.json.JSONObject;
    66 import org.json.JSONTokener;
    67 import org.openide.util.lookup.ServiceProvider;
    68 
    69 /** This is an implementation module that provides support for
    70  * WebSocket protocol for {@link OnReceive} communication end point for
    71  * JDK7.
    72  * <p>
    73  * Don't deal with this module directly, rather use the 
    74  * {@link OnReceive @OnReceive(url="ws://...", ...)} API to establish your
    75  * WebSocket connection.
    76  * <p>
    77  * There is no need to include this module in your application if you are
    78  * running on JDK8. JDK8 WebView provides its own implementation of the
    79  * WebSocket API based on WebSocket object inside a browser. This is included
    80  * in the <code>org.apidesign.html:ko-fx:0.5</code> module.
    81  *
    82  * @author Jaroslav Tulach <jtulach@netbeans.org>
    83  */
    84 @ServiceProvider(service = Contexts.Provider.class)
    85 public final class TyrusContext implements Contexts.Provider, WSTransfer<Comm> {
    86     @Override
    87     public void fillContext(Contexts.Builder context, Class<?> requestor) {
    88         // default WebSocket transfer implementation is registered
    89         // in ko-fx module with 100, provide this one as a fallback only
    90         context.register(WSTransfer.class, this, 1000);
    91     }
    92 
    93     @Override
    94     public Comm open(String url, JSONCall callback) {
    95         try {
    96             return new Comm(new URI(url), callback);
    97         } catch (URISyntaxException ex) {
    98             throw new IllegalStateException(ex);
    99         }
   100     }
   101 
   102     @Override
   103     public void send(Comm socket, JSONCall data) {
   104         socket.session.getAsyncRemote().sendText(data.getMessage());
   105     }
   106 
   107     @Override
   108     public void close(Comm socket) {
   109         try {
   110             final Session s = socket.session;
   111             if (s != null) {
   112                 s.close();
   113             }
   114         } catch (IOException ex) {
   115             socket.callback.notifyError(ex);
   116         }
   117     }
   118     
   119     /** Implementation class in an implementation. Represents a {@link ClientEndpoint} of the
   120      * WebSocket channel. You are unlikely to get on hold of it.
   121      */
   122     @ClientEndpoint
   123     public static final class Comm {
   124         private final JSONCall callback;
   125         private Session session;
   126 
   127         Comm(final URI url, JSONCall callback) {
   128             this.callback = callback;
   129             try {
   130                 final WebSocketContainer c = ContainerProvider.getWebSocketContainer();
   131                 c.connectToServer(Comm.this, url);
   132             } catch (DeploymentException | IOException ex) {
   133                 wasAnError(ex);
   134             }
   135         }
   136 
   137         @OnOpen
   138         public synchronized void open(Session s) {
   139             this.session = s;
   140             callback.notifySuccess(null);
   141         }
   142 
   143         @OnClose
   144         public void close() {
   145             this.session = null;
   146             callback.notifyError(null);
   147         }
   148 
   149         @OnMessage
   150         public void message(final String orig, Session s) {
   151             Object json;
   152             String data = orig.trim();
   153             try {
   154                 JSONTokener tok = new JSONTokener(data);
   155                 Object obj = data.startsWith("[") ? new JSONArray(tok) : new JSONObject(tok);
   156                 json = convertToArray(obj);
   157             } catch (JSONException ex) {
   158                 json = data;
   159             }
   160             callback.notifySuccess(json);
   161         }
   162 
   163         @OnError
   164         public void wasAnError(Throwable t) {
   165             callback.notifyError(t);
   166         }
   167 
   168         static Object convertToArray(Object o) throws JSONException {
   169             if (o instanceof JSONArray) {
   170                 JSONArray ja = (JSONArray) o;
   171                 Object[] arr = new Object[ja.length()];
   172                 for (int i = 0; i < arr.length; i++) {
   173                     arr[i] = convertToArray(ja.get(i));
   174                 }
   175                 return arr;
   176             } else if (o instanceof JSONObject) {
   177                 JSONObject obj = (JSONObject) o;
   178                 Iterator it = obj.keys();
   179                 while (it.hasNext()) {
   180                     String key = (String) it.next();
   181                     obj.put(key, convertToArray(obj.get(key)));
   182                 }
   183                 return obj;
   184             } else {
   185                 return o;
   186             }
   187         }
   188         
   189     } // end of Comm
   190 }