Java Code Examples for javax.websocket.MessageHandler#Whole

The following examples show how to use javax.websocket.MessageHandler#Whole . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AdminEndpoind.java    From opencps-v2 with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * @Override onOpen websocket connect
 * 
 * @param Session
 * @param EndpointConfig
 */
@Override
public void onOpen(Session session, EndpointConfig config) {

	session.setMaxBinaryMessageBufferSize(8388608);
	session.setMaxTextMessageBufferSize(8388608);
	
	MessageHandler handler = new MessageHandler.Whole<String>() {

		@Override
		public void onMessage(String text) {
			try {
				onMessageHandler(text, session);
			} catch (Exception e) {
				_log.error(e);
			}
		}

	};

	session.addMessageHandler(handler);

}
 
Example 2
Source File: WebsocketClient.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
public WebsocketClient(String uri, final String sessionID, MessageHandler.Whole<String> messageHandler)
    throws IOException {

// add session ID so the request gets through LAMS security
Builder configBuilder = ClientEndpointConfig.Builder.create();
configBuilder.configurator(new Configurator() {
    @Override
    public void beforeRequest(Map<String, List<String>> headers) {
	headers.put("Cookie", Arrays.asList("JSESSIONID=" + sessionID));
    }
});
ClientEndpointConfig clientConfig = configBuilder.build();
this.websocketEndpoint = new WebsocketEndpoint(messageHandler);
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
try {
    container.connectToServer(websocketEndpoint, clientConfig, new URI(uri));
} catch (DeploymentException | URISyntaxException e) {
    throw new IOException("Error while connecting to websocket server", e);
}
   }
 
Example 3
Source File: WsSession.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@SuppressWarnings("unchecked")
private void doAddMessageHandler(Class<?> target, MessageHandler listener) {
    checkState();

    // Message handlers that require decoders may map to text messages,
    // binary messages, both or neither.

    // The frame processing code expects binary message handlers to
    // accept ByteBuffer

    // Use the POJO message handler wrappers as they are designed to wrap
    // arbitrary objects with MessageHandlers and can wrap MessageHandlers
    // just as easily.

    Set<MessageHandlerResult> mhResults = Util.getMessageHandlers(target, listener,
            endpointConfig, this);

    for (MessageHandlerResult mhResult : mhResults) {
        switch (mhResult.getType()) {
        case TEXT: {
            if (textMessageHandler != null) {
                throw new IllegalStateException(sm.getString("wsSession.duplicateHandlerText"));
            }
            textMessageHandler = mhResult.getHandler();
            break;
        }
        case BINARY: {
            if (binaryMessageHandler != null) {
                throw new IllegalStateException(
                        sm.getString("wsSession.duplicateHandlerBinary"));
            }
            binaryMessageHandler = mhResult.getHandler();
            break;
        }
        case PONG: {
            if (pongMessageHandler != null) {
                throw new IllegalStateException(sm.getString("wsSession.duplicateHandlerPong"));
            }
            MessageHandler handler = mhResult.getHandler();
            if (handler instanceof MessageHandler.Whole<?>) {
                pongMessageHandler = (MessageHandler.Whole<PongMessage>) handler;
            } else {
                throw new IllegalStateException(
                        sm.getString("wsSession.invalidHandlerTypePong"));
            }

            break;
        }
        default: {
            throw new IllegalArgumentException(
                    sm.getString("wsSession.unknownHandlerType", listener, mhResult.getType()));
        }
        }
    }
}
 
Example 4
Source File: WsSession.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
protected MessageHandler.Whole<PongMessage> getPongMessageHandler() {
    return pongMessageHandler;
}
 
