/** Return the {@link FrameType} for the given {@link Class}. */ protected HandlerWrapper createHandlerWrapper( Class<?> type, MessageHandler handler, boolean partialHandler) { if (partialHandler) { // Partial message handler supports only String, byte[] and ByteBuffer. // See JavaDocs of the MessageHandler.Partial interface. if (type == String.class) { return new HandlerWrapper(FrameType.TEXT, handler, type, false, true); } if (type == byte[].class || type == ByteBuffer.class) { return new HandlerWrapper(FrameType.BYTE, handler, type, false, true); } throw JsrWebSocketMessages.MESSAGES.unsupportedFrameType(type); } if (type == byte[].class || type == ByteBuffer.class || type == InputStream.class) { return new HandlerWrapper(FrameType.BYTE, handler, type, false, false); } if (type == String.class || type == Reader.class) { return new HandlerWrapper(FrameType.TEXT, handler, type, false, false); } if (type == PongMessage.class) { return new HandlerWrapper(FrameType.PONG, handler, type, false, false); } Encoding encoding = session.getEncoding(); if (encoding.canDecodeText(type)) { return new HandlerWrapper(FrameType.TEXT, handler, type, true, false); } else if (encoding.canDecodeBinary(type)) { return new HandlerWrapper(FrameType.BYTE, handler, type, true, false); } throw JsrWebSocketMessages.MESSAGES.unsupportedFrameType(type); }
public final void addHandler(MessageHandler handler) { Map<Class<?>, Boolean> types = ClassUtils.getHandlerTypes(handler.getClass()); for (Entry<Class<?>, Boolean> e : types.entrySet()) { Class<?> type = e.getKey(); verify(type, handler); HandlerWrapper handlerWrapper = createHandlerWrapper(type, handler, e.getValue()); if (handlers.containsKey(handlerWrapper.getFrameType())) { throw JsrWebSocketMessages.MESSAGES.handlerAlreadyRegistered(handlerWrapper.getFrameType()); } else { if (handlers.putIfAbsent(handlerWrapper.getFrameType(), handlerWrapper) != null) { throw JsrWebSocketMessages.MESSAGES.handlerAlreadyRegistered( handlerWrapper.getFrameType()); } } } }
@Override public void sendText(final String partialMessage, final boolean isLast) throws IOException { if (binaryFrameSender != null) { throw JsrWebSocketMessages.MESSAGES.cannotSendInMiddleOfFragmentedMessage(); } if (textFrameSender == null) { textFrameSender = webSocketChannel.send(WebSocketFrameType.TEXT); } try { Channels.writeBlocking(textFrameSender, WebSocketUtils.fromUtf8String(partialMessage)); if (isLast) { textFrameSender.shutdownWrites(); Channels.flushBlocking(textFrameSender); } } finally { if (isLast) { textFrameSender = null; } } }
@Override public void sendBinary(final ByteBuffer partialByte, final boolean isLast) throws IOException { if (textFrameSender != null) { throw JsrWebSocketMessages.MESSAGES.cannotSendInMiddleOfFragmentedMessage(); } if (binaryFrameSender == null) { binaryFrameSender = webSocketChannel.send(WebSocketFrameType.BINARY); } try { Channels.writeBlocking(binaryFrameSender, partialByte); if (isLast) { binaryFrameSender.shutdownWrites(); Channels.flushBlocking(binaryFrameSender); } } finally { if (isLast) { binaryFrameSender = null; } } }
public void assertNotInFragment() { if (textFrameSender != null || binaryFrameSender != null) { throw JsrWebSocketMessages.MESSAGES.cannotSendInMiddleOfFragmentedMessage(); } }