View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
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  import javax.websocket.CloseReason;
28  import javax.websocket.DecodeException;
29  import javax.websocket.EndpointConfig;
30  import javax.websocket.RemoteEndpoint;
31  
32  import org.eclipse.jetty.util.log.Log;
33  import org.eclipse.jetty.util.log.Logger;
34  import org.eclipse.jetty.websocket.jsr356.JsrSession;
35  
36  /**
37   * The live event methods found for a specific Annotated Endpoint
38   */
39  public class JsrEvents<T extends Annotation, C extends EndpointConfig>
40  {
41      private static final Logger LOG = Log.getLogger(JsrEvents.class);
42      private final AnnotatedEndpointMetadata<T, C> metadata;
43  
44      /**
45       * Callable for &#064;{@link OnOpen} annotation.
46       */
47      private final OnOpenCallable onOpen;
48  
49      /**
50       * Callable for &#064;{@link OnClose} annotation
51       */
52      private final OnCloseCallable onClose;
53  
54      /**
55       * Callable for &#064;{@link OnError} annotation
56       */
57      private final OnErrorCallable onError;
58  
59      /**
60       * Callable for &#064;{@link OnMessage} annotation dealing with Text Message Format
61       */
62      private final OnMessageTextCallable onText;
63  
64      /**
65       * Callable for &#064;{@link OnMessage} annotation dealing with Text Streaming Message Format
66       */
67      private final OnMessageTextStreamCallable onTextStream;
68  
69      /**
70       * Callable for &#064;{@link OnMessage} annotation dealing with Binary Message Format
71       */
72      private final OnMessageBinaryCallable onBinary;
73  
74      /**
75       * Callable for &#064;{@link OnMessage} annotation dealing with Binary Streaming Message Format
76       */
77      private final OnMessageBinaryStreamCallable onBinaryStream;
78  
79      /**
80       * Callable for &#064;{@link OnMessage} annotation dealing with Pong Message Format
81       */
82      private OnMessagePongCallable onPong;
83  
84      /**
85       * The Request Parameters (from resolved javax.websocket.server.PathParam entries)
86       */
87      private Map<String, String> pathParameters;
88  
89      public JsrEvents(AnnotatedEndpointMetadata<T, C> metadata)
90      {
91          this.metadata = metadata;
92          this.onOpen = (metadata.onOpen == null)?null:new OnOpenCallable(metadata.onOpen);
93          this.onClose = (metadata.onClose == null)?null:new OnCloseCallable(metadata.onClose);
94          this.onError = (metadata.onError == null)?null:new OnErrorCallable(metadata.onError);
95          this.onBinary = (metadata.onBinary == null)?null:new OnMessageBinaryCallable(metadata.onBinary);
96          this.onBinaryStream = (metadata.onBinaryStream == null)?null:new OnMessageBinaryStreamCallable(metadata.onBinaryStream);
97          this.onText = (metadata.onText == null)?null:new OnMessageTextCallable(metadata.onText);
98          this.onTextStream = (metadata.onTextStream == null)?null:new OnMessageTextStreamCallable(metadata.onTextStream);
99          this.onPong = (metadata.onPong == null)?null:new OnMessagePongCallable(metadata.onPong);
100     }
101 
102     public void callBinary(RemoteEndpoint.Async endpoint, Object websocket, ByteBuffer buf, boolean fin) throws DecodeException
103     {
104         if (onBinary == null)
105         {
106             return;
107         }
108 
109         Object ret = onBinary.call(websocket,buf,fin);
110         if (ret != null)
111         {
112             if (LOG.isDebugEnabled())
113             {
114                 LOG.debug("returning: {}",ret);
115             }
116             endpoint.sendObject(ret);
117         }
118     }
119 
120     public void callBinaryStream(RemoteEndpoint.Async endpoint, Object websocket, InputStream stream) throws DecodeException, IOException
121     {
122         if (onBinaryStream == null)
123         {
124             return;
125         }
126 
127         Object ret = onBinaryStream.call(websocket,stream);
128         if (ret != null)
129         {
130             if (LOG.isDebugEnabled())
131             {
132                 LOG.debug("returning: {}",ret);
133             }
134             endpoint.sendObject(ret);
135         }
136     }
137 
138     public void callClose(Object websocket, CloseReason close)
139     {
140         if (onClose == null)
141         {
142             return;
143         }
144         onClose.call(websocket,close);
145     }
146 
147     public void callError(Object websocket, Throwable cause)
148     {
149         if (onError == null)
150         {
151             return;
152         }
153         onError.call(websocket,cause);
154     }
155 
156     public void callOpen(Object websocket, EndpointConfig config)
157     {
158         if (onOpen == null)
159         {
160             return;
161         }
162         onOpen.call(websocket,config);
163     }
164 
165     public void callPong(RemoteEndpoint.Async endpoint, Object websocket, ByteBuffer pong) throws DecodeException, IOException
166     {
167         if (onPong == null)
168         {
169             return;
170         }
171 
172         Object ret = onPong.call(websocket,pong);
173         if (ret != null)
174         {
175             if (LOG.isDebugEnabled())
176             {
177                 LOG.debug("returning: {}",ret);
178             }
179             endpoint.sendObject(ret);
180         }
181     }
182 
183     public void callText(RemoteEndpoint.Async endpoint, Object websocket, String text, boolean fin) throws DecodeException
184     {
185         if (onText == null)
186         {
187             return;
188         }
189         Object ret = onText.call(websocket,text,fin);
190         if (ret != null)
191         {
192             if (LOG.isDebugEnabled())
193             {
194                 LOG.debug("returning: {}",ret);
195             }
196             endpoint.sendObject(ret);
197         }
198     }
199 
200     public void callTextStream(RemoteEndpoint.Async endpoint, Object websocket, Reader reader) throws DecodeException, IOException
201     {
202         if (onTextStream == null)
203         {
204             return;
205         }
206         Object ret = onTextStream.call(websocket,reader);
207         if (ret != null)
208         {
209             if (LOG.isDebugEnabled())
210             {
211                 LOG.debug("returning: {}",ret);
212             }
213             endpoint.sendObject(ret);
214         }
215     }
216 
217     public AnnotatedEndpointMetadata<T, C> getMetadata()
218     {
219         return metadata;
220     }
221 
222     public boolean hasBinary()
223     {
224         return (onBinary != null);
225     }
226 
227     public boolean hasBinaryStream()
228     {
229         return (onBinaryStream != null);
230     }
231 
232     public boolean hasText()
233     {
234         return (onText != null);
235     }
236 
237     public boolean hasTextStream()
238     {
239         return (onTextStream != null);
240     }
241 
242     public void init(JsrSession session)
243     {
244         session.setPathParameters(pathParameters);
245 
246         if (onOpen != null)
247         {
248             onOpen.init(session);
249         }
250         if (onClose != null)
251         {
252             onClose.init(session);
253         }
254         if (onError != null)
255         {
256             onError.init(session);
257         }
258         if (onText != null)
259         {
260             onText.init(session);
261         }
262         if (onTextStream != null)
263         {
264             onTextStream.init(session);
265         }
266         if (onBinary != null)
267         {
268             onBinary.init(session);
269         }
270         if (onBinaryStream != null)
271         {
272             onBinaryStream.init(session);
273         }
274         if (onPong != null)
275         {
276             onPong.init(session);
277         }
278     }
279 
280     public boolean isBinaryPartialSupported()
281     {
282         if (onBinary == null)
283         {
284             return false;
285         }
286         return onBinary.isPartialMessageSupported();
287     }
288 
289     public boolean isTextPartialSupported()
290     {
291         if (onText == null)
292         {
293             return false;
294         }
295         return onText.isPartialMessageSupported();
296     }
297 
298     public void setPathParameters(Map<String, String> pathParameters)
299     {
300         this.pathParameters = pathParameters;
301     }
302 }