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 javafx.application.Platform;
|
jaroslav@260
|
28 |
import javax.websocket.ClientEndpoint;
|
jaroslav@260
|
29 |
import javax.websocket.ContainerProvider;
|
jaroslav@260
|
30 |
import javax.websocket.DeploymentException;
|
jaroslav@260
|
31 |
import javax.websocket.OnClose;
|
jaroslav@260
|
32 |
import javax.websocket.OnError;
|
jaroslav@260
|
33 |
import javax.websocket.OnMessage;
|
jaroslav@260
|
34 |
import javax.websocket.OnOpen;
|
jaroslav@260
|
35 |
import javax.websocket.Session;
|
jaroslav@260
|
36 |
import javax.websocket.WebSocketContainer;
|
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@260
|
47 |
/**
|
jaroslav@260
|
48 |
*
|
jaroslav@260
|
49 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jaroslav@260
|
50 |
*/
|
jaroslav@260
|
51 |
@ServiceProvider(service = Contexts.Provider.class)
|
jaroslav@260
|
52 |
public final class TyrusContext implements Contexts.Provider, WSTransfer<Comm> {
|
jaroslav@260
|
53 |
@Override
|
jaroslav@260
|
54 |
public void fillContext(Contexts.Builder context, Class<?> requestor) {
|
jaroslav@260
|
55 |
// default WebSocket transfer implementation is registered
|
jaroslav@260
|
56 |
// in ko-fx module with 100, provide this one as a fallback only
|
jaroslav@260
|
57 |
context.register(WSTransfer.class, this, 1000);
|
jaroslav@260
|
58 |
}
|
jaroslav@260
|
59 |
|
jaroslav@260
|
60 |
@Override
|
jaroslav@260
|
61 |
public Comm open(String url, JSONCall callback) {
|
jaroslav@260
|
62 |
try {
|
jaroslav@260
|
63 |
return new Comm(new URI(url), callback);
|
jaroslav@260
|
64 |
} catch (URISyntaxException ex) {
|
jaroslav@260
|
65 |
throw new IllegalStateException(ex);
|
jaroslav@260
|
66 |
}
|
jaroslav@260
|
67 |
}
|
jaroslav@260
|
68 |
|
jaroslav@260
|
69 |
@Override
|
jaroslav@260
|
70 |
public void send(Comm socket, JSONCall data) {
|
jaroslav@260
|
71 |
socket.session.getAsyncRemote().sendText(data.getMessage());
|
jaroslav@260
|
72 |
}
|
jaroslav@260
|
73 |
|
jaroslav@260
|
74 |
@Override
|
jaroslav@260
|
75 |
public void close(Comm socket) {
|
jaroslav@260
|
76 |
try {
|
jaroslav@260
|
77 |
socket.session.close();
|
jaroslav@260
|
78 |
} catch (IOException ex) {
|
jaroslav@260
|
79 |
socket.callback.notifyError(ex);
|
jaroslav@260
|
80 |
}
|
jaroslav@260
|
81 |
}
|
jaroslav@260
|
82 |
|
jaroslav@260
|
83 |
@ClientEndpoint
|
jaroslav@260
|
84 |
public static final class Comm {
|
jaroslav@260
|
85 |
private final JSONCall callback;
|
jaroslav@260
|
86 |
private Session session;
|
jaroslav@260
|
87 |
|
jaroslav@260
|
88 |
Comm(final URI url, JSONCall callback) {
|
jaroslav@260
|
89 |
this.callback = callback;
|
jaroslav@260
|
90 |
try {
|
jaroslav@260
|
91 |
final WebSocketContainer c = ContainerProvider.getWebSocketContainer();
|
jaroslav@260
|
92 |
c.connectToServer(Comm.this, url);
|
jaroslav@260
|
93 |
} catch (DeploymentException | IOException ex) {
|
jaroslav@260
|
94 |
wasAnError(ex);
|
jaroslav@260
|
95 |
}
|
jaroslav@260
|
96 |
}
|
jaroslav@260
|
97 |
|
jaroslav@260
|
98 |
@OnOpen
|
jaroslav@260
|
99 |
public synchronized void open(Session s) {
|
jaroslav@260
|
100 |
this.session = s;
|
jaroslav@260
|
101 |
callback.notifySuccess(null);
|
jaroslav@260
|
102 |
}
|
jaroslav@260
|
103 |
|
jaroslav@260
|
104 |
@OnClose
|
jaroslav@260
|
105 |
public void close() {
|
jaroslav@260
|
106 |
this.session = null;
|
jaroslav@260
|
107 |
callback.notifyError(null);
|
jaroslav@260
|
108 |
}
|
jaroslav@260
|
109 |
|
jaroslav@260
|
110 |
@OnMessage
|
jaroslav@260
|
111 |
public void message(final String orig, Session s) {
|
jaroslav@260
|
112 |
class R implements Runnable {
|
jaroslav@260
|
113 |
Object json;
|
jaroslav@260
|
114 |
public R() {
|
jaroslav@260
|
115 |
String data = orig.trim();
|
jaroslav@260
|
116 |
try {
|
jaroslav@260
|
117 |
JSONTokener tok = new JSONTokener(data);
|
jaroslav@260
|
118 |
Object obj = data.startsWith("[") ? new JSONArray(tok) : new JSONObject(tok);
|
jaroslav@260
|
119 |
json = convertToArray(obj);
|
jaroslav@260
|
120 |
} catch (JSONException ex) {
|
jaroslav@260
|
121 |
json = data;
|
jaroslav@260
|
122 |
}
|
jaroslav@260
|
123 |
}
|
jaroslav@260
|
124 |
@Override
|
jaroslav@260
|
125 |
public void run() {
|
jaroslav@260
|
126 |
callback.notifySuccess(json);
|
jaroslav@260
|
127 |
}
|
jaroslav@260
|
128 |
}
|
jaroslav@260
|
129 |
Platform.runLater(new R());
|
jaroslav@260
|
130 |
}
|
jaroslav@260
|
131 |
|
jaroslav@260
|
132 |
@OnError
|
jaroslav@260
|
133 |
public void wasAnError(Throwable t) {
|
jaroslav@260
|
134 |
callback.notifyError(t);
|
jaroslav@260
|
135 |
}
|
jaroslav@260
|
136 |
|
jaroslav@260
|
137 |
static Object convertToArray(Object o) throws JSONException {
|
jaroslav@260
|
138 |
if (o instanceof JSONArray) {
|
jaroslav@260
|
139 |
JSONArray ja = (JSONArray) o;
|
jaroslav@260
|
140 |
Object[] arr = new Object[ja.length()];
|
jaroslav@260
|
141 |
for (int i = 0; i < arr.length; i++) {
|
jaroslav@260
|
142 |
arr[i] = convertToArray(ja.get(i));
|
jaroslav@260
|
143 |
}
|
jaroslav@260
|
144 |
return arr;
|
jaroslav@260
|
145 |
} else if (o instanceof JSONObject) {
|
jaroslav@260
|
146 |
JSONObject obj = (JSONObject) o;
|
jaroslav@260
|
147 |
Iterator it = obj.keys();
|
jaroslav@260
|
148 |
while (it.hasNext()) {
|
jaroslav@260
|
149 |
String key = (String) it.next();
|
jaroslav@260
|
150 |
obj.put(key, convertToArray(obj.get(key)));
|
jaroslav@260
|
151 |
}
|
jaroslav@260
|
152 |
return obj;
|
jaroslav@260
|
153 |
} else {
|
jaroslav@260
|
154 |
return o;
|
jaroslav@260
|
155 |
}
|
jaroslav@260
|
156 |
}
|
jaroslav@260
|
157 |
|
jaroslav@260
|
158 |
} // end of Comm
|
jaroslav@260
|
159 |
}
|