Example 5
Source File: WsFrameBase.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
private boolean processDataControl() throws IOException {
    TransformationResult tr = transformation.getMoreData(opCode, fin, rsv, controlBufferBinary);
    if (TransformationResult.UNDERFLOW.equals(tr)) {
        return false;
    }
    // Control messages have fixed message size so
    // TransformationResult.OVERFLOW is not possible here

    controlBufferBinary.flip();
    if (opCode == Constants.OPCODE_CLOSE) {
        open = false;
        String reason = null;
        int code = CloseCodes.NORMAL_CLOSURE.getCode();
        if (controlBufferBinary.remaining() == 1) {
            controlBufferBinary.clear();
            // Payload must be zero or 2+ bytes long
            throw new WsIOException(new CloseReason(
                    CloseCodes.PROTOCOL_ERROR,
                    sm.getString("wsFrame.oneByteCloseCode")));
        }
        if (controlBufferBinary.remaining() > 1) {
            code = controlBufferBinary.getShort();
            if (controlBufferBinary.remaining() > 0) {
                CoderResult cr = utf8DecoderControl.decode(controlBufferBinary,
                        controlBufferText, true);
                if (cr.isError()) {
                    controlBufferBinary.clear();
                    controlBufferText.clear();
                    throw new WsIOException(new CloseReason(
                            CloseCodes.PROTOCOL_ERROR,
                            sm.getString("wsFrame.invalidUtf8Close")));
                }
                // There will be no overflow as the output buffer is big
                // enough. There will be no underflow as all the data is
                // passed to the decoder in a single call.
                controlBufferText.flip();
                reason = controlBufferText.toString();
            }
        }
        wsSession.onClose(new CloseReason(Util.getCloseCode(code), reason));
    } else if (opCode == Constants.OPCODE_PING) {
        if (wsSession.isOpen()) {
            wsSession.getBasicRemote().sendPong(controlBufferBinary);
        }
    } else if (opCode == Constants.OPCODE_PONG) {
        MessageHandler.Whole<PongMessage> mhPong = wsSession.getPongMessageHandler();
        if (mhPong != null) {
            try {
                mhPong.onMessage(new WsPongMessage(controlBufferBinary));
            } catch (Throwable t) {
                handleThrowableOnSend(t);
            } finally {
                controlBufferBinary.clear();
            }
        }
    } else {
        // Should have caught this earlier but just in case...
        controlBufferBinary.clear();
        throw new WsIOException(new CloseReason(
                CloseCodes.PROTOCOL_ERROR,
                sm.getString("wsFrame.invalidOpCode", Integer.valueOf(opCode))));
    }
    controlBufferBinary.clear();
    newFrame();
    return true;
}
 
Example 6
Source File: UndertowSession.java    From quarkus-http with Apache License 2.0 4 votes vote down vote up
@Override
public <T> void addMessageHandler(Class<T> clazz, MessageHandler.Whole<T> handler) {
    frameHandler.addHandler(clazz, handler);
}
 
Example 7
Source File: WsSession.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
private void doAddMessageHandler(Class<?> target, MessageHandler listener) {
    checkState();

    // Message handlers that require decoders may map to text messages,
    // binary messages, both or neither.

    // The frame processing code expects binary message handlers to
    // accept ByteBuffer

    // Use the POJO message handler wrappers as they are designed to wrap
    // arbitrary objects with MessageHandlers and can wrap MessageHandlers
    // just as easily.

    Set<MessageHandlerResult> mhResults =
            Util.getMessageHandlers(target, listener, endpointConfig, this);

    for (MessageHandlerResult mhResult : mhResults) {
        switch (mhResult.getType()) {
            case TEXT: {
                if (textMessageHandler != null) {
                    throw new IllegalStateException(
                            sm.getString("wsSession.duplicateHandlerText"));
                }
                textMessageHandler = mhResult.getHandler();
                break;
            }
            case BINARY: {
                if (binaryMessageHandler != null) {
                    throw new IllegalStateException(
                            sm.getString("wsSession.duplicateHandlerBinary"));
                }
                binaryMessageHandler = mhResult.getHandler();
                break;
            }
            case PONG: {
                if (pongMessageHandler != null) {
                    throw new IllegalStateException(
                            sm.getString("wsSession.duplicateHandlerPong"));
                }
                MessageHandler handler = mhResult.getHandler();
                if (handler instanceof MessageHandler.Whole<?>) {
                    pongMessageHandler =
                            (MessageHandler.Whole<PongMessage>) handler;
                } else {
                    throw new IllegalStateException(
                            sm.getString("wsSession.invalidHandlerTypePong"));
                }

                break;
            }
            default: {
                throw new IllegalArgumentException(sm.getString(
                        "wsSession.unknownHandlerType", listener,
                        mhResult.getType()));
            }
        }
    }
}
 
