@Override public StreamingSockJsSession createSession( String sessionId, WebSocketHandler handler, Map<String, Object> attributes) { return new HtmlFileStreamingSockJsSession(sessionId, getServiceConfig(), handler, attributes); }
@Override public StreamingSockJsSession createSession( String sessionId, WebSocketHandler handler, Map<String, Object> attributes) { return new XhrStreamingSockJsSession(sessionId, getServiceConfig(), handler, attributes); }
@Override public StreamingSockJsSession createSession( String sessionId, WebSocketHandler handler, Map<String, Object> attributes) { return new EventSourceStreamingSockJsSession(sessionId, getServiceConfig(), handler, attributes); }
private static Set<TransportHandler> getDefaultTransportHandlers(@Nullable Collection<TransportHandler> overrides) { Set<TransportHandler> result = new LinkedHashSet<>(8); result.add(new XhrPollingTransportHandler()); result.add(new XhrReceivingTransportHandler()); result.add(new XhrStreamingTransportHandler()); result.add(new EventSourceTransportHandler()); result.add(new HtmlFileTransportHandler()); try { result.add(new WebSocketTransportHandler(new DefaultHandshakeHandler())); } catch (Exception ex) { Log logger = LogFactory.getLog(DefaultSockJsService.class); if (logger.isWarnEnabled()) { logger.warn("Failed to create a default WebSocketTransportHandler", ex); } } if (overrides != null) { result.addAll(overrides); } return result; }
@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); }
@Test public void defaultTransportHandlersWithOverride() { XhrReceivingTransportHandler xhrHandler = new XhrReceivingTransportHandler(); DefaultSockJsService service = new DefaultSockJsService(mock(TaskScheduler.class), xhrHandler); Map<TransportType, TransportHandler> handlers = service.getTransportHandlers(); assertEquals(6, handlers.size()); assertSame(xhrHandler, handlers.get(xhrHandler.getTransportType())); }
@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); } }
@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); }
private void handleRequestAndExpectFailure() throws Exception { resetResponse(); WebSocketHandler wsHandler = mock(WebSocketHandler.class); AbstractSockJsSession session = new TestHttpSockJsSession("1", new StubSockJsServiceConfig(), wsHandler, null); new XhrReceivingTransportHandler().handleRequest(this.request, this.response, wsHandler, session); assertEquals(500, this.servletResponse.getStatus()); verifyNoMoreInteractions(wsHandler); }
@Test(expected = IllegalArgumentException.class) public void readMessagesNoSession() throws Exception { WebSocketHandler webSocketHandler = mock(WebSocketHandler.class); new XhrReceivingTransportHandler().handleRequest(this.request, this.response, webSocketHandler, null); }
@Test public void readMessagesXhr() throws Exception { this.servletRequest.setContent("[\"x\"]".getBytes("UTF-8")); handleRequest(new XhrReceivingTransportHandler()); assertEquals(204, this.servletResponse.getStatus()); }
@Override public void setServletContext(ServletContext servletContext) { for (TransportHandler handler : getTransportHandlers().values()) { if (handler instanceof ServletContextAware) { ((ServletContextAware) handler).setServletContext(servletContext); } } } }
/** * Create a DefaultSockJsService with overridden {@link TransportHandler handler} types * replacing the corresponding default handler implementation. * @param scheduler a task scheduler for heart-beat messages and removing timed-out sessions; * the provided TaskScheduler should be declared as a Spring bean to ensure it gets * initialized at start-up and shuts down when the application stops * @param handlerOverrides zero or more overrides to the default transport handler types */ public DefaultSockJsService(TaskScheduler scheduler, Collection<TransportHandler> handlerOverrides) { super(scheduler, getDefaultTransportHandlers(handlerOverrides)); }
@Override public void start() { if (!isRunning()) { this.running = true; if (this.handshakeHandler instanceof Lifecycle) { ((Lifecycle) this.handshakeHandler).start(); } } }
@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); }
@Override protected byte[] getPrelude(ServerHttpRequest request) { // We already validated the parameter above... String callback = getCallbackParam(request); String html = String.format(PARTIAL_HTML_CONTENT, callback); return html.getBytes(StandardCharsets.UTF_8); } }
@Override public PollingSockJsSession createSession( String sessionId, WebSocketHandler handler, Map<String, Object> attributes) { return new PollingSockJsSession(sessionId, getServiceConfig(), handler, attributes); }
@Override public AbstractSockJsSession createSession(String id, WebSocketHandler handler, Map<String, Object> attrs) { return new WebSocketServerSockJsSession(id, getServiceConfig(), handler, attrs); }
/** * Create a DefaultSockJsService with default {@link TransportHandler handler} types. * @param scheduler a task scheduler for heart-beat messages and removing * timed-out sessions; the provided TaskScheduler should be declared as a * Spring bean to ensure it is initialized at start up and shut down when the * application stops. */ public DefaultSockJsService(TaskScheduler scheduler) { this(scheduler, getDefaultTransportHandlers(null)); }
@Override public void stop() { if (isRunning()) { this.running = false; if (this.handshakeHandler instanceof Lifecycle) { ((Lifecycle) this.handshakeHandler).stop(); } } }