类org.springframework.web.socket.sockjs.frame.SockJsFrameFormat源码实例Demo

下面列出了怎么用org.springframework.web.socket.sockjs.frame.SockJsFrameFormat的API类实例代码及写法,或者点击链接到github查看源代码。

/**
 * 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);
		}
	}
}
 
@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);
}
 
/**
 * 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);
		}
	}
}
 
@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);
}
 
/**
 * 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);
		}
	}
}
 
@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();
		}
	}
}
 
@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);
		}
	};
}
 
@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();
		}
	}
}
 
@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);
		}
	};
}
 
/**
 * 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);
		}
	}
}
 
@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);
		}
	};
}
 
@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);
		}
	};
}
 
@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);
}
 
@Override
protected SockJsFrameFormat getFrameFormat(ServerHttpRequest request) {
	return new DefaultSockJsFrameFormat("data: %s\r\n\r\n");
}
 
@Override
protected SockJsFrameFormat getFrameFormat(ServerHttpRequest request) {
	return new DefaultSockJsFrameFormat("%s\n");
}
 
@Override
protected SockJsFrameFormat getFrameFormat(ServerHttpRequest request) {
	return new DefaultSockJsFrameFormat("%s\n");
}
 
@Override
protected SockJsFrameFormat getFrameFormat(ServerHttpRequest request) {
	return new DefaultSockJsFrameFormat("data: %s\r\n\r\n");
}
 
@Override
protected SockJsFrameFormat getFrameFormat(ServerHttpRequest request) {
	return new DefaultSockJsFrameFormat("%s\n");
}
 
@Override
protected SockJsFrameFormat getFrameFormat(ServerHttpRequest request) {
	return new DefaultSockJsFrameFormat("%s\n");
}
 
@Override
protected SockJsFrameFormat getFrameFormat(ServerHttpRequest request) {
	return new DefaultSockJsFrameFormat("data: %s\r\n\r\n");
}
 
@Override
protected SockJsFrameFormat getFrameFormat(ServerHttpRequest request) {
	return new DefaultSockJsFrameFormat("%s\n");
}
 
@Override
protected SockJsFrameFormat getFrameFormat(ServerHttpRequest request) {
	return new DefaultSockJsFrameFormat("%s\n");
}
 
protected abstract SockJsFrameFormat getFrameFormat(ServerHttpRequest request); 
protected abstract SockJsFrameFormat getFrameFormat(ServerHttpRequest request); 
protected abstract SockJsFrameFormat getFrameFormat(ServerHttpRequest request); 
 类方法
 同包方法