Example 8
Source File: WsSession.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
protected MessageHandler.Whole<PongMessage> getPongMessageHandler() {
    return pongMessageHandler;
}
 
Example 9
Source File: WsFrameBase.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
private boolean processDataControl() throws IOException {
    TransformationResult tr = transformation.getMoreData(opCode, fin, rsv, controlBufferBinary);
    if (TransformationResult.UNDERFLOW.equals(tr)) {
        return false;
    }
    // Control messages have fixed message size so
    // TransformationResult.OVERFLOW is not possible here

    controlBufferBinary.flip();
    if (opCode == Constants.OPCODE_CLOSE) {
        open = false;
        String reason = null;
        int code = CloseCodes.NORMAL_CLOSURE.getCode();
        if (controlBufferBinary.remaining() == 1) {
            controlBufferBinary.clear();
            // Payload must be zero or greater than 2
            throw new WsIOException(new CloseReason(
                    CloseCodes.PROTOCOL_ERROR,
                    sm.getString("wsFrame.oneByteCloseCode")));
        }
        if (controlBufferBinary.remaining() > 1) {
            code = controlBufferBinary.getShort();
            if (controlBufferBinary.remaining() > 0) {
                CoderResult cr = utf8DecoderControl.decode(
                        controlBufferBinary, controlBufferText, true);
                if (cr.isError()) {
                    controlBufferBinary.clear();
                    controlBufferText.clear();
                    throw new WsIOException(new CloseReason(
                            CloseCodes.PROTOCOL_ERROR,
                            sm.getString("wsFrame.invalidUtf8Close")));
                }
                // There will be no overflow as the output buffer is big
                // enough. There will be no underflow as all the data is
                // passed to the decoder in a single call.
                controlBufferText.flip();
                reason = controlBufferText.toString();
            }
        }
        wsSession.onClose(new CloseReason(Util.getCloseCode(code), reason));
    } else if (opCode == Constants.OPCODE_PING) {
        if (wsSession.isOpen()) {
            wsSession.getBasicRemote().sendPong(controlBufferBinary);
        }
    } else if (opCode == Constants.OPCODE_PONG) {
        MessageHandler.Whole<PongMessage> mhPong =
                wsSession.getPongMessageHandler();
        if (mhPong != null) {
            try {
                mhPong.onMessage(new WsPongMessage(controlBufferBinary));
            } catch (Throwable t) {
                handleThrowableOnSend(t);
            } finally {
                controlBufferBinary.clear();
            }
        }
    } else {
        // Should have caught this earlier but just in case...
        controlBufferBinary.clear();
        throw new WsIOException(new CloseReason(
                CloseCodes.PROTOCOL_ERROR,
                sm.getString("wsFrame.invalidOpCode",
                        Integer.valueOf(opCode))));
    }
    controlBufferBinary.clear();
    newFrame();
    return true;
}
 
Example 10
Source File: WebsocketClient.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
WebsocketEndpoint(MessageHandler.Whole<String> messageHandler) {
this.messageHandler = messageHandler;
   }
 
Example 11
Source File: MCRWebCLIResourceSockets.java    From mycore with GNU General Public License v3.0 4 votes vote down vote up
@Override
public <T> void addMessageHandler(Class<T> clazz, MessageHandler.Whole<T> handler) {
    delegate.addMessageHandler(clazz, handler);
}
 
