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;
20  
21  import javax.websocket.MessageHandler;
22  
23  import org.eclipse.jetty.websocket.jsr356.metadata.MessageHandlerMetadata;
24  
25  /**
26   * Expose a {@link MessageHandler} instance along with its associated {@link MessageHandlerMetadata} and {@link DecoderFactory.Wrapper}
27   */
28  public class MessageHandlerWrapper
29  {
30      private final MessageHandler handler;
31      private final MessageHandlerMetadata metadata;
32      private final DecoderFactory.Wrapper decoder;
33  
34      public MessageHandlerWrapper(MessageHandler handler, MessageHandlerMetadata metadata, DecoderFactory.Wrapper decoder)
35      {
36          this.handler = handler;
37          this.metadata = metadata;
38          this.decoder = decoder;
39      }
40  
41      public DecoderFactory.Wrapper getDecoder()
42      {
43          return decoder;
44      }
45  
46      public MessageHandler getHandler()
47      {
48          return handler;
49      }
50  
51      public MessageHandlerMetadata getMetadata()
52      {
53          return metadata;
54      }
55  
56      public boolean isMessageType(Class<?> msgType)
57      {
58          return msgType.isAssignableFrom(metadata.getMessageClass());
59      }
60  
61      /**
62       * Flag for a onMessage() that wants partial messages.
63       * <p>
64       * This indicates the use of MessageHandler.{@link Partial}.
65       * 
66       * @return true for use of MessageHandler.{@link Partial}, false for use of MessageHandler.{@link Whole}
67       */
68      public boolean wantsPartialMessages()
69      {
70          return metadata.isPartialSupported();
71      }
72  
73      /**
74       * Flag for a onMessage() method that wants MessageHandler.{@link Whole} with a Decoder that is based on {@link TextStream} or {@link BinaryStream}
75       * 
76       * @return true for Streaming based Decoder, false for normal decoder for whole messages.
77       */
78      public boolean wantsStreams()
79      {
80          return decoder.getMetadata().isStreamed();
81      }
82  }