Java Code Examples for org.springframework.web.socket.sockjs.SockJsTransportFailureException

The following examples show how to use org.springframework.web.socket.sockjs.SockJsTransportFailureException. These examples are extracted from open source projects. 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 Project: spring-analysis-note   Author: Vip-Augus   File: JettyXhrTransport.java    License: MIT License 6 votes vote down vote up
protected ResponseEntity<String> executeRequest(URI url, HttpMethod method,
		HttpHeaders headers, @Nullable String body) {

	Request httpRequest = this.httpClient.newRequest(url).method(method);
	addHttpHeaders(httpRequest, headers);
	if (body != null) {
		httpRequest.content(new StringContentProvider(body));
	}
	ContentResponse response;
	try {
		response = httpRequest.send();
	}
	catch (Exception ex) {
		throw new SockJsTransportFailureException("Failed to execute request to " + url, ex);
	}
	HttpStatus status = HttpStatus.valueOf(response.getStatus());
	HttpHeaders responseHeaders = toHttpHeaders(response.getHeaders());
	return (response.getContent() != null ?
			new ResponseEntity<>(response.getContentAsString(), responseHeaders, status) :
			new ResponseEntity<>(responseHeaders, status));
}
 
Example #2
Source Project: spring-analysis-note   Author: Vip-Augus   File: AbstractSockJsSession.java    License: MIT License 6 votes vote down vote up
/**
 * For internal use within a TransportHandler and the (TransportHandler-specific)
 * session class.
 */
protected void writeFrame(SockJsFrame frame) throws SockJsTransportFailureException {
	if (logger.isTraceEnabled()) {
		logger.trace("Preparing to write " + frame);
	}
	try {
		writeFrameInternal(frame);
	}
	catch (Throwable ex) {
		logWriteFrameFailure(ex);
		try {
			// Force disconnect (so we won't try to send close frame)
			disconnect(CloseStatus.SERVER_ERROR);
		}
		catch (Throwable disconnectFailure) {
			// Ignore
		}
		try {
			close(CloseStatus.SERVER_ERROR);
		}
		catch (Throwable closeFailure) {
			// Nothing of consequence, already forced disconnect
		}
		throw new SockJsTransportFailureException("Failed to write " + frame, getId(), ex);
	}
}
 
Example #3
Source Project: spring-analysis-note   Author: Vip-Augus   File: AbstractHttpSockJsSession.java    License: MIT License 6 votes vote down vote up
/**
 * Handle all requests, except the first one, to receive messages on a SockJS
 * HTTP transport based session.
 * <p>Long polling-based transports (e.g. "xhr", "jsonp") complete the request
 * after writing any buffered message frames (or the next one). Streaming-based
 * transports ("xhr_streaming", "eventsource", and "htmlfile") leave the
 * response open longer for further streaming of message frames but will also
 * close it eventually after some amount of data has been sent.
 * @param request the current request
 * @param response the current response
 * @param frameFormat the transport-specific SocksJS frame format to use
 */
public void handleSuccessiveRequest(ServerHttpRequest request, ServerHttpResponse response,
		SockJsFrameFormat frameFormat) throws SockJsException {

	synchronized (this.responseLock) {
		try {
			if (isClosed()) {
				response.getBody().write(SockJsFrame.closeFrameGoAway().getContentBytes());
				return;
			}
			this.response = response;
			this.frameFormat = frameFormat;
			ServerHttpAsyncRequestControl control = request.getAsyncRequestControl(response);
			this.asyncRequestControl = control;
			control.start(-1);
			disableShallowEtagHeaderFilter(request);
			handleRequestInternal(request, response, false);
			this.readyToSend = isActive();
		}
		catch (Throwable ex) {
			tryCloseWithSockJsTransportError(ex, CloseStatus.SERVER_ERROR);
			throw new SockJsTransportFailureException("Failed to handle SockJS receive request", getId(), ex);
		}
	}
}
 
