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