org.springframework.web.socket.sockjs.frame.SockJsFrameFormat Java Examples

The following examples show how to use org.springframework.web.socket.sockjs.frame.SockJsFrameFormat. 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: 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 #2
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 #3
Source File: HttpSendingTransportHandlerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void frameFormats() throws Exception {
	this.servletRequest.setQueryString("c=callback");
	this.servletRequest.addParameter("c", "callback");

	SockJsFrame frame = SockJsFrame.openFrame();

	SockJsFrameFormat format = new XhrPollingTransportHandler().getFrameFormat(this.request);
	String formatted = format.format(frame);
	assertEquals(frame.getContent() + "\n", formatted);

	format = new XhrStreamingTransportHandler().getFrameFormat(this.request);
	formatted = format.format(frame);
	assertEquals(frame.getContent() + "\n", formatted);

	format = new HtmlFileTransportHandler().getFrameFormat(this.request);
	formatted = format.format(frame);
	assertEquals("<script>\np(\"" + frame.getContent() + "\");\n</script>\r\n", formatted);

	format = new EventSourceTransportHandler().getFrameFormat(this.request);
	formatted = format.format(frame);
	assertEquals("data: " + frame.getContent() + "\r\n\r\n", formatted);
}
 
Example #4
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 #5
Source File: HttpSendingTransportHandlerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void frameFormats() throws Exception {
	this.servletRequest.setQueryString("c=callback");
	this.servletRequest.addParameter("c", "callback");

	SockJsFrame frame = SockJsFrame.openFrame();

	SockJsFrameFormat format = new XhrPollingTransportHandler().getFrameFormat(this.request);
	String formatted = format.format(frame);
	assertEquals(frame.getContent() + "\n", formatted);

	format = new XhrStreamingTransportHandler().getFrameFormat(this.request);
	formatted = format.format(frame);
	assertEquals(frame.getContent() + "\n", formatted);

	format = new HtmlFileTransportHandler().getFrameFormat(this.request);
	formatted = format.format(frame);
	assertEquals("<script>\np(\"" + frame.getContent() + "\");\n</script>\r\n", formatted);

	format = new EventSourceTransportHandler().getFrameFormat(this.request);
	formatted = format.format(frame);
	assertEquals("data: " + frame.getContent() + "\r\n\r\n", formatted);
}
 
Example #6
Source File: AbstractHttpSockJsSession.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
protected void writeFrameInternal(SockJsFrame frame) throws IOException {
	if (isActive()) {
		SockJsFrameFormat frameFormat = this.frameFormat;
		ServerHttpResponse response = this.response;
		if (frameFormat != null && response != null) {
			String formattedFrame = frameFormat.format(frame);
			if (logger.isTraceEnabled()) {
				logger.trace("Writing to HTTP response: " + formattedFrame);
			}
			response.getBody().write(formattedFrame.getBytes(SockJsFrame.CHARSET));
			response.flush();
		}
	}
}
 
Example #7
Source File: HttpSendingTransportHandlerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void frameFormats() throws Exception {
	this.servletRequest.setQueryString("c=callback");
	this.servletRequest.addParameter("c", "callback");

	SockJsFrame frame = SockJsFrame.openFrame();

	SockJsFrameFormat format = new XhrPollingTransportHandler().getFrameFormat(this.request);
	String formatted = format.format(frame);
	assertEquals(frame.getContent() + "\n", formatted);

	format = new XhrStreamingTransportHandler().getFrameFormat(this.request);
	formatted = format.format(frame);
	assertEquals(frame.getContent() + "\n", formatted);

	format = new HtmlFileTransportHandler().getFrameFormat(this.request);
	formatted = format.format(frame);
	assertEquals("<script>\np(\"" + frame.getContent() + "\");\n</script>\r\n", formatted);

	format = new EventSourceTransportHandler().getFrameFormat(this.request);
	formatted = format.format(frame);
	assertEquals("data: " + frame.getContent() + "\r\n\r\n", formatted);

	format = new JsonpPollingTransportHandler().getFrameFormat(this.request);
	formatted = format.format(frame);
	assertEquals("/**/callback(\"" + frame.getContent() + "\");\r\n", formatted);
}
 
Example #8
Source File: JsonpPollingTransportHandler.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
protected SockJsFrameFormat getFrameFormat(ServerHttpRequest request) {
	// We already validated the parameter above...
	String callback = getCallbackParam(request);

	return new DefaultSockJsFrameFormat("/**/" + callback + "(\"%s\");\r\n") {
		@Override
		protected String preProcessContent(String content) {
			return JavaScriptUtils.javaScriptEscape(content);
		}
	};
}
 