Example #4
Source Project: spring-analysis-note   Author: Vip-Augus   File: AbstractHttpSockJsSession.java    License: MIT License 6 votes vote down vote up
@Override
protected final void sendMessageInternal(String message) throws SockJsTransportFailureException {
	synchronized (this.responseLock) {
		this.messageCache.add(message);
		if (logger.isTraceEnabled()) {
			logger.trace(this.messageCache.size() + " message(s) to flush in session " + getId());
		}
		if (isActive() && this.readyToSend) {
			if (logger.isTraceEnabled()) {
				logger.trace("Session is active, ready to flush.");
			}
			cancelHeartbeat();
			flushCache();
		}
		else {
			if (logger.isTraceEnabled()) {
				logger.trace("Session is not active, not ready to flush.");
			}
		}
	}
}
 
Example #5
Source Project: spring-analysis-note   Author: Vip-Augus   File: WebSocketServerSockJsSession.java    License: MIT License 6 votes vote down vote up
@Override
public void sendMessageInternal(String message) throws SockJsTransportFailureException {
	// Open frame not sent yet?
	// If in the session initialization thread, then cache, otherwise wait.
	if (!this.openFrameSent) {
		synchronized (this.initSessionLock) {
			if (!this.openFrameSent) {
				this.initSessionCache.add(message);
				return;
			}
		}
	}

	cancelHeartbeat();
	writeFrame(SockJsFrame.messageFrame(getMessageCodec(), message));
	scheduleHeartbeat();
}
 
Example #6
Source Project: spring-analysis-note   Author: Vip-Augus   File: StreamingSockJsSession.java    License: MIT License 6 votes vote down vote up
@Override
protected void flushCache() throws SockJsTransportFailureException {
	while (!getMessageCache().isEmpty()) {
		String message = getMessageCache().poll();
		SockJsMessageCodec messageCodec = getSockJsServiceConfig().getMessageCodec();
		SockJsFrame frame = SockJsFrame.messageFrame(messageCodec, message);
		writeFrame(frame);

		this.byteCount += (frame.getContentBytes().length + 1);
		if (logger.isTraceEnabled()) {
			logger.trace(this.byteCount + " bytes written so far, " +
					getMessageCache().size() + " more messages not flushed");
		}
		if (this.byteCount >= getSockJsServiceConfig().getStreamBytesLimit()) {
			logger.trace("Streamed bytes limit reached, recycling current request");
			resetRequest();
			this.byteCount = 0;
			break;
		}
	}
	scheduleHeartbeat();
}
 
Example #7
Source Project: spring-analysis-note   Author: Vip-Augus   File: HtmlFileTransportHandler.java    License: MIT License 6 votes vote down vote up
@Override
public void handleRequestInternal(ServerHttpRequest request, ServerHttpResponse response,
		AbstractHttpSockJsSession sockJsSession) throws SockJsException {

	String callback = getCallbackParam(request);
	if (!StringUtils.hasText(callback)) {
		response.setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR);
		try {
			response.getBody().write("\"callback\" parameter required".getBytes(StandardCharsets.UTF_8));
		}
		catch (IOException ex) {
			sockJsSession.tryCloseWithSockJsTransportError(ex, CloseStatus.SERVER_ERROR);
			throw new SockJsTransportFailureException("Failed to write to response", sockJsSession.getId(), ex);
		}
		return;
	}

	super.handleRequestInternal(request, response, sockJsSession);
}
 
Example #8
Source Project: java-technology-stack   Author: codeEngraver   File: JettyXhrTransport.java    License: MIT License 6 votes vote down vote up
protected ResponseEntity<String> executeRequest(URI url, HttpMethod method,
		HttpHeaders headers, @Nullable String body) {

	Request httpRequest = this.httpClient.newRequest(url).method(method);
	addHttpHeaders(httpRequest, headers);
	if (body != null) {
		httpRequest.content(new StringContentProvider(body));
	}
	ContentResponse response;
	try {
		response = httpRequest.send();
	}
	catch (Exception ex) {
		throw new SockJsTransportFailureException("Failed to execute request to " + url, ex);
	}
	HttpStatus status = HttpStatus.valueOf(response.getStatus());
	HttpHeaders responseHeaders = toHttpHeaders(response.getHeaders());
	return (response.getContent() != null ?
			new ResponseEntity<>(response.getContentAsString(), responseHeaders, status) :
			new ResponseEntity<>(responseHeaders, status));
}
 
