private void testTransportUrl(String scheme, String expectedScheme, TransportType transportType) throws Exception { SockJsUrlInfo info = new SockJsUrlInfo(new URI(scheme + "://example.com")); String serverId = info.getServerId(); String sessionId = info.getSessionId(); String transport = transportType.toString().toLowerCase(); URI expected = new URI(expectedScheme + "://example.com/" + serverId + "/" + sessionId + "/" + transport); assertThat(info.getTransportUrl(transportType), equalTo(expected)); }
private void testInfoUrl(String scheme, String expectedScheme) throws Exception { SockJsUrlInfo info = new SockJsUrlInfo(new URI(scheme + "://example.com")); Assert.assertThat(info.getInfoUrl(), is(equalTo(new URI(expectedScheme + "://example.com/info")))); }
public URI getTransportUrl(TransportType transportType) { return UriComponentsBuilder.fromUri(this.sockJsUrl) .scheme(getScheme(transportType)) .pathSegment(getServerId()) .pathSegment(getSessionId()) .pathSegment(transportType.toString()) .build(true).toUri(); }
@Test public void sessionId() throws Exception { SockJsUrlInfo info = new SockJsUrlInfo(new URI("http://example.com")); assertEquals("Invalid sessionId: " + info.getSessionId(), 32, info.getSessionId().length()); }
@Test public void serverId() throws Exception { SockJsUrlInfo info = new SockJsUrlInfo(new URI("http://example.com")); int serverId = Integer.valueOf(info.getServerId()); assertTrue("Invalid serverId: " + serverId, serverId >= 0 && serverId < 1000); }
@Override public final ListenableFuture<WebSocketSession> doHandshake( WebSocketHandler handler, @Nullable WebSocketHttpHeaders headers, URI url) { Assert.notNull(handler, "WebSocketHandler is required"); Assert.notNull(url, "URL is required"); String scheme = url.getScheme(); if (!supportedProtocols.contains(scheme)) { throw new IllegalArgumentException("Invalid scheme: '" + scheme + "'"); } SettableListenableFuture<WebSocketSession> connectFuture = new SettableListenableFuture<>(); try { SockJsUrlInfo sockJsUrlInfo = new SockJsUrlInfo(url); ServerInfo serverInfo = getServerInfo(sockJsUrlInfo, getHttpRequestHeaders(headers)); createRequest(sockJsUrlInfo, headers, serverInfo).connect(handler, connectFuture); } catch (Throwable exception) { if (logger.isErrorEnabled()) { logger.error("Initial SockJS \"Info\" request to server failed, url=" + url, exception); } connectFuture.setException(exception); } return connectFuture; }
@Override public URI getTransportUrl() { return this.sockJsUrlInfo.getTransportUrl(this.serverTransportType); }
@Override public String getId() { return this.request.getSockJsUrlInfo().getSessionId(); }
public String getServerId() { if (this.serverId == null) { this.serverId = String.valueOf(Math.abs(getUuid().getMostSignificantBits()) % 1000); } return this.serverId; }
private ServerInfo getServerInfo(SockJsUrlInfo sockJsUrlInfo, @Nullable HttpHeaders headers) { URI infoUrl = sockJsUrlInfo.getInfoUrl(); ServerInfo info = this.serverInfoCache.get(infoUrl); if (info == null) { long start = System.currentTimeMillis(); String response = this.infoReceiver.executeInfoRequest(infoUrl, headers); long infoRequestTime = System.currentTimeMillis() - start; info = new ServerInfo(response, infoRequestTime); this.serverInfoCache.put(infoUrl, info); } return info; }
public URI getInfoUrl() { return UriComponentsBuilder.fromUri(this.sockJsUrl) .scheme(getScheme(TransportType.XHR)) .pathSegment("info") .build(true).toUri(); }
@Override public URI getUri() { return this.request.getSockJsUrlInfo().getSockJsUrl(); }
protected DefaultTransportRequest createTransportRequest(Transport transport, TransportType type) throws Exception { SockJsUrlInfo urlInfo = new SockJsUrlInfo(new URI("http://example.com")); return new DefaultTransportRequest(urlInfo, new HttpHeaders(), new HttpHeaders(), transport, type, CODEC); }
public XhrClientSockJsSession(TransportRequest request, WebSocketHandler handler, XhrTransport transport, SettableListenableFuture<WebSocketSession> connectFuture) { super(request, handler, connectFuture); Assert.notNull(transport, "XhrTransport is required"); this.transport = transport; this.headers = request.getHttpRequestHeaders(); this.sendHeaders = new HttpHeaders(); this.sendHeaders.putAll(this.headers); this.sendHeaders.setContentType(MediaType.APPLICATION_JSON); this.sendUrl = request.getSockJsUrlInfo().getTransportUrl(TransportType.XHR_SEND); }
String message = "Connect timed out for " + DefaultTransportRequest.this; logger.error(message); ex = new SockJsTransportFailureException(message, getSockJsUrlInfo().getSessionId(), ex);
public String getSessionId() { if (this.sessionId == null) { this.sessionId = StringUtils.delete(getUuid().toString(), "-"); } return this.sessionId; }
private ServerInfo getServerInfo(SockJsUrlInfo sockJsUrlInfo, @Nullable HttpHeaders headers) { URI infoUrl = sockJsUrlInfo.getInfoUrl(); ServerInfo info = this.serverInfoCache.get(infoUrl); if (info == null) { long start = System.currentTimeMillis(); String response = this.infoReceiver.executeInfoRequest(infoUrl, headers); long infoRequestTime = System.currentTimeMillis() - start; info = new ServerInfo(response, infoRequestTime); this.serverInfoCache.put(infoUrl, info); } return info; }
public URI getInfoUrl() { return UriComponentsBuilder.fromUri(this.sockJsUrl) .scheme(getScheme(TransportType.XHR)) .pathSegment("info") .build(true).toUri(); }
@Override public URI getUri() { return this.request.getSockJsUrlInfo().getSockJsUrl(); }
public URI getTransportUrl(TransportType transportType) { return UriComponentsBuilder.fromUri(this.sockJsUrl) .scheme(getScheme(transportType)) .pathSegment(getServerId()) .pathSegment(getSessionId()) .pathSegment(transportType.toString()) .build(true).toUri(); }