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