List of usage examples for org.springframework.web.socket.sockjs SockJsException SockJsException
public SockJsException(String message, @Nullable String sessionId, @Nullable Throwable cause)
From source file:org.springframework.web.socket.sockjs.support.AbstractSockJsService.java
/** * This method determines the SockJS path and handles SockJS static URLs. * Session URLs and raw WebSocket requests are delegated to abstract methods. *//*from www .j a v a2 s.co m*/ @Override public final void handleRequest(ServerHttpRequest request, ServerHttpResponse response, @Nullable String sockJsPath, WebSocketHandler wsHandler) throws SockJsException { if (sockJsPath == null) { if (logger.isWarnEnabled()) { logger.warn("Expected SockJS path. Failing request: " + request.getURI()); } response.setStatusCode(HttpStatus.NOT_FOUND); return; } try { request.getHeaders(); } catch (InvalidMediaTypeException ex) { // As per SockJS protocol content-type can be ignored (it's always json) } String requestInfo = (logger.isDebugEnabled() ? request.getMethod() + " " + request.getURI() : null); try { if (sockJsPath.equals("") || sockJsPath.equals("/")) { if (requestInfo != null) { logger.debug("Processing transport request: " + requestInfo); } response.getHeaders().setContentType(new MediaType("text", "plain", StandardCharsets.UTF_8)); response.getBody().write("Welcome to SockJS!\n".getBytes(StandardCharsets.UTF_8)); } else if (sockJsPath.equals("/info")) { if (requestInfo != null) { logger.debug("Processing transport request: " + requestInfo); } this.infoHandler.handle(request, response); } else if (sockJsPath.matches("/iframe[0-9-.a-z_]*.html")) { if (!this.allowedOrigins.isEmpty() && !this.allowedOrigins.contains("*")) { if (requestInfo != null) { logger.debug("Iframe support is disabled when an origin check is required. " + "Ignoring transport request: " + requestInfo); } response.setStatusCode(HttpStatus.NOT_FOUND); return; } if (this.allowedOrigins.isEmpty()) { response.getHeaders().add(XFRAME_OPTIONS_HEADER, "SAMEORIGIN"); } if (requestInfo != null) { logger.debug("Processing transport request: " + requestInfo); } this.iframeHandler.handle(request, response); } else if (sockJsPath.equals("/websocket")) { if (isWebSocketEnabled()) { if (requestInfo != null) { logger.debug("Processing transport request: " + requestInfo); } handleRawWebSocketRequest(request, response, wsHandler); } else if (requestInfo != null) { logger.debug("WebSocket disabled. Ignoring transport request: " + requestInfo); } } else { String[] pathSegments = StringUtils.tokenizeToStringArray(sockJsPath.substring(1), "/"); if (pathSegments.length != 3) { if (logger.isWarnEnabled()) { logger.warn("Invalid SockJS path '" + sockJsPath + "' - required to have 3 path segments"); } if (requestInfo != null) { logger.debug("Ignoring transport request: " + requestInfo); } response.setStatusCode(HttpStatus.NOT_FOUND); return; } String serverId = pathSegments[0]; String sessionId = pathSegments[1]; String transport = pathSegments[2]; if (!isWebSocketEnabled() && transport.equals("websocket")) { if (requestInfo != null) { logger.debug("WebSocket disabled. Ignoring transport request: " + requestInfo); } response.setStatusCode(HttpStatus.NOT_FOUND); return; } else if (!validateRequest(serverId, sessionId, transport) || !validatePath(request)) { if (requestInfo != null) { logger.debug("Ignoring transport request: " + requestInfo); } response.setStatusCode(HttpStatus.NOT_FOUND); return; } if (requestInfo != null) { logger.debug("Processing transport request: " + requestInfo); } handleTransportRequest(request, response, wsHandler, sessionId, transport); } response.close(); } catch (IOException ex) { throw new SockJsException("Failed to write to the response", null, ex); } }
From source file:org.springframework.web.socket.sockjs.transport.TransportHandlingSockJsService.java
@Override protected void handleTransportRequest(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler handler, String sessionId, String transport) throws SockJsException { TransportType transportType = TransportType.fromValue(transport); if (transportType == null) { logger.error("Unknown transport type for " + request.getURI()); response.setStatusCode(HttpStatus.NOT_FOUND); return;/*from w ww. j a v a 2 s . c om*/ } TransportHandler transportHandler = this.handlers.get(transportType); if (transportHandler == null) { logger.error("No TransportHandler for " + request.getURI()); response.setStatusCode(HttpStatus.NOT_FOUND); return; } HttpMethod supportedMethod = transportType.getHttpMethod(); if (!supportedMethod.equals(request.getMethod())) { if (HttpMethod.OPTIONS.equals(request.getMethod()) && transportType.supportsCors()) { response.setStatusCode(HttpStatus.NO_CONTENT); addCorsHeaders(request, response, HttpMethod.OPTIONS, supportedMethod); addCacheHeaders(response); } else if (transportType.supportsCors()) { sendMethodNotAllowed(response, supportedMethod, HttpMethod.OPTIONS); } else { sendMethodNotAllowed(response, supportedMethod); } return; } HandshakeInterceptorChain chain = new HandshakeInterceptorChain(this.interceptors, handler); SockJsException failure = null; try { SockJsSession session = this.sessions.get(sessionId); if (session == null) { if (transportHandler instanceof SockJsSessionFactory) { Map<String, Object> attributes = new HashMap<String, Object>(); if (!chain.applyBeforeHandshake(request, response, attributes)) { return; } SockJsSessionFactory sessionFactory = (SockJsSessionFactory) transportHandler; session = createSockJsSession(sessionId, sessionFactory, handler, attributes); } else { response.setStatusCode(HttpStatus.NOT_FOUND); if (logger.isDebugEnabled()) { logger.debug( "Session not found, sessionId=" + sessionId + ". The session may have been closed " + "(e.g. missed heart-beat) while a message was coming in."); } return; } } if (transportType.sendsNoCacheInstruction()) { addNoCacheHeaders(response); } if (transportType.supportsCors()) { addCorsHeaders(request, response); } transportHandler.handleRequest(request, response, handler, session); chain.applyAfterHandshake(request, response, null); } catch (SockJsException ex) { failure = ex; } catch (Throwable ex) { failure = new SockJsException("Uncaught failure for request " + request.getURI(), sessionId, ex); } finally { if (failure != null) { chain.applyAfterHandshake(request, response, failure); throw failure; } } }