Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 76670a8524ead603035f2841aa533da28cde523e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
//
//  ========================================================================
//  Copyright (c) 1995-2015 Mort Bay Consulting Pty. Ltd.
//  ------------------------------------------------------------------------
//  All rights reserved. This program and the accompanying materials
//  are made available under the terms of the Eclipse Public License v1.0
//  and Apache License v2.0 which accompanies this distribution.
//
//      The Eclipse Public License is available at
//      http://www.eclipse.org/legal/epl-v10.html
//
//      The Apache License v2.0 is available at
//      http://www.opensource.org/licenses/apache2.0.php
//
//  You may elect to redistribute this code under either of these licenses.
//  ========================================================================
//

package org.eclipse.jetty.websocket.jsr356.annotations;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.lang.annotation.Annotation;
import java.nio.ByteBuffer;
import java.util.Map;

import javax.websocket.CloseReason;
import javax.websocket.DecodeException;
import javax.websocket.EndpointConfig;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.RemoteEndpoint;

import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.websocket.jsr356.JsrSession;

/**
 * The live event methods found for a specific Annotated Endpoint
 * @param <T> the annotation type
 * @param <C> the endpoint config type
 */
public class JsrEvents<T extends Annotation, C extends EndpointConfig>
{
    private static final Logger LOG = Log.getLogger(JsrEvents.class);
    private final AnnotatedEndpointMetadata<T, C> metadata;

    /**
     * Callable for &#064;{@link OnOpen} annotation.
     */
    private final OnOpenCallable onOpen;

    /**
     * Callable for &#064;{@link OnClose} annotation
     */
    private final OnCloseCallable onClose;

    /**
     * Callable for &#064;{@link OnError} annotation
     */
    private final OnErrorCallable onError;

    /**
     * Callable for &#064;{@link OnMessage} annotation dealing with Text Message Format
     */
    private final OnMessageTextCallable onText;

    /**
     * Callable for &#064;{@link OnMessage} annotation dealing with Text Streaming Message Format
     */
    private final OnMessageTextStreamCallable onTextStream;

    /**
     * Callable for &#064;{@link OnMessage} annotation dealing with Binary Message Format
     */
    private final OnMessageBinaryCallable onBinary;

    /**
     * Callable for &#064;{@link OnMessage} annotation dealing with Binary Streaming Message Format
     */
    private final OnMessageBinaryStreamCallable onBinaryStream;

    /**
     * Callable for &#064;{@link OnMessage} annotation dealing with Pong Message Format
     */
    private OnMessagePongCallable onPong;

    /**
     * The Request Parameters (from resolved javax.websocket.server.PathParam entries)
     */
    private Map<String, String> pathParameters;

    public JsrEvents(AnnotatedEndpointMetadata<T, C> metadata)
    {
        this.metadata = metadata;
        this.onOpen = (metadata.onOpen == null)?null:new OnOpenCallable(metadata.onOpen);
        this.onClose = (metadata.onClose == null)?null:new OnCloseCallable(metadata.onClose);
        this.onError = (metadata.onError == null)?null:new OnErrorCallable(metadata.onError);
        this.onBinary = (metadata.onBinary == null)?null:new OnMessageBinaryCallable(metadata.onBinary);
        this.onBinaryStream = (metadata.onBinaryStream == null)?null:new OnMessageBinaryStreamCallable(metadata.onBinaryStream);
        this.onText = (metadata.onText == null)?null:new OnMessageTextCallable(metadata.onText);
        this.onTextStream = (metadata.onTextStream == null)?null:new OnMessageTextStreamCallable(metadata.onTextStream);
        this.onPong = (metadata.onPong == null)?null:new OnMessagePongCallable(metadata.onPong);
    }

    public void callBinary(RemoteEndpoint.Async endpoint, Object websocket, ByteBuffer buf, boolean fin) throws DecodeException
    {
        if (onBinary == null)
        {
            return;
        }

        Object ret = onBinary.call(websocket,buf,fin);
        if (ret != null)
        {
            if (LOG.isDebugEnabled())
            {
                LOG.debug("returning: {}",ret);
            }
            endpoint.sendObject(ret);
        }
    }

