private HandshakeInfo createHandshakeInfo(URI url, Session jettySession) { HttpHeaders headers = new HttpHeaders(); jettySession.getUpgradeResponse().getHeaders().forEach(headers::put); String protocol = headers.getFirst("Sec-WebSocket-Protocol"); return new HandshakeInfo(url, headers, Mono.empty(), protocol); }
@Override public void initializeNativeSession(Session session) { super.initializeNativeSession(session); this.uri = session.getUpgradeRequest().getRequestURI(); HttpHeaders headers = new HttpHeaders(); headers.putAll(session.getUpgradeRequest().getHeaders()); this.headers = HttpHeaders.readOnlyHttpHeaders(headers); this.acceptedProtocol = session.getUpgradeResponse().getAcceptedSubProtocol(); List<ExtensionConfig> jettyExtensions = session.getUpgradeResponse().getExtensions(); if (!CollectionUtils.isEmpty(jettyExtensions)) { List<WebSocketExtension> extensions = new ArrayList<>(jettyExtensions.size()); for (ExtensionConfig jettyExtension : jettyExtensions) { extensions.add(new WebSocketExtension(jettyExtension.getName(), jettyExtension.getParameters())); } this.extensions = Collections.unmodifiableList(extensions); } else { this.extensions = Collections.emptyList(); } if (this.user == null) { this.user = session.getUpgradeRequest().getUserPrincipal(); } }
@Override public void onHandshakeResponse(UpgradeResponse response) { resp.setStatus(response.getStatusCode()); for (String header : response.getHeaderNames()) { switch (header.toLowerCase()) { case "connection": case "date": case "sec-websocket-accept": case "upgrade": continue; } for (String value : response.getHeaders(header)) { resp.addHeader(header, value); } } try { wsFactory.acceptWebSocket(req, resp); } catch (IOException e) { throw new RuntimeException(e); } async.complete(); } });
@Test @SuppressWarnings("resource") public void getPrincipalNotAvailable() { UpgradeRequest request = Mockito.mock(UpgradeRequest.class); given(request.getUserPrincipal()).willReturn(null); UpgradeResponse response = Mockito.mock(UpgradeResponse.class); given(response.getAcceptedSubProtocol()).willReturn(null); Session nativeSession = Mockito.mock(Session.class); given(nativeSession.getUpgradeRequest()).willReturn(request); given(nativeSession.getUpgradeResponse()).willReturn(response); JettyWebSocketSession session = new JettyWebSocketSession(attributes); session.initializeNativeSession(nativeSession); reset(nativeSession); assertNull(session.getPrincipal()); verifyNoMoreInteractions(nativeSession); }
/** * Get the accepted WebSocket protocol. * * @return the accepted WebSocket protocol. */ public String getAcceptedSubProtocol() { return getHeader(SEC_WEBSOCKET_PROTOCOL); }
/** * Set the accepted WebSocket Protocol. * * @param protocol * the protocol to list as accepted */ public void setAcceptedSubProtocol(String protocol) { setHeader(SEC_WEBSOCKET_PROTOCOL,protocol); }
@Test @SuppressWarnings("resource") public void getAcceptedProtocol() { String protocol = "foo"; UpgradeRequest request = Mockito.mock(UpgradeRequest.class); given(request.getUserPrincipal()).willReturn(null); UpgradeResponse response = Mockito.mock(UpgradeResponse.class); given(response.getAcceptedSubProtocol()).willReturn(protocol); Session nativeSession = Mockito.mock(Session.class); given(nativeSession.getUpgradeRequest()).willReturn(request); given(nativeSession.getUpgradeResponse()).willReturn(response); JettyWebSocketSession session = new JettyWebSocketSession(attributes); session.initializeNativeSession(nativeSession); reset(nativeSession); assertSame(protocol, session.getAcceptedProtocol()); verifyNoMoreInteractions(nativeSession); }
/** * Get the accepted WebSocket protocol. * * @return the accepted WebSocket protocol. */ public String getAcceptedSubProtocol() { return getHeader(SEC_WEBSOCKET_PROTOCOL); }
/** * Set the accepted WebSocket Protocol. * * @param protocol * the protocol to list as accepted */ public void setAcceptedSubProtocol(String protocol) { setHeader(SEC_WEBSOCKET_PROTOCOL,protocol); }
@Override public Map<String, List<String>> getHeaders() { return response.getHeaders(); } }
@Override public void initializeNativeSession(Session session) { super.initializeNativeSession(session); this.uri = session.getUpgradeRequest().getRequestURI(); HttpHeaders headers = new HttpHeaders(); headers.putAll(session.getUpgradeRequest().getHeaders()); this.headers = HttpHeaders.readOnlyHttpHeaders(headers); this.acceptedProtocol = session.getUpgradeResponse().getAcceptedSubProtocol(); List<ExtensionConfig> jettyExtensions = session.getUpgradeResponse().getExtensions(); if (!CollectionUtils.isEmpty(jettyExtensions)) { List<WebSocketExtension> extensions = new ArrayList<>(jettyExtensions.size()); for (ExtensionConfig jettyExtension : jettyExtensions) { extensions.add(new WebSocketExtension(jettyExtension.getName(), jettyExtension.getParameters())); } this.extensions = Collections.unmodifiableList(extensions); } else { this.extensions = Collections.emptyList(); } if (this.user == null) { this.user = session.getUpgradeRequest().getUserPrincipal(); } }
@Test @SuppressWarnings("resource") public void getPrincipalFromNativeSession() { TestPrincipal user = new TestPrincipal("joe"); UpgradeRequest request = Mockito.mock(UpgradeRequest.class); given(request.getUserPrincipal()).willReturn(user); UpgradeResponse response = Mockito.mock(UpgradeResponse.class); given(response.getAcceptedSubProtocol()).willReturn(null); Session nativeSession = Mockito.mock(Session.class); given(nativeSession.getUpgradeRequest()).willReturn(request); given(nativeSession.getUpgradeResponse()).willReturn(response); JettyWebSocketSession session = new JettyWebSocketSession(attributes); session.initializeNativeSession(nativeSession); reset(nativeSession); assertSame(user, session.getPrincipal()); verifyNoMoreInteractions(nativeSession); }
public String getExternalId(final String path, final Session session) { final String sessionHandshake = session.getUpgradeResponse().getHeader(WS_HAND_SHAKE_KEY); for (SessionIdWrapper sw : sessionMap.get(path)) { if (sw.getSession().getUpgradeResponse().getHeader(WS_HAND_SHAKE_KEY).equals(sessionHandshake)) { return sw.getId(); } } return null; }
@Override public void onHandshakeResponse(UpgradeResponse response) { storeCookies(response.getHeaders()); }
@Override public void initializeNativeSession(Session session) { super.initializeNativeSession(session); this.uri = session.getUpgradeRequest().getRequestURI(); HttpHeaders headers = new HttpHeaders(); headers.putAll(session.getUpgradeRequest().getHeaders()); this.headers = HttpHeaders.readOnlyHttpHeaders(headers); this.acceptedProtocol = session.getUpgradeResponse().getAcceptedSubProtocol(); List<ExtensionConfig> jettyExtensions = session.getUpgradeResponse().getExtensions(); if (!CollectionUtils.isEmpty(jettyExtensions)) { List<WebSocketExtension> extensions = new ArrayList<>(jettyExtensions.size()); for (ExtensionConfig jettyExtension : jettyExtensions) { extensions.add(new WebSocketExtension(jettyExtension.getName(), jettyExtension.getParameters())); } this.extensions = Collections.unmodifiableList(extensions); } else { this.extensions = Collections.emptyList(); } if (this.user == null) { this.user = session.getUpgradeRequest().getUserPrincipal(); } }
@Override public String getSubProtocol() { return this.raw.getUpgradeResponse().getAcceptedSubProtocol(); }
/** * * Removes the closing client's session from 'sessionMap' (using the handshake identifier). * * @param session */ public void removeSession(final Session session) { logger.debug("removeSession called"); final String sessionHandshake = session.getUpgradeResponse().getHeader(WS_HAND_SHAKE_KEY); sessionMap.values().forEach(sessionSet -> sessionSet.forEach( s -> { if (s.getSession().getUpgradeResponse().getHeader(WS_HAND_SHAKE_KEY).equals(sessionHandshake)) { sessionSet.remove(s); liveLoggingHandler.broadcast(LiveLoggingUtils.buildLiveLogOutboundDTO(s.getTraceId(), null, null, "Websocket closed", false, false)); if (s.isLogMockCalls()) LiveLoggingUtils.MOCK_TRAFFIC_LOGGER.info(LiveLoggingUtils.buildLiveLogOutboundFileEntry(s.getTraceId(), null, null, "Websocket closed", false, false)); return; } }) ); }
public String getHeader(String name) List<String> values = getHeaders(name);
@Before public void setup() throws Exception { client.start(); URI socketUri = new URI(getAPIv2().path("socket").getUri().toString().replace("http://", "ws://")); ClientUpgradeRequest request = new ClientUpgradeRequest(); request.setSubProtocols(Lists.newArrayList(getAuthHeaderValue())); this.socket = new TestSocket(); client.connect(socket, socketUri, request); socket.awaitConnection(2); assertEquals(getAuthHeaderValue(), socket.session.getUpgradeResponse().getAcceptedSubProtocol()); }
/** * * Stores all websocket client sessions in the internal map 'sessionMap'. * * Note sessions are 'internally' identified using the encrypted handshake 'Sec-WebSocket-Accept' value and * 'externally' identified using an allocated UUID. * * * @param mockExtId * @param path * @param idleTimeoutMillis * @param session * */ public void registerSession(final String mockExtId, final String path, final long idleTimeoutMillis, final boolean proxyPushIdOnConnect, final Session session, final boolean logMockCalls) { logger.debug("registerSession called"); session.setIdleTimeout((idleTimeoutMillis > 0) ? idleTimeoutMillis : MAX_IDLE_TIMEOUT_MILLIS ); final Set<SessionIdWrapper> sessions = sessionMap.getOrDefault(path, new HashSet<>()); final String assignedId = GeneralUtils.generateUUID(); final String traceId = session.getUpgradeResponse().getHeader(GeneralUtils.LOG_REQ_ID); sessions.add(new SessionIdWrapper(assignedId, traceId, session, GeneralUtils.getCurrentDate(), logMockCalls)); sessionMap.put(path, sessions); if (proxyPushIdOnConnect) { sendMessage(assignedId, new WebSocketDTO(path, "clientId: " + assignedId)); } liveLoggingHandler.broadcast(LiveLoggingUtils.buildLiveLogOutboundDTO(traceId, 101, null, "Websocket established (clientId: " + assignedId + ")", false, false)); if (logMockCalls) LiveLoggingUtils.MOCK_TRAFFIC_LOGGER.info(LiveLoggingUtils.buildLiveLogOutboundFileEntry(traceId, 101, null, "Websocket established (clientId: " + assignedId + ")", false, false)); }