@Override public String getAcceptedProtocol() { checkNativeSessionInitialized(); return this.acceptedProtocol; }
/** * Determine if a given message can be decoded. * @see #decode(Object) * @see javax.websocket.Decoder.Text#willDecode(String) * @see javax.websocket.Decoder.Binary#willDecode(ByteBuffer) */ public boolean willDecode(M bytes) { return getConversionService().canConvert(getType(), getMessageType()); }
@Override public int getBinaryMessageSizeLimit() { checkNativeSessionInitialized(); return getNativeSession().getMaxBinaryMessageBufferSize(); }
@Before public void setup() { this.session = mock(Session.class); this.webSocketHandler = mock(WebSocketHandler.class); this.webSocketSession = new StandardWebSocketSession(null, null, null, null); this.adapter = new StandardWebSocketHandlerAdapter(this.webSocketHandler, this.webSocketSession); }
@Test public void cannotFindConversionService() throws Exception { setup(NoConfig.class); MyBinaryEncoder encoder = new MyBinaryEncoder(); encoder.init(null); thown.expect(IllegalStateException.class); thown.expectMessage("Unable to find ConversionService"); encoder.encode(myType); }
@Bean public ConversionService webSocketConversionService() { GenericConversionService conversionService = new DefaultConversionService(); conversionService.addConverter(new MyTypeToStringConverter()); conversionService.addConverter(new MyTypeToBytesConverter()); conversionService.addConverter(new StringToMyTypeConverter()); conversionService.addConverter(new BytesToMyTypeConverter()); return conversionService; }
/** * Returns the type being converted. By default the type is resolved using * the generic arguments of the class. */ protected TypeDescriptor getType() { return TypeDescriptor.valueOf(resolveTypeArguments()[0]); }
@Override public boolean isOpen() { return getNativeSession().isOpen(); }
@Test public void encodeToTextCannotConvert() throws Exception { setup(NoConvertersConfig.class); thown.expect(EncodeException.class); thown.expectCause(isA(ConverterNotFoundException.class)); new MyTextEncoder().encode(myType); }
@Test public void encodeToBinaryCannotConvert() throws Exception { setup(NoConvertersConfig.class); thown.expect(EncodeException.class); thown.expectCause(isA(ConverterNotFoundException.class)); new MyBinaryEncoder().encode(myType); }
@Override public String convert(MyType source) { return "_" + source.toString(); } }
@Override public MyType convert(String source) { return new MyType(source.substring(1)); } }
@Before public void setup() { setup(Config.class); }
@Override public void setTextMessageSizeLimit(int messageSizeLimit) { checkNativeSessionInitialized(); getNativeSession().setMaxTextMessageBufferSize(messageSizeLimit); }
@Override @Nullable public URI getUri() { checkNativeSessionInitialized(); return this.uri; }
/** * Returns the websocket message type. By default the type is resolved using * the generic arguments of the class. */ protected TypeDescriptor getMessageType() { return TypeDescriptor.valueOf(resolveTypeArguments()[1]); }
@Override public byte[] convert(MyType source) { return ("~" + source.toString()).getBytes(); } }
@Override public MyType convert(byte[] source) { return new MyType(new String(source).substring(1)); } }
@Override public void setBinaryMessageSizeLimit(int messageSizeLimit) { checkNativeSessionInitialized(); getNativeSession().setMaxBinaryMessageBufferSize(messageSizeLimit); }
@Override public int getTextMessageSizeLimit() { checkNativeSessionInitialized(); return getNativeSession().getMaxTextMessageBufferSize(); }