@Test @SuppressWarnings("resource") public void getPrincipalWithNativeSession() { TestPrincipal user = new TestPrincipal("joe"); Session nativeSession = Mockito.mock(Session.class); given(nativeSession.getUserPrincipal()).willReturn(user); StandardWebSocketSession session = new StandardWebSocketSession(this.headers, this.attributes, null, null); session.initializeNativeSession(nativeSession); assertSame(user, session.getPrincipal()); }
@Override public int getBinaryMessageSizeLimit() { checkNativeSessionInitialized(); return getNativeSession().getMaxBinaryMessageBufferSize(); }
@Test @SuppressWarnings("resource") public void getAcceptedProtocol() { String protocol = "foo"; Session nativeSession = Mockito.mock(Session.class); given(nativeSession.getNegotiatedSubprotocol()).willReturn(protocol); StandardWebSocketSession session = new StandardWebSocketSession(this.headers, this.attributes, null, null); session.initializeNativeSession(nativeSession); reset(nativeSession); assertEquals(protocol, session.getAcceptedProtocol()); verifyNoMoreInteractions(nativeSession); }
@Test @SuppressWarnings("resource") public void getPrincipalWithConstructorArg() { TestPrincipal user = new TestPrincipal("joe"); StandardWebSocketSession session = new StandardWebSocketSession(this.headers, this.attributes, null, null, user); assertSame(user, session.getPrincipal()); }
@Override public boolean isOpen() { return getNativeSession().isOpen(); }
@Override public void upgrade(ServerHttpRequest request, ServerHttpResponse response, @Nullable String selectedProtocol, List<WebSocketExtension> selectedExtensions, @Nullable Principal user, WebSocketHandler wsHandler, Map<String, Object> attrs) throws HandshakeFailureException { HttpHeaders headers = request.getHeaders(); InetSocketAddress localAddr = null; try { localAddr = request.getLocalAddress(); } catch (Exception ex) { // Ignore } InetSocketAddress remoteAddr = null; try { remoteAddr = request.getRemoteAddress(); } catch (Exception ex) { // Ignore } StandardWebSocketSession session = new StandardWebSocketSession(headers, attrs, localAddr, remoteAddr, user); StandardWebSocketHandlerAdapter endpoint = new StandardWebSocketHandlerAdapter(wsHandler, session); List<Extension> extensions = new ArrayList<>(); for (WebSocketExtension extension : selectedExtensions) { extensions.add(new WebSocketToStandardExtensionAdapter(extension)); } upgradeInternal(request, response, selectedProtocol, extensions, endpoint); }
@Override public String getAcceptedProtocol() { checkNativeSessionInitialized(); return this.acceptedProtocol; }
@Override public void onOpen(final javax.websocket.Session session, EndpointConfig config) { this.wsSession.initializeNativeSession(session);
@Test public void onOpen() throws Throwable { URI uri = URI.create("http://example.org"); given(this.session.getRequestURI()).willReturn(uri); this.adapter.onOpen(this.session, null); verify(this.webSocketHandler).afterConnectionEstablished(this.webSocketSession); verify(this.session, atLeast(2)).addMessageHandler(any(MessageHandler.Whole.class)); given(this.session.getRequestURI()).willReturn(uri); assertEquals(uri, this.webSocketSession.getUri()); }
@Override public void initializeNativeSession(Session session) { super.initializeNativeSession(session); this.uri = session.getRequestURI(); this.acceptedProtocol = session.getNegotiatedSubprotocol(); List<Extension> standardExtensions = getNativeSession().getNegotiatedExtensions(); if (!CollectionUtils.isEmpty(standardExtensions)) { this.extensions = new ArrayList<>(standardExtensions.size()); for (Extension standardExtension : standardExtensions) { this.extensions.add(new StandardToWebSocketExtensionAdapter(standardExtension)); } this.extensions = Collections.unmodifiableList(this.extensions); } else { this.extensions = Collections.emptyList(); } if (this.user == null) { this.user = session.getUserPrincipal(); } }
@Override protected ListenableFuture<WebSocketSession> doHandshakeInternal(WebSocketHandler webSocketHandler, HttpHeaders headers, final URI uri, List<String> protocols, List<WebSocketExtension> extensions, Map<String, Object> attributes) { int port = getPort(uri); InetSocketAddress localAddress = new InetSocketAddress(getLocalHost(), port); InetSocketAddress remoteAddress = new InetSocketAddress(uri.getHost(), port); final StandardWebSocketSession session = new StandardWebSocketSession(headers, attributes, localAddress, remoteAddress); final ClientEndpointConfig endpointConfig = ClientEndpointConfig.Builder.create() .configurator(new StandardWebSocketClientConfigurator(headers)) .preferredSubprotocols(protocols) .extensions(adaptExtensions(extensions)).build(); endpointConfig.getUserProperties().putAll(getUserProperties()); final Endpoint endpoint = new StandardWebSocketHandlerAdapter(webSocketHandler, session); Callable<WebSocketSession> connectTask = () -> { this.webSocketContainer.connectToServer(endpoint, endpointConfig, uri); return session; }; if (this.taskExecutor != null) { return this.taskExecutor.submitListenable(connectTask); } else { ListenableFutureTask<WebSocketSession> task = new ListenableFutureTask<>(connectTask); task.run(); return task; } }
@Override @Nullable public URI getUri() { checkNativeSessionInitialized(); return this.uri; }
@Override public void onOpen(final javax.websocket.Session session, EndpointConfig config) { this.wsSession.initializeNativeSession(session);
@Override public void setTextMessageSizeLimit(int messageSizeLimit) { checkNativeSessionInitialized(); getNativeSession().setMaxTextMessageBufferSize(messageSizeLimit); }
@Override protected void sendPingMessage(PingMessage message) throws IOException { getNativeSession().getBasicRemote().sendPing(message.getPayload()); }
@Test @SuppressWarnings("resource") public void getPrincipalNone() { Session nativeSession = Mockito.mock(Session.class); given(nativeSession.getUserPrincipal()).willReturn(null); StandardWebSocketSession session = new StandardWebSocketSession(this.headers, this.attributes, null, null); session.initializeNativeSession(nativeSession); reset(nativeSession); assertNull(session.getPrincipal()); verifyNoMoreInteractions(nativeSession); }
@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); }
@Override public String getAcceptedProtocol() { checkNativeSessionInitialized(); return this.acceptedProtocol; }
@Override public void onOpen(final javax.websocket.Session session, EndpointConfig config) { this.wsSession.initializeNativeSession(session);
@Override public void setBinaryMessageSizeLimit(int messageSizeLimit) { checkNativeSessionInitialized(); getNativeSession().setMaxBinaryMessageBufferSize(messageSizeLimit); }