@SuppressWarnings("unchecked") @Override public void addMessageHandler(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(listener, endpointConfig, this); for (MessageHandlerResult mhResult : mhResults) { switch (mhResult.getType()) { case TEXT: { if (textMessageHandler != null) { throw MESSAGES.duplicateHandlerText(); } textMessageHandler = mhResult.getHandler(); break; } case BINARY: { if (binaryMessageHandler != null) { throw MESSAGES.duplicateHandlerBinary(); } binaryMessageHandler = mhResult.getHandler(); break; } case PONG: { if (pongMessageHandler != null) { throw MESSAGES.duplicateHandlerPong(); } MessageHandler handler = mhResult.getHandler(); if (handler instanceof MessageHandler.Whole<?>) { pongMessageHandler = (MessageHandler.Whole<PongMessage>) handler; } else { throw MESSAGES.invalidHandlerPong(); } break; } default: { throw MESSAGES.invalidMessageHandler(listener, mhResult.getType()); } } } }
@Override public Set<MessageHandler> getMessageHandlers() { checkState(); Set<MessageHandler> result = new HashSet<MessageHandler>(); if (binaryMessageHandler != null) { result.add(binaryMessageHandler); } if (textMessageHandler != null) { result.add(textMessageHandler); } if (pongMessageHandler != null) { result.add(pongMessageHandler); } return result; }
@Override public void removeMessageHandler(MessageHandler listener) { checkState(); if (listener == null) { return; } MessageHandler wrapped = null; if (listener instanceof WrappedMessageHandler) { wrapped = ((WrappedMessageHandler) listener).getWrappedHandler(); } if (wrapped == null) { wrapped = listener; } boolean removed = false; if (wrapped.equals(textMessageHandler) || listener.equals(textMessageHandler)) { textMessageHandler = null; removed = true; } if (listener.equals(binaryMessageHandler) || listener.equals(binaryMessageHandler)) { binaryMessageHandler = null; removed = true; } if (listener.equals(pongMessageHandler) || listener.equals(pongMessageHandler)) { pongMessageHandler = null; removed = true; } if (!removed) { // ISE for now. Could swallow this silently / log this if the ISE // becomes a problem throw MESSAGES.cannotRemoveHandler(listener); } }
@Override public RemoteEndpoint.Basic getBasicRemote() { checkState(); return remoteEndpointBasic; }
@Override public RemoteEndpoint.Async getAsyncRemote() { checkState(); return remoteEndpointAsync; }
@Override public Set<Session> getOpenSessions() { checkState(); return webSocketContainer.getOpenSessions(localEndpoint.getClass()); }
@Override public int getMaxTextMessageBufferSize() { checkState(); return maxTextMessageBufferSize; }
@Override public Map<String, String> getPathParameters() { checkState(); return pathParameters; }
@Override public String getQueryString() { checkState(); return queryString; }
@Override public boolean isSecure() { checkState(); return secure; }
@Override public List<Extension> getNegotiatedExtensions() { checkState(); return Collections.emptyList(); }
@Override public String getNegotiatedSubprotocol() { checkState(); return subProtocol; }
@Override public String getProtocolVersion() { checkState(); return Constants.WS_VERSION_HEADER_VALUE; }
@Override public URI getRequestURI() { checkState(); return requestUri; }
@Override public long getMaxIdleTimeout() { checkState(); return maxIdleTimeout; }
@Override public Map<String, List<String>> getRequestParameterMap() { checkState(); return requestParameterMap; }
@Override public void setMaxIdleTimeout(long timeout) { checkState(); this.maxIdleTimeout = timeout; }
@Override public Principal getUserPrincipal() { checkState(); return userPrincipal; }
@Override public void setMaxTextMessageBufferSize(int max) { checkState(); this.maxTextMessageBufferSize = max; }
@Override public Map<String, Object> getUserProperties() { checkState(); return userProperties; }
@Override public WebSocketContainer getContainer() { checkState(); return webSocketContainer; }