Example #9
Source Project: java-technology-stack   Author: codeEngraver   File: AbstractSockJsSession.java    License: MIT License 6 votes vote down vote up
/**
 * For internal use within a TransportHandler and the (TransportHandler-specific)
 * session class.
 */
protected void writeFrame(SockJsFrame frame) throws SockJsTransportFailureException {
	if (logger.isTraceEnabled()) {
		logger.trace("Preparing to write " + frame);
	}
	try {
		writeFrameInternal(frame);
	}
	catch (Throwable ex) {
		logWriteFrameFailure(ex);
		try {
			// Force disconnect (so we won't try to send close frame)
			disconnect(CloseStatus.SERVER_ERROR);
		}
		catch (Throwable disconnectFailure) {
			// Ignore
		}
		try {
			close(CloseStatus.SERVER_ERROR);
		}
		catch (Throwable closeFailure) {
			// Nothing of consequence, already forced disconnect
		}
		throw new SockJsTransportFailureException("Failed to write " + frame, getId(), ex);
	}
}
 
Example #10
Source Project: java-technology-stack   Author: codeEngraver   File: AbstractHttpSockJsSession.java    License: MIT License 6 votes vote down vote up
/**
 * Handle all requests, except the first one, to receive messages on a SockJS
 * HTTP transport based session.
 * <p>Long polling-based transports (e.g. "xhr", "jsonp") complete the request
 * after writing any buffered message frames (or the next one). Streaming-based
 * transports ("xhr_streaming", "eventsource", and "htmlfile") leave the
 * response open longer for further streaming of message frames but will also
 * close it eventually after some amount of data has been sent.
 * @param request the current request
 * @param response the current response
 * @param frameFormat the transport-specific SocksJS frame format to use
 */
public void handleSuccessiveRequest(ServerHttpRequest request, ServerHttpResponse response,
		SockJsFrameFormat frameFormat) throws SockJsException {

	synchronized (this.responseLock) {
		try {
			if (isClosed()) {
				response.getBody().write(SockJsFrame.closeFrameGoAway().getContentBytes());
				return;
			}
			this.response = response;
			this.frameFormat = frameFormat;
			ServerHttpAsyncRequestControl control = request.getAsyncRequestControl(response);
			this.asyncRequestControl = control;
			control.start(-1);
			disableShallowEtagHeaderFilter(request);
			handleRequestInternal(request, response, false);
			this.readyToSend = isActive();
		}
		catch (Throwable ex) {
			tryCloseWithSockJsTransportError(ex, CloseStatus.SERVER_ERROR);
			throw new SockJsTransportFailureException("Failed to handle SockJS receive request", getId(), ex);
		}
	}
}
 
Example #11
Source Project: java-technology-stack   Author: codeEngraver   File: AbstractHttpSockJsSession.java    License: MIT License 6 votes vote down vote up
@Override
protected final void sendMessageInternal(String message) throws SockJsTransportFailureException {
	synchronized (this.responseLock) {
		this.messageCache.add(message);
		if (logger.isTraceEnabled()) {
			logger.trace(this.messageCache.size() + " message(s) to flush in session " + getId());
		}
		if (isActive() && this.readyToSend) {
			if (logger.isTraceEnabled()) {
				logger.trace("Session is active, ready to flush.");
			}
			cancelHeartbeat();
			flushCache();
		}
		else {
			if (logger.isTraceEnabled()) {
				logger.trace("Session is not active, not ready to flush.");
			}
		}
	}
}
 
Example #12
Source Project: java-technology-stack   Author: codeEngraver   File: WebSocketServerSockJsSession.java    License: MIT License 6 votes vote down vote up
@Override
public void sendMessageInternal(String message) throws SockJsTransportFailureException {
	// Open frame not sent yet?
	// If in the session initialization thread, then cache, otherwise wait.
	if (!this.openFrameSent) {
		synchronized (this.initSessionLock) {
			if (!this.openFrameSent) {
				this.initSessionCache.add(message);
				return;
			}
		}
	}

	cancelHeartbeat();
	writeFrame(SockJsFrame.messageFrame(getMessageCodec(), message));
	scheduleHeartbeat();
}
 
Example #13
Source Project: java-technology-stack   Author: codeEngraver   File: StreamingSockJsSession.java    License: MIT License 6 votes vote down vote up
@Override
protected void flushCache() throws SockJsTransportFailureException {
	while (!getMessageCache().isEmpty()) {
		String message = getMessageCache().poll();
		SockJsMessageCodec messageCodec = getSockJsServiceConfig().getMessageCodec();
		SockJsFrame frame = SockJsFrame.messageFrame(messageCodec, message);
		writeFrame(frame);

		this.byteCount += (frame.getContentBytes().length + 1);
		if (logger.isTraceEnabled()) {
			logger.trace(this.byteCount + " bytes written so far, " +
					getMessageCache().size() + " more messages not flushed");
		}
		if (this.byteCount >= getSockJsServiceConfig().getStreamBytesLimit()) {
			logger.trace("Streamed bytes limit reached, recycling current request");
			resetRequest();
			this.byteCount = 0;
			break;
		}
	}
	scheduleHeartbeat();
}
 
Example #14
Source Project: java-technology-stack   Author: codeEngraver   File: HtmlFileTransportHandler.java    License: MIT License 6 votes vote down vote up
@Override
public void handleRequestInternal(ServerHttpRequest request, ServerHttpResponse response,
		AbstractHttpSockJsSession sockJsSession) throws SockJsException {

	String callback = getCallbackParam(request);
	if (!StringUtils.hasText(callback)) {
		response.setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR);
		try {
			response.getBody().write("\"callback\" parameter required".getBytes(StandardCharsets.UTF_8));
		}
		catch (IOException ex) {
			sockJsSession.tryCloseWithSockJsTransportError(ex, CloseStatus.SERVER_ERROR);
			throw new SockJsTransportFailureException("Failed to write to response", sockJsSession.getId(), ex);
		}
		return;
	}

	super.handleRequestInternal(request, response, sockJsSession);
}
 
Example #15
Source Project: spring4-understanding   Author: langtianya   File: JettyXhrTransport.java    License: Apache License 2.0 6 votes vote down vote up
protected ResponseEntity<String> executeRequest(URI url, HttpMethod method, HttpHeaders headers, String body) {
	Request httpRequest = this.httpClient.newRequest(url).method(method);
	addHttpHeaders(httpRequest, headers);
	if (body != null) {
		httpRequest.content(new StringContentProvider(body));
	}
	ContentResponse response;
	try {
		response = httpRequest.send();
	}
	catch (Exception ex) {
		throw new SockJsTransportFailureException("Failed to execute request to " + url, ex);
	}
	HttpStatus status = HttpStatus.valueOf(response.getStatus());
	HttpHeaders responseHeaders = toHttpHeaders(response.getHeaders());
	return (response.getContent() != null ?
		new ResponseEntity<String>(response.getContentAsString(), responseHeaders, status) :
		new ResponseEntity<String>(responseHeaders, status));
}
 
Example #16
Source Project: spring4-understanding   Author: langtianya   File: AbstractSockJsSession.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * For internal use within a TransportHandler and the (TransportHandler-specific)
 * session class.
 */
protected void writeFrame(SockJsFrame frame) throws SockJsTransportFailureException {
	if (logger.isTraceEnabled()) {
		logger.trace("Preparing to write " + frame);
	}
	try {
		writeFrameInternal(frame);
	}
	catch (Throwable ex) {
		logWriteFrameFailure(ex);
		try {
			// Force disconnect (so we won't try to send close frame)
			disconnect(CloseStatus.SERVER_ERROR);
		}
		catch (Throwable disconnectFailure) {
			// Ignore
		}
		try {
			close(CloseStatus.SERVER_ERROR);
		}
		catch (Throwable closeFailure) {
			// Nothing of consequence, already forced disconnect
		}
		throw new SockJsTransportFailureException("Failed to write " + frame, getId(), ex);
	}
}
 
Example #17
Source Project: spring4-understanding   Author: langtianya   File: AbstractHttpSockJsSession.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Handle all requests, except the first one, to receive messages on a SockJS
 * HTTP transport based session.
 * <p>Long polling-based transports (e.g. "xhr", "jsonp") complete the request
 * after writing any buffered message frames (or the next one). Streaming-based
 * transports ("xhr_streaming", "eventsource", and "htmlfile") leave the
 * response open longer for further streaming of message frames but will also
 * close it eventually after some amount of data has been sent.
 * @param request the current request
 * @param response the current response
 * @param frameFormat the transport-specific SocksJS frame format to use
 */
public void handleSuccessiveRequest(ServerHttpRequest request, ServerHttpResponse response,
		SockJsFrameFormat frameFormat) throws SockJsException {

	synchronized (this.responseLock) {
		try {
			if (isClosed()) {
				response.getBody().write(SockJsFrame.closeFrameGoAway().getContentBytes());
				return;
			}
			this.response = response;
			this.frameFormat = frameFormat;
			this.asyncRequestControl = request.getAsyncRequestControl(response);
			this.asyncRequestControl.start(-1);

			disableShallowEtagHeaderFilter(request);

			handleRequestInternal(request, response, false);
			this.readyToSend = isActive();
		}
		catch (Throwable ex) {
			tryCloseWithSockJsTransportError(ex, CloseStatus.SERVER_ERROR);
			throw new SockJsTransportFailureException("Failed to handle SockJS receive request", getId(), ex);
		}
	}
}
 
Example #18
Source Project: spring4-understanding   Author: langtianya   File: AbstractHttpSockJsSession.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected final void sendMessageInternal(String message) throws SockJsTransportFailureException {
	synchronized (this.responseLock) {
		this.messageCache.add(message);
		if (logger.isTraceEnabled()) {
			logger.trace(this.messageCache.size() + " message(s) to flush in session " + this.getId());
		}
		if (isActive() && this.readyToSend) {
			if (logger.isTraceEnabled()) {
				logger.trace("Session is active, ready to flush.");
			}
			cancelHeartbeat();
			flushCache();
		}
		else {
			if (logger.isTraceEnabled()) {
				logger.trace("Session is not active, not ready to flush.");
			}
		}
	}
}
 
Example #19
Source Project: spring4-understanding   Author: langtianya   File: WebSocketServerSockJsSession.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void sendMessageInternal(String message) throws SockJsTransportFailureException {

	// Open frame not sent yet?
	// If in the session initialization thread, then cache, otherwise wait.

	if (!this.openFrameSent) {
		synchronized (this.initSessionLock) {
			if (!this.openFrameSent) {
				this.initSessionCache.add(message);
				return;
			}
		}
	}
	cancelHeartbeat();
	writeFrame(SockJsFrame.messageFrame(getMessageCodec(), message));
	scheduleHeartbeat();
}
 
Example #20
Source Project: spring4-understanding   Author: langtianya   File: StreamingSockJsSession.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected void flushCache() throws SockJsTransportFailureException {
	while (!getMessageCache().isEmpty()) {
		String message = getMessageCache().poll();
		SockJsMessageCodec messageCodec = getSockJsServiceConfig().getMessageCodec();
		SockJsFrame frame = SockJsFrame.messageFrame(messageCodec, message);
		writeFrame(frame);

		this.byteCount += (frame.getContentBytes().length + 1);
		if (logger.isTraceEnabled()) {
			logger.trace(this.byteCount + " bytes written so far, " +
					getMessageCache().size() + " more messages not flushed");
		}
		if (this.byteCount >= getSockJsServiceConfig().getStreamBytesLimit()) {
			logger.trace("Streamed bytes limit reached, recycling current request");
			resetRequest();
			this.byteCount = 0;
			break;
		}
	}
	scheduleHeartbeat();
}
 