Example #9
Source File: HtmlFileTransportHandler.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
protected SockJsFrameFormat getFrameFormat(ServerHttpRequest request) {
	return new DefaultSockJsFrameFormat("<script>\np(\"%s\");\n</script>\r\n") {
		@Override
		protected String preProcessContent(String content) {
			return JavaScriptUtils.javaScriptEscape(content);
		}
	};
}
 
Example #10
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 #11
Source File: HtmlFileTransportHandler.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
protected SockJsFrameFormat getFrameFormat(ServerHttpRequest request) {
	return new DefaultSockJsFrameFormat("<script>\np(\"%s\");\n</script>\r\n") {
		@Override
		protected String preProcessContent(String content) {
			return JavaScriptUtils.javaScriptEscape(content);
		}
	};
}
 
Example #12
Source File: AbstractHttpSockJsSession.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
protected void writeFrameInternal(SockJsFrame frame) throws IOException {
	if (isActive()) {
		SockJsFrameFormat frameFormat = this.frameFormat;
		ServerHttpResponse response = this.response;
		if (frameFormat != null && response != null) {
			String formattedFrame = frameFormat.format(frame);
			if (logger.isTraceEnabled()) {
				logger.trace("Writing to HTTP response: " + formattedFrame);
			}
			response.getBody().write(formattedFrame.getBytes(SockJsFrame.CHARSET));
			response.flush();
		}
	}
}
 
Example #13
Source File: HtmlFileTransportHandler.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
protected SockJsFrameFormat getFrameFormat(ServerHttpRequest request) {
	return new DefaultSockJsFrameFormat("<script>\np(\"%s\");\n</script>\r\n") {
		@Override
		protected String preProcessContent(String content) {
			return JavaScriptUtils.javaScriptEscape(content);
		}
	};
}
 
Example #14
Source File: EventSourceTransportHandler.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
protected SockJsFrameFormat getFrameFormat(ServerHttpRequest request) {
	return new DefaultSockJsFrameFormat("data: %s\r\n\r\n");
}
 
Example #15
Source File: XhrPollingTransportHandler.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
protected SockJsFrameFormat getFrameFormat(ServerHttpRequest request) {
	return new DefaultSockJsFrameFormat("%s\n");
}
 
Example #16
Source File: XhrStreamingTransportHandler.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
protected SockJsFrameFormat getFrameFormat(ServerHttpRequest request) {
	return new DefaultSockJsFrameFormat("%s\n");
}
 
Example #17
Source File: XhrStreamingTransportHandler.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
protected SockJsFrameFormat getFrameFormat(ServerHttpRequest request) {
	return new DefaultSockJsFrameFormat("%s\n");
}
 
Example #18
Source File: XhrPollingTransportHandler.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
protected SockJsFrameFormat getFrameFormat(ServerHttpRequest request) {
	return new DefaultSockJsFrameFormat("%s\n");
}
 
Example #19
Source File: EventSourceTransportHandler.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
protected SockJsFrameFormat getFrameFormat(ServerHttpRequest request) {
	return new DefaultSockJsFrameFormat("data: %s\r\n\r\n");
}
 
Example #20
Source File: EventSourceTransportHandler.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
protected SockJsFrameFormat getFrameFormat(ServerHttpRequest request) {
	return new DefaultSockJsFrameFormat("data: %s\r\n\r\n");
}
 
Example #21
Source File: XhrPollingTransportHandler.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
protected SockJsFrameFormat getFrameFormat(ServerHttpRequest request) {
	return new DefaultSockJsFrameFormat("%s\n");
}
 
Example #22
Source File: XhrStreamingTransportHandler.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
protected SockJsFrameFormat getFrameFormat(ServerHttpRequest request) {
	return new DefaultSockJsFrameFormat("%s\n");
}
 
Example #23
Source File: AbstractHttpSendingTransportHandler.java    From java-technology-stack with MIT License votes vote down vote up
protected abstract SockJsFrameFormat getFrameFormat(ServerHttpRequest request); 
Example #24
Source File: AbstractHttpSendingTransportHandler.java    From spring-analysis-note with MIT License votes vote down vote up
protected abstract SockJsFrameFormat getFrameFormat(ServerHttpRequest request); 
Example #25
Source File: AbstractHttpSendingTransportHandler.java    From spring4-understanding with Apache License 2.0 votes vote down vote up
protected abstract SockJsFrameFormat getFrameFormat(ServerHttpRequest request);