    public void callBinaryStream(RemoteEndpoint.Async endpoint, Object websocket, InputStream stream) throws DecodeException, IOException
    {
        if (onBinaryStream == null)
        {
            return;
        }

        Object ret = onBinaryStream.call(websocket,stream);
        if (ret != null)
        {
            if (LOG.isDebugEnabled())
            {
                LOG.debug("returning: {}",ret);
            }
            endpoint.sendObject(ret);
        }
    }

    public void callClose(Object websocket, CloseReason close)
    {
        if (onClose == null)
        {
            return;
        }
        onClose.call(websocket,close);
    }

    public void callError(Object websocket, Throwable cause)
    {
        if (onError == null)
        {
            LOG.warn("Unable to report throwable to websocket (no @OnError handler declared): " + websocket.getClass().getName(), cause);
            return;
        }
        onError.call(websocket,cause);
    }

    public void callOpen(Object websocket, EndpointConfig config)
    {
        if (onOpen == null)
        {
            return;
        }
        onOpen.call(websocket,config);
    }

    public void callPong(RemoteEndpoint.Async endpoint, Object websocket, ByteBuffer pong)
    {
        if (onPong == null)
        {
            return;
        }

        Object ret = onPong.call(websocket,pong);
        if (ret != null)
        {
            if (LOG.isDebugEnabled())
            {
                LOG.debug("returning: {}",ret);
            }
            endpoint.sendObject(ret);
        }
    }

    public void callText(RemoteEndpoint.Async endpoint, Object websocket, String text, boolean fin) throws DecodeException
    {
        if (onText == null)
        {
            return;
        }
        Object ret = onText.call(websocket,text,fin);
        if (ret != null)
        {
            if (LOG.isDebugEnabled())
            {
                LOG.debug("returning: {}",ret);
            }
            endpoint.sendObject(ret);
        }
    }

    public void callTextStream(RemoteEndpoint.Async endpoint, Object websocket, Reader reader) throws DecodeException, IOException
    {
        if (onTextStream == null)
        {
            return;
        }
        Object ret = onTextStream.call(websocket,reader);
        if (ret != null)
        {
            if (LOG.isDebugEnabled())
            {
                LOG.debug("returning: {}",ret);
            }
            endpoint.sendObject(ret);
        }
    }

    public AnnotatedEndpointMetadata<T, C> getMetadata()
    {
        return metadata;
    }

    public boolean hasBinary()
    {
        return (onBinary != null);
    }

    public boolean hasBinaryStream()
    {
        return (onBinaryStream != null);
    }

    public boolean hasText()
    {
        return (onText != null);
    }

    public boolean hasTextStream()
    {
        return (onTextStream != null);
    }

    public void init(JsrSession session)
    {
        session.setPathParameters(pathParameters);

        if (onOpen != null)
        {
            onOpen.init(session);
        }
        if (onClose != null)
        {
            onClose.init(session);
        }
        if (onError != null)
        {
            onError.init(session);
        }
        if (onText != null)
        {
            onText.init(session);
        }
        if (onTextStream != null)
        {
            onTextStream.init(session);
        }
        if (onBinary != null)
        {
            onBinary.init(session);
        }
        if (onBinaryStream != null)
        {
            onBinaryStream.init(session);
        }
        if (onPong != null)
        {
            onPong.init(session);
        }
    }

    public boolean isBinaryPartialSupported()
    {
        if (onBinary == null)
        {
            return false;
        }
        return onBinary.isPartialMessageSupported();
    }

    public boolean isTextPartialSupported()
    {
        if (onText == null)
        {
            return false;
        }
        return onText.isPartialMessageSupported();
    }

    public void setPathParameters(Map<String, String> pathParameters)
    {
        this.pathParameters = pathParameters;
    }
}

Back to the top