1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.websocket.jsr356.annotations;
20
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.Reader;
24 import java.lang.annotation.Annotation;
25 import java.nio.ByteBuffer;
26 import java.util.Map;
27
28 import javax.websocket.CloseReason;
29 import javax.websocket.DecodeException;
30 import javax.websocket.EndpointConfig;
31 import javax.websocket.OnClose;
32 import javax.websocket.OnError;
33 import javax.websocket.OnMessage;
34 import javax.websocket.OnOpen;
35 import javax.websocket.RemoteEndpoint;
36
37 import org.eclipse.jetty.util.log.Log;
38 import org.eclipse.jetty.util.log.Logger;
39 import org.eclipse.jetty.websocket.jsr356.JsrSession;
40
41
42
43
44 public class JsrEvents<T extends Annotation, C extends EndpointConfig>
45 {
46 private static final Logger LOG = Log.getLogger(JsrEvents.class);
47 private final AnnotatedEndpointMetadata<T, C> metadata;
48
49
50
51
52 private final OnOpenCallable onOpen;
53
54
55
56
57 private final OnCloseCallable onClose;
58
59
60
61
62 private final OnErrorCallable onError;
63
64
65
66
67 private final OnMessageTextCallable onText;
68
69
70
71
72 private final OnMessageTextStreamCallable onTextStream;
73
74
75
76
77 private final OnMessageBinaryCallable onBinary;
78
79
80
81
82 private final OnMessageBinaryStreamCallable onBinaryStream;
83
84
85
86
87 private OnMessagePongCallable onPong;
88
89
90
91
92 private Map<String, String> pathParameters;
93
94 public JsrEvents(AnnotatedEndpointMetadata<T, C> metadata)
95 {
96 this.metadata = metadata;
97 this.onOpen = (metadata.onOpen == null)?null:new OnOpenCallable(metadata.onOpen);
98 this.onClose = (metadata.onClose == null)?null:new OnCloseCallable(metadata.onClose);
99 this.onError = (metadata.onError == null)?null:new OnErrorCallable(metadata.onError);
100 this.onBinary = (metadata.onBinary == null)?null:new OnMessageBinaryCallable(metadata.onBinary);
101 this.onBinaryStream = (metadata.onBinaryStream == null)?null:new OnMessageBinaryStreamCallable(metadata.onBinaryStream);
102 this.onText = (metadata.onText == null)?null:new OnMessageTextCallable(metadata.onText);
103 this.onTextStream = (metadata.onTextStream == null)?null:new OnMessageTextStreamCallable(metadata.onTextStream);
104 this.onPong = (metadata.onPong == null)?null:new OnMessagePongCallable(metadata.onPong);
105 }
106
107 public void callBinary(RemoteEndpoint.Async endpoint, Object websocket, ByteBuffer buf, boolean fin) throws DecodeException
108 {
109 if (onBinary == null)
110 {
111 return;
112 }
113
114 Object ret = onBinary.call(websocket,buf,fin);
115 if (ret != null)
116 {
117 LOG.debug("returning: {}",ret);
118 endpoint.sendObject(ret);
119 }
120 }
121
122 public void callBinaryStream(RemoteEndpoint.Async endpoint, Object websocket, InputStream stream) throws DecodeException, IOException
123 {
124 if (onBinaryStream == null)
125 {
126 return;
127 }
128
129 Object ret = onBinaryStream.call(websocket,stream);
130 if (ret != null)
131 {
132 LOG.debug("returning: {}",ret);
133 endpoint.sendObject(ret);
134 }
135 }
136
137 public void callClose(Object websocket, CloseReason close)
138 {
139 if (onClose == null)
140 {
141 return;
142 }
143 onClose.call(websocket,close);
144 }
145
146 public void callError(Object websocket, Throwable cause)
147 {
148 if (onError == null)
149 {
150 return;
151 }
152 onError.call(websocket,cause);
153 }
154
155 public void callOpen(Object websocket, EndpointConfig config)
156 {
157 if (onOpen == null)
158 {
159 return;
160 }
161 onOpen.call(websocket,config);
162 }
163
164 public void callPong(RemoteEndpoint.Async endpoint, Object websocket, ByteBuffer pong) throws DecodeException, IOException
165 {
166 if (onPong == null)
167 {
168 return;
169 }
170
171 Object ret = onPong.call(websocket,pong);
172 if (ret != null)
173 {
174 LOG.debug("returning: {}",ret);
175 endpoint.sendObject(ret);
176 }
177 }
178
179 public void callText(RemoteEndpoint.Async endpoint, Object websocket, String text, boolean fin) throws DecodeException
180 {
181 if (onText == null)
182 {
183 return;
184 }
185 Object ret = onText.call(websocket,text,fin);
186 if (ret != null)
187 {
188 LOG.debug("returning: {}",ret);
189 endpoint.sendObject(ret);
190 }
191 }
192
193 public void callTextStream(RemoteEndpoint.Async endpoint, Object websocket, Reader reader) throws DecodeException, IOException
194 {
195 if (onTextStream == null)
196 {
197 return;
198 }
199 Object ret = onTextStream.call(websocket,reader);
200 if (ret != null)
201 {
202 LOG.debug("returning: {}",ret);
203 endpoint.sendObject(ret);
204 }
205 }
206
207 public AnnotatedEndpointMetadata<T, C> getMetadata()
208 {
209 return metadata;
210 }
211
212 public boolean hasBinary()
213 {
214 return (onBinary != null);
215 }
216
217 public boolean hasBinaryStream()
218 {
219 return (onBinaryStream != null);
220 }
221
222 public boolean hasText()
223 {
224 return (onText != null);
225 }
226
227 public boolean hasTextStream()
228 {
229 return (onTextStream != null);
230 }
231
232 public void init(JsrSession session)
233 {
234 session.setPathParameters(pathParameters);
235
236 if (onOpen != null)
237 {
238 onOpen.init(session);
239 }
240 if (onClose != null)
241 {
242 onClose.init(session);
243 }
244 if (onError != null)
245 {
246 onError.init(session);
247 }
248 if (onText != null)
249 {
250 onText.init(session);
251 }
252 if (onTextStream != null)
253 {
254 onTextStream.init(session);
255 }
256 if (onBinary != null)
257 {
258 onBinary.init(session);
259 }
260 if (onBinaryStream != null)
261 {
262 onBinaryStream.init(session);
263 }
264 if (onPong != null)
265 {
266 onPong.init(session);
267 }
268 }
269
270 public boolean isBinaryPartialSupported()
271 {
272 if (onBinary == null)
273 {
274 return false;
275 }
276 return onBinary.isPartialMessageSupported();
277 }
278
279 public boolean isTextPartialSupported()
280 {
281 if (onText == null)
282 {
283 return false;
284 }
285 return onText.isPartialMessageSupported();
286 }
287
288 public void setPathParameters(Map<String, String> pathParameters)
289 {
290 this.pathParameters = pathParameters;
291 }
292 }