Search in sources :

Example 1 with SockJsException

use of org.springframework.web.socket.sockjs.SockJsException in project spring-framework by spring-projects.

the class AbstractHttpReceivingTransportHandler method handleReadError.

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);
    }
}
Also used : SockJsException(org.springframework.web.socket.sockjs.SockJsException) IOException(java.io.IOException)

Example 2 with SockJsException

use of org.springframework.web.socket.sockjs.SockJsException in project spring-framework by spring-projects.

the class AbstractHttpSendingTransportHandler method getCallbackParam.

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);
    }
}
Also used : SockJsException(org.springframework.web.socket.sockjs.SockJsException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 3 with SockJsException

use of org.springframework.web.socket.sockjs.SockJsException in project spring-framework by spring-projects.

the class AbstractHttpSockJsSession method handleInitialRequest.

/**
	 * 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();
    try {
        this.localAddress = request.getLocalAddress();
    } catch (Exception ex) {
    // Ignore
    }
    try {
        this.remoteAddress = request.getRemoteAddress();
    } catch (Exception ex) {
    // Ignore
    }
    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);
        }
    }
}
Also used : SockJsTransportFailureException(org.springframework.web.socket.sockjs.SockJsTransportFailureException) SockJsTransportFailureException(org.springframework.web.socket.sockjs.SockJsTransportFailureException) SockJsException(org.springframework.web.socket.sockjs.SockJsException) IOException(java.io.IOException)

Example 4 with SockJsException

use of org.springframework.web.socket.sockjs.SockJsException in project spring-framework by spring-projects.

the class AbstractSockJsService method handleRequest.

/**
	 * This method determines the SockJS path and handles SockJS static URLs.
	 * Session URLs and raw WebSocket requests are delegated to abstract methods.
	 */
@Override
public final void handleRequest(ServerHttpRequest request, ServerHttpResponse response, 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);
    }
}
Also used : SockJsException(org.springframework.web.socket.sockjs.SockJsException) MediaType(org.springframework.http.MediaType) IOException(java.io.IOException) InvalidMediaTypeException(org.springframework.http.InvalidMediaTypeException)

Example 5 with SockJsException

use of org.springframework.web.socket.sockjs.SockJsException in project spring-framework by spring-projects.

the class SockJsHttpRequestHandler method handleRequest.

@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);
    }
}
Also used : ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) SockJsException(org.springframework.web.socket.sockjs.SockJsException) ServerHttpRequest(org.springframework.http.server.ServerHttpRequest) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) ServletServerHttpResponse(org.springframework.http.server.ServletServerHttpResponse) ServerHttpResponse(org.springframework.http.server.ServerHttpResponse) ServletServerHttpResponse(org.springframework.http.server.ServletServerHttpResponse)

Aggregations

SockJsException (org.springframework.web.socket.sockjs.SockJsException)6 IOException (java.io.IOException)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 HttpMethod (org.springframework.http.HttpMethod)1 InvalidMediaTypeException (org.springframework.http.InvalidMediaTypeException)1 MediaType (org.springframework.http.MediaType)1 ServerHttpRequest (org.springframework.http.server.ServerHttpRequest)1 ServerHttpResponse (org.springframework.http.server.ServerHttpResponse)1 ServletServerHttpRequest (org.springframework.http.server.ServletServerHttpRequest)1 ServletServerHttpResponse (org.springframework.http.server.ServletServerHttpResponse)1 HandshakeInterceptorChain (org.springframework.web.socket.server.support.HandshakeInterceptorChain)1 SockJsTransportFailureException (org.springframework.web.socket.sockjs.SockJsTransportFailureException)1