org.springframework.web.socket.sockjs.SockJsException Java Examples

The following examples show how to use org.springframework.web.socket.sockjs.SockJsException. 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 File: AbstractHttpSendingTransportHandler.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
protected final String getCallbackParam(ServerHttpRequest request) {
	String query = request.getURI().getQuery();
	MultiValueMap<String, String> params = UriComponentsBuilder.newInstance().query(query).build().getQueryParams();
	String value = params.getFirst("c");
	if (StringUtils.isEmpty(value)) {
		return null;
	}
	try {
		String result = UriUtils.decode(value, "UTF-8");
		return (CALLBACK_PARAM_PATTERN.matcher(result).matches() ? result : null);
	}
	catch (UnsupportedEncodingException ex) {
		// should never happen
		throw new SockJsException("Unable to decode callback query parameter", null, ex);
	}
}
 
Example #2
Source File: JettyXhrTransport.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
public void onContent(Response response, ByteBuffer buffer) {
	while (true) {
		if (this.sockJsSession.isDisconnected()) {
			if (logger.isDebugEnabled()) {
				logger.debug("SockJS sockJsSession closed, closing response.");
			}
			response.abort(new SockJsException("Session closed.", this.sockJsSession.getId(), null));
			return;
		}
		if (buffer.remaining() == 0) {
			break;
		}
		int b = buffer.get();
		if (b == '\n') {
			handleFrame();
		}
		else {
			this.outputStream.write(b);
		}
	}
}
 
Example #3
Source File: HtmlFileTransportHandler.java    From java-technology-stack with 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 #4
Source File: JettyXhrTransport.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
public void onContent(Response response, ByteBuffer buffer) {
	while (true) {
		if (this.sockJsSession.isDisconnected()) {
			if (logger.isDebugEnabled()) {
				logger.debug("SockJS sockJsSession closed, closing response.");
			}
			response.abort(new SockJsException("Session closed.", this.sockJsSession.getId(), null));
			return;
		}
		if (buffer.remaining() == 0) {
			break;
		}
		int b = buffer.get();
		if (b == '\n') {
			handleFrame();
		}
		else {
			this.outputStream.write(b);
		}
	}
}
 
Example #5
Source File: AbstractHttpSockJsSession.java    From spring-analysis-note with 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 #6
Source File: HtmlFileTransportHandler.java    From spring-analysis-note with 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 #7
Source File: JsonpPollingTransportHandler.java    From spring4-understanding with 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 #8
Source File: AbstractHttpSockJsSession.java    From java-technology-stack with 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 #9
Source File: JettyXhrTransport.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
public void onContent(Response response, ByteBuffer buffer) {
	while (true) {
		if (this.sockJsSession.isDisconnected()) {
			if (logger.isDebugEnabled()) {
				logger.debug("SockJS sockJsSession closed, closing response.");
			}
			response.abort(new SockJsException("Session closed.", this.sockJsSession.getId(), null));
			return;
		}
		if (buffer.remaining() == 0) {
			break;
		}
		int b = buffer.get();
		if (b == '\n') {
			handleFrame();
		}
		else {
			this.outputStream.write(b);
		}
	}
}
 
Example #10
Source File: HtmlFileTransportHandler.java    From spring4-understanding with 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 #11
Source File: AbstractHttpSockJsSession.java    From spring4-understanding with 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 #12
Source File: JettyXhrTransport.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void start() {
	try {
		if (!this.httpClient.isRunning()) {
			this.httpClient.start();
		}
	}
	catch (Exception e) {
		throw new SockJsException("Failed to start " + this, e);
	}
}
 
Example #13
Source File: AbstractHttpReceivingTransportHandler.java    From java-technology-stack with MIT License 5 votes vote down vote up
private void handleReadError(ServerHttpResponse response, String error, String sessionId) {
	try {
		response.setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR);
		response.getBody().write(error.getBytes(StandardCharsets.UTF_8));
	}
	catch (IOException ex) {
		throw new SockJsException("Failed to send error: " + error, sessionId, ex);
	}
}
 
Example #14
Source File: SockJsHttpRequestHandler.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void handleRequest(HttpServletRequest servletRequest, HttpServletResponse servletResponse)
		throws ServletException, IOException {

	ServerHttpRequest request = new ServletServerHttpRequest(servletRequest);
	ServerHttpResponse response = new ServletServerHttpResponse(servletResponse);

	try {
		this.sockJsService.handleRequest(request, response, getSockJsPath(servletRequest), this.webSocketHandler);
	}
	catch (Throwable ex) {
		throw new SockJsException("Uncaught failure in SockJS request, uri=" + request.getURI(), ex);
	}
}
 
Example #15
Source File: SockJsServiceTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
protected void handleTransportRequest(ServerHttpRequest req, ServerHttpResponse res, WebSocketHandler handler,
		String sessionId, String transport) throws SockJsException {
	this.sessionId = sessionId;
	this.transport = transport;
	this.handler = handler;
}
 
Example #16
Source File: AbstractHttpSendingTransportHandler.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public final void handleRequest(ServerHttpRequest request, ServerHttpResponse response,
		WebSocketHandler wsHandler, SockJsSession wsSession) throws SockJsException {

	AbstractHttpSockJsSession sockJsSession = (AbstractHttpSockJsSession) wsSession;

	// https://github.com/sockjs/sockjs-client/issues/130
	// sockJsSession.setAcceptedProtocol(protocol);

	// Set content type before writing
	response.getHeaders().setContentType(getContentType());

	handleRequestInternal(request, response, sockJsSession);
}
 