Example #21
Source Project: spring4-understanding   Author: langtianya   File: HtmlFileTransportHandler.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void handleRequestInternal(ServerHttpRequest request, ServerHttpResponse response,
		AbstractHttpSockJsSession sockJsSession) throws SockJsException {

	String callback = getCallbackParam(request);
	if (!StringUtils.hasText(callback)) {
		response.setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR);
		try {
			response.getBody().write("\"callback\" parameter required".getBytes(UTF8_CHARSET));
		}
		catch (IOException ex) {
			sockJsSession.tryCloseWithSockJsTransportError(ex, CloseStatus.SERVER_ERROR);
			throw new SockJsTransportFailureException("Failed to write to response", sockJsSession.getId(), ex);
		}
		return;
	}

	super.handleRequestInternal(request, response, sockJsSession);
}
 
Example #22
Source Project: spring4-understanding   Author: langtianya   File: JsonpPollingTransportHandler.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void handleRequestInternal(ServerHttpRequest request, ServerHttpResponse response,
		AbstractHttpSockJsSession sockJsSession) throws SockJsException {

	try {
		String callback = getCallbackParam(request);
		if (!StringUtils.hasText(callback)) {
			response.setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR);
			response.getBody().write("\"callback\" parameter required".getBytes(UTF8_CHARSET));
			return;
		}
	}
	catch (Throwable ex) {
		sockJsSession.tryCloseWithSockJsTransportError(ex, CloseStatus.SERVER_ERROR);
		throw new SockJsTransportFailureException("Failed to send error", sockJsSession.getId(), ex);
	}

	super.handleRequestInternal(request, response, sockJsSession);
}
 
Example #23
Source Project: spring-analysis-note   Author: Vip-Augus   File: UndertowXhrTransport.java    License: MIT License 5 votes vote down vote up
private void executeReceiveRequest(final TransportRequest transportRequest,
		final URI url, final HttpHeaders headers, final XhrClientSockJsSession session,
		final SettableListenableFuture<WebSocketSession> connectFuture) {

	if (logger.isTraceEnabled()) {
		logger.trace("Starting XHR receive request for " + url);
	}

	ClientCallback<ClientConnection> clientCallback = new ClientCallback<ClientConnection>() {
		@Override
		public void completed(ClientConnection connection) {
			ClientRequest request = new ClientRequest().setMethod(Methods.POST).setPath(url.getPath());
			HttpString headerName = HttpString.tryFromString(HttpHeaders.HOST);
			request.getRequestHeaders().add(headerName, url.getHost());
			addHttpHeaders(request, headers);
			HttpHeaders httpHeaders = transportRequest.getHttpRequestHeaders();
			connection.sendRequest(request, createReceiveCallback(transportRequest,
					url, httpHeaders, session, connectFuture));
		}

		@Override
		public void failed(IOException ex) {
			throw new SockJsTransportFailureException("Failed to execute request to " + url, ex);
		}
	};

	this.httpClient.connect(clientCallback, url, this.worker, this.bufferPool, this.optionMap);
}
 
Example #24
Source Project: spring-analysis-note   Author: Vip-Augus   File: AbstractSockJsSession.java    License: MIT License 5 votes vote down vote up
protected void sendHeartbeat() throws SockJsTransportFailureException {
	synchronized (this.responseLock) {
		if (isActive() && !this.heartbeatDisabled) {
			writeFrame(SockJsFrame.heartbeatFrame());
			scheduleHeartbeat();
		}
	}
}
 
Example #25
Source Project: spring-analysis-note   Author: Vip-Augus   File: PollingSockJsSession.java    License: MIT License 5 votes vote down vote up
@Override
protected void flushCache() throws SockJsTransportFailureException {
	String[] messages = new String[getMessageCache().size()];
	for (int i = 0; i < messages.length; i++) {
		messages[i] = getMessageCache().poll();
	}
	SockJsMessageCodec messageCodec = getSockJsServiceConfig().getMessageCodec();
	SockJsFrame frame = SockJsFrame.messageFrame(messageCodec, messages);
	writeFrame(frame);
}
 
Example #26
Source Project: spring-analysis-note   Author: Vip-Augus   File: WebSocketTransportHandler.java    License: MIT License 5 votes vote down vote up
@Override
public void handleRequest(ServerHttpRequest request, ServerHttpResponse response,
		WebSocketHandler wsHandler, SockJsSession wsSession) throws SockJsException {

	WebSocketServerSockJsSession sockJsSession = (WebSocketServerSockJsSession) wsSession;
	try {
		wsHandler = new SockJsWebSocketHandler(getServiceConfig(), wsHandler, sockJsSession);
		this.handshakeHandler.doHandshake(request, response, wsHandler, sockJsSession.getAttributes());
	}
	catch (Throwable ex) {
		sockJsSession.tryCloseWithSockJsTransportError(ex, CloseStatus.SERVER_ERROR);
		throw new SockJsTransportFailureException("WebSocket handshake failure", wsSession.getId(), ex);
	}
}
 
Example #27
Source Project: spring-analysis-note   Author: Vip-Augus   File: SockJsSessionTests.java    License: MIT License 5 votes vote down vote up
@Test
public void writeFrameIoException() throws Exception {
	this.session.setExceptionOnWrite(new IOException());
	this.session.delegateConnectionEstablished();

	try {
		this.session.writeFrame(SockJsFrame.openFrame());
		fail("expected exception");
	}
	catch (SockJsTransportFailureException ex) {
		assertEquals(CloseStatus.SERVER_ERROR, this.session.getCloseStatus());
		verify(this.webSocketHandler).afterConnectionClosed(this.session, CloseStatus.SERVER_ERROR);
	}
}
 
Example #28
Source Project: java-technology-stack   Author: codeEngraver   File: UndertowXhrTransport.java    License: MIT License 5 votes vote down vote up
private void executeReceiveRequest(final TransportRequest transportRequest,
		final URI url, final HttpHeaders headers, final XhrClientSockJsSession session,
		final SettableListenableFuture<WebSocketSession> connectFuture) {

	if (logger.isTraceEnabled()) {
		logger.trace("Starting XHR receive request for " + url);
	}

	ClientCallback<ClientConnection> clientCallback = new ClientCallback<ClientConnection>() {
		@Override
		public void completed(ClientConnection connection) {
			ClientRequest request = new ClientRequest().setMethod(Methods.POST).setPath(url.getPath());
			HttpString headerName = HttpString.tryFromString(HttpHeaders.HOST);
			request.getRequestHeaders().add(headerName, url.getHost());
			addHttpHeaders(request, headers);
			HttpHeaders httpHeaders = transportRequest.getHttpRequestHeaders();
			connection.sendRequest(request, createReceiveCallback(transportRequest,
					url, httpHeaders, session, connectFuture));
		}

		@Override
		public void failed(IOException ex) {
			throw new SockJsTransportFailureException("Failed to execute request to " + url, ex);
		}
	};

	this.httpClient.connect(clientCallback, url, this.worker, this.bufferPool, this.optionMap);
}
 
Example #29
Source Project: java-technology-stack   Author: codeEngraver   File: AbstractSockJsSession.java    License: MIT License 5 votes vote down vote up
protected void sendHeartbeat() throws SockJsTransportFailureException {
	synchronized (this.responseLock) {
		if (isActive() && !this.heartbeatDisabled) {
			writeFrame(SockJsFrame.heartbeatFrame());
			scheduleHeartbeat();
		}
	}
}
 
Example #30
Source Project: java-technology-stack   Author: codeEngraver   File: PollingSockJsSession.java    License: MIT License 5 votes vote down vote up
@Override
protected void flushCache() throws SockJsTransportFailureException {
	String[] messages = new String[getMessageCache().size()];
	for (int i = 0; i < messages.length; i++) {
		messages[i] = getMessageCache().poll();
	}
	SockJsMessageCodec messageCodec = getSockJsServiceConfig().getMessageCodec();
	SockJsFrame frame = SockJsFrame.messageFrame(messageCodec, messages);
	writeFrame(frame);
}