jaroslav@260
|
1 |
/**
|
jaroslav@260
|
2 |
* HTML via Java(tm) Language Bindings
|
jaroslav@260
|
3 |
* Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jaroslav@260
|
4 |
*
|
jaroslav@260
|
5 |
* This program is free software: you can redistribute it and/or modify
|
jaroslav@260
|
6 |
* it under the terms of the GNU General Public License as published by
|
jaroslav@260
|
7 |
* the Free Software Foundation, version 2 of the License.
|
jaroslav@260
|
8 |
*
|
jaroslav@260
|
9 |
* This program is distributed in the hope that it will be useful,
|
jaroslav@260
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
jaroslav@260
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
jaroslav@260
|
12 |
* GNU General Public License for more details. apidesign.org
|
jaroslav@260
|
13 |
* designates this particular file as subject to the
|
jaroslav@260
|
14 |
* "Classpath" exception as provided by apidesign.org
|
jaroslav@260
|
15 |
* in the License file that accompanied this code.
|
jaroslav@260
|
16 |
*
|
jaroslav@260
|
17 |
* You should have received a copy of the GNU General Public License
|
jaroslav@260
|
18 |
* along with this program. Look for COPYING file in the top folder.
|
jaroslav@260
|
19 |
* If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
|
jaroslav@260
|
20 |
*/
|
jaroslav@260
|
21 |
package org.apidesign.html.wstyrus;
|
jaroslav@260
|
22 |
|
jaroslav@260
|
23 |
import java.io.IOException;
|
jaroslav@260
|
24 |
import java.net.URI;
|
jaroslav@260
|
25 |
import java.net.URISyntaxException;
|
jaroslav@260
|
26 |
import java.util.Iterator;
|
jaroslav@260
|
27 |
import javax.websocket.ClientEndpoint;
|
jaroslav@260
|
28 |
import javax.websocket.ContainerProvider;
|
jaroslav@260
|
29 |
import javax.websocket.DeploymentException;
|
jaroslav@260
|
30 |
import javax.websocket.OnClose;
|
jaroslav@260
|
31 |
import javax.websocket.OnError;
|
jaroslav@260
|
32 |
import javax.websocket.OnMessage;
|
jaroslav@260
|
33 |
import javax.websocket.OnOpen;
|
jaroslav@260
|
34 |
import javax.websocket.Session;
|
jaroslav@260
|
35 |
import javax.websocket.WebSocketContainer;
|
jaroslav@263
|
36 |
import net.java.html.json.OnReceive;
|
jaroslav@260
|
37 |
import org.apidesign.html.context.spi.Contexts;
|
jaroslav@260
|
38 |
import org.apidesign.html.json.spi.JSONCall;
|
jaroslav@260
|
39 |
import org.apidesign.html.json.spi.WSTransfer;
|
jaroslav@260
|
40 |
import org.apidesign.html.wstyrus.TyrusContext.Comm;
|
jaroslav@260
|
41 |
import org.json.JSONArray;
|
jaroslav@260
|
42 |
import org.json.JSONException;
|
jaroslav@260
|
43 |
import org.json.JSONObject;
|
jaroslav@260
|
44 |
import org.json.JSONTokener;
|
jaroslav@260
|
45 |
import org.openide.util.lookup.ServiceProvider;
|
jaroslav@260
|
46 |
|
jaroslav@263
|
47 |
/** This is an implementation module that provides support for
|
jaroslav@263
|
48 |
* WebSocket protocol for {@link OnReceive} communication end point for
|
jaroslav@263
|
49 |
* JDK7.
|
jaroslav@263
|
50 |
* <p>
|
jaroslav@263
|
51 |
* Don't deal with this module directly, rather use the
|
jaroslav@263
|
52 |
* {@link OnReceive @OnReceive(url="ws://...", ...)} API to establish your
|
jaroslav@263
|
53 |
* WebSocket connection.
|
jaroslav@263
|
54 |
* <p>
|
jaroslav@263
|
55 |
* There is no need to include this module in your application if you are
|
jaroslav@263
|
56 |
* running on JDK8. JDK8 WebView provides its own implementation of the
|
jaroslav@263
|
57 |
* WebSocket API based on WebSocket object inside a browser. This is included
|
jaroslav@263
|
58 |
* in the <code>org.apidesign.html:ko-fx:0.5</code> module.
|
jaroslav@260
|
59 |
*
|
jaroslav@260
|
60 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jaroslav@260
|
61 |
*/
|
jaroslav@260
|
62 |
@ServiceProvider(service = Contexts.Provider.class)
|
jaroslav@260
|
63 |
public final class TyrusContext implements Contexts.Provider, WSTransfer<Comm> {
|
jaroslav@260
|
64 |
@Override
|
jaroslav@260
|
65 |
public void fillContext(Contexts.Builder context, Class<?> requestor) {
|
jaroslav@260
|
66 |
// default WebSocket transfer implementation is registered
|
jaroslav@260
|
67 |
// in ko-fx module with 100, provide this one as a fallback only
|
jaroslav@260
|
68 |
context.register(WSTransfer.class, this, 1000);
|
jaroslav@260
|
69 |
}
|
jaroslav@260
|
70 |
|
jaroslav@260
|
71 |
@Override
|
jaroslav@260
|
72 |
public Comm open(String url, JSONCall callback) {
|
jaroslav@260
|
73 |
try {
|
jaroslav@260
|
74 |
return new Comm(new URI(url), callback);
|
jaroslav@260
|
75 |
} catch (URISyntaxException ex) {
|
jaroslav@260
|
76 |
throw new IllegalStateException(ex);
|
jaroslav@260
|
77 |
}
|
jaroslav@260
|
78 |
}
|
jaroslav@260
|
79 |
|
jaroslav@260
|
80 |
@Override
|
jaroslav@260
|
81 |
public void send(Comm socket, JSONCall data) {
|
jaroslav@260
|
82 |
socket.session.getAsyncRemote().sendText(data.getMessage());
|
jaroslav@260
|
83 |
}
|
jaroslav@260
|
84 |
|
jaroslav@260
|
85 |
@Override
|
jaroslav@260
|
86 |
public void close(Comm socket) {
|
jaroslav@260
|
87 |
try {
|
jaroslav@260
|
88 |
socket.session.close();
|
jaroslav@260
|
89 |
} catch (IOException ex) {
|
jaroslav@260
|
90 |
socket.callback.notifyError(ex);
|
jaroslav@260
|
91 |
}
|
jaroslav@260
|
92 |
}
|
jaroslav@260
|
93 |
|
jaroslav@263
|
94 |
/** Implementation class in an implementation. Represents a {@link ClientEndpoint} of the
|
jaroslav@263
|
95 |
* WebSocket channel. You are unlikely to get on hold of it.
|
jaroslav@263
|
96 |
*/
|
jaroslav@260
|
97 |
@ClientEndpoint
|
jaroslav@260
|
98 |
public static final class Comm {
|
jaroslav@260
|
99 |
private final JSONCall callback;
|
jaroslav@260
|
100 |
private Session session;
|
jaroslav@260
|
101 |
|
jaroslav@260
|
102 |
Comm(final URI url, JSONCall callback) {
|
jaroslav@260
|
103 |
this.callback = callback;
|
jaroslav@260
|
104 |
try {
|
jaroslav@260
|
105 |
final WebSocketContainer c = ContainerProvider.getWebSocketContainer();
|
jaroslav@260
|
106 |
c.connectToServer(Comm.this, url);
|
jaroslav@260
|
107 |
} catch (DeploymentException | IOException ex) {
|
jaroslav@260
|
108 |
wasAnError(ex);
|
jaroslav@260
|
109 |
}
|
jaroslav@260
|
110 |
}
|
jaroslav@260
|
111 |
|
jaroslav@260
|
112 |
@OnOpen
|
jaroslav@260
|
113 |
public synchronized void open(Session s) {
|
jaroslav@260
|
114 |
this.session = s;
|
jaroslav@260
|
115 |
callback.notifySuccess(null);
|
jaroslav@260
|
116 |
}
|
jaroslav@260
|
117 |
|
jaroslav@260
|
118 |
@OnClose
|
jaroslav@260
|
119 |
public void close() {
|
jaroslav@260
|
120 |
this.session = null;
|
jaroslav@260
|
121 |
callback.notifyError(null);
|
jaroslav@260
|
122 |
}
|
jaroslav@260
|
123 |
|
jaroslav@260
|
124 |
@OnMessage
|
jaroslav@260
|
125 |
public void message(final String orig, Session s) {
|
jaroslav@262
|
126 |
Object json;
|
jaroslav@262
|
127 |
String data = orig.trim();
|
jaroslav@262
|
128 |
try {
|
jaroslav@262
|
129 |
JSONTokener tok = new JSONTokener(data);
|
jaroslav@262
|
130 |
Object obj = data.startsWith("[") ? new JSONArray(tok) : new JSONObject(tok);
|
jaroslav@262
|
131 |
json = convertToArray(obj);
|
jaroslav@262
|
132 |
} catch (JSONException ex) {
|
jaroslav@262
|
133 |
json = data;
|
jaroslav@260
|
134 |
}
|
jaroslav@262
|
135 |
callback.notifySuccess(json);
|
jaroslav@260
|
136 |
}
|
jaroslav@260
|
137 |
|
jaroslav@260
|
138 |
@OnError
|
jaroslav@260
|
139 |
public void wasAnError(Throwable t) {
|
jaroslav@260
|
140 |
callback.notifyError(t);
|
jaroslav@260
|
141 |
}
|
jaroslav@260
|
142 |
|
jaroslav@260
|
143 |
static Object convertToArray(Object o) throws JSONException {
|
jaroslav@260
|
144 |
if (o instanceof JSONArray) {
|
jaroslav@260
|
145 |
JSONArray ja = (JSONArray) o;
|
jaroslav@260
|
146 |
Object[] arr = new Object[ja.length()];
|
jaroslav@260
|
147 |
for (int i = 0; i < arr.length; i++) {
|
jaroslav@260
|
148 |
arr[i] = convertToArray(ja.get(i));
|
jaroslav@260
|
149 |
}
|
jaroslav@260
|
150 |
return arr;
|
jaroslav@260
|
151 |
} else if (o instanceof JSONObject) {
|
jaroslav@260
|
152 |
JSONObject obj = (JSONObject) o;
|
jaroslav@260
|
153 |
Iterator it = obj.keys();
|
jaroslav@260
|
154 |
while (it.hasNext()) {
|
jaroslav@260
|
155 |
String key = (String) it.next();
|
jaroslav@260
|
156 |
obj.put(key, convertToArray(obj.get(key)));
|
jaroslav@260
|
157 |
}
|
jaroslav@260
|
158 |
return obj;
|
jaroslav@260
|
159 |
} else {
|
jaroslav@260
|
160 |
return o;
|
jaroslav@260
|
161 |
}
|
jaroslav@260
|
162 |
}
|
jaroslav@260
|
163 |
|
jaroslav@260
|
164 |
} // end of Comm
|
jaroslav@260
|
165 |
}
|