Example 12
Source File: WsSession.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
private void doAddMessageHandler(Class<?> target, MessageHandler listener) {
    checkState();

    // Message handlers that require decoders may map to text messages,
    // binary messages, both or neither.

    // The frame processing code expects binary message handlers to
    // accept ByteBuffer

    // Use the POJO message handler wrappers as they are designed to wrap
    // arbitrary objects with MessageHandlers and can wrap MessageHandlers
    // just as easily.

    Set<MessageHandlerResult> mhResults =
            Util.getMessageHandlers(target, listener, endpointConfig, this);

    for (MessageHandlerResult mhResult : mhResults) {
        switch (mhResult.getType()) {
            case TEXT: {
                if (textMessageHandler != null) {
                    throw new IllegalStateException(
                            sm.getString("wsSession.duplicateHandlerText"));
                }
                textMessageHandler = mhResult.getHandler();
                break;
            }
            case BINARY: {
                if (binaryMessageHandler != null) {
                    throw new IllegalStateException(
                            sm.getString("wsSession.duplicateHandlerBinary"));
                }
                binaryMessageHandler = mhResult.getHandler();
                break;
            }
            case PONG: {
                if (pongMessageHandler != null) {
                    throw new IllegalStateException(
                            sm.getString("wsSession.duplicateHandlerPong"));
                }
                MessageHandler handler = mhResult.getHandler();
                if (handler instanceof MessageHandler.Whole<?>) {
                    pongMessageHandler =
                            (MessageHandler.Whole<PongMessage>) handler;
                } else {
                    throw new IllegalStateException(
                            sm.getString("wsSession.invalidHandlerTypePong"));
                }

                break;
            }
            default: {
                throw new IllegalArgumentException(sm.getString(
                        "wsSession.unknownHandlerType", listener,
                        mhResult.getType()));
            }
        }
    }
}
 
Example 13
Source File: WsSession.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
protected MessageHandler.Whole<PongMessage> getPongMessageHandler() {
    return pongMessageHandler;
}
 
Example 14
Source File: WsFrameBase.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
private boolean processDataControl() throws IOException {
    TransformationResult tr = transformation.getMoreData(opCode, fin, rsv, controlBufferBinary);
    if (TransformationResult.UNDERFLOW.equals(tr)) {
        return false;
    }
    // Control messages have fixed message size so
    // TransformationResult.OVERFLOW is not possible here

    controlBufferBinary.flip();
    if (opCode == Constants.OPCODE_CLOSE) {
        open = false;
        String reason = null;
        int code = CloseCodes.NORMAL_CLOSURE.getCode();
        if (controlBufferBinary.remaining() == 1) {
            controlBufferBinary.clear();
            // Payload must be zero or greater than 2
            throw new WsIOException(new CloseReason(
                    CloseCodes.PROTOCOL_ERROR,
                    sm.getString("wsFrame.oneByteCloseCode")));
        }
        if (controlBufferBinary.remaining() > 1) {
            code = controlBufferBinary.getShort();
            if (controlBufferBinary.remaining() > 0) {
                CoderResult cr = utf8DecoderControl.decode(
                        controlBufferBinary, controlBufferText, true);
                if (cr.isError()) {
                    controlBufferBinary.clear();
                    controlBufferText.clear();
                    throw new WsIOException(new CloseReason(
                            CloseCodes.PROTOCOL_ERROR,
                            sm.getString("wsFrame.invalidUtf8Close")));
                }
                // There will be no overflow as the output buffer is big
                // enough. There will be no underflow as all the data is
                // passed to the decoder in a single call.
                controlBufferText.flip();
                reason = controlBufferText.toString();
            }
        }
        wsSession.onClose(new CloseReason(Util.getCloseCode(code), reason));
    } else if (opCode == Constants.OPCODE_PING) {
        if (wsSession.isOpen()) {
            wsSession.getBasicRemote().sendPong(controlBufferBinary);
        }
    } else if (opCode == Constants.OPCODE_PONG) {
        MessageHandler.Whole<PongMessage> mhPong =
                wsSession.getPongMessageHandler();
        if (mhPong != null) {
            try {
                mhPong.onMessage(new WsPongMessage(controlBufferBinary));
            } catch (Throwable t) {
                handleThrowableOnSend(t);
            } finally {
                controlBufferBinary.clear();
            }
        }
    } else {
        // Should have caught this earlier but just in case...
        controlBufferBinary.clear();
        throw new WsIOException(new CloseReason(
                CloseCodes.PROTOCOL_ERROR,
                sm.getString("wsFrame.invalidOpCode",
                        Integer.valueOf(opCode))));
    }
    controlBufferBinary.clear();
    newFrame();
    return true;
}