Example #17
Source File: WebSocketTransportHandler.java    From java-technology-stack with 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 #18
Source File: JettyXhrTransport.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void stop() {
	try {
		if (this.httpClient.isRunning()) {
			this.httpClient.stop();
		}
	}
	catch (Exception e) {
		throw new SockJsException("Failed to stop " + this, e);
	}
}
 
Example #19
Source File: AbstractHttpSockJsSession.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Handle the first request for receiving messages on a SockJS HTTP transport
 * based session.
 * <p>Long polling-based transports (e.g. "xhr", "jsonp") complete the request
 * after writing the open frame. 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 handleInitialRequest(ServerHttpRequest request, ServerHttpResponse response,
		SockJsFrameFormat frameFormat) throws SockJsException {

	this.uri = request.getURI();
	this.handshakeHeaders = request.getHeaders();
	this.principal = request.getPrincipal();
	this.localAddress = request.getLocalAddress();
	this.remoteAddress = request.getRemoteAddress();

	synchronized (this.responseLock) {
		try {
			this.response = response;
			this.frameFormat = frameFormat;
			this.asyncRequestControl = request.getAsyncRequestControl(response);
			this.asyncRequestControl.start(-1);

			disableShallowEtagHeaderFilter(request);

			// Let "our" handler know before sending the open frame to the remote handler
			delegateConnectionEstablished();

			handleRequestInternal(request, response, true);

			// Request might have been reset (e.g. polling sessions do after writing)
			this.readyToSend = isActive();
		}
		catch (Throwable ex) {
			tryCloseWithSockJsTransportError(ex, CloseStatus.SERVER_ERROR);
			throw new SockJsTransportFailureException("Failed to open session", getId(), ex);
		}
	}
}
 
Example #20
Source File: WebSocketTransportHandler.java    From spring4-understanding with Apache License 2.0 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 #21
Source File: JsonpReceivingTransportHandler.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void handleRequestInternal(ServerHttpRequest request, ServerHttpResponse response,
		WebSocketHandler wsHandler, AbstractHttpSockJsSession sockJsSession) throws SockJsException {

	super.handleRequestInternal(request, response, wsHandler, sockJsSession);
	try {
		response.getBody().write("ok".getBytes(UTF8_CHARSET));
	}
	catch (IOException ex) {
		throw new SockJsException("Failed to write to the response body", sockJsSession.getId(), ex);
	}
}
 
Example #22
Source File: AbstractHttpReceivingTransportHandler.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public final void handleRequest(ServerHttpRequest request, ServerHttpResponse response,
		WebSocketHandler wsHandler, SockJsSession wsSession) throws SockJsException {

	Assert.notNull(wsSession, "No session");
	AbstractHttpSockJsSession sockJsSession = (AbstractHttpSockJsSession) wsSession;

	handleRequestInternal(request, response, wsHandler, sockJsSession);
}
 
Example #23
Source File: AbstractHttpReceivingTransportHandler.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private void handleReadError(ServerHttpResponse response, String error, String sessionId) {
	try {
		response.setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR);
		response.getBody().write(error.getBytes(UTF8_CHARSET));
	}
	catch (IOException ex) {
		throw new SockJsException("Failed to send error: " + error, sessionId, ex);
	}
}
 
Example #24
Source File: AbstractHttpSendingTransportHandler.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public final void handleRequest(ServerHttpRequest request, ServerHttpResponse response,
		WebSocketHandler wsHandler, SockJsSession wsSession) throws SockJsException {

	AbstractHttpSockJsSession sockJsSession = (AbstractHttpSockJsSession) wsSession;

	String protocol = null;  // https://github.com/sockjs/sockjs-client/issues/130
	sockJsSession.setAcceptedProtocol(protocol);

	// Set content type before writing
	response.getHeaders().setContentType(getContentType());

	handleRequestInternal(request, response, sockJsSession);
}
 
Example #25
Source File: SockJsServiceTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
protected void handleTransportRequest(ServerHttpRequest req, ServerHttpResponse res, WebSocketHandler handler,
		String sessionId, String transport) throws SockJsException {
	this.sessionId = sessionId;
	this.transport = transport;
	this.handler = handler;
}
 
Example #26
Source File: SockJsServiceTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
protected void handleTransportRequest(ServerHttpRequest req, ServerHttpResponse res, WebSocketHandler handler,
		String sessionId, String transport) throws SockJsException {
	this.sessionId = sessionId;
	this.transport = transport;
	this.handler = handler;
}
 
Example #27
Source File: SockJsHttpRequestHandler.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public void handleRequest(HttpServletRequest servletRequest, HttpServletResponse servletResponse)
		throws ServletException, IOException {

	ServerHttpRequest request = new ServletServerHttpRequest(servletRequest);
	ServerHttpResponse response = new ServletServerHttpResponse(servletResponse);

	try {
		this.sockJsService.handleRequest(request, response, getSockJsPath(servletRequest), this.webSocketHandler);
	}
	catch (Throwable ex) {
		throw new SockJsException("Uncaught failure in SockJS request, uri=" + request.getURI(), ex);
	}
}
 
Example #28
Source File: JettyXhrTransport.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public void start() {
	try {
		if (!this.httpClient.isRunning()) {
			this.httpClient.start();
		}
	}
	catch (Exception ex) {
		throw new SockJsException("Failed to start JettyXhrTransport", ex);
	}
}
 
Example #29
Source File: JettyXhrTransport.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public void stop() {
	try {
		if (this.httpClient.isRunning()) {
			this.httpClient.stop();
		}
	}
	catch (Exception ex) {
		throw new SockJsException("Failed to stop JettyXhrTransport", ex);
	}
}
 
Example #30
Source File: WebSocketTransportHandler.java    From spring-analysis-note with 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